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