1 /*- 2 * Copyright (c) 2001 Atsushi Onoe 3 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * IEEE 802.11 generic crypto support. 32 */ 33 #include <sys/param.h> 34 #include <sys/mbuf.h> 35 36 #include <sys/socket.h> 37 38 #include <net/if.h> 39 #include <net/if_media.h> 40 #include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 41 42 #include <net80211/ieee80211_var.h> 43 44 /* 45 * Table of registered cipher modules. 46 */ 47 static const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 48 49 static int _ieee80211_crypto_delkey(struct ieee80211com *, 50 struct ieee80211_key *); 51 52 /* 53 * Default "null" key management routines. 54 */ 55 static int 56 null_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k, 57 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 58 { 59 if (!(&ic->ic_nw_keys[0] <= k && 60 k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) { 61 /* 62 * Not in the global key table, the driver should handle this 63 * by allocating a slot in the h/w key table/cache. In 64 * lieu of that return key slot 0 for any unicast key 65 * request. We disallow the request if this is a group key. 66 * This default policy does the right thing for legacy hardware 67 * with a 4 key table. It also handles devices that pass 68 * packets through untouched when marked with the WEP bit 69 * and key index 0. 70 */ 71 if (k->wk_flags & IEEE80211_KEY_GROUP) 72 return 0; 73 *keyix = 0; /* NB: use key index 0 for ucast key */ 74 } else { 75 *keyix = k - ic->ic_nw_keys; 76 } 77 *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 78 return 1; 79 } 80 static int 81 null_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 82 { 83 return 1; 84 } 85 static int 86 null_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 87 const uint8_t mac[IEEE80211_ADDR_LEN]) 88 { 89 return 1; 90 } 91 static void null_key_update(struct ieee80211com *ic) {} 92 93 /* 94 * Write-arounds for common operations. 95 */ 96 static __inline void 97 cipher_detach(struct ieee80211_key *key) 98 { 99 key->wk_cipher->ic_detach(key); 100 } 101 102 static __inline void * 103 cipher_attach(struct ieee80211com *ic, struct ieee80211_key *key) 104 { 105 return key->wk_cipher->ic_attach(ic, key); 106 } 107 108 /* 109 * Wrappers for driver key management methods. 110 */ 111 static __inline int 112 dev_key_alloc(struct ieee80211com *ic, 113 const struct ieee80211_key *key, 114 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 115 { 116 return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix); 117 } 118 119 static __inline int 120 dev_key_delete(struct ieee80211com *ic, 121 const struct ieee80211_key *key) 122 { 123 return ic->ic_crypto.cs_key_delete(ic, key); 124 } 125 126 static __inline int 127 dev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key, 128 const uint8_t mac[IEEE80211_ADDR_LEN]) 129 { 130 return ic->ic_crypto.cs_key_set(ic, key, mac); 131 } 132 133 /* 134 * Setup crypto support. 135 */ 136 void 137 ieee80211_crypto_attach(struct ieee80211com *ic) 138 { 139 struct ieee80211_crypto_state *cs = &ic->ic_crypto; 140 int i; 141 142 /* NB: we assume everything is pre-zero'd */ 143 cs->cs_def_txkey = IEEE80211_KEYIX_NONE; 144 cs->cs_max_keyix = IEEE80211_WEP_NKID; 145 ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 146 for (i = 0; i < IEEE80211_WEP_NKID; i++) 147 ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i], 148 IEEE80211_KEYIX_NONE); 149 /* 150 * Initialize the driver key support routines to noop entries. 151 * This is useful especially for the cipher test modules. 152 */ 153 cs->cs_key_alloc = null_key_alloc; 154 cs->cs_key_set = null_key_set; 155 cs->cs_key_delete = null_key_delete; 156 cs->cs_key_update_begin = null_key_update; 157 cs->cs_key_update_end = null_key_update; 158 } 159 160 /* 161 * Teardown crypto support. 162 */ 163 void 164 ieee80211_crypto_detach(struct ieee80211com *ic) 165 { 166 ieee80211_crypto_delglobalkeys(ic); 167 } 168 169 /* 170 * Register a crypto cipher module. 171 */ 172 void 173 ieee80211_crypto_register(const struct ieee80211_cipher *cip) 174 { 175 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 176 printf("%s: cipher %s has an invalid cipher index %u\n", 177 __func__, cip->ic_name, cip->ic_cipher); 178 return; 179 } 180 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 181 printf("%s: cipher %s registered with a different template\n", 182 __func__, cip->ic_name); 183 return; 184 } 185 ciphers[cip->ic_cipher] = cip; 186 } 187 188 /* 189 * Unregister a crypto cipher module. 190 */ 191 void 192 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 193 { 194 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 195 printf("%s: cipher %s has an invalid cipher index %u\n", 196 __func__, cip->ic_name, cip->ic_cipher); 197 return; 198 } 199 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 200 printf("%s: cipher %s registered with a different template\n", 201 __func__, cip->ic_name); 202 return; 203 } 204 /* NB: don't complain about not being registered */ 205 /* XXX disallow if references */ 206 ciphers[cip->ic_cipher] = NULL; 207 } 208 209 int 210 ieee80211_crypto_available(u_int cipher) 211 { 212 return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 213 } 214 215 /* XXX well-known names! */ 216 static const char *cipher_modnames[] = { 217 "wlan_wep", /* IEEE80211_CIPHER_WEP */ 218 "wlan_tkip", /* IEEE80211_CIPHER_TKIP */ 219 "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */ 220 "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */ 221 "wlan_ckip", /* IEEE80211_CIPHER_CKIP */ 222 }; 223 224 /* 225 * Establish a relationship between the specified key and cipher 226 * and, if necessary, allocate a hardware index from the driver. 227 * Note that when a fixed key index is required it must be specified 228 * and we blindly assign it w/o consulting the driver (XXX). 229 * 230 * This must be the first call applied to a key; all the other key 231 * routines assume wk_cipher is setup. 232 * 233 * Locking must be handled by the caller using: 234 * ieee80211_key_update_begin(ic); 235 * ieee80211_key_update_end(ic); 236 */ 237 int 238 ieee80211_crypto_newkey(struct ieee80211com *ic, 239 int cipher, int flags, struct ieee80211_key *key) 240 { 241 #define N(a) (sizeof(a) / sizeof(a[0])) 242 const struct ieee80211_cipher *cip; 243 ieee80211_keyix keyix, rxkeyix; 244 void *keyctx; 245 int oflags; 246 247 /* 248 * Validate cipher and set reference to cipher routines. 249 */ 250 if (cipher >= IEEE80211_CIPHER_MAX) { 251 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 252 "%s: invalid cipher %u\n", __func__, cipher); 253 ic->ic_stats.is_crypto_badcipher++; 254 return 0; 255 } 256 cip = ciphers[cipher]; 257 if (cip == NULL) { 258 /* 259 * Auto-load cipher module if we have a well-known name 260 * for it. It might be better to use string names rather 261 * than numbers and craft a module name based on the cipher 262 * name; e.g. wlan_cipher_<cipher-name>. 263 */ 264 if (cipher < N(cipher_modnames)) { 265 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 266 "%s: unregistered cipher %u, load module %s\n", 267 __func__, cipher, cipher_modnames[cipher]); 268 ieee80211_load_module(cipher_modnames[cipher]); 269 /* 270 * If cipher module loaded it should immediately 271 * call ieee80211_crypto_register which will fill 272 * in the entry in the ciphers array. 273 */ 274 cip = ciphers[cipher]; 275 } 276 if (cip == NULL) { 277 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 278 "%s: unable to load cipher %u, module %s\n", 279 __func__, cipher, 280 cipher < N(cipher_modnames) ? 281 cipher_modnames[cipher] : "<unknown>"); 282 ic->ic_stats.is_crypto_nocipher++; 283 return 0; 284 } 285 } 286 287 oflags = key->wk_flags; 288 flags &= IEEE80211_KEY_COMMON; 289 /* 290 * If the hardware does not support the cipher then 291 * fallback to a host-based implementation. 292 */ 293 if ((ic->ic_caps & (1<<cipher)) == 0) { 294 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 295 "%s: no h/w support for cipher %s, falling back to s/w\n", 296 __func__, cip->ic_name); 297 flags |= IEEE80211_KEY_SWCRYPT; 298 } 299 /* 300 * Hardware TKIP with software MIC is an important 301 * combination; we handle it by flagging each key, 302 * the cipher modules honor it. 303 */ 304 if (cipher == IEEE80211_CIPHER_TKIP && 305 (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) { 306 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 307 "%s: no h/w support for TKIP MIC, falling back to s/w\n", 308 __func__); 309 flags |= IEEE80211_KEY_SWMIC; 310 } 311 312 /* 313 * Bind cipher to key instance. Note we do this 314 * after checking the device capabilities so the 315 * cipher module can optimize space usage based on 316 * whether or not it needs to do the cipher work. 317 */ 318 if (key->wk_cipher != cip || key->wk_flags != flags) { 319 again: 320 /* 321 * Fillin the flags so cipher modules can see s/w 322 * crypto requirements and potentially allocate 323 * different state and/or attach different method 324 * pointers. 325 * 326 * XXX this is not right when s/w crypto fallback 327 * fails and we try to restore previous state. 328 */ 329 key->wk_flags = flags; 330 keyctx = cip->ic_attach(ic, key); 331 if (keyctx == NULL) { 332 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 333 "%s: unable to attach cipher %s\n", 334 __func__, cip->ic_name); 335 key->wk_flags = oflags; /* restore old flags */ 336 ic->ic_stats.is_crypto_attachfail++; 337 return 0; 338 } 339 cipher_detach(key); 340 key->wk_cipher = cip; /* XXX refcnt? */ 341 key->wk_private = keyctx; 342 } 343 /* 344 * Commit to requested usage so driver can see the flags. 345 */ 346 key->wk_flags = flags; 347 348 /* 349 * Ask the driver for a key index if we don't have one. 350 * Note that entries in the global key table always have 351 * an index; this means it's safe to call this routine 352 * for these entries just to setup the reference to the 353 * cipher template. Note also that when using software 354 * crypto we also call the driver to give us a key index. 355 */ 356 if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 357 if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) { 358 /* 359 * Driver has no room; fallback to doing crypto 360 * in the host. We change the flags and start the 361 * procedure over. If we get back here then there's 362 * no hope and we bail. Note that this can leave 363 * the key in a inconsistent state if the caller 364 * continues to use it. 365 */ 366 if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 367 ic->ic_stats.is_crypto_swfallback++; 368 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 369 "%s: no h/w resources for cipher %s, " 370 "falling back to s/w\n", __func__, 371 cip->ic_name); 372 oflags = key->wk_flags; 373 flags |= IEEE80211_KEY_SWCRYPT; 374 if (cipher == IEEE80211_CIPHER_TKIP) 375 flags |= IEEE80211_KEY_SWMIC; 376 goto again; 377 } 378 ic->ic_stats.is_crypto_keyfail++; 379 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 380 "%s: unable to setup cipher %s\n", 381 __func__, cip->ic_name); 382 return 0; 383 } 384 key->wk_keyix = keyix; 385 key->wk_rxkeyix = rxkeyix; 386 } 387 return 1; 388 #undef N 389 } 390 391 /* 392 * Remove the key (no locking, for internal use). 393 */ 394 static int 395 _ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 396 { 397 ieee80211_keyix keyix; 398 399 KASSERT(key->wk_cipher != NULL, ("No cipher!")); 400 401 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 402 "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 403 __func__, key->wk_cipher->ic_name, 404 key->wk_keyix, key->wk_flags, 405 key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 406 407 keyix = key->wk_keyix; 408 if (keyix != IEEE80211_KEYIX_NONE) { 409 /* 410 * Remove hardware entry. 411 */ 412 /* XXX key cache */ 413 if (!dev_key_delete(ic, key)) { 414 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 415 "%s: driver did not delete key index %u\n", 416 __func__, keyix); 417 ic->ic_stats.is_crypto_delkey++; 418 /* XXX recovery? */ 419 } 420 } 421 cipher_detach(key); 422 memset(key, 0, sizeof(*key)); 423 ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE); 424 return 1; 425 } 426 427 /* 428 * Remove the specified key. 429 */ 430 int 431 ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 432 { 433 int status; 434 435 ieee80211_key_update_begin(ic); 436 status = _ieee80211_crypto_delkey(ic, key); 437 ieee80211_key_update_end(ic); 438 return status; 439 } 440 441 /* 442 * Clear the global key table. 443 */ 444 void 445 ieee80211_crypto_delglobalkeys(struct ieee80211com *ic) 446 { 447 int i; 448 449 ieee80211_key_update_begin(ic); 450 for (i = 0; i < IEEE80211_WEP_NKID; i++) 451 (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]); 452 ieee80211_key_update_end(ic); 453 } 454 455 /* 456 * Set the contents of the specified key. 457 * 458 * Locking must be handled by the caller using: 459 * ieee80211_key_update_begin(ic); 460 * ieee80211_key_update_end(ic); 461 */ 462 int 463 ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key, 464 const uint8_t macaddr[IEEE80211_ADDR_LEN]) 465 { 466 const struct ieee80211_cipher *cip = key->wk_cipher; 467 468 KASSERT(cip != NULL, ("No cipher!")); 469 470 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 471 "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 472 __func__, cip->ic_name, key->wk_keyix, 473 key->wk_flags, ether_sprintf(macaddr), 474 key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 475 476 /* 477 * Give cipher a chance to validate key contents. 478 * XXX should happen before modifying state. 479 */ 480 if (!cip->ic_setkey(key)) { 481 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 482 "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 483 __func__, cip->ic_name, key->wk_keyix, 484 key->wk_keylen, key->wk_flags); 485 ic->ic_stats.is_crypto_setkey_cipher++; 486 return 0; 487 } 488 if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 489 /* XXX nothing allocated, should not happen */ 490 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 491 "%s: no key index; should not happen!\n", __func__); 492 ic->ic_stats.is_crypto_setkey_nokey++; 493 return 0; 494 } 495 return dev_key_set(ic, key, macaddr); 496 } 497 498 /* 499 * Add privacy headers appropriate for the specified key. 500 */ 501 struct ieee80211_key * 502 ieee80211_crypto_encap(struct ieee80211com *ic, 503 struct ieee80211_node *ni, struct mbuf *m) 504 { 505 struct ieee80211_key *k; 506 struct ieee80211_frame *wh; 507 const struct ieee80211_cipher *cip; 508 uint8_t keyid; 509 510 /* 511 * Multicast traffic always uses the multicast key. 512 * Otherwise if a unicast key is set we use that and 513 * it is always key index 0. When no unicast key is 514 * set we fall back to the default transmit key. 515 */ 516 wh = mtod(m, struct ieee80211_frame *); 517 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 518 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) { 519 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) { 520 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 521 "[%s] no default transmit key (%s) deftxkey %u\n", 522 ether_sprintf(wh->i_addr1), __func__, 523 ic->ic_def_txkey); 524 ic->ic_stats.is_tx_nodefkey++; 525 return NULL; 526 } 527 keyid = ic->ic_def_txkey; 528 k = &ic->ic_nw_keys[ic->ic_def_txkey]; 529 } else { 530 keyid = 0; 531 k = &ni->ni_ucastkey; 532 } 533 cip = k->wk_cipher; 534 return (cip->ic_encap(k, m, keyid<<6) ? k : NULL); 535 } 536 537 /* 538 * Validate and strip privacy headers (and trailer) for a 539 * received frame that has the WEP/Privacy bit set. 540 */ 541 struct ieee80211_key * 542 ieee80211_crypto_decap(struct ieee80211com *ic, 543 struct ieee80211_node *ni, struct mbuf *m, int hdrlen) 544 { 545 #define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 546 #define IEEE80211_WEP_MINLEN \ 547 (sizeof(struct ieee80211_frame) + \ 548 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 549 struct ieee80211_key *k; 550 struct ieee80211_frame *wh; 551 const struct ieee80211_cipher *cip; 552 const uint8_t *ivp; 553 uint8_t keyid; 554 555 /* NB: this minimum size data frame could be bigger */ 556 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 557 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 558 "%s: WEP data frame too short, len %u\n", 559 __func__, m->m_pkthdr.len); 560 ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */ 561 return NULL; 562 } 563 564 /* 565 * Locate the key. If unicast and there is no unicast 566 * key then we fall back to the key id in the header. 567 * This assumes unicast keys are only configured when 568 * the key id in the header is meaningless (typically 0). 569 */ 570 wh = mtod(m, struct ieee80211_frame *); 571 ivp = mtod(m, const uint8_t *) + hdrlen; /* XXX contig */ 572 keyid = ivp[IEEE80211_WEP_IVLEN]; 573 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 574 IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) 575 k = &ic->ic_nw_keys[keyid >> 6]; 576 else 577 k = &ni->ni_ucastkey; 578 579 /* 580 * Insure crypto header is contiguous for all decap work. 581 */ 582 cip = k->wk_cipher; 583 if (m->m_len < hdrlen + cip->ic_header && 584 (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 585 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 586 "[%s] unable to pullup %s header\n", 587 ether_sprintf(wh->i_addr2), cip->ic_name); 588 ic->ic_stats.is_rx_wepfail++; /* XXX */ 589 return NULL; 590 } 591 592 return (cip->ic_decap(k, m, hdrlen) ? k : NULL); 593 #undef IEEE80211_WEP_MINLEN 594 #undef IEEE80211_WEP_HDRLEN 595 } 596