11a1e1d21SSam Leffler /*- 27535e66aSSam Leffler * Copyright (c) 2001 Atsushi Onoe 31f1d7810SSam Leffler * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 41a1e1d21SSam Leffler * All rights reserved. 51a1e1d21SSam Leffler * 61a1e1d21SSam Leffler * Redistribution and use in source and binary forms, with or without 71a1e1d21SSam Leffler * modification, are permitted provided that the following conditions 81a1e1d21SSam Leffler * are met: 91a1e1d21SSam Leffler * 1. Redistributions of source code must retain the above copyright 107535e66aSSam Leffler * notice, this list of conditions and the following disclaimer. 117535e66aSSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 127535e66aSSam Leffler * notice, this list of conditions and the following disclaimer in the 137535e66aSSam Leffler * documentation and/or other materials provided with the distribution. 147535e66aSSam Leffler * 3. The name of the author may not be used to endorse or promote products 157535e66aSSam Leffler * derived from this software without specific prior written permission. 161a1e1d21SSam Leffler * 171a1e1d21SSam Leffler * Alternatively, this software may be distributed under the terms of the 181a1e1d21SSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free 191a1e1d21SSam Leffler * Software Foundation. 201a1e1d21SSam Leffler * 217535e66aSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 227535e66aSSam Leffler * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 237535e66aSSam Leffler * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 247535e66aSSam Leffler * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 257535e66aSSam Leffler * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 267535e66aSSam Leffler * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 277535e66aSSam Leffler * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 287535e66aSSam Leffler * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 297535e66aSSam Leffler * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 307535e66aSSam Leffler * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 311a1e1d21SSam Leffler */ 321a1e1d21SSam Leffler 331a1e1d21SSam Leffler #include <sys/cdefs.h> 341a1e1d21SSam Leffler __FBSDID("$FreeBSD$"); 351a1e1d21SSam Leffler 368a1b9b6aSSam Leffler /* 378a1b9b6aSSam Leffler * IEEE 802.11 generic crypto support. 388a1b9b6aSSam Leffler */ 391a1e1d21SSam Leffler #include <sys/param.h> 401a1e1d21SSam Leffler #include <sys/mbuf.h> 411a1e1d21SSam Leffler 428a1b9b6aSSam Leffler #include <sys/socket.h> 431a1e1d21SSam Leffler 441a1e1d21SSam Leffler #include <net/if.h> 451a1e1d21SSam Leffler #include <net/if_media.h> 468a1b9b6aSSam Leffler #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 471a1e1d21SSam Leffler 481a1e1d21SSam Leffler #include <net80211/ieee80211_var.h> 491a1e1d21SSam Leffler 508a1b9b6aSSam Leffler /* 518a1b9b6aSSam Leffler * Table of registered cipher modules. 528a1b9b6aSSam Leffler */ 538a1b9b6aSSam Leffler static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 541a1e1d21SSam Leffler 558a1b9b6aSSam Leffler static int _ieee80211_crypto_delkey(struct ieee80211com *, 568a1b9b6aSSam Leffler struct ieee80211_key *); 571a1e1d21SSam Leffler 588a1b9b6aSSam Leffler /* 598a1b9b6aSSam Leffler * Default "null" key management routines. 608a1b9b6aSSam Leffler */ 618a1b9b6aSSam Leffler static int 62c1225b52SSam Leffler null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k, 63c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 641a1e1d21SSam Leffler { 655c8a7b1bSSam Leffler if (!(&ic->ic_nw_keys[0] <= k && 665c8a7b1bSSam Leffler k < &ic->ic_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 { 81c1225b52SSam Leffler *keyix = k - ic->ic_nw_keys; 828a1b9b6aSSam Leffler } 83c1225b52SSam Leffler *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84c1225b52SSam Leffler return 1; 855c8a7b1bSSam Leffler } 868a1b9b6aSSam Leffler static int 878a1b9b6aSSam Leffler null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 888a1b9b6aSSam Leffler { 898a1b9b6aSSam Leffler return 1; 908a1b9b6aSSam Leffler } 918a1b9b6aSSam Leffler static int 928a1b9b6aSSam Leffler null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 938a1b9b6aSSam Leffler const u_int8_t mac[IEEE80211_ADDR_LEN]) 948a1b9b6aSSam Leffler { 958a1b9b6aSSam Leffler return 1; 968a1b9b6aSSam Leffler } 978a1b9b6aSSam Leffler static void null_key_update(struct ieee80211com *ic) {} 988a1b9b6aSSam Leffler 998a1b9b6aSSam Leffler /* 1008a1b9b6aSSam Leffler * Write-arounds for common operations. 1018a1b9b6aSSam Leffler */ 1028a1b9b6aSSam Leffler static __inline void 1038a1b9b6aSSam Leffler cipher_detach(struct ieee80211_key *key) 1048a1b9b6aSSam Leffler { 1058a1b9b6aSSam Leffler key->wk_cipher->ic_detach(key); 1068a1b9b6aSSam Leffler } 1078a1b9b6aSSam Leffler 1088a1b9b6aSSam Leffler static __inline void * 1098a1b9b6aSSam Leffler cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key) 1108a1b9b6aSSam Leffler { 1118a1b9b6aSSam Leffler return key->wk_cipher->ic_attach(ic, key); 1128a1b9b6aSSam Leffler } 1138a1b9b6aSSam Leffler 1148a1b9b6aSSam Leffler /* 1158a1b9b6aSSam Leffler * Wrappers for driver key management methods. 1168a1b9b6aSSam Leffler */ 1178a1b9b6aSSam Leffler static __inline int 1188a1b9b6aSSam Leffler dev_key_alloc(struct ieee80211com *ic, 119c1225b52SSam Leffler const struct ieee80211_key *key, 120c1225b52SSam Leffler ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 1218a1b9b6aSSam Leffler { 122c1225b52SSam Leffler return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix); 1238a1b9b6aSSam Leffler } 1248a1b9b6aSSam Leffler 1258a1b9b6aSSam Leffler static __inline int 1268a1b9b6aSSam Leffler dev_key_delete(struct ieee80211com *ic, 1278a1b9b6aSSam Leffler const struct ieee80211_key *key) 1288a1b9b6aSSam Leffler { 1298a1b9b6aSSam Leffler return ic->ic_crypto.cs_key_delete(ic, key); 1308a1b9b6aSSam Leffler } 1318a1b9b6aSSam Leffler 1328a1b9b6aSSam Leffler static __inline int 1338a1b9b6aSSam Leffler dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key, 1348a1b9b6aSSam Leffler const u_int8_t mac[IEEE80211_ADDR_LEN]) 1358a1b9b6aSSam Leffler { 1368a1b9b6aSSam Leffler return ic->ic_crypto.cs_key_set(ic, key, mac); 1378a1b9b6aSSam Leffler } 1381a1e1d21SSam Leffler 1391a1e1d21SSam Leffler /* 1401a1e1d21SSam Leffler * Setup crypto support. 1411a1e1d21SSam Leffler */ 1421a1e1d21SSam Leffler void 1438a1b9b6aSSam Leffler ieee80211_crypto_attach(struct ieee80211com *ic) 1441a1e1d21SSam Leffler { 1458a1b9b6aSSam Leffler struct ieee80211_crypto_state *cs = &ic->ic_crypto; 1468a1b9b6aSSam Leffler int i; 1471a1e1d21SSam Leffler 1488a1b9b6aSSam Leffler /* NB: we assume everything is pre-zero'd */ 1498a1b9b6aSSam Leffler cs->cs_def_txkey = IEEE80211_KEYIX_NONE; 150c1225b52SSam Leffler cs->cs_max_keyix = IEEE80211_WEP_NKID; 1518a1b9b6aSSam Leffler ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 1528a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 153dd70e17bSSam Leffler ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i], 154dd70e17bSSam Leffler IEEE80211_KEYIX_NONE); 1551a1e1d21SSam Leffler /* 1568a1b9b6aSSam Leffler * Initialize the driver key support routines to noop entries. 1578a1b9b6aSSam Leffler * This is useful especially for the cipher test modules. 1581a1e1d21SSam Leffler */ 1598a1b9b6aSSam Leffler cs->cs_key_alloc = null_key_alloc; 1608a1b9b6aSSam Leffler cs->cs_key_set = null_key_set; 1618a1b9b6aSSam Leffler cs->cs_key_delete = null_key_delete; 1628a1b9b6aSSam Leffler cs->cs_key_update_begin = null_key_update; 1638a1b9b6aSSam Leffler cs->cs_key_update_end = null_key_update; 1641a1e1d21SSam Leffler } 1651a1e1d21SSam Leffler 1668a1b9b6aSSam Leffler /* 1678a1b9b6aSSam Leffler * Teardown crypto support. 1688a1b9b6aSSam Leffler */ 1698a1b9b6aSSam Leffler void 1708a1b9b6aSSam Leffler ieee80211_crypto_detach(struct ieee80211com *ic) 1718a1b9b6aSSam Leffler { 1728a1b9b6aSSam Leffler ieee80211_crypto_delglobalkeys(ic); 1731a1e1d21SSam Leffler } 1741a1e1d21SSam Leffler 1758a1b9b6aSSam Leffler /* 1768a1b9b6aSSam Leffler * Register a crypto cipher module. 1778a1b9b6aSSam Leffler */ 1788a1b9b6aSSam Leffler void 1798a1b9b6aSSam Leffler ieee80211_crypto_register(const struct ieee80211_cipher *cip) 1808a1b9b6aSSam Leffler { 1818a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 1828a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 1838a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 1848a1b9b6aSSam Leffler return; 1858a1b9b6aSSam Leffler } 1868a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 1878a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 1888a1b9b6aSSam Leffler __func__, cip->ic_name); 1898a1b9b6aSSam Leffler return; 1908a1b9b6aSSam Leffler } 1918a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = cip; 1928a1b9b6aSSam Leffler } 1938a1b9b6aSSam Leffler 1948a1b9b6aSSam Leffler /* 1958a1b9b6aSSam Leffler * Unregister a crypto cipher module. 1968a1b9b6aSSam Leffler */ 1978a1b9b6aSSam Leffler void 1988a1b9b6aSSam Leffler ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 1998a1b9b6aSSam Leffler { 2008a1b9b6aSSam Leffler if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 2018a1b9b6aSSam Leffler printf("%s: cipher %s has an invalid cipher index %u\n", 2028a1b9b6aSSam Leffler __func__, cip->ic_name, cip->ic_cipher); 2038a1b9b6aSSam Leffler return; 2048a1b9b6aSSam Leffler } 2058a1b9b6aSSam Leffler if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 2068a1b9b6aSSam Leffler printf("%s: cipher %s registered with a different template\n", 2078a1b9b6aSSam Leffler __func__, cip->ic_name); 2088a1b9b6aSSam Leffler return; 2098a1b9b6aSSam Leffler } 2108a1b9b6aSSam Leffler /* NB: don't complain about not being registered */ 2118a1b9b6aSSam Leffler /* XXX disallow if references */ 2128a1b9b6aSSam Leffler ciphers[cip->ic_cipher] = NULL; 2138a1b9b6aSSam Leffler } 2148a1b9b6aSSam Leffler 2158a1b9b6aSSam Leffler int 2168a1b9b6aSSam Leffler ieee80211_crypto_available(u_int cipher) 2178a1b9b6aSSam Leffler { 2188a1b9b6aSSam Leffler return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 2198a1b9b6aSSam Leffler } 2208a1b9b6aSSam Leffler 2218a1b9b6aSSam Leffler /* XXX well-known names! */ 2228a1b9b6aSSam Leffler static const char *cipher_modnames[] = { 2238a1b9b6aSSam Leffler "wlan_wep", /* IEEE80211_CIPHER_WEP */ 2248a1b9b6aSSam Leffler "wlan_tkip", /* IEEE80211_CIPHER_TKIP */ 2258a1b9b6aSSam Leffler "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */ 2268a1b9b6aSSam Leffler "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */ 2278a1b9b6aSSam Leffler "wlan_ckip", /* IEEE80211_CIPHER_CKIP */ 2288a1b9b6aSSam Leffler }; 2298a1b9b6aSSam Leffler 2308a1b9b6aSSam Leffler /* 2318a1b9b6aSSam Leffler * Establish a relationship between the specified key and cipher 232dd70e17bSSam Leffler * and, if necessary, allocate a hardware index from the driver. 233dd70e17bSSam Leffler * Note that when a fixed key index is required it must be specified 234dd70e17bSSam Leffler * and we blindly assign it w/o consulting the driver (XXX). 2358a1b9b6aSSam Leffler * 2368a1b9b6aSSam Leffler * This must be the first call applied to a key; all the other key 2378a1b9b6aSSam Leffler * routines assume wk_cipher is setup. 2388a1b9b6aSSam Leffler * 2398a1b9b6aSSam Leffler * Locking must be handled by the caller using: 2408a1b9b6aSSam Leffler * ieee80211_key_update_begin(ic); 2418a1b9b6aSSam Leffler * ieee80211_key_update_end(ic); 2428a1b9b6aSSam Leffler */ 2438a1b9b6aSSam Leffler int 2448a1b9b6aSSam Leffler ieee80211_crypto_newkey(struct ieee80211com *ic, 245dd70e17bSSam Leffler int cipher, int flags, struct ieee80211_key *key) 2468a1b9b6aSSam Leffler { 2478a1b9b6aSSam Leffler #define N(a) (sizeof(a) / sizeof(a[0])) 2488a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 249c1225b52SSam Leffler ieee80211_keyix keyix, rxkeyix; 2508a1b9b6aSSam Leffler void *keyctx; 2518a1b9b6aSSam Leffler int oflags; 2528a1b9b6aSSam Leffler 2538a1b9b6aSSam Leffler /* 2548a1b9b6aSSam Leffler * Validate cipher and set reference to cipher routines. 2558a1b9b6aSSam Leffler */ 2568a1b9b6aSSam Leffler if (cipher >= IEEE80211_CIPHER_MAX) { 2578a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2588a1b9b6aSSam Leffler "%s: invalid cipher %u\n", __func__, cipher); 2598a1b9b6aSSam Leffler ic->ic_stats.is_crypto_badcipher++; 2608a1b9b6aSSam Leffler return 0; 2618a1b9b6aSSam Leffler } 2628a1b9b6aSSam Leffler cip = ciphers[cipher]; 2638a1b9b6aSSam Leffler if (cip == NULL) { 2648a1b9b6aSSam Leffler /* 2658a1b9b6aSSam Leffler * Auto-load cipher module if we have a well-known name 2668a1b9b6aSSam Leffler * for it. It might be better to use string names rather 2678a1b9b6aSSam Leffler * than numbers and craft a module name based on the cipher 2688a1b9b6aSSam Leffler * name; e.g. wlan_cipher_<cipher-name>. 2698a1b9b6aSSam Leffler */ 2708a1b9b6aSSam Leffler if (cipher < N(cipher_modnames)) { 2718a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2728a1b9b6aSSam Leffler "%s: unregistered cipher %u, load module %s\n", 2738a1b9b6aSSam Leffler __func__, cipher, cipher_modnames[cipher]); 2748a1b9b6aSSam Leffler ieee80211_load_module(cipher_modnames[cipher]); 2758a1b9b6aSSam Leffler /* 2768a1b9b6aSSam Leffler * If cipher module loaded it should immediately 2778a1b9b6aSSam Leffler * call ieee80211_crypto_register which will fill 2788a1b9b6aSSam Leffler * in the entry in the ciphers array. 2798a1b9b6aSSam Leffler */ 2808a1b9b6aSSam Leffler cip = ciphers[cipher]; 2818a1b9b6aSSam Leffler } 2828a1b9b6aSSam Leffler if (cip == NULL) { 2838a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 2848a1b9b6aSSam Leffler "%s: unable to load cipher %u, module %s\n", 2858a1b9b6aSSam Leffler __func__, cipher, 2868a1b9b6aSSam Leffler cipher < N(cipher_modnames) ? 2878a1b9b6aSSam Leffler cipher_modnames[cipher] : "<unknown>"); 2888a1b9b6aSSam Leffler ic->ic_stats.is_crypto_nocipher++; 2898a1b9b6aSSam Leffler return 0; 2908a1b9b6aSSam Leffler } 2918a1b9b6aSSam Leffler } 2928a1b9b6aSSam Leffler 2938a1b9b6aSSam Leffler oflags = key->wk_flags; 294dd70e17bSSam Leffler flags &= IEEE80211_KEY_COMMON; 2958a1b9b6aSSam Leffler /* 2968a1b9b6aSSam Leffler * If the hardware does not support the cipher then 2978a1b9b6aSSam Leffler * fallback to a host-based implementation. 2988a1b9b6aSSam Leffler */ 2998a1b9b6aSSam Leffler if ((ic->ic_caps & (1<<cipher)) == 0) { 3008a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3018a1b9b6aSSam Leffler "%s: no h/w support for cipher %s, falling back to s/w\n", 3028a1b9b6aSSam Leffler __func__, cip->ic_name); 303dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWCRYPT; 3048a1b9b6aSSam Leffler } 3058a1b9b6aSSam Leffler /* 3068a1b9b6aSSam Leffler * Hardware TKIP with software MIC is an important 3078a1b9b6aSSam Leffler * combination; we handle it by flagging each key, 3088a1b9b6aSSam Leffler * the cipher modules honor it. 3098a1b9b6aSSam Leffler */ 3108a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP && 3118a1b9b6aSSam Leffler (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) { 3128a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3138a1b9b6aSSam Leffler "%s: no h/w support for TKIP MIC, falling back to s/w\n", 3148a1b9b6aSSam Leffler __func__); 315dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWMIC; 3168a1b9b6aSSam Leffler } 3178a1b9b6aSSam Leffler 3188a1b9b6aSSam Leffler /* 3198a1b9b6aSSam Leffler * Bind cipher to key instance. Note we do this 3208a1b9b6aSSam Leffler * after checking the device capabilities so the 3218a1b9b6aSSam Leffler * cipher module can optimize space usage based on 3228a1b9b6aSSam Leffler * whether or not it needs to do the cipher work. 3238a1b9b6aSSam Leffler */ 324dd70e17bSSam Leffler if (key->wk_cipher != cip || key->wk_flags != flags) { 3258a1b9b6aSSam Leffler again: 326dd70e17bSSam Leffler /* 327dd70e17bSSam Leffler * Fillin the flags so cipher modules can see s/w 328dd70e17bSSam Leffler * crypto requirements and potentially allocate 329dd70e17bSSam Leffler * different state and/or attach different method 330dd70e17bSSam Leffler * pointers. 331dd70e17bSSam Leffler * 332dd70e17bSSam Leffler * XXX this is not right when s/w crypto fallback 333dd70e17bSSam Leffler * fails and we try to restore previous state. 334dd70e17bSSam Leffler */ 335dd70e17bSSam Leffler key->wk_flags = flags; 3368a1b9b6aSSam Leffler keyctx = cip->ic_attach(ic, key); 3378a1b9b6aSSam Leffler if (keyctx == NULL) { 3388a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3398a1b9b6aSSam Leffler "%s: unable to attach cipher %s\n", 3408a1b9b6aSSam Leffler __func__, cip->ic_name); 3418a1b9b6aSSam Leffler key->wk_flags = oflags; /* restore old flags */ 3428a1b9b6aSSam Leffler ic->ic_stats.is_crypto_attachfail++; 3438a1b9b6aSSam Leffler return 0; 3448a1b9b6aSSam Leffler } 3458a1b9b6aSSam Leffler cipher_detach(key); 3468a1b9b6aSSam Leffler key->wk_cipher = cip; /* XXX refcnt? */ 3478a1b9b6aSSam Leffler key->wk_private = keyctx; 3488a1b9b6aSSam Leffler } 349dd70e17bSSam Leffler /* 350dd70e17bSSam Leffler * Commit to requested usage so driver can see the flags. 351dd70e17bSSam Leffler */ 352dd70e17bSSam Leffler key->wk_flags = flags; 3538a1b9b6aSSam Leffler 3548a1b9b6aSSam Leffler /* 3558a1b9b6aSSam Leffler * Ask the driver for a key index if we don't have one. 3568a1b9b6aSSam Leffler * Note that entries in the global key table always have 3578a1b9b6aSSam Leffler * an index; this means it's safe to call this routine 3588a1b9b6aSSam Leffler * for these entries just to setup the reference to the 3598a1b9b6aSSam Leffler * cipher template. Note also that when using software 3608a1b9b6aSSam Leffler * crypto we also call the driver to give us a key index. 3618a1b9b6aSSam Leffler */ 3628a1b9b6aSSam Leffler if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 363c1225b52SSam Leffler if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) { 3648a1b9b6aSSam Leffler /* 3658a1b9b6aSSam Leffler * Driver has no room; fallback to doing crypto 3668a1b9b6aSSam Leffler * in the host. We change the flags and start the 3678a1b9b6aSSam Leffler * procedure over. If we get back here then there's 3688a1b9b6aSSam Leffler * no hope and we bail. Note that this can leave 3698a1b9b6aSSam Leffler * the key in a inconsistent state if the caller 3708a1b9b6aSSam Leffler * continues to use it. 3718a1b9b6aSSam Leffler */ 3728a1b9b6aSSam Leffler if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 3738a1b9b6aSSam Leffler ic->ic_stats.is_crypto_swfallback++; 3748a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3758a1b9b6aSSam Leffler "%s: no h/w resources for cipher %s, " 3768a1b9b6aSSam Leffler "falling back to s/w\n", __func__, 3778a1b9b6aSSam Leffler cip->ic_name); 3788a1b9b6aSSam Leffler oflags = key->wk_flags; 379dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWCRYPT; 3808a1b9b6aSSam Leffler if (cipher == IEEE80211_CIPHER_TKIP) 381dd70e17bSSam Leffler flags |= IEEE80211_KEY_SWMIC; 3828a1b9b6aSSam Leffler goto again; 3838a1b9b6aSSam Leffler } 3848a1b9b6aSSam Leffler ic->ic_stats.is_crypto_keyfail++; 3858a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 3868a1b9b6aSSam Leffler "%s: unable to setup cipher %s\n", 3878a1b9b6aSSam Leffler __func__, cip->ic_name); 3888a1b9b6aSSam Leffler return 0; 3898a1b9b6aSSam Leffler } 390c1225b52SSam Leffler key->wk_keyix = keyix; 391c1225b52SSam Leffler key->wk_rxkeyix = rxkeyix; 3928a1b9b6aSSam Leffler } 3938a1b9b6aSSam Leffler return 1; 3948a1b9b6aSSam Leffler #undef N 3958a1b9b6aSSam Leffler } 3968a1b9b6aSSam Leffler 3978a1b9b6aSSam Leffler /* 3988a1b9b6aSSam Leffler * Remove the key (no locking, for internal use). 3998a1b9b6aSSam Leffler */ 4008a1b9b6aSSam Leffler static int 4018a1b9b6aSSam Leffler _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 4028a1b9b6aSSam Leffler { 403c1225b52SSam Leffler ieee80211_keyix keyix; 4048a1b9b6aSSam Leffler 4058a1b9b6aSSam Leffler KASSERT(key->wk_cipher != NULL, ("No cipher!")); 4068a1b9b6aSSam Leffler 407fc508e8eSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 408fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 409fc508e8eSSam Leffler __func__, key->wk_cipher->ic_name, 410fc508e8eSSam Leffler key->wk_keyix, key->wk_flags, 411fc508e8eSSam Leffler key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 412fc508e8eSSam Leffler 4138a1b9b6aSSam Leffler keyix = key->wk_keyix; 4148a1b9b6aSSam Leffler if (keyix != IEEE80211_KEYIX_NONE) { 4158a1b9b6aSSam Leffler /* 4168a1b9b6aSSam Leffler * Remove hardware entry. 4178a1b9b6aSSam Leffler */ 4188a1b9b6aSSam Leffler /* XXX key cache */ 4198a1b9b6aSSam Leffler if (!dev_key_delete(ic, key)) { 4208a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4218a1b9b6aSSam Leffler "%s: driver did not delete key index %u\n", 4228a1b9b6aSSam Leffler __func__, keyix); 4238a1b9b6aSSam Leffler ic->ic_stats.is_crypto_delkey++; 4248a1b9b6aSSam Leffler /* XXX recovery? */ 4258a1b9b6aSSam Leffler } 4268a1b9b6aSSam Leffler } 4278a1b9b6aSSam Leffler cipher_detach(key); 4288a1b9b6aSSam Leffler memset(key, 0, sizeof(*key)); 429dd70e17bSSam Leffler ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE); 4308a1b9b6aSSam Leffler return 1; 4318a1b9b6aSSam Leffler } 4328a1b9b6aSSam Leffler 4338a1b9b6aSSam Leffler /* 4348a1b9b6aSSam Leffler * Remove the specified key. 4358a1b9b6aSSam Leffler */ 4368a1b9b6aSSam Leffler int 4378a1b9b6aSSam Leffler ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 4388a1b9b6aSSam Leffler { 4398a1b9b6aSSam Leffler int status; 4408a1b9b6aSSam Leffler 4418a1b9b6aSSam Leffler ieee80211_key_update_begin(ic); 4428a1b9b6aSSam Leffler status = _ieee80211_crypto_delkey(ic, key); 4438a1b9b6aSSam Leffler ieee80211_key_update_end(ic); 4448a1b9b6aSSam Leffler return status; 4458a1b9b6aSSam Leffler } 4468a1b9b6aSSam Leffler 4478a1b9b6aSSam Leffler /* 4488a1b9b6aSSam Leffler * Clear the global key table. 4498a1b9b6aSSam Leffler */ 4508a1b9b6aSSam Leffler void 4518a1b9b6aSSam Leffler ieee80211_crypto_delglobalkeys(struct ieee80211com *ic) 4528a1b9b6aSSam Leffler { 4538a1b9b6aSSam Leffler int i; 4548a1b9b6aSSam Leffler 4558a1b9b6aSSam Leffler ieee80211_key_update_begin(ic); 4568a1b9b6aSSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 4578a1b9b6aSSam Leffler (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]); 4588a1b9b6aSSam Leffler ieee80211_key_update_end(ic); 4598a1b9b6aSSam Leffler } 4608a1b9b6aSSam Leffler 4618a1b9b6aSSam Leffler /* 4628a1b9b6aSSam Leffler * Set the contents of the specified key. 4638a1b9b6aSSam Leffler * 4648a1b9b6aSSam Leffler * Locking must be handled by the caller using: 4658a1b9b6aSSam Leffler * ieee80211_key_update_begin(ic); 4668a1b9b6aSSam Leffler * ieee80211_key_update_end(ic); 4678a1b9b6aSSam Leffler */ 4688a1b9b6aSSam Leffler int 4698a1b9b6aSSam Leffler ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key, 4708a1b9b6aSSam Leffler const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 4718a1b9b6aSSam Leffler { 4728a1b9b6aSSam Leffler const struct ieee80211_cipher *cip = key->wk_cipher; 4738a1b9b6aSSam Leffler 4748a1b9b6aSSam Leffler KASSERT(cip != NULL, ("No cipher!")); 4758a1b9b6aSSam Leffler 476fc508e8eSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 477fc508e8eSSam Leffler "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 478fc508e8eSSam Leffler __func__, cip->ic_name, key->wk_keyix, 479fc508e8eSSam Leffler key->wk_flags, ether_sprintf(macaddr), 480fc508e8eSSam Leffler key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 481fc508e8eSSam Leffler 4828a1b9b6aSSam Leffler /* 4838a1b9b6aSSam Leffler * Give cipher a chance to validate key contents. 4848a1b9b6aSSam Leffler * XXX should happen before modifying state. 4858a1b9b6aSSam Leffler */ 4868a1b9b6aSSam Leffler if (!cip->ic_setkey(key)) { 4878a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4888a1b9b6aSSam Leffler "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 4898a1b9b6aSSam Leffler __func__, cip->ic_name, key->wk_keyix, 4908a1b9b6aSSam Leffler key->wk_keylen, key->wk_flags); 4918a1b9b6aSSam Leffler ic->ic_stats.is_crypto_setkey_cipher++; 4928a1b9b6aSSam Leffler return 0; 4938a1b9b6aSSam Leffler } 4948a1b9b6aSSam Leffler if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 4958a1b9b6aSSam Leffler /* XXX nothing allocated, should not happen */ 4968a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 4978a1b9b6aSSam Leffler "%s: no key index; should not happen!\n", __func__); 4988a1b9b6aSSam Leffler ic->ic_stats.is_crypto_setkey_nokey++; 4998a1b9b6aSSam Leffler return 0; 5008a1b9b6aSSam Leffler } 5018a1b9b6aSSam Leffler return dev_key_set(ic, key, macaddr); 5028a1b9b6aSSam Leffler } 5038a1b9b6aSSam Leffler 5048a1b9b6aSSam Leffler /* 5058a1b9b6aSSam Leffler * Add privacy headers appropriate for the specified key. 5068a1b9b6aSSam Leffler */ 5078a1b9b6aSSam Leffler struct ieee80211_key * 5088a1b9b6aSSam Leffler ieee80211_crypto_encap(struct ieee80211com *ic, 5098a1b9b6aSSam Leffler struct ieee80211_node *ni, struct mbuf *m) 5108a1b9b6aSSam Leffler { 5118a1b9b6aSSam Leffler struct ieee80211_key *k; 5128a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5138a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 514dd70e17bSSam Leffler u_int8_t keyid; 5158a1b9b6aSSam Leffler 5168a1b9b6aSSam Leffler /* 5178a1b9b6aSSam Leffler * Multicast traffic always uses the multicast key. 5188a1b9b6aSSam Leffler * Otherwise if a unicast key is set we use that and 5198a1b9b6aSSam Leffler * it is always key index 0. When no unicast key is 5208a1b9b6aSSam Leffler * set we fall back to the default transmit key. 5218a1b9b6aSSam Leffler */ 5228a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 5238a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 524cda15ce1SSam Leffler IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 5258a1b9b6aSSam Leffler if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) { 5268a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 527fc508e8eSSam Leffler "[%s] no default transmit key (%s) deftxkey %u\n", 528fc508e8eSSam Leffler ether_sprintf(wh->i_addr1), __func__, 529fc508e8eSSam Leffler ic->ic_def_txkey); 5308a1b9b6aSSam Leffler ic->ic_stats.is_tx_nodefkey++; 5318a1b9b6aSSam Leffler return NULL; 5328a1b9b6aSSam Leffler } 533dd70e17bSSam Leffler keyid = ic->ic_def_txkey; 5348a1b9b6aSSam Leffler k = &ic->ic_nw_keys[ic->ic_def_txkey]; 5358a1b9b6aSSam Leffler } else { 536dd70e17bSSam Leffler keyid = 0; 5378a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 5388a1b9b6aSSam Leffler } 5398a1b9b6aSSam Leffler cip = k->wk_cipher; 540dd70e17bSSam Leffler return (cip->ic_encap(k, m, keyid<<6) ? k : NULL); 5418a1b9b6aSSam Leffler } 5428a1b9b6aSSam Leffler 5438a1b9b6aSSam Leffler /* 5448a1b9b6aSSam Leffler * Validate and strip privacy headers (and trailer) for a 5458a1b9b6aSSam Leffler * received frame that has the WEP/Privacy bit set. 5468a1b9b6aSSam Leffler */ 5478a1b9b6aSSam Leffler struct ieee80211_key * 5488a1b9b6aSSam Leffler ieee80211_crypto_decap(struct ieee80211com *ic, 5492cc12adeSSam Leffler struct ieee80211_node *ni, struct mbuf *m, int hdrlen) 5508a1b9b6aSSam Leffler { 5518a1b9b6aSSam Leffler #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 5528a1b9b6aSSam Leffler #define IEEE80211_WEP_MINLEN \ 553a92c6eb0SSam Leffler (sizeof(struct ieee80211_frame) + \ 5548a1b9b6aSSam Leffler IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 5558a1b9b6aSSam Leffler struct ieee80211_key *k; 5568a1b9b6aSSam Leffler struct ieee80211_frame *wh; 5578a1b9b6aSSam Leffler const struct ieee80211_cipher *cip; 5587432b7ccSSam Leffler const u_int8_t *ivp; 5598a1b9b6aSSam Leffler u_int8_t keyid; 5608a1b9b6aSSam Leffler 5618a1b9b6aSSam Leffler /* NB: this minimum size data frame could be bigger */ 5628a1b9b6aSSam Leffler if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 5638a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 5648a1b9b6aSSam Leffler "%s: WEP data frame too short, len %u\n", 5658a1b9b6aSSam Leffler __func__, m->m_pkthdr.len); 5668a1b9b6aSSam Leffler ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */ 5671a1e1d21SSam Leffler return NULL; 5681a1e1d21SSam Leffler } 5691a1e1d21SSam Leffler 5701a1e1d21SSam Leffler /* 5718a1b9b6aSSam Leffler * Locate the key. If unicast and there is no unicast 5728a1b9b6aSSam Leffler * key then we fall back to the key id in the header. 5738a1b9b6aSSam Leffler * This assumes unicast keys are only configured when 5748a1b9b6aSSam Leffler * the key id in the header is meaningless (typically 0). 5751a1e1d21SSam Leffler */ 5768a1b9b6aSSam Leffler wh = mtod(m, struct ieee80211_frame *); 5777432b7ccSSam Leffler ivp = mtod(m, const u_int8_t *) + hdrlen; /* XXX contig */ 5788a1b9b6aSSam Leffler keyid = ivp[IEEE80211_WEP_IVLEN]; 5798a1b9b6aSSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 580cda15ce1SSam Leffler IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 5818a1b9b6aSSam Leffler k = &ic->ic_nw_keys[keyid >> 6]; 5821a1e1d21SSam Leffler else 5838a1b9b6aSSam Leffler k = &ni->ni_ucastkey; 5841a1e1d21SSam Leffler 5851a1e1d21SSam Leffler /* 5868a1b9b6aSSam Leffler * Insure crypto header is contiguous for all decap work. 5871a1e1d21SSam Leffler */ 5888a1b9b6aSSam Leffler cip = k->wk_cipher; 5898a1b9b6aSSam Leffler if (m->m_len < hdrlen + cip->ic_header && 5908a1b9b6aSSam Leffler (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 5918a1b9b6aSSam Leffler IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 5928a1b9b6aSSam Leffler "[%s] unable to pullup %s header\n", 5938a1b9b6aSSam Leffler ether_sprintf(wh->i_addr2), cip->ic_name); 5948a1b9b6aSSam Leffler ic->ic_stats.is_rx_wepfail++; /* XXX */ 5958a1b9b6aSSam Leffler return 0; 5968a1b9b6aSSam Leffler } 5971a1e1d21SSam Leffler 5982cc12adeSSam Leffler return (cip->ic_decap(k, m, hdrlen) ? k : NULL); 5998a1b9b6aSSam Leffler #undef IEEE80211_WEP_MINLEN 6008a1b9b6aSSam Leffler #undef IEEE80211_WEP_HDRLEN 6011a1e1d21SSam Leffler } 602