1116742Ssam/*- 2116904Ssam * Copyright (c) 2001 Atsushi Onoe 3178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 4116742Ssam * All rights reserved. 5116742Ssam * 6116742Ssam * Redistribution and use in source and binary forms, with or without 7116742Ssam * modification, are permitted provided that the following conditions 8116742Ssam * are met: 9116742Ssam * 1. Redistributions of source code must retain the above copyright 10116904Ssam * notice, this list of conditions and the following disclaimer. 11116904Ssam * 2. Redistributions in binary form must reproduce the above copyright 12116904Ssam * notice, this list of conditions and the following disclaimer in the 13116904Ssam * documentation and/or other materials provided with the distribution. 14116742Ssam * 15116904Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16116904Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17116904Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18116904Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19116904Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20116904Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21116904Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22116904Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23116904Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24116904Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25116742Ssam */ 26116742Ssam 27116742Ssam#include <sys/cdefs.h> 28116742Ssam__FBSDID("$FreeBSD: releng/11.0/sys/net80211/ieee80211_crypto.c 288635 2015-10-03 21:48:27Z adrian $"); 29116742Ssam 30138568Ssam/* 31138568Ssam * IEEE 802.11 generic crypto support. 32138568Ssam */ 33178354Ssam#include "opt_wlan.h" 34178354Ssam 35116742Ssam#include <sys/param.h> 36178354Ssam#include <sys/kernel.h> 37178354Ssam#include <sys/malloc.h> 38116742Ssam#include <sys/mbuf.h> 39138568Ssam 40116742Ssam#include <sys/socket.h> 41116742Ssam 42116742Ssam#include <net/if.h> 43116742Ssam#include <net/if_media.h> 44138568Ssam#include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 45116742Ssam 46116742Ssam#include <net80211/ieee80211_var.h> 47116742Ssam 48178354SsamMALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state"); 49178354Ssam 50178354Ssamstatic int _ieee80211_crypto_delkey(struct ieee80211vap *, 51178354Ssam struct ieee80211_key *); 52178354Ssam 53138568Ssam/* 54138568Ssam * Table of registered cipher modules. 55138568Ssam */ 56138568Ssamstatic const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 57116742Ssam 58138568Ssam/* 59138568Ssam * Default "null" key management routines. 60138568Ssam */ 61138568Ssamstatic int 62183248Ssamnull_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 63148863Ssam ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 64138568Ssam{ 65178354Ssam if (!(&vap->iv_nw_keys[0] <= k && 66178354Ssam k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 67147870Ssam /* 68147870Ssam * Not in the global key table, the driver should handle this 69147870Ssam * by allocating a slot in the h/w key table/cache. In 70147870Ssam * lieu of that return key slot 0 for any unicast key 71147870Ssam * request. We disallow the request if this is a group key. 72147870Ssam * This default policy does the right thing for legacy hardware 73147870Ssam * with a 4 key table. It also handles devices that pass 74147870Ssam * packets through untouched when marked with the WEP bit 75147870Ssam * and key index 0. 76147870Ssam */ 77148863Ssam if (k->wk_flags & IEEE80211_KEY_GROUP) 78148863Ssam return 0; 79148863Ssam *keyix = 0; /* NB: use key index 0 for ucast key */ 80148863Ssam } else { 81178354Ssam *keyix = k - vap->iv_nw_keys; 82147870Ssam } 83148863Ssam *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84148863Ssam return 1; 85138568Ssam} 86138568Ssamstatic int 87178354Ssamnull_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 88138568Ssam{ 89138568Ssam return 1; 90138568Ssam} 91138568Ssamstatic int 92288635Sadriannull_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 93138568Ssam{ 94138568Ssam return 1; 95138568Ssam} 96178354Ssamstatic void null_key_update(struct ieee80211vap *vap) {} 97116742Ssam 98138568Ssam/* 99138568Ssam * Write-arounds for common operations. 100138568Ssam */ 101138568Ssamstatic __inline void 102138568Ssamcipher_detach(struct ieee80211_key *key) 103138568Ssam{ 104138568Ssam key->wk_cipher->ic_detach(key); 105138568Ssam} 106116742Ssam 107138568Ssamstatic __inline void * 108178354Ssamcipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key) 109138568Ssam{ 110178354Ssam return key->wk_cipher->ic_attach(vap, key); 111138568Ssam} 112138568Ssam 113138568Ssam/* 114138568Ssam * Wrappers for driver key management methods. 115138568Ssam */ 116138568Ssamstatic __inline int 117178354Ssamdev_key_alloc(struct ieee80211vap *vap, 118183248Ssam struct ieee80211_key *key, 119148863Ssam ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 120138568Ssam{ 121178354Ssam return vap->iv_key_alloc(vap, key, keyix, rxkeyix); 122138568Ssam} 123138568Ssam 124138568Ssamstatic __inline int 125178354Ssamdev_key_delete(struct ieee80211vap *vap, 126138568Ssam const struct ieee80211_key *key) 127138568Ssam{ 128178354Ssam return vap->iv_key_delete(vap, key); 129138568Ssam} 130138568Ssam 131138568Ssamstatic __inline int 132179395Ssamdev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key) 133138568Ssam{ 134288635Sadrian return vap->iv_key_set(vap, key); 135138568Ssam} 136138568Ssam 137138568Ssam/* 138178354Ssam * Setup crypto support for a device/shared instance. 139138568Ssam */ 140116742Ssamvoid 141138568Ssamieee80211_crypto_attach(struct ieee80211com *ic) 142116742Ssam{ 143178354Ssam /* NB: we assume everything is pre-zero'd */ 144178354Ssam ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 145178354Ssam} 146178354Ssam 147178354Ssam/* 148178354Ssam * Teardown crypto support. 149178354Ssam */ 150178354Ssamvoid 151178354Ssamieee80211_crypto_detach(struct ieee80211com *ic) 152178354Ssam{ 153178354Ssam} 154178354Ssam 155178354Ssam/* 156178354Ssam * Setup crypto support for a vap. 157178354Ssam */ 158178354Ssamvoid 159178354Ssamieee80211_crypto_vattach(struct ieee80211vap *vap) 160178354Ssam{ 161138568Ssam int i; 162116742Ssam 163138568Ssam /* NB: we assume everything is pre-zero'd */ 164178354Ssam vap->iv_max_keyix = IEEE80211_WEP_NKID; 165178354Ssam vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 166138568Ssam for (i = 0; i < IEEE80211_WEP_NKID; i++) 167178354Ssam ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 168144960Ssam IEEE80211_KEYIX_NONE); 169116742Ssam /* 170138568Ssam * Initialize the driver key support routines to noop entries. 171138568Ssam * This is useful especially for the cipher test modules. 172116742Ssam */ 173178354Ssam vap->iv_key_alloc = null_key_alloc; 174178354Ssam vap->iv_key_set = null_key_set; 175178354Ssam vap->iv_key_delete = null_key_delete; 176178354Ssam vap->iv_key_update_begin = null_key_update; 177178354Ssam vap->iv_key_update_end = null_key_update; 178116742Ssam} 179116742Ssam 180138568Ssam/* 181178354Ssam * Teardown crypto support for a vap. 182138568Ssam */ 183116742Ssamvoid 184178354Ssamieee80211_crypto_vdetach(struct ieee80211vap *vap) 185116742Ssam{ 186178354Ssam ieee80211_crypto_delglobalkeys(vap); 187138568Ssam} 188116742Ssam 189138568Ssam/* 190138568Ssam * Register a crypto cipher module. 191138568Ssam */ 192138568Ssamvoid 193138568Ssamieee80211_crypto_register(const struct ieee80211_cipher *cip) 194138568Ssam{ 195138568Ssam if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 196138568Ssam printf("%s: cipher %s has an invalid cipher index %u\n", 197138568Ssam __func__, cip->ic_name, cip->ic_cipher); 198138568Ssam return; 199116742Ssam } 200138568Ssam if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 201138568Ssam printf("%s: cipher %s registered with a different template\n", 202138568Ssam __func__, cip->ic_name); 203138568Ssam return; 204138568Ssam } 205138568Ssam ciphers[cip->ic_cipher] = cip; 206116742Ssam} 207116742Ssam 208138568Ssam/* 209138568Ssam * Unregister a crypto cipher module. 210138568Ssam */ 211138568Ssamvoid 212138568Ssamieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 213116742Ssam{ 214138568Ssam if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 215138568Ssam printf("%s: cipher %s has an invalid cipher index %u\n", 216138568Ssam __func__, cip->ic_name, cip->ic_cipher); 217138568Ssam return; 218116742Ssam } 219138568Ssam if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 220138568Ssam printf("%s: cipher %s registered with a different template\n", 221138568Ssam __func__, cip->ic_name); 222138568Ssam return; 223121180Ssam } 224138568Ssam /* NB: don't complain about not being registered */ 225138568Ssam /* XXX disallow if references */ 226138568Ssam ciphers[cip->ic_cipher] = NULL; 227138568Ssam} 228138568Ssam 229138568Ssamint 230138568Ssamieee80211_crypto_available(u_int cipher) 231138568Ssam{ 232138568Ssam return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 233138568Ssam} 234138568Ssam 235138568Ssam/* XXX well-known names! */ 236178354Ssamstatic const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 237193665Ssam [IEEE80211_CIPHER_WEP] = "wlan_wep", 238193665Ssam [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 239193665Ssam [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 240193665Ssam [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 241193665Ssam [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 242193665Ssam [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 243193665Ssam [IEEE80211_CIPHER_NONE] = "wlan_none", 244138568Ssam}; 245138568Ssam 246195812Ssam/* NB: there must be no overlap between user-supplied and device-owned flags */ 247195812SsamCTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 248195812Ssam 249138568Ssam/* 250138568Ssam * Establish a relationship between the specified key and cipher 251144960Ssam * and, if necessary, allocate a hardware index from the driver. 252195812Ssam * Note that when a fixed key index is required it must be specified. 253138568Ssam * 254138568Ssam * This must be the first call applied to a key; all the other key 255138568Ssam * routines assume wk_cipher is setup. 256138568Ssam * 257138568Ssam * Locking must be handled by the caller using: 258178354Ssam * ieee80211_key_update_begin(vap); 259178354Ssam * ieee80211_key_update_end(vap); 260138568Ssam */ 261138568Ssamint 262178354Ssamieee80211_crypto_newkey(struct ieee80211vap *vap, 263144960Ssam int cipher, int flags, struct ieee80211_key *key) 264138568Ssam{ 265178354Ssam struct ieee80211com *ic = vap->iv_ic; 266138568Ssam const struct ieee80211_cipher *cip; 267148863Ssam ieee80211_keyix keyix, rxkeyix; 268138568Ssam void *keyctx; 269138568Ssam int oflags; 270138568Ssam 271184268Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 272184268Ssam "%s: cipher %u flags 0x%x keyix %u\n", 273184268Ssam __func__, cipher, flags, key->wk_keyix); 274184268Ssam 275138568Ssam /* 276138568Ssam * Validate cipher and set reference to cipher routines. 277138568Ssam */ 278138568Ssam if (cipher >= IEEE80211_CIPHER_MAX) { 279178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 280178354Ssam "%s: invalid cipher %u\n", __func__, cipher); 281178354Ssam vap->iv_stats.is_crypto_badcipher++; 282138568Ssam return 0; 283116742Ssam } 284138568Ssam cip = ciphers[cipher]; 285138568Ssam if (cip == NULL) { 286116742Ssam /* 287138568Ssam * Auto-load cipher module if we have a well-known name 288138568Ssam * for it. It might be better to use string names rather 289138568Ssam * than numbers and craft a module name based on the cipher 290138568Ssam * name; e.g. wlan_cipher_<cipher-name>. 291116742Ssam */ 292178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 293178354Ssam "%s: unregistered cipher %u, load module %s\n", 294178354Ssam __func__, cipher, cipher_modnames[cipher]); 295178354Ssam ieee80211_load_module(cipher_modnames[cipher]); 296178354Ssam /* 297178354Ssam * If cipher module loaded it should immediately 298178354Ssam * call ieee80211_crypto_register which will fill 299178354Ssam * in the entry in the ciphers array. 300178354Ssam */ 301178354Ssam cip = ciphers[cipher]; 302138568Ssam if (cip == NULL) { 303178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 304178354Ssam "%s: unable to load cipher %u, module %s\n", 305178354Ssam __func__, cipher, cipher_modnames[cipher]); 306178354Ssam vap->iv_stats.is_crypto_nocipher++; 307138568Ssam return 0; 308138568Ssam } 309116742Ssam } 310116742Ssam 311138568Ssam oflags = key->wk_flags; 312144960Ssam flags &= IEEE80211_KEY_COMMON; 313195812Ssam /* NB: preserve device attributes */ 314195812Ssam flags |= (oflags & IEEE80211_KEY_DEVICE); 315138568Ssam /* 316138568Ssam * If the hardware does not support the cipher then 317138568Ssam * fallback to a host-based implementation. 318138568Ssam */ 319178354Ssam if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 320178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 321138568Ssam "%s: no h/w support for cipher %s, falling back to s/w\n", 322138568Ssam __func__, cip->ic_name); 323144960Ssam flags |= IEEE80211_KEY_SWCRYPT; 324138568Ssam } 325138568Ssam /* 326138568Ssam * Hardware TKIP with software MIC is an important 327138568Ssam * combination; we handle it by flagging each key, 328138568Ssam * the cipher modules honor it. 329138568Ssam */ 330138568Ssam if (cipher == IEEE80211_CIPHER_TKIP && 331178354Ssam (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 332178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 333138568Ssam "%s: no h/w support for TKIP MIC, falling back to s/w\n", 334138568Ssam __func__); 335144960Ssam flags |= IEEE80211_KEY_SWMIC; 336138568Ssam } 337138568Ssam 338138568Ssam /* 339138568Ssam * Bind cipher to key instance. Note we do this 340138568Ssam * after checking the device capabilities so the 341138568Ssam * cipher module can optimize space usage based on 342138568Ssam * whether or not it needs to do the cipher work. 343138568Ssam */ 344144960Ssam if (key->wk_cipher != cip || key->wk_flags != flags) { 345144960Ssam /* 346144960Ssam * Fillin the flags so cipher modules can see s/w 347144960Ssam * crypto requirements and potentially allocate 348144960Ssam * different state and/or attach different method 349144960Ssam * pointers. 350144960Ssam */ 351144960Ssam key->wk_flags = flags; 352178354Ssam keyctx = cip->ic_attach(vap, key); 353138568Ssam if (keyctx == NULL) { 354178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 355138568Ssam "%s: unable to attach cipher %s\n", 356138568Ssam __func__, cip->ic_name); 357138568Ssam key->wk_flags = oflags; /* restore old flags */ 358178354Ssam vap->iv_stats.is_crypto_attachfail++; 359138568Ssam return 0; 360116742Ssam } 361138568Ssam cipher_detach(key); 362138568Ssam key->wk_cipher = cip; /* XXX refcnt? */ 363138568Ssam key->wk_private = keyctx; 364138568Ssam } 365138568Ssam 366138568Ssam /* 367138568Ssam * Ask the driver for a key index if we don't have one. 368138568Ssam * Note that entries in the global key table always have 369138568Ssam * an index; this means it's safe to call this routine 370138568Ssam * for these entries just to setup the reference to the 371138568Ssam * cipher template. Note also that when using software 372138568Ssam * crypto we also call the driver to give us a key index. 373138568Ssam */ 374183248Ssam if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 375178354Ssam if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 376138568Ssam /* 377183248Ssam * Unable to setup driver state. 378138568Ssam */ 379178354Ssam vap->iv_stats.is_crypto_keyfail++; 380178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 381138568Ssam "%s: unable to setup cipher %s\n", 382138568Ssam __func__, cip->ic_name); 383138568Ssam return 0; 384116742Ssam } 385183248Ssam if (key->wk_flags != flags) { 386183248Ssam /* 387183248Ssam * Driver overrode flags we setup; typically because 388183248Ssam * resources were unavailable to handle _this_ key. 389183248Ssam * Re-attach the cipher context to allow cipher 390183248Ssam * modules to handle differing requirements. 391183248Ssam */ 392183248Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 393183248Ssam "%s: driver override for cipher %s, flags " 394183248Ssam "0x%x -> 0x%x\n", __func__, cip->ic_name, 395183248Ssam oflags, key->wk_flags); 396183248Ssam keyctx = cip->ic_attach(vap, key); 397183248Ssam if (keyctx == NULL) { 398183248Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 399183248Ssam "%s: unable to attach cipher %s with " 400183248Ssam "flags 0x%x\n", __func__, cip->ic_name, 401183248Ssam key->wk_flags); 402183248Ssam key->wk_flags = oflags; /* restore old flags */ 403183248Ssam vap->iv_stats.is_crypto_attachfail++; 404183248Ssam return 0; 405183248Ssam } 406183248Ssam cipher_detach(key); 407183248Ssam key->wk_cipher = cip; /* XXX refcnt? */ 408183248Ssam key->wk_private = keyctx; 409183248Ssam } 410148863Ssam key->wk_keyix = keyix; 411148863Ssam key->wk_rxkeyix = rxkeyix; 412183248Ssam key->wk_flags |= IEEE80211_KEY_DEVKEY; 413116742Ssam } 414138568Ssam return 1; 415138568Ssam} 416138568Ssam 417138568Ssam/* 418138568Ssam * Remove the key (no locking, for internal use). 419138568Ssam */ 420138568Ssamstatic int 421178354Ssam_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 422138568Ssam{ 423138568Ssam KASSERT(key->wk_cipher != NULL, ("No cipher!")); 424138568Ssam 425178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 426139504Ssam "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 427139504Ssam __func__, key->wk_cipher->ic_name, 428139504Ssam key->wk_keyix, key->wk_flags, 429178354Ssam key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 430178354Ssam key->wk_keylen); 431139504Ssam 432183248Ssam if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 433138568Ssam /* 434138568Ssam * Remove hardware entry. 435138568Ssam */ 436138568Ssam /* XXX key cache */ 437178354Ssam if (!dev_key_delete(vap, key)) { 438178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 439138568Ssam "%s: driver did not delete key index %u\n", 440183248Ssam __func__, key->wk_keyix); 441178354Ssam vap->iv_stats.is_crypto_delkey++; 442138568Ssam /* XXX recovery? */ 443116742Ssam } 444116742Ssam } 445138568Ssam cipher_detach(key); 446138568Ssam memset(key, 0, sizeof(*key)); 447178354Ssam ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 448138568Ssam return 1; 449138568Ssam} 450116742Ssam 451138568Ssam/* 452138568Ssam * Remove the specified key. 453138568Ssam */ 454138568Ssamint 455178354Ssamieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 456138568Ssam{ 457138568Ssam int status; 458138568Ssam 459178354Ssam ieee80211_key_update_begin(vap); 460178354Ssam status = _ieee80211_crypto_delkey(vap, key); 461178354Ssam ieee80211_key_update_end(vap); 462138568Ssam return status; 463116742Ssam} 464116742Ssam 465116742Ssam/* 466138568Ssam * Clear the global key table. 467116742Ssam */ 468138568Ssamvoid 469178354Ssamieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 470138568Ssam{ 471138568Ssam int i; 472116742Ssam 473178354Ssam ieee80211_key_update_begin(vap); 474138568Ssam for (i = 0; i < IEEE80211_WEP_NKID; i++) 475178354Ssam (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 476178354Ssam ieee80211_key_update_end(vap); 477138568Ssam} 478116742Ssam 479138568Ssam/* 480138568Ssam * Set the contents of the specified key. 481138568Ssam * 482138568Ssam * Locking must be handled by the caller using: 483178354Ssam * ieee80211_key_update_begin(vap); 484178354Ssam * ieee80211_key_update_end(vap); 485138568Ssam */ 486138568Ssamint 487179395Ssamieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 488116742Ssam{ 489138568Ssam const struct ieee80211_cipher *cip = key->wk_cipher; 490116742Ssam 491138568Ssam KASSERT(cip != NULL, ("No cipher!")); 492138568Ssam 493178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 494139504Ssam "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 495139504Ssam __func__, cip->ic_name, key->wk_keyix, 496179395Ssam key->wk_flags, ether_sprintf(key->wk_macaddr), 497178354Ssam key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 498178354Ssam key->wk_keylen); 499139504Ssam 500183248Ssam if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 501183248Ssam /* XXX nothing allocated, should not happen */ 502183248Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 503183248Ssam "%s: no device key setup done; should not happen!\n", 504183248Ssam __func__); 505183248Ssam vap->iv_stats.is_crypto_setkey_nokey++; 506183248Ssam return 0; 507183248Ssam } 508138568Ssam /* 509138568Ssam * Give cipher a chance to validate key contents. 510138568Ssam * XXX should happen before modifying state. 511138568Ssam */ 512138568Ssam if (!cip->ic_setkey(key)) { 513178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 514138568Ssam "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 515138568Ssam __func__, cip->ic_name, key->wk_keyix, 516138568Ssam key->wk_keylen, key->wk_flags); 517178354Ssam vap->iv_stats.is_crypto_setkey_cipher++; 518138568Ssam return 0; 519138568Ssam } 520179395Ssam return dev_key_set(vap, key); 521138568Ssam} 522138568Ssam 523288523Sadrianuint8_t 524288523Sadrianieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 525288523Sadrian{ 526288523Sadrian if (k >= &vap->iv_nw_keys[0] && 527288523Sadrian k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 528288523Sadrian return (k - vap->iv_nw_keys); 529288523Sadrian else 530288523Sadrian return (0); 531288523Sadrian} 532288523Sadrian 533138568Ssamstruct ieee80211_key * 534288527Sadrianieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 535138568Ssam{ 536178354Ssam struct ieee80211vap *vap = ni->ni_vap; 537138568Ssam struct ieee80211_frame *wh; 538138568Ssam 539138568Ssam /* 540138568Ssam * Multicast traffic always uses the multicast key. 541138568Ssam * Otherwise if a unicast key is set we use that and 542138568Ssam * it is always key index 0. When no unicast key is 543138568Ssam * set we fall back to the default transmit key. 544138568Ssam */ 545138568Ssam wh = mtod(m, struct ieee80211_frame *); 546138568Ssam if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 547167432Ssam IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 548178354Ssam if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 549178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 550178354Ssam wh->i_addr1, 551178354Ssam "no default transmit key (%s) deftxkey %u", 552178354Ssam __func__, vap->iv_def_txkey); 553178354Ssam vap->iv_stats.is_tx_nodefkey++; 554138568Ssam return NULL; 555116742Ssam } 556288527Sadrian return &vap->iv_nw_keys[vap->iv_def_txkey]; 557288527Sadrian } 558288523Sadrian 559288527Sadrian return &ni->ni_ucastkey; 560116742Ssam} 561116742Ssam 562116742Ssam/* 563288527Sadrian * Add privacy headers appropriate for the specified key. 564288527Sadrian */ 565288527Sadrianstruct ieee80211_key * 566288527Sadrianieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 567288527Sadrian{ 568288527Sadrian struct ieee80211_key *k; 569288527Sadrian const struct ieee80211_cipher *cip; 570288527Sadrian 571288527Sadrian if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 572288527Sadrian cip = k->wk_cipher; 573288527Sadrian return (cip->ic_encap(k, m) ? k : NULL); 574288527Sadrian } 575288527Sadrian 576288527Sadrian return NULL; 577288527Sadrian} 578288527Sadrian 579288527Sadrian/* 580138568Ssam * Validate and strip privacy headers (and trailer) for a 581138568Ssam * received frame that has the WEP/Privacy bit set. 582116742Ssam */ 583138568Ssamstruct ieee80211_key * 584178354Ssamieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen) 585116742Ssam{ 586138568Ssam#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 587138568Ssam#define IEEE80211_WEP_MINLEN \ 588148303Ssam (sizeof(struct ieee80211_frame) + \ 589138568Ssam IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 590178354Ssam struct ieee80211vap *vap = ni->ni_vap; 591138568Ssam struct ieee80211_key *k; 592138568Ssam struct ieee80211_frame *wh; 593138568Ssam const struct ieee80211_cipher *cip; 594170530Ssam uint8_t keyid; 595116742Ssam 596138568Ssam /* NB: this minimum size data frame could be bigger */ 597138568Ssam if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 598178354Ssam IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 599138568Ssam "%s: WEP data frame too short, len %u\n", 600138568Ssam __func__, m->m_pkthdr.len); 601178354Ssam vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 602138568Ssam return NULL; 603138568Ssam } 604138568Ssam 605138568Ssam /* 606138568Ssam * Locate the key. If unicast and there is no unicast 607138568Ssam * key then we fall back to the key id in the header. 608138568Ssam * This assumes unicast keys are only configured when 609138568Ssam * the key id in the header is meaningless (typically 0). 610138568Ssam */ 611138568Ssam wh = mtod(m, struct ieee80211_frame *); 612178354Ssam m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 613138568Ssam if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 614167432Ssam IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 615178354Ssam k = &vap->iv_nw_keys[keyid >> 6]; 616138568Ssam else 617138568Ssam k = &ni->ni_ucastkey; 618138568Ssam 619138568Ssam /* 620138568Ssam * Insure crypto header is contiguous for all decap work. 621138568Ssam */ 622138568Ssam cip = k->wk_cipher; 623138568Ssam if (m->m_len < hdrlen + cip->ic_header && 624138568Ssam (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 625178354Ssam IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 626178354Ssam "unable to pullup %s header", cip->ic_name); 627178354Ssam vap->iv_stats.is_rx_wepfail++; /* XXX */ 628170530Ssam return NULL; 629138568Ssam } 630138568Ssam 631147252Ssam return (cip->ic_decap(k, m, hdrlen) ? k : NULL); 632138568Ssam#undef IEEE80211_WEP_MINLEN 633138568Ssam#undef IEEE80211_WEP_HDRLEN 634116742Ssam} 635179396Ssam 636179396Ssamstatic void 637179396Ssamload_ucastkey(void *arg, struct ieee80211_node *ni) 638179396Ssam{ 639179396Ssam struct ieee80211vap *vap = ni->ni_vap; 640179396Ssam struct ieee80211_key *k; 641179396Ssam 642179396Ssam if (vap->iv_state != IEEE80211_S_RUN) 643179396Ssam return; 644179396Ssam k = &ni->ni_ucastkey; 645183248Ssam if (k->wk_flags & IEEE80211_KEY_DEVKEY) 646179396Ssam dev_key_set(vap, k); 647179396Ssam} 648179396Ssam 649179396Ssam/* 650179396Ssam * Re-load all keys known to the 802.11 layer that may 651179396Ssam * have hardware state backing them. This is used by 652179396Ssam * drivers on resume to push keys down into the device. 653179396Ssam */ 654179396Ssamvoid 655179396Ssamieee80211_crypto_reload_keys(struct ieee80211com *ic) 656179396Ssam{ 657179396Ssam struct ieee80211vap *vap; 658179396Ssam int i; 659179396Ssam 660179396Ssam /* 661179396Ssam * Keys in the global key table of each vap. 662179396Ssam */ 663179396Ssam /* NB: used only during resume so don't lock for now */ 664179396Ssam TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 665179396Ssam if (vap->iv_state != IEEE80211_S_RUN) 666179396Ssam continue; 667179396Ssam for (i = 0; i < IEEE80211_WEP_NKID; i++) { 668179396Ssam const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 669183248Ssam if (k->wk_flags & IEEE80211_KEY_DEVKEY) 670179396Ssam dev_key_set(vap, k); 671179396Ssam } 672179396Ssam } 673179396Ssam /* 674179396Ssam * Unicast keys. 675179396Ssam */ 676179396Ssam ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 677179396Ssam} 678