1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Atsushi Onoe 5 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* 31 * IEEE 802.11 generic crypto support. 32 */ 33 #include "opt_wlan.h" 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 40 #include <sys/socket.h> 41 42 #include <net/if.h> 43 #include <net/if_media.h> 44 #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 45 46 #include <net80211/ieee80211_var.h> 47 48 MALLOC_DEFINE(M_80211_CRYPTO, "80211crypto", "802.11 crypto state"); 49 50 static int _ieee80211_crypto_delkey(struct ieee80211vap *, 51 struct ieee80211_key *); 52 53 /* 54 * Table of registered cipher modules. 55 */ 56 static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 57 58 /* 59 * Default "null" key management routines. 60 */ 61 static int 62 null_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 63 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 64 { 65 66 if (!ieee80211_is_key_global(vap, k)) { 67 /* 68 * Not in the global key table, the driver should handle this 69 * by allocating a slot in the h/w key table/cache. In 70 * lieu of that return key slot 0 for any unicast key 71 * request. We disallow the request if this is a group key. 72 * This default policy does the right thing for legacy hardware 73 * with a 4 key table. It also handles devices that pass 74 * packets through untouched when marked with the WEP bit 75 * and key index 0. 76 */ 77 if (k->wk_flags & IEEE80211_KEY_GROUP) 78 return 0; 79 *keyix = 0; /* NB: use key index 0 for ucast key */ 80 } else { 81 *keyix = ieee80211_crypto_get_key_wepidx(vap, k); 82 } 83 *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84 return 1; 85 } 86 static int 87 null_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 88 { 89 return 1; 90 } 91 static int 92 null_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 93 { 94 return 1; 95 } 96 static void null_key_update(struct ieee80211vap *vap) {} 97 98 /* 99 * Write-arounds for common operations. 100 */ 101 static __inline void 102 cipher_detach(struct ieee80211_key *key) 103 { 104 key->wk_cipher->ic_detach(key); 105 } 106 107 static __inline void * 108 cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key) 109 { 110 return key->wk_cipher->ic_attach(vap, key); 111 } 112 113 /* 114 * Wrappers for driver key management methods. 115 */ 116 static __inline int 117 dev_key_alloc(struct ieee80211vap *vap, 118 struct ieee80211_key *key, 119 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 120 { 121 return vap->iv_key_alloc(vap, key, keyix, rxkeyix); 122 } 123 124 static __inline int 125 dev_key_delete(struct ieee80211vap *vap, 126 const struct ieee80211_key *key) 127 { 128 return vap->iv_key_delete(vap, key); 129 } 130 131 static __inline int 132 dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key) 133 { 134 return vap->iv_key_set(vap, key); 135 } 136 137 /* 138 * Setup crypto support for a device/shared instance. 139 */ 140 void 141 ieee80211_crypto_attach(struct ieee80211com *ic) 142 { 143 /* NB: we assume everything is pre-zero'd */ 144 ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 145 146 /* 147 * Default set of net80211 supported ciphers. 148 * 149 * These are the default set that all drivers are expected to 150 * support, either/or in hardware and software. 151 * 152 * Drivers can add their own support to this and the 153 * hardware cipher list (ic_cryptocaps.) 154 */ 155 ic->ic_sw_cryptocaps = IEEE80211_CRYPTO_WEP | 156 IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_AES_CCM; 157 158 /* 159 * Default set of key management types supported by net80211. 160 * 161 * These are supported by software net80211 and announced/ 162 * driven by hostapd + wpa_supplicant. 163 * 164 * Drivers doing full supplicant offload must not set 165 * anything here. 166 * 167 * Note that IEEE80211_C_WPA1 and IEEE80211_C_WPA2 are the 168 * "old" style way of drivers announcing key management 169 * capabilities. There are many, many more key management 170 * suites in 802.11-2016 (see 9.4.2.25.3 - AKM suites.) 171 * For now they still need to be set - these flags are checked 172 * when assembling a beacon to reserve space for the WPA 173 * vendor IE (WPA 1) and RSN IE (WPA 2). 174 */ 175 ic->ic_sw_keymgmtcaps = 0; 176 } 177 178 /* 179 * Teardown crypto support. 180 */ 181 void 182 ieee80211_crypto_detach(struct ieee80211com *ic) 183 { 184 } 185 186 /* 187 * Set the supported ciphers for software encryption. 188 */ 189 void 190 ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic, 191 uint32_t cipher_set) 192 { 193 ic->ic_sw_cryptocaps = cipher_set; 194 } 195 196 /* 197 * Set the supported ciphers for hardware encryption. 198 */ 199 void 200 ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic, 201 uint32_t cipher_set) 202 { 203 ic->ic_cryptocaps = cipher_set; 204 } 205 206 /* 207 * Set the supported software key management by the driver. 208 * 209 * These are the key management suites that are supported via 210 * the driver via hostapd/wpa_supplicant. 211 * 212 * Key management which is completely offloaded (ie, the supplicant 213 * runs in hardware/firmware) must not be set here. 214 */ 215 void 216 ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *ic, 217 uint32_t keymgmt_set) 218 { 219 220 ic->ic_sw_keymgmtcaps = keymgmt_set; 221 } 222 223 /* 224 * Setup crypto support for a vap. 225 */ 226 void 227 ieee80211_crypto_vattach(struct ieee80211vap *vap) 228 { 229 int i; 230 231 /* NB: we assume everything is pre-zero'd */ 232 vap->iv_max_keyix = IEEE80211_WEP_NKID; 233 vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 234 for (i = 0; i < IEEE80211_WEP_NKID; i++) 235 ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 236 IEEE80211_KEYIX_NONE); 237 /* 238 * Initialize the driver key support routines to noop entries. 239 * This is useful especially for the cipher test modules. 240 */ 241 vap->iv_key_alloc = null_key_alloc; 242 vap->iv_key_set = null_key_set; 243 vap->iv_key_delete = null_key_delete; 244 vap->iv_key_update_begin = null_key_update; 245 vap->iv_key_update_end = null_key_update; 246 } 247 248 /* 249 * Teardown crypto support for a vap. 250 */ 251 void 252 ieee80211_crypto_vdetach(struct ieee80211vap *vap) 253 { 254 ieee80211_crypto_delglobalkeys(vap); 255 } 256 257 /* 258 * Register a crypto cipher module. 259 */ 260 void 261 ieee80211_crypto_register(const struct ieee80211_cipher *cip) 262 { 263 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 264 net80211_printf("%s: cipher %s has an invalid cipher index %u\n", 265 __func__, cip->ic_name, cip->ic_cipher); 266 return; 267 } 268 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 269 net80211_printf("%s: cipher %s registered with a different template\n", 270 __func__, cip->ic_name); 271 return; 272 } 273 ciphers[cip->ic_cipher] = cip; 274 } 275 276 /* 277 * Unregister a crypto cipher module. 278 */ 279 void 280 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 281 { 282 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 283 net80211_printf("%s: cipher %s has an invalid cipher index %u\n", 284 __func__, cip->ic_name, cip->ic_cipher); 285 return; 286 } 287 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 288 net80211_printf("%s: cipher %s registered with a different template\n", 289 __func__, cip->ic_name); 290 return; 291 } 292 /* NB: don't complain about not being registered */ 293 /* XXX disallow if references */ 294 ciphers[cip->ic_cipher] = NULL; 295 } 296 297 int 298 ieee80211_crypto_available(u_int cipher) 299 { 300 return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 301 } 302 303 /* XXX well-known names! */ 304 static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 305 [IEEE80211_CIPHER_WEP] = "wlan_wep", 306 [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 307 [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 308 [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 309 [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 310 [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 311 [IEEE80211_CIPHER_NONE] = "wlan_none", 312 [IEEE80211_CIPHER_AES_CCM_256] = "wlan_ccmp", 313 [IEEE80211_CIPHER_BIP_CMAC_128] = "wlan_bip_cmac", 314 [IEEE80211_CIPHER_BIP_CMAC_256] = "wlan_bip_cmac", 315 [IEEE80211_CIPHER_BIP_GMAC_128] = "wlan_bip_gmac", 316 [IEEE80211_CIPHER_BIP_GMAC_256] = "wlan_bip_gmac", 317 [IEEE80211_CIPHER_AES_GCM_128] = "wlan_gcmp", 318 [IEEE80211_CIPHER_AES_GCM_256] = "wlan_gcmp", 319 }; 320 321 /* NB: there must be no overlap between user-supplied and device-owned flags */ 322 CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 323 324 /* 325 * Establish a relationship between the specified key and cipher 326 * and, if necessary, allocate a hardware index from the driver. 327 * Note that when a fixed key index is required it must be specified. 328 * 329 * This must be the first call applied to a key; all the other key 330 * routines assume wk_cipher is setup. 331 * 332 * Locking must be handled by the caller using: 333 * ieee80211_key_update_begin(vap); 334 * ieee80211_key_update_end(vap); 335 */ 336 int 337 ieee80211_crypto_newkey(struct ieee80211vap *vap, 338 int cipher, int flags, struct ieee80211_key *key) 339 { 340 struct ieee80211com *ic = vap->iv_ic; 341 const struct ieee80211_cipher *cip; 342 ieee80211_keyix keyix, rxkeyix; 343 void *keyctx; 344 int oflags; 345 346 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 347 "%s: cipher %u flags 0x%x keyix %u\n", 348 __func__, cipher, flags, key->wk_keyix); 349 350 /* 351 * Validate cipher and set reference to cipher routines. 352 */ 353 if (cipher >= IEEE80211_CIPHER_MAX) { 354 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 355 "%s: invalid cipher %u\n", __func__, cipher); 356 vap->iv_stats.is_crypto_badcipher++; 357 return 0; 358 } 359 cip = ciphers[cipher]; 360 if (cip == NULL) { 361 /* 362 * Auto-load cipher module if we have a well-known name 363 * for it. It might be better to use string names rather 364 * than numbers and craft a module name based on the cipher 365 * name; e.g. wlan_cipher_<cipher-name>. 366 */ 367 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 368 "%s: unregistered cipher %u, load module %s\n", 369 __func__, cipher, cipher_modnames[cipher]); 370 ieee80211_load_module(cipher_modnames[cipher]); 371 /* 372 * If cipher module loaded it should immediately 373 * call ieee80211_crypto_register which will fill 374 * in the entry in the ciphers array. 375 */ 376 cip = ciphers[cipher]; 377 if (cip == NULL) { 378 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 379 "%s: unable to load cipher %u, module %s\n", 380 __func__, cipher, cipher_modnames[cipher]); 381 vap->iv_stats.is_crypto_nocipher++; 382 return 0; 383 } 384 } 385 386 oflags = key->wk_flags; 387 flags &= IEEE80211_KEY_COMMON; 388 /* NB: preserve device attributes */ 389 flags |= (oflags & IEEE80211_KEY_DEVICE); 390 /* 391 * If the hardware does not support the cipher then 392 * fallback to a host-based implementation. 393 */ 394 if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 395 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 396 "%s: no h/w support for cipher %s, falling back to s/w\n", 397 __func__, cip->ic_name); 398 flags |= IEEE80211_KEY_SWCRYPT; 399 } 400 /* 401 * Check if the software cipher is available; if not then 402 * fail it early. 403 * 404 * Some devices do not support all ciphers in software 405 * (for example they don't support a "raw" data path.) 406 */ 407 if ((flags & IEEE80211_KEY_SWCRYPT) && 408 (ic->ic_sw_cryptocaps & (1<<cipher)) == 0) { 409 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 410 "%s: no s/w support for cipher %s, rejecting\n", 411 __func__, cip->ic_name); 412 vap->iv_stats.is_crypto_swcipherfail++; 413 return (0); 414 } 415 /* 416 * Hardware TKIP with software MIC is an important 417 * combination; we handle it by flagging each key, 418 * the cipher modules honor it. 419 */ 420 if (cipher == IEEE80211_CIPHER_TKIP && 421 (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 422 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 423 "%s: no h/w support for TKIP MIC, falling back to s/w\n", 424 __func__); 425 flags |= IEEE80211_KEY_SWMIC; 426 } 427 428 /* 429 * Bind cipher to key instance. Note we do this 430 * after checking the device capabilities so the 431 * cipher module can optimize space usage based on 432 * whether or not it needs to do the cipher work. 433 */ 434 if (key->wk_cipher != cip || key->wk_flags != flags) { 435 /* 436 * Fillin the flags so cipher modules can see s/w 437 * crypto requirements and potentially allocate 438 * different state and/or attach different method 439 * pointers. 440 */ 441 key->wk_flags = flags; 442 keyctx = cip->ic_attach(vap, key); 443 if (keyctx == NULL) { 444 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 445 "%s: unable to attach cipher %s\n", 446 __func__, cip->ic_name); 447 key->wk_flags = oflags; /* restore old flags */ 448 vap->iv_stats.is_crypto_attachfail++; 449 return 0; 450 } 451 cipher_detach(key); 452 key->wk_cipher = cip; /* XXX refcnt? */ 453 key->wk_private = keyctx; 454 } 455 456 /* 457 * Ask the driver for a key index if we don't have one. 458 * Note that entries in the global key table always have 459 * an index; this means it's safe to call this routine 460 * for these entries just to setup the reference to the 461 * cipher template. Note also that when using software 462 * crypto we also call the driver to give us a key index. 463 */ 464 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 465 if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 466 /* 467 * Unable to setup driver state. 468 */ 469 vap->iv_stats.is_crypto_keyfail++; 470 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 471 "%s: unable to setup cipher %s\n", 472 __func__, cip->ic_name); 473 return 0; 474 } 475 if (key->wk_flags != flags) { 476 /* 477 * Driver overrode flags we setup; typically because 478 * resources were unavailable to handle _this_ key. 479 * Re-attach the cipher context to allow cipher 480 * modules to handle differing requirements. 481 */ 482 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 483 "%s: driver override for cipher %s, flags " 484 "%b -> %b\n", __func__, cip->ic_name, 485 oflags, IEEE80211_KEY_BITS, 486 key->wk_flags, IEEE80211_KEY_BITS); 487 keyctx = cip->ic_attach(vap, key); 488 if (keyctx == NULL) { 489 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 490 "%s: unable to attach cipher %s with " 491 "flags %b\n", __func__, cip->ic_name, 492 key->wk_flags, IEEE80211_KEY_BITS); 493 key->wk_flags = oflags; /* restore old flags */ 494 vap->iv_stats.is_crypto_attachfail++; 495 return 0; 496 } 497 cipher_detach(key); 498 key->wk_cipher = cip; /* XXX refcnt? */ 499 key->wk_private = keyctx; 500 } 501 key->wk_keyix = keyix; 502 key->wk_rxkeyix = rxkeyix; 503 key->wk_flags |= IEEE80211_KEY_DEVKEY; 504 } 505 return 1; 506 } 507 508 /* 509 * Remove the key (no locking, for internal use). 510 */ 511 static int 512 _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 513 { 514 KASSERT(key->wk_cipher != NULL, ("No cipher!")); 515 516 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 517 "%s: %s keyix %u flags %b rsc %ju tsc %ju len %u\n", 518 __func__, key->wk_cipher->ic_name, 519 key->wk_keyix, key->wk_flags, IEEE80211_KEY_BITS, 520 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 521 key->wk_keylen); 522 523 if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 524 /* 525 * Remove hardware entry. 526 */ 527 /* XXX key cache */ 528 if (!dev_key_delete(vap, key)) { 529 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 530 "%s: driver did not delete key index %u\n", 531 __func__, key->wk_keyix); 532 vap->iv_stats.is_crypto_delkey++; 533 /* XXX recovery? */ 534 } 535 } 536 cipher_detach(key); 537 memset(key, 0, sizeof(*key)); 538 ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 539 return 1; 540 } 541 542 /* 543 * Remove the specified key. 544 */ 545 int 546 ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 547 { 548 int status; 549 550 ieee80211_key_update_begin(vap); 551 status = _ieee80211_crypto_delkey(vap, key); 552 ieee80211_key_update_end(vap); 553 return status; 554 } 555 556 /* 557 * Clear the global key table. 558 */ 559 void 560 ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 561 { 562 int i; 563 564 ieee80211_key_update_begin(vap); 565 for (i = 0; i < IEEE80211_WEP_NKID; i++) 566 (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 567 ieee80211_key_update_end(vap); 568 } 569 570 /* 571 * Set the contents of the specified key. 572 * 573 * Locking must be handled by the caller using: 574 * ieee80211_key_update_begin(vap); 575 * ieee80211_key_update_end(vap); 576 */ 577 int 578 ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 579 { 580 const struct ieee80211_cipher *cip = key->wk_cipher; 581 582 KASSERT(cip != NULL, ("No cipher!")); 583 584 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 585 "%s: %s keyix %u flags %b mac %s rsc %ju tsc %ju len %u\n", 586 __func__, cip->ic_name, key->wk_keyix, 587 key->wk_flags, IEEE80211_KEY_BITS, ether_sprintf(key->wk_macaddr), 588 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 589 key->wk_keylen); 590 591 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 592 /* XXX nothing allocated, should not happen */ 593 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 594 "%s: no device key setup done; should not happen!\n", 595 __func__); 596 vap->iv_stats.is_crypto_setkey_nokey++; 597 return 0; 598 } 599 /* 600 * Give cipher a chance to validate key contents. 601 * XXX should happen before modifying state. 602 */ 603 if (!cip->ic_setkey(key)) { 604 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 605 "%s: cipher %s rejected key index %u len %u flags %b\n", 606 __func__, cip->ic_name, key->wk_keyix, 607 key->wk_keylen, key->wk_flags, IEEE80211_KEY_BITS); 608 vap->iv_stats.is_crypto_setkey_cipher++; 609 return 0; 610 } 611 return dev_key_set(vap, key); 612 } 613 614 /** 615 * @brief Return index if the key is a WEP key (0..3); -1 otherwise. 616 * 617 * This is different to "get_keyid" which defaults to returning 618 * 0 for unicast keys; it assumes that it won't be used for WEP. 619 * 620 * @param vap the current VAP 621 * @param k ieee80211_key to check 622 * @returns 0..3 if it's a global/WEP key, -1 otherwise. 623 */ 624 int 625 ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap, 626 const struct ieee80211_key *k) 627 { 628 629 if (ieee80211_is_key_global(vap, k)) { 630 return (k - vap->iv_nw_keys); 631 } 632 return (-1); 633 } 634 635 /** 636 * @brief Return the index of a unicast, global or IGTK key. 637 * 638 * Return the index of a key. For unicast keys the index is 0..1. 639 * For global/WEP keys it's 0..3. For IGTK keys its 4..5. 640 * 641 * TODO: support >1 unicast key 642 * TODO: support IGTK keys 643 * 644 * @param vap the current VAP 645 * @param k ieee80211_key to check 646 * @returns 0..3 for a WEP/global key, 0..1 for unicast key, 4..5 for IGTK key 647 */ 648 uint8_t 649 ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 650 { 651 if (ieee80211_is_key_global(vap, k)) { 652 return (k - vap->iv_nw_keys); 653 } 654 655 return (0); 656 } 657 658 /** 659 * @param Return the key to use for encrypting an mbuf frame to a node 660 * 661 * This routine chooses a suitable key used to encrypt the given frame with. 662 * It doesn't do the encryption; it only chooses the key. If a key is not 663 * available then the routine will return NULL. 664 * 665 * It's up to the caller to enforce whether a key is absolutely required or not. 666 * 667 * @param ni The ieee80211_node to send the frame to 668 * @param m the mbuf to encrypt 669 * @returns the ieee80211_key to encrypt with, or NULL if there's no suitable key 670 */ 671 struct ieee80211_key * 672 ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 673 { 674 struct ieee80211vap *vap = ni->ni_vap; 675 struct ieee80211_frame *wh; 676 677 /* 678 * Multicast traffic always uses the multicast key. 679 * 680 * Historically we would fall back to the default 681 * transmit key if there was no unicast key. This 682 * behaviour was documented up to IEEE Std 802.11-2016, 683 * 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the 684 * 'else' case but is no longer in later versions of 685 * the standard. Additionally falling back to the 686 * group key for unicast was a security risk. 687 */ 688 wh = mtod(m, struct ieee80211_frame *); 689 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 690 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 691 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 692 wh->i_addr1, 693 "no default transmit key (%s) deftxkey %u", 694 __func__, vap->iv_def_txkey); 695 vap->iv_stats.is_tx_nodefkey++; 696 return NULL; 697 } 698 return &vap->iv_nw_keys[vap->iv_def_txkey]; 699 } 700 701 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 702 return NULL; 703 return &ni->ni_ucastkey; 704 } 705 706 /** 707 * @brief Privacy encapsulate and encrypt the given mbuf. 708 * 709 * This routine handles the mechanics of encryption - expanding the 710 * mbuf to add privacy headers, IV, ICV, MIC, MMIC, and then encrypts 711 * the given mbuf if required. 712 * 713 * This should be called by the driver in its TX path as part of 714 * encapsulation before passing frames to the hardware/firmware 715 * queues. 716 * 717 * Drivers/hardware which does its own entirely offload path 718 * should still call this for completeness - it indicates to the 719 * driver that the frame itself should be encrypted. 720 * 721 * The driver should have set capability bits in the attach / 722 * key allocation path to disable various encapsulation/encryption 723 * features. 724 * 725 * @param ni ieee80211_node for this frame 726 * @param mbuf mbuf to modify 727 * @returns the key used if the frame is to be encrypted, NULL otherwise 728 */ 729 struct ieee80211_key * 730 ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 731 { 732 struct ieee80211_key *k; 733 const struct ieee80211_cipher *cip; 734 735 if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 736 cip = k->wk_cipher; 737 return (cip->ic_encap(k, m) ? k : NULL); 738 } 739 740 return NULL; 741 } 742 743 /** 744 * @brief Decapsulate and validate an encrypted frame. 745 * 746 * This handles an encrypted frame (one with the privacy bit set.) 747 * It also obeys the key / config / receive packet flags for how 748 * the driver says its already been processed. 749 * 750 * Unlike ieee80211_crypto_encap(), this isn't called in the driver. 751 * Instead, drivers passed the potentially decrypted frame - fully, 752 * partial, or not at all - and net80211 will call this as appropriate. 753 * 754 * This handles NICs (like ath(4)) which have a variable size between 755 * the 802.11 header and 802.11 payload due to DMA alignment / encryption 756 * engine concerns. 757 * 758 * If the frame was decrypted and validated successfully then 1 is returned 759 * and the mbuf can be treated as an 802.11 frame. If it is not decrypted 760 * successfully or it was decrypted but failed validation/checks, then 761 * 0 is returned. 762 * 763 * @param ni ieee80211_node for received frame 764 * @param m mbuf frame to receive 765 * @param hdrlen length of the 802.11 header, including trailing null bytes 766 * @param key pointer to ieee80211_key that will be set if appropriate 767 * @returns 0 if the frame wasn't decrypted/validated, 1 if decrypted/validated. 768 */ 769 int 770 ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen, 771 struct ieee80211_key **key) 772 { 773 #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 774 #define IEEE80211_WEP_MINLEN \ 775 (sizeof(struct ieee80211_frame) + \ 776 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 777 struct ieee80211vap *vap = ni->ni_vap; 778 struct ieee80211_key *k; 779 struct ieee80211_frame *wh; 780 const struct ieee80211_rx_stats *rxs; 781 const struct ieee80211_cipher *cip; 782 uint8_t keyid; 783 784 /* 785 * Check for hardware decryption and IV stripping. 786 * If the IV is stripped then we definitely can't find a key. 787 * Set the key to NULL but return true; upper layers 788 * will need to handle a NULL key for a successful 789 * decrypt. 790 */ 791 rxs = ieee80211_get_rx_params_ptr(m); 792 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 793 if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) { 794 /* 795 * Hardware decrypted, IV stripped. 796 * We can't find a key with a stripped IV. 797 * Return successful. 798 */ 799 *key = NULL; 800 return (1); 801 } 802 } 803 804 /* NB: this minimum size data frame could be bigger */ 805 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 806 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 807 "%s: WEP data frame too short, len %u\n", 808 __func__, m->m_pkthdr.len); 809 vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 810 *key = NULL; 811 return (0); 812 } 813 814 /* 815 * Locate the key. If unicast and there is no unicast 816 * key then we fall back to the key id in the header. 817 * This assumes unicast keys are only configured when 818 * the key id in the header is meaningless (typically 0). 819 */ 820 wh = mtod(m, struct ieee80211_frame *); 821 m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 822 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 823 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 824 k = &vap->iv_nw_keys[keyid >> 6]; 825 else 826 k = &ni->ni_ucastkey; 827 828 /* 829 * Ensure crypto header is contiguous and long enough for all 830 * decap work. 831 */ 832 cip = k->wk_cipher; 833 if (m->m_len < hdrlen + cip->ic_header) { 834 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 835 "frame is too short (%d < %u) for crypto decap", 836 cip->ic_name, m->m_len, hdrlen + cip->ic_header); 837 vap->iv_stats.is_rx_tooshort++; 838 *key = NULL; 839 return (0); 840 } 841 842 /* 843 * Attempt decryption. 844 * 845 * If we fail then don't return the key - return NULL 846 * and an error. 847 */ 848 if (cip->ic_decap(k, m, hdrlen)) { 849 /* success */ 850 *key = k; 851 return (1); 852 } 853 854 /* Failure */ 855 *key = NULL; 856 return (0); 857 #undef IEEE80211_WEP_MINLEN 858 #undef IEEE80211_WEP_HDRLEN 859 } 860 861 /** 862 * @brief Check and remove any post-defragmentation MIC from an MSDU. 863 * 864 * This is called after defragmentation. Crypto types that implement 865 * a MIC/ICV check per MSDU will not implement this function. 866 * 867 * As an example, TKIP decapsulation covers both MIC/ICV checks per 868 * MPDU (the "WEP" ICV) and then a Michael MIC verification on the 869 * defragmented MSDU. Please see 802.11-2020 12.5.2.1.3 (TKIP decapsulation) 870 * for more information. 871 * 872 * @param vap the current VAP 873 * @param k the current key 874 * @param m the mbuf representing the MSDU 875 * @param f set to 1 to force a MSDU MIC check, even if HW decrypted 876 * @returns 0 if error / MIC check failed, 1 if OK 877 */ 878 int 879 ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k, 880 struct mbuf *m, int force) 881 { 882 const struct ieee80211_cipher *cip; 883 const struct ieee80211_rx_stats *rxs; 884 struct ieee80211_frame *wh; 885 886 rxs = ieee80211_get_rx_params_ptr(m); 887 wh = mtod(m, struct ieee80211_frame *); 888 889 /* 890 * Handle demic / mic errors from hardware-decrypted offload devices. 891 */ 892 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 893 if ((rxs->c_pktflags & IEEE80211_RX_F_FAIL_MMIC) != 0) { 894 /* 895 * Hardware has said MMIC failed. We don't care about 896 * whether it was stripped or not. 897 * 898 * Eventually - teach the demic methods in crypto 899 * modules to handle a NULL key and not to dereference 900 * it. 901 */ 902 ieee80211_notify_michael_failure(vap, wh, 903 IEEE80211_KEYIX_NONE); 904 return (0); 905 } 906 907 if ((rxs->c_pktflags & 908 (IEEE80211_RX_F_MIC_STRIP|IEEE80211_RX_F_MMIC_STRIP)) != 0) { 909 /* 910 * Hardware has decrypted and not indicated a 911 * MIC failure and has stripped the MIC. 912 * We may not have a key, so for now just 913 * return OK. 914 */ 915 return (1); 916 } 917 } 918 919 /* 920 * If we don't have a key at this point then we don't 921 * have to demic anything. 922 */ 923 if (k == NULL) 924 return (1); 925 926 cip = k->wk_cipher; 927 return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1); 928 } 929 930 static void 931 load_ucastkey(void *arg, struct ieee80211_node *ni) 932 { 933 struct ieee80211vap *vap = ni->ni_vap; 934 struct ieee80211_key *k; 935 936 if (vap->iv_state != IEEE80211_S_RUN) 937 return; 938 k = &ni->ni_ucastkey; 939 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 940 dev_key_set(vap, k); 941 } 942 943 /* 944 * Re-load all keys known to the 802.11 layer that may 945 * have hardware state backing them. This is used by 946 * drivers on resume to push keys down into the device. 947 */ 948 void 949 ieee80211_crypto_reload_keys(struct ieee80211com *ic) 950 { 951 struct ieee80211vap *vap; 952 int i; 953 954 /* 955 * Keys in the global key table of each vap. 956 */ 957 /* NB: used only during resume so don't lock for now */ 958 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 959 if (vap->iv_state != IEEE80211_S_RUN) 960 continue; 961 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 962 const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 963 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 964 dev_key_set(vap, k); 965 } 966 } 967 /* 968 * Unicast keys. 969 */ 970 ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 971 } 972 973 /* 974 * Set the default key index for WEP, or KEYIX_NONE for no default TX key. 975 * 976 * This should be done as part of a key update block (iv_key_update_begin / 977 * iv_key_update_end.) 978 */ 979 void 980 ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid) 981 { 982 983 /* XXX TODO: assert we're in a key update block */ 984 985 vap->iv_update_deftxkey(vap, kid); 986 } 987 988 /** 989 * @brief Calculate the AAD required for this frame for AES-GCM/AES-CCM. 990 * 991 * The contents are described in 802.11-2020 12.5.3.3.3 (Construct AAD) 992 * under AES-CCM and are shared with AES-GCM as covered in 12.5.5.3.3 993 * (Construct AAD) (AES-GCM). 994 * 995 * NOTE: the first two bytes are a 16 bit big-endian length, which are used 996 * by AES-CCM as part of the Adata field (RFC 3610, section 2.2 997 * (Authentication)) to indicate the length of the Adata field itself. 998 * Since this is small and fits in 0xfeff bytes, the length field 999 * uses the two byte big endian option. 1000 * 1001 * AES-GCM doesn't require the length at the beginning and will need to 1002 * skip it. 1003 * 1004 * TODO: net80211 currently doesn't support negotiating SPP (Signaling 1005 * and Payload Protected A-MSDUs) and thus bit 7 of the QoS control field 1006 * is always masked. 1007 * 1008 * TODO: net80211 currently doesn't support DMG (802.11ad) so bit 7 1009 * (A-MSDU present) and bit 8 (A-MSDU type) are always masked. 1010 * 1011 * @param wh 802.11 frame to calculate the AAD over 1012 * @param aad AAD (additional authentication data) buffer 1013 * @param len The AAD buffer length in bytes. 1014 * @returns The number of AAD payload bytes (ignoring the first two 1015 * bytes, which are the AAD payload length in big-endian). 1016 */ 1017 uint16_t 1018 ieee80211_crypto_init_aad(const struct ieee80211_frame *wh, uint8_t *aad, 1019 int len) 1020 { 1021 uint16_t aad_len; 1022 1023 memset(aad, 0, len); 1024 1025 /* 1026 * AAD for PV0 MPDUs: 1027 * 1028 * FC with bits 4..6 and 11..13 masked to zero; 14 is always one 1029 * A1 | A2 | A3 1030 * SC with bits 4..15 (seq#) masked to zero 1031 * A4 (if present) 1032 * QC (if present) 1033 */ 1034 aad[0] = 0; /* AAD length >> 8 */ 1035 /* NB: aad[1] set below */ 1036 aad[2] = wh->i_fc[0] & 0x8f; /* see above for bitfields */ 1037 aad[3] = wh->i_fc[1] & 0xc7; /* see above for bitfields */ 1038 /* mask aad[3] b7 if frame is data frame w/ QoS control field */ 1039 if (IEEE80211_IS_QOS_ANY(wh)) 1040 aad[3] &= 0x7f; 1041 1042 /* NB: we know 3 addresses are contiguous */ 1043 memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN); 1044 aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK; 1045 aad[23] = 0; /* all bits masked */ 1046 /* 1047 * Construct variable-length portion of AAD based 1048 * on whether this is a 4-address frame/QOS frame. 1049 * We always zero-pad to 32 bytes before running it 1050 * through the cipher. 1051 */ 1052 if (IEEE80211_IS_DSTODS(wh)) { 1053 IEEE80211_ADDR_COPY(aad + 24, 1054 ((const struct ieee80211_frame_addr4 *)wh)->i_addr4); 1055 if (IEEE80211_IS_QOS_ANY(wh)) { 1056 const struct ieee80211_qosframe_addr4 *qwh4 = 1057 (const struct ieee80211_qosframe_addr4 *) wh; 1058 /* TODO: SPP A-MSDU / A-MSDU present bit */ 1059 aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */ 1060 aad[31] = 0; 1061 aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN + 2; 1062 } else { 1063 *(uint16_t *)&aad[30] = 0; 1064 aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN; 1065 } 1066 } else { 1067 if (IEEE80211_IS_QOS_ANY(wh)) { 1068 const struct ieee80211_qosframe *qwh = 1069 (const struct ieee80211_qosframe*) wh; 1070 /* TODO: SPP A-MSDU / A-MSDU present bit */ 1071 aad[24] = qwh->i_qos[0] & 0x0f; /* just priority bits */ 1072 aad[25] = 0; 1073 aad_len = aad[1] = 22 + 2; 1074 } else { 1075 *(uint16_t *)&aad[24] = 0; 1076 aad_len = aad[1] = 22; 1077 } 1078 *(uint16_t *)&aad[26] = 0; 1079 *(uint32_t *)&aad[28] = 0; 1080 } 1081 1082 return (aad_len); 1083 } 1084