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