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 if (!(&vap->iv_nw_keys[0] <= k && 66 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 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 /* 160 * Teardown crypto support. 161 */ 162 void 163 ieee80211_crypto_detach(struct ieee80211com *ic) 164 { 165 } 166 167 /* 168 * Set the supported ciphers for software encryption. 169 */ 170 void 171 ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic, 172 uint32_t cipher_set) 173 { 174 ic->ic_sw_cryptocaps = cipher_set; 175 } 176 177 /* 178 * Set the supported ciphers for hardware encryption. 179 */ 180 void 181 ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic, 182 uint32_t cipher_set) 183 { 184 ic->ic_cryptocaps = cipher_set; 185 } 186 187 188 /* 189 * Setup crypto support for a vap. 190 */ 191 void 192 ieee80211_crypto_vattach(struct ieee80211vap *vap) 193 { 194 int i; 195 196 /* NB: we assume everything is pre-zero'd */ 197 vap->iv_max_keyix = IEEE80211_WEP_NKID; 198 vap->iv_def_txkey = IEEE80211_KEYIX_NONE; 199 for (i = 0; i < IEEE80211_WEP_NKID; i++) 200 ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i], 201 IEEE80211_KEYIX_NONE); 202 /* 203 * Initialize the driver key support routines to noop entries. 204 * This is useful especially for the cipher test modules. 205 */ 206 vap->iv_key_alloc = null_key_alloc; 207 vap->iv_key_set = null_key_set; 208 vap->iv_key_delete = null_key_delete; 209 vap->iv_key_update_begin = null_key_update; 210 vap->iv_key_update_end = null_key_update; 211 } 212 213 /* 214 * Teardown crypto support for a vap. 215 */ 216 void 217 ieee80211_crypto_vdetach(struct ieee80211vap *vap) 218 { 219 ieee80211_crypto_delglobalkeys(vap); 220 } 221 222 /* 223 * Register a crypto cipher module. 224 */ 225 void 226 ieee80211_crypto_register(const struct ieee80211_cipher *cip) 227 { 228 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 229 printf("%s: cipher %s has an invalid cipher index %u\n", 230 __func__, cip->ic_name, cip->ic_cipher); 231 return; 232 } 233 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 234 printf("%s: cipher %s registered with a different template\n", 235 __func__, cip->ic_name); 236 return; 237 } 238 ciphers[cip->ic_cipher] = cip; 239 } 240 241 /* 242 * Unregister a crypto cipher module. 243 */ 244 void 245 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 246 { 247 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 248 printf("%s: cipher %s has an invalid cipher index %u\n", 249 __func__, cip->ic_name, cip->ic_cipher); 250 return; 251 } 252 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 253 printf("%s: cipher %s registered with a different template\n", 254 __func__, cip->ic_name); 255 return; 256 } 257 /* NB: don't complain about not being registered */ 258 /* XXX disallow if references */ 259 ciphers[cip->ic_cipher] = NULL; 260 } 261 262 int 263 ieee80211_crypto_available(u_int cipher) 264 { 265 return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 266 } 267 268 /* XXX well-known names! */ 269 static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = { 270 [IEEE80211_CIPHER_WEP] = "wlan_wep", 271 [IEEE80211_CIPHER_TKIP] = "wlan_tkip", 272 [IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb", 273 [IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp", 274 [IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */ 275 [IEEE80211_CIPHER_CKIP] = "wlan_ckip", 276 [IEEE80211_CIPHER_NONE] = "wlan_none", 277 [IEEE80211_CIPHER_AES_CCM_256] = "wlan_ccmp", 278 [IEEE80211_CIPHER_BIP_CMAC_128] = "wlan_bip_cmac", 279 [IEEE80211_CIPHER_BIP_CMAC_256] = "wlan_bip_cmac", 280 [IEEE80211_CIPHER_BIP_GMAC_128] = "wlan_bip_gmac", 281 [IEEE80211_CIPHER_BIP_GMAC_256] = "wlan_bip_gmac", 282 [IEEE80211_CIPHER_AES_GCM_128] = "wlan_gcmp", 283 [IEEE80211_CIPHER_AES_GCM_256] = "wlan_gcmp", 284 }; 285 286 /* NB: there must be no overlap between user-supplied and device-owned flags */ 287 CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0); 288 289 /* 290 * Establish a relationship between the specified key and cipher 291 * and, if necessary, allocate a hardware index from the driver. 292 * Note that when a fixed key index is required it must be specified. 293 * 294 * This must be the first call applied to a key; all the other key 295 * routines assume wk_cipher is setup. 296 * 297 * Locking must be handled by the caller using: 298 * ieee80211_key_update_begin(vap); 299 * ieee80211_key_update_end(vap); 300 */ 301 int 302 ieee80211_crypto_newkey(struct ieee80211vap *vap, 303 int cipher, int flags, struct ieee80211_key *key) 304 { 305 struct ieee80211com *ic = vap->iv_ic; 306 const struct ieee80211_cipher *cip; 307 ieee80211_keyix keyix, rxkeyix; 308 void *keyctx; 309 int oflags; 310 311 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 312 "%s: cipher %u flags 0x%x keyix %u\n", 313 __func__, cipher, flags, key->wk_keyix); 314 315 /* 316 * Validate cipher and set reference to cipher routines. 317 */ 318 if (cipher >= IEEE80211_CIPHER_MAX) { 319 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 320 "%s: invalid cipher %u\n", __func__, cipher); 321 vap->iv_stats.is_crypto_badcipher++; 322 return 0; 323 } 324 cip = ciphers[cipher]; 325 if (cip == NULL) { 326 /* 327 * Auto-load cipher module if we have a well-known name 328 * for it. It might be better to use string names rather 329 * than numbers and craft a module name based on the cipher 330 * name; e.g. wlan_cipher_<cipher-name>. 331 */ 332 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 333 "%s: unregistered cipher %u, load module %s\n", 334 __func__, cipher, cipher_modnames[cipher]); 335 ieee80211_load_module(cipher_modnames[cipher]); 336 /* 337 * If cipher module loaded it should immediately 338 * call ieee80211_crypto_register which will fill 339 * in the entry in the ciphers array. 340 */ 341 cip = ciphers[cipher]; 342 if (cip == NULL) { 343 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 344 "%s: unable to load cipher %u, module %s\n", 345 __func__, cipher, cipher_modnames[cipher]); 346 vap->iv_stats.is_crypto_nocipher++; 347 return 0; 348 } 349 } 350 351 oflags = key->wk_flags; 352 flags &= IEEE80211_KEY_COMMON; 353 /* NB: preserve device attributes */ 354 flags |= (oflags & IEEE80211_KEY_DEVICE); 355 /* 356 * If the hardware does not support the cipher then 357 * fallback to a host-based implementation. 358 */ 359 if ((ic->ic_cryptocaps & (1<<cipher)) == 0) { 360 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 361 "%s: no h/w support for cipher %s, falling back to s/w\n", 362 __func__, cip->ic_name); 363 flags |= IEEE80211_KEY_SWCRYPT; 364 } 365 /* 366 * Hardware TKIP with software MIC is an important 367 * combination; we handle it by flagging each key, 368 * the cipher modules honor it. 369 */ 370 if (cipher == IEEE80211_CIPHER_TKIP && 371 (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) { 372 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 373 "%s: no h/w support for TKIP MIC, falling back to s/w\n", 374 __func__); 375 flags |= IEEE80211_KEY_SWMIC; 376 } 377 378 /* 379 * Bind cipher to key instance. Note we do this 380 * after checking the device capabilities so the 381 * cipher module can optimize space usage based on 382 * whether or not it needs to do the cipher work. 383 */ 384 if (key->wk_cipher != cip || key->wk_flags != flags) { 385 /* 386 * Fillin the flags so cipher modules can see s/w 387 * crypto requirements and potentially allocate 388 * different state and/or attach different method 389 * pointers. 390 */ 391 key->wk_flags = flags; 392 keyctx = cip->ic_attach(vap, key); 393 if (keyctx == NULL) { 394 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 395 "%s: unable to attach cipher %s\n", 396 __func__, cip->ic_name); 397 key->wk_flags = oflags; /* restore old flags */ 398 vap->iv_stats.is_crypto_attachfail++; 399 return 0; 400 } 401 cipher_detach(key); 402 key->wk_cipher = cip; /* XXX refcnt? */ 403 key->wk_private = keyctx; 404 } 405 406 /* 407 * Ask the driver for a key index if we don't have one. 408 * Note that entries in the global key table always have 409 * an index; this means it's safe to call this routine 410 * for these entries just to setup the reference to the 411 * cipher template. Note also that when using software 412 * crypto we also call the driver to give us a key index. 413 */ 414 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 415 if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) { 416 /* 417 * Unable to setup driver state. 418 */ 419 vap->iv_stats.is_crypto_keyfail++; 420 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 421 "%s: unable to setup cipher %s\n", 422 __func__, cip->ic_name); 423 return 0; 424 } 425 if (key->wk_flags != flags) { 426 /* 427 * Driver overrode flags we setup; typically because 428 * resources were unavailable to handle _this_ key. 429 * Re-attach the cipher context to allow cipher 430 * modules to handle differing requirements. 431 */ 432 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 433 "%s: driver override for cipher %s, flags " 434 "0x%x -> 0x%x\n", __func__, cip->ic_name, 435 oflags, key->wk_flags); 436 keyctx = cip->ic_attach(vap, key); 437 if (keyctx == NULL) { 438 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 439 "%s: unable to attach cipher %s with " 440 "flags 0x%x\n", __func__, cip->ic_name, 441 key->wk_flags); 442 key->wk_flags = oflags; /* restore old flags */ 443 vap->iv_stats.is_crypto_attachfail++; 444 return 0; 445 } 446 cipher_detach(key); 447 key->wk_cipher = cip; /* XXX refcnt? */ 448 key->wk_private = keyctx; 449 } 450 key->wk_keyix = keyix; 451 key->wk_rxkeyix = rxkeyix; 452 key->wk_flags |= IEEE80211_KEY_DEVKEY; 453 } 454 return 1; 455 } 456 457 /* 458 * Remove the key (no locking, for internal use). 459 */ 460 static int 461 _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 462 { 463 KASSERT(key->wk_cipher != NULL, ("No cipher!")); 464 465 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 466 "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 467 __func__, key->wk_cipher->ic_name, 468 key->wk_keyix, key->wk_flags, 469 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 470 key->wk_keylen); 471 472 if (key->wk_flags & IEEE80211_KEY_DEVKEY) { 473 /* 474 * Remove hardware entry. 475 */ 476 /* XXX key cache */ 477 if (!dev_key_delete(vap, key)) { 478 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 479 "%s: driver did not delete key index %u\n", 480 __func__, key->wk_keyix); 481 vap->iv_stats.is_crypto_delkey++; 482 /* XXX recovery? */ 483 } 484 } 485 cipher_detach(key); 486 memset(key, 0, sizeof(*key)); 487 ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE); 488 return 1; 489 } 490 491 /* 492 * Remove the specified key. 493 */ 494 int 495 ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key) 496 { 497 int status; 498 499 ieee80211_key_update_begin(vap); 500 status = _ieee80211_crypto_delkey(vap, key); 501 ieee80211_key_update_end(vap); 502 return status; 503 } 504 505 /* 506 * Clear the global key table. 507 */ 508 void 509 ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap) 510 { 511 int i; 512 513 ieee80211_key_update_begin(vap); 514 for (i = 0; i < IEEE80211_WEP_NKID; i++) 515 (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]); 516 ieee80211_key_update_end(vap); 517 } 518 519 /* 520 * Set the contents of the specified key. 521 * 522 * Locking must be handled by the caller using: 523 * ieee80211_key_update_begin(vap); 524 * ieee80211_key_update_end(vap); 525 */ 526 int 527 ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key) 528 { 529 const struct ieee80211_cipher *cip = key->wk_cipher; 530 531 KASSERT(cip != NULL, ("No cipher!")); 532 533 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 534 "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 535 __func__, cip->ic_name, key->wk_keyix, 536 key->wk_flags, ether_sprintf(key->wk_macaddr), 537 key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc, 538 key->wk_keylen); 539 540 if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) { 541 /* XXX nothing allocated, should not happen */ 542 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 543 "%s: no device key setup done; should not happen!\n", 544 __func__); 545 vap->iv_stats.is_crypto_setkey_nokey++; 546 return 0; 547 } 548 /* 549 * Give cipher a chance to validate key contents. 550 * XXX should happen before modifying state. 551 */ 552 if (!cip->ic_setkey(key)) { 553 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO, 554 "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 555 __func__, cip->ic_name, key->wk_keyix, 556 key->wk_keylen, key->wk_flags); 557 vap->iv_stats.is_crypto_setkey_cipher++; 558 return 0; 559 } 560 return dev_key_set(vap, key); 561 } 562 563 /* 564 * Return index if the key is a WEP key (0..3); -1 otherwise. 565 * 566 * This is different to "get_keyid" which defaults to returning 567 * 0 for unicast keys; it assumes that it won't be used for WEP. 568 */ 569 int 570 ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap, 571 const struct ieee80211_key *k) 572 { 573 574 if (k >= &vap->iv_nw_keys[0] && 575 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 576 return (k - vap->iv_nw_keys); 577 return (-1); 578 } 579 580 /* 581 * Note: only supports a single unicast key (0). 582 */ 583 uint8_t 584 ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k) 585 { 586 if (k >= &vap->iv_nw_keys[0] && 587 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) 588 return (k - vap->iv_nw_keys); 589 else 590 return (0); 591 } 592 593 struct ieee80211_key * 594 ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m) 595 { 596 struct ieee80211vap *vap = ni->ni_vap; 597 struct ieee80211_frame *wh; 598 599 /* 600 * Multicast traffic always uses the multicast key. 601 * 602 * Historically we would fall back to the default 603 * transmit key if there was no unicast key. This 604 * behaviour was documented up to IEEE Std 802.11-2016, 605 * 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the 606 * 'else' case but is no longer in later versions of 607 * the standard. Additionally falling back to the 608 * group key for unicast was a security risk. 609 */ 610 wh = mtod(m, struct ieee80211_frame *); 611 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 612 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) { 613 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, 614 wh->i_addr1, 615 "no default transmit key (%s) deftxkey %u", 616 __func__, vap->iv_def_txkey); 617 vap->iv_stats.is_tx_nodefkey++; 618 return NULL; 619 } 620 return &vap->iv_nw_keys[vap->iv_def_txkey]; 621 } 622 623 if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 624 return NULL; 625 return &ni->ni_ucastkey; 626 } 627 628 /* 629 * Add privacy headers appropriate for the specified key. 630 */ 631 struct ieee80211_key * 632 ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m) 633 { 634 struct ieee80211_key *k; 635 const struct ieee80211_cipher *cip; 636 637 if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) { 638 cip = k->wk_cipher; 639 return (cip->ic_encap(k, m) ? k : NULL); 640 } 641 642 return NULL; 643 } 644 645 /* 646 * Validate and strip privacy headers (and trailer) for a 647 * received frame that has the WEP/Privacy bit set. 648 */ 649 int 650 ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen, 651 struct ieee80211_key **key) 652 { 653 #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 654 #define IEEE80211_WEP_MINLEN \ 655 (sizeof(struct ieee80211_frame) + \ 656 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 657 struct ieee80211vap *vap = ni->ni_vap; 658 struct ieee80211_key *k; 659 struct ieee80211_frame *wh; 660 const struct ieee80211_rx_stats *rxs; 661 const struct ieee80211_cipher *cip; 662 uint8_t keyid; 663 664 /* 665 * Check for hardware decryption and IV stripping. 666 * If the IV is stripped then we definitely can't find a key. 667 * Set the key to NULL but return true; upper layers 668 * will need to handle a NULL key for a successful 669 * decrypt. 670 */ 671 rxs = ieee80211_get_rx_params_ptr(m); 672 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 673 if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) { 674 /* 675 * Hardware decrypted, IV stripped. 676 * We can't find a key with a stripped IV. 677 * Return successful. 678 */ 679 *key = NULL; 680 return (1); 681 } 682 } 683 684 /* NB: this minimum size data frame could be bigger */ 685 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 686 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 687 "%s: WEP data frame too short, len %u\n", 688 __func__, m->m_pkthdr.len); 689 vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */ 690 *key = NULL; 691 return (0); 692 } 693 694 /* 695 * Locate the key. If unicast and there is no unicast 696 * key then we fall back to the key id in the header. 697 * This assumes unicast keys are only configured when 698 * the key id in the header is meaningless (typically 0). 699 */ 700 wh = mtod(m, struct ieee80211_frame *); 701 m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid); 702 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 703 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 704 k = &vap->iv_nw_keys[keyid >> 6]; 705 else 706 k = &ni->ni_ucastkey; 707 708 /* 709 * Insure crypto header is contiguous and long enough for all 710 * decap work. 711 */ 712 cip = k->wk_cipher; 713 if (m->m_len < hdrlen + cip->ic_header) { 714 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2, 715 "frame is too short (%d < %u) for crypto decap", 716 cip->ic_name, m->m_len, hdrlen + cip->ic_header); 717 vap->iv_stats.is_rx_tooshort++; 718 *key = NULL; 719 return (0); 720 } 721 722 /* 723 * Attempt decryption. 724 * 725 * If we fail then don't return the key - return NULL 726 * and an error. 727 */ 728 if (cip->ic_decap(k, m, hdrlen)) { 729 /* success */ 730 *key = k; 731 return (1); 732 } 733 734 /* Failure */ 735 *key = NULL; 736 return (0); 737 #undef IEEE80211_WEP_MINLEN 738 #undef IEEE80211_WEP_HDRLEN 739 } 740 741 /* 742 * Check and remove any MIC. 743 */ 744 int 745 ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k, 746 struct mbuf *m, int force) 747 { 748 const struct ieee80211_cipher *cip; 749 const struct ieee80211_rx_stats *rxs; 750 struct ieee80211_frame *wh; 751 752 rxs = ieee80211_get_rx_params_ptr(m); 753 wh = mtod(m, struct ieee80211_frame *); 754 755 /* 756 * Handle demic / mic errors from hardware-decrypted offload devices. 757 */ 758 if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) { 759 if (rxs->c_pktflags & IEEE80211_RX_F_FAIL_MIC) { 760 /* 761 * Hardware has said MIC failed. We don't care about 762 * whether it was stripped or not. 763 * 764 * Eventually - teach the demic methods in crypto 765 * modules to handle a NULL key and not to dereference 766 * it. 767 */ 768 ieee80211_notify_michael_failure(vap, wh, -1); 769 return (0); 770 } 771 772 if (rxs->c_pktflags & IEEE80211_RX_F_MMIC_STRIP) { 773 /* 774 * Hardware has decrypted and not indicated a 775 * MIC failure and has stripped the MIC. 776 * We may not have a key, so for now just 777 * return OK. 778 */ 779 return (1); 780 } 781 } 782 783 /* 784 * If we don't have a key at this point then we don't 785 * have to demic anything. 786 */ 787 if (k == NULL) 788 return (1); 789 790 cip = k->wk_cipher; 791 return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1); 792 } 793 794 static void 795 load_ucastkey(void *arg, struct ieee80211_node *ni) 796 { 797 struct ieee80211vap *vap = ni->ni_vap; 798 struct ieee80211_key *k; 799 800 if (vap->iv_state != IEEE80211_S_RUN) 801 return; 802 k = &ni->ni_ucastkey; 803 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 804 dev_key_set(vap, k); 805 } 806 807 /* 808 * Re-load all keys known to the 802.11 layer that may 809 * have hardware state backing them. This is used by 810 * drivers on resume to push keys down into the device. 811 */ 812 void 813 ieee80211_crypto_reload_keys(struct ieee80211com *ic) 814 { 815 struct ieee80211vap *vap; 816 int i; 817 818 /* 819 * Keys in the global key table of each vap. 820 */ 821 /* NB: used only during resume so don't lock for now */ 822 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 823 if (vap->iv_state != IEEE80211_S_RUN) 824 continue; 825 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 826 const struct ieee80211_key *k = &vap->iv_nw_keys[i]; 827 if (k->wk_flags & IEEE80211_KEY_DEVKEY) 828 dev_key_set(vap, k); 829 } 830 } 831 /* 832 * Unicast keys. 833 */ 834 ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL); 835 } 836 837 /* 838 * Set the default key index for WEP, or KEYIX_NONE for no default TX key. 839 * 840 * This should be done as part of a key update block (iv_key_update_begin / 841 * iv_key_update_end.) 842 */ 843 void 844 ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid) 845 { 846 847 /* XXX TODO: assert we're in a key update block */ 848 849 vap->iv_update_deftxkey(vap, kid); 850 } 851