ieee80211_crypto.c revision 288527
1115396Skan/*- 2115396Skan * Copyright (c) 2001 Atsushi Onoe 3115396Skan * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 4115396Skan * All rights reserved. 5115396Skan * 6115396Skan * Redistribution and use in source and binary forms, with or without 7115396Skan * modification, are permitted provided that the following conditions 8115396Skan * are met: 9115396Skan * 1. Redistributions of source code must retain the above copyright 10115396Skan * notice, this list of conditions and the following disclaimer. 11115396Skan * 2. Redistributions in binary form must reproduce the above copyright 12115396Skan * notice, this list of conditions and the following disclaimer in the 13115396Skan * documentation and/or other materials provided with the distribution. 14115396Skan * 15115396Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16115396Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17115396Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18115396Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19115396Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20115396Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21115396Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22115396Skan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23115396Skan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24115396Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25115396Skan */ 26115396Skan 27115396Skan#include <sys/cdefs.h> 28115396Skan__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_crypto.c 288527 2015-10-03 00:57:33Z adrian $"); 29115396Skan 30115396Skan/* 31115396Skan * IEEE 802.11 generic crypto support. 32115396Skan */ 33115396Skan#include "opt_wlan.h" 34115396Skan 35115396Skan#include <sys/param.h> 36115396Skan#include <sys/kernel.h> 37115396Skan#include <sys/malloc.h> 38115396Skan#include <sys/mbuf.h> 39115396Skan 40115396Skan#include <sys/socket.h> 41115396Skan 42115396Skan#include <net/if.h> 43115396Skan#include <net/if_media.h> 44115396Skan#include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 45191303Srwatson 46115396Skan#include <net80211/ieee80211_var.h> 47115396Skan 48115396SkanMALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state"); 49115396Skan 50115396Skanstatic int _ieee80211_crypto_delkey(struct ieee80211vap *, 51115396Skan struct ieee80211_key *); 52115396Skan 53115396Skan/* 54115396Skan * Table of registered cipher modules. 55115396Skan */ 56115396Skanstatic const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 57115396Skan 58168311Skan/* 59115396Skan * Default "null" key management routines. 60115396Skan */ 61115396Skanstatic int 62115396Skannull_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 63115396Skan ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 64115396Skan{ 65115396Skan if (!(&vap->iv_nw_keys[0] <= k && 66115396Skan k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 67115396Skan /* 68115396Skan * Not in the global key table, the driver should handle this 69115396Skan * by allocating a slot in the h/w key table/cache. In 70115396Skan * lieu of that return key slot 0 for any unicast key 71115396Skan * request. We disallow the request if this is a group key. 72115396Skan * This default policy does the right thing for legacy hardware 73115396Skan * with a 4 key table. It also handles devices that pass 74115396Skan * packets through untouched when marked with the WEP bit 75115396Skan * and key index 0. 76115396Skan */ 77115396Skan if (k->wk_flags & IEEE80211_KEY_GROUP) 78115396Skan return 0; 79115396Skan *keyix = 0; /* NB: use key index 0 for ucast key */ 80115396Skan } else { 81115396Skan *keyix = k - vap->iv_nw_keys; 82115396Skan } 83115396Skan *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84115396Skan return 1; 85115396Skan} 86115396Skanstatic int 87115396Skannull_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 88115396Skan{ 89115396Skan return 1; 90115396Skan} 91115396Skanstatic int 92115396Skannull_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k, 93115396Skan const uint8_t mac[IEEE80211_ADDR_LEN]) 94115396Skan{ 95115396Skan return 1; 96115396Skan} 97115396Skanstatic void null_key_update(struct ieee80211vap *vap) {} 98115396Skan 99115396Skan/* 100115396Skan * Write-arounds for common operations. 101115396Skan */ 102115396Skanstatic __inline void 103115396Skancipher_detach(struct ieee80211_key *key) 104115396Skan{ 105115396Skan key->wk_cipher->ic_detach(key); 106115396Skan} 107115396Skan 108115396Skanstatic __inline void * 109115396Skancipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key) 110115396Skan{ 111115396Skan return key->wk_cipher->ic_attach(vap, key); 112115396Skan} 113115396Skan 114115396Skan/* 115115396Skan * Wrappers for driver key management methods. 116115396Skan */ 117115396Skanstatic __inline int 118115396Skandev_key_alloc(struct ieee80211vap *vap, 119115396Skan struct ieee80211_key *key, 120115396Skan ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 121115396Skan{ 122115396Skan return vap->iv_key_alloc(vap, key, keyix, rxkeyix); 123115396Skan} 124115396Skan 125115396Skanstatic __inline int 126115396Skandev_key_delete(struct ieee80211vap *vap, 127115396Skan const struct ieee80211_key *key) 128115396Skan{ 129115396Skan return vap->iv_key_delete(vap, key); 130115396Skan} 131115396Skan 132115396Skanstatic __inline int 133115396Skandev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key) 134115396Skan{ 135115396Skan return vap->iv_key_set(vap, key, key->wk_macaddr); 136115396Skan} 137115396Skan 138115396Skan/* 139115396Skan * Setup crypto support for a device/shared instance. 140115396Skan */ 141115396Skanvoid 142115396Skanieee80211_crypto_attach(struct ieee80211com *ic) 143115396Skan{ 144115396Skan /* NB: we assume everything is pre-zero'd */ 145115396Skan ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 146115396Skan} 147115396Skan 148115396Skan/* 149115396Skan * Teardown crypto support. 150115396Skan */ 151115396Skanvoid 152115396Skanieee80211_crypto_detach(struct ieee80211com *ic) 153115396Skan{ 154115396Skan} 155115396Skan 156115396Skan/* 157115396Skan * Setup crypto support for a vap. 158115396Skan */ 159115396Skanvoid 160115396Skanieee80211_crypto_vattach(struct ieee80211vap *vap) 161115396Skan{ 162115396Skan int i; 163131575Sstefanf 164115396Skan /* NB: we assume everything is pre-zero'd */ 165115396Skan vap->iv_max_keyix = IEEE80211_WEP_NKID; 166115396Skan vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 167115396Skan for (i = 0; i < IEEE80211_WEP_NKID; i++) 168115396Skan ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 169131575Sstefanf IEEE80211_KEYIX_NONE); 170115396Skan /* 171115396Skan * Initialize the driver key support routines to noop entries. 172115396Skan * This is useful especially for the cipher test modules. 173115396Skan */ 174115396Skan vap->iv_key_alloc = null_key_alloc; 175178807Skib vap->iv_key_set = null_key_set; 176115396Skan vap->iv_key_delete = null_key_delete; 177115396Skan vap->iv_key_update_begin = null_key_update; 178115396Skan vap->iv_key_update_end = null_key_update; 179115396Skan} 180115396Skan 181115396Skan/* 182115396Skan * Teardown crypto support for a vap. 183178807Skib */ 184115396Skanvoid 185216695Skibieee80211_crypto_vdetach(struct ieee80211vap *vap) 186216695Skib{ 187115396Skan ieee80211_crypto_delglobalkeys(vap); 188216695Skib} 189216695Skib 190216695Skib/* 191216695Skib * Register a crypto cipher module. 192183061Sdavidxu */ 193216695Skibvoid 194216695Skibieee80211_crypto_register(const struct ieee80211_cipher *cip) 195216695Skib{ 196115396Skan if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 197115396Skan printf("%s: cipher %s has an invalid cipher index %u\n", 198216695Skib __func__, cip->ic_name, cip->ic_cipher); 199115396Skan return; 200115396Skan } 201216695Skib if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 202216695Skib printf("%s: cipher %s registered with a different template\n", 203115396Skan __func__, cip->ic_name); 204216695Skib return; 205216695Skib } 206216695Skib ciphers[cip->ic_cipher] = cip; 207216695Skib} 208183061Sdavidxu 209216695Skib/* 210216695Skib * Unregister a crypto cipher module. 211216695Skib */ 212115396Skanvoid 213115396Skanieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 214216695Skib{ 215115396Skan if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 216115396Skan printf("%s: cipher %s has an invalid cipher index %u\n", 217115396Skan __func__, cip->ic_name, cip->ic_cipher); 218216695Skib return; 219115396Skan } 220216695Skib if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 221216695Skib printf("%s: cipher %s registered with a different template\n", 222216695Skib __func__, cip->ic_name); 223216695Skib return; 224216695Skib } 225216695Skib /* NB: don't complain about not being registered */ 226216695Skib /* XXX disallow if references */ 227216695Skib ciphers[cip->ic_cipher] = NULL; 228216695Skib} 229216695Skib 230216695Skibint 231216695Skibieee80211_crypto_available(u_int cipher) 232216695Skib{ 233216695Skib return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 234216695Skib} 235115396Skan 236115396Skan/* XXX well-known names! */ 237115396Skanstatic const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 238216695Skib [IEEE80211_CIPHER_WEP] = "wlan_wep", 239115396Skan [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 240216695Skib [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 241216695Skib [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 242216695Skib [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 243216695Skib [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 244216695Skib [IEEE80211_CIPHER_NONE] = "wlan_none", 245216695Skib}; 246115396Skan 247115396Skan/* NB: there must be no overlap between user-supplied and device-owned flags */ 248115396SkanCTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 249216695Skib 250216695Skib/* 251216695Skib * Establish a relationship between the specified key and cipher 252216695Skib * and, if necessary, allocate a hardware index from the driver. 253216695Skib * Note that when a fixed key index is required it must be specified. 254216695Skib * 255216695Skib * This must be the first call applied to a key; all the other key 256216695Skib * routines assume wk_cipher is setup. 257216695Skib * 258216695Skib * Locking must be handled by the caller using: 259216695Skib * ieee80211_key_update_begin(vap); 260218476Skib * ieee80211_key_update_end(vap); 261216695Skib */ 262216695Skibint 263216695Skibieee80211_crypto_newkey(struct ieee80211vap *vap, 264216695Skib int cipher, int flags, struct ieee80211_key *key) 265216695Skib{ 266216695Skib struct ieee80211com *ic = vap->iv_ic; 267216695Skib const struct ieee80211_cipher *cip; 268115396Skan ieee80211_keyix keyix, rxkeyix; 269115396Skan void *keyctx; 270115396Skan int oflags; 271115396Skan 272115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 273115396Skan "%s: cipher %u flags 0x%x keyix %u\n", 274115396Skan __func__, cipher, flags, key->wk_keyix); 275115396Skan 276115396Skan /* 277115396Skan * Validate cipher and set reference to cipher routines. 278115396Skan */ 279115396Skan if (cipher >= IEEE80211_CIPHER_MAX) { 280115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 281115396Skan "%s: invalid cipher %u\n", __func__, cipher); 282115396Skan vap->iv_stats.is_crypto_badcipher++; 283115396Skan return 0; 284115396Skan } 285115396Skan cip = ciphers[cipher]; 286115396Skan if (cip == NULL) { 287115396Skan /* 288115396Skan * Auto-load cipher module if we have a well-known name 289115396Skan * for it. It might be better to use string names rather 290115396Skan * than numbers and craft a module name based on the cipher 291115396Skan * name; e.g. wlan_cipher_<cipher-name>. 292115396Skan */ 293115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 294115396Skan "%s: unregistered cipher %u, load module %s\n", 295115396Skan __func__, cipher, cipher_modnames[cipher]); 296115396Skan ieee80211_load_module(cipher_modnames[cipher]); 297115396Skan /* 298115396Skan * If cipher module loaded it should immediately 299115396Skan * call ieee80211_crypto_register which will fill 300115396Skan * in the entry in the ciphers array. 301115396Skan */ 302115396Skan cip = ciphers[cipher]; 303115396Skan if (cip == NULL) { 304115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 305115396Skan "%s: unable to load cipher %u, module %s\n", 306115396Skan __func__, cipher, cipher_modnames[cipher]); 307115396Skan vap->iv_stats.is_crypto_nocipher++; 308115396Skan return 0; 309115396Skan } 310115396Skan } 311115396Skan 312115396Skan oflags = key->wk_flags; 313115396Skan flags &= IEEE80211_KEY_COMMON; 314115396Skan /* NB: preserve device attributes */ 315115396Skan flags |= (oflags & IEEE80211_KEY_DEVICE); 316115396Skan /* 317115396Skan * If the hardware does not support the cipher then 318115396Skan * fallback to a host-based implementation. 319115396Skan */ 320115396Skan if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 321115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 322115396Skan "%s: no h/w support for cipher %s, falling back to s/w\n", 323115396Skan __func__, cip->ic_name); 324115396Skan flags |= IEEE80211_KEY_SWCRYPT; 325115396Skan } 326115396Skan /* 327115396Skan * Hardware TKIP with software MIC is an important 328115396Skan * combination; we handle it by flagging each key, 329115396Skan * the cipher modules honor it. 330115396Skan */ 331115396Skan if (cipher == IEEE80211_CIPHER_TKIP && 332115396Skan (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 333115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 334115396Skan "%s: no h/w support for TKIP MIC, falling back to s/w\n", 335115396Skan __func__); 336115396Skan flags |= IEEE80211_KEY_SWMIC; 337115396Skan } 338115396Skan 339115396Skan /* 340115396Skan * Bind cipher to key instance. Note we do this 341115396Skan * after checking the device capabilities so the 342115396Skan * cipher module can optimize space usage based on 343115396Skan * whether or not it needs to do the cipher work. 344115396Skan */ 345115396Skan if (key->wk_cipher != cip || key->wk_flags != flags) { 346115396Skan /* 347115396Skan * Fillin the flags so cipher modules can see s/w 348115396Skan * crypto requirements and potentially allocate 349115396Skan * different state and/or attach different method 350115396Skan * pointers. 351115396Skan */ 352115396Skan key->wk_flags = flags; 353115396Skan keyctx = cip->ic_attach(vap, key); 354115396Skan if (keyctx == NULL) { 355115396Skan IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 356115396Skan "%s: unable to attach cipher %s\n", 357115396Skan __func__, cip->ic_name); 358115396Skan key->wk_flags = oflags; /* restore old flags */ 359115396Skan vap->iv_stats.is_crypto_attachfail++; 360115396Skan return 0; 361115396Skan } 362185369Skib cipher_detach(key); 363185369Skib key->wk_cipher = cip; /* XXX refcnt? */ 364185369Skib key->wk_private = keyctx; 365185369Skib } 366216695Skib 367185369Skib /* 368216695Skib * Ask the driver for a key index if we don't have one. 369216695Skib * Note that entries in the global key table always have 370216695Skib * an index; this means it's safe to call this routine 371216695Skib * for these entries just to setup the reference to the 372216695Skib * cipher template. Note also that when using software 373216695Skib * crypto we also call the driver to give us a key index. 374185369Skib */ 375185369Skib if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 376185369Skib if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 377185369Skib /* 378185369Skib * Unable to setup driver state. 379216695Skib */ 380185369Skib vap->iv_stats.is_crypto_keyfail++; 381216695Skib IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 382216695Skib "%s: unable to setup cipher %s\n", 383216695Skib __func__, cip->ic_name); 384216695Skib return 0; 385216695Skib } 386185369Skib if (key->wk_flags != flags) { 387 /* 388 * Driver overrode flags we setup; typically because 389 * resources were unavailable to handle _this_ key. 390 * Re-attach the cipher context to allow cipher 391 * modules to handle differing requirements. 392 */ 393 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 394 "%s: driver override for cipher %s, flags " 395 "0x%x -> 0x%x\n", __func__, cip->ic_name, 396 oflags, key->wk_flags); 397 keyctx = cip->ic_attach(vap, key); 398 if (keyctx == NULL) { 399 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 400 "%s: unable to attach cipher %s with " 401 "flags 0x%x\n", __func__, cip->ic_name, 402 key->wk_flags); 403 key->wk_flags = oflags; /* restore old flags */ 404 vap->iv_stats.is_crypto_attachfail++; 405 return 0; 406 } 407 cipher_detach(key); 408 key->wk_cipher = cip; /* XXX refcnt? */ 409 key->wk_private = keyctx; 410 } 411 key->wk_keyix = keyix; 412 key->wk_rxkeyix = rxkeyix; 413 key->wk_flags |= IEEE80211_KEY_DEVKEY; 414 } 415 return 1; 416} 417 418/* 419 * Remove the key (no locking, for internal use). 420 */ 421static int 422_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 423{ 424 KASSERT(key->wk_cipher != NULL, ("No cipher!")); 425 426 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 427 "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 428 __func__, key->wk_cipher->ic_name, 429 key->wk_keyix, key->wk_flags, 430 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 431 key->wk_keylen); 432 433 if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 434 /* 435 * Remove hardware entry. 436 */ 437 /* XXX key cache */ 438 if (!dev_key_delete(vap, key)) { 439 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 440 "%s: driver did not delete key index %u\n", 441 __func__, key->wk_keyix); 442 vap->iv_stats.is_crypto_delkey++; 443 /* XXX recovery? */ 444 } 445 } 446 cipher_detach(key); 447 memset(key, 0, sizeof(*key)); 448 ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 449 return 1; 450} 451 452/* 453 * Remove the specified key. 454 */ 455int 456ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 457{ 458 int status; 459 460 ieee80211_key_update_begin(vap); 461 status = _ieee80211_crypto_delkey(vap, key); 462 ieee80211_key_update_end(vap); 463 return status; 464} 465 466/* 467 * Clear the global key table. 468 */ 469void 470ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 471{ 472 int i; 473 474 ieee80211_key_update_begin(vap); 475 for (i = 0; i < IEEE80211_WEP_NKID; i++) 476 (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 477 ieee80211_key_update_end(vap); 478} 479 480/* 481 * Set the contents of the specified key. 482 * 483 * Locking must be handled by the caller using: 484 * ieee80211_key_update_begin(vap); 485 * ieee80211_key_update_end(vap); 486 */ 487int 488ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 489{ 490 const struct ieee80211_cipher *cip = key->wk_cipher; 491 492 KASSERT(cip != NULL, ("No cipher!")); 493 494 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 495 "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 496 __func__, cip->ic_name, key->wk_keyix, 497 key->wk_flags, ether_sprintf(key->wk_macaddr), 498 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 499 key->wk_keylen); 500 501 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 502 /* XXX nothing allocated, should not happen */ 503 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 504 "%s: no device key setup done; should not happen!\n", 505 __func__); 506 vap->iv_stats.is_crypto_setkey_nokey++; 507 return 0; 508 } 509 /* 510 * Give cipher a chance to validate key contents. 511 * XXX should happen before modifying state. 512 */ 513 if (!cip->ic_setkey(key)) { 514 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 515 "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 516 __func__, cip->ic_name, key->wk_keyix, 517 key->wk_keylen, key->wk_flags); 518 vap->iv_stats.is_crypto_setkey_cipher++; 519 return 0; 520 } 521 return dev_key_set(vap, key); 522} 523 524uint8_t 525ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 526{ 527 if (k >= &vap->iv_nw_keys[0] && 528 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 529 return (k - vap->iv_nw_keys); 530 else 531 return (0); 532} 533 534struct ieee80211_key * 535ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 536{ 537 struct ieee80211vap *vap = ni->ni_vap; 538 struct ieee80211_frame *wh; 539 540 /* 541 * Multicast traffic always uses the multicast key. 542 * Otherwise if a unicast key is set we use that and 543 * it is always key index 0. When no unicast key is 544 * set we fall back to the default transmit key. 545 */ 546 wh = mtod(m, struct ieee80211_frame *); 547 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 548 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 549 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 550 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 551 wh->i_addr1, 552 "no default transmit key (%s) deftxkey %u", 553 __func__, vap->iv_def_txkey); 554 vap->iv_stats.is_tx_nodefkey++; 555 return NULL; 556 } 557 return &vap->iv_nw_keys[vap->iv_def_txkey]; 558 } 559 560 return &ni->ni_ucastkey; 561} 562 563/* 564 * Add privacy headers appropriate for the specified key. 565 */ 566struct ieee80211_key * 567ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 568{ 569 struct ieee80211_key *k; 570 const struct ieee80211_cipher *cip; 571 572 if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 573 cip = k->wk_cipher; 574 return (cip->ic_encap(k, m) ? k : NULL); 575 } 576 577 return NULL; 578} 579 580/* 581 * Validate and strip privacy headers (and trailer) for a 582 * received frame that has the WEP/Privacy bit set. 583 */ 584struct ieee80211_key * 585ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen) 586{ 587#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 588#define IEEE80211_WEP_MINLEN \ 589 (sizeof(struct ieee80211_frame) + \ 590 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 591 struct ieee80211vap *vap = ni->ni_vap; 592 struct ieee80211_key *k; 593 struct ieee80211_frame *wh; 594 const struct ieee80211_cipher *cip; 595 uint8_t keyid; 596 597 /* NB: this minimum size data frame could be bigger */ 598 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 599 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 600 "%s: WEP data frame too short, len %u\n", 601 __func__, m->m_pkthdr.len); 602 vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 603 return NULL; 604 } 605 606 /* 607 * Locate the key. If unicast and there is no unicast 608 * key then we fall back to the key id in the header. 609 * This assumes unicast keys are only configured when 610 * the key id in the header is meaningless (typically 0). 611 */ 612 wh = mtod(m, struct ieee80211_frame *); 613 m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 614 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 615 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 616 k = &vap->iv_nw_keys[keyid >> 6]; 617 else 618 k = &ni->ni_ucastkey; 619 620 /* 621 * Insure crypto header is contiguous for all decap work. 622 */ 623 cip = k->wk_cipher; 624 if (m->m_len < hdrlen + cip->ic_header && 625 (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 626 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 627 "unable to pullup %s header", cip->ic_name); 628 vap->iv_stats.is_rx_wepfail++; /* XXX */ 629 return NULL; 630 } 631 632 return (cip->ic_decap(k, m, hdrlen) ? k : NULL); 633#undef IEEE80211_WEP_MINLEN 634#undef IEEE80211_WEP_HDRLEN 635} 636 637static void 638load_ucastkey(void *arg, struct ieee80211_node *ni) 639{ 640 struct ieee80211vap *vap = ni->ni_vap; 641 struct ieee80211_key *k; 642 643 if (vap->iv_state != IEEE80211_S_RUN) 644 return; 645 k = &ni->ni_ucastkey; 646 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 647 dev_key_set(vap, k); 648} 649 650/* 651 * Re-load all keys known to the 802.11 layer that may 652 * have hardware state backing them. This is used by 653 * drivers on resume to push keys down into the device. 654 */ 655void 656ieee80211_crypto_reload_keys(struct ieee80211com *ic) 657{ 658 struct ieee80211vap *vap; 659 int i; 660 661 /* 662 * Keys in the global key table of each vap. 663 */ 664 /* NB: used only during resume so don't lock for now */ 665 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 666 if (vap->iv_state != IEEE80211_S_RUN) 667 continue; 668 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 669 const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 670 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 671 dev_key_set(vap, k); 672 } 673 } 674 /* 675 * Unicast keys. 676 */ 677 ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 678} 679