ieee80211_crypto.c revision 148863
155714Skris/*- 2280304Sjkim * Copyright (c) 2001 Atsushi Onoe 3280304Sjkim * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting 4280304Sjkim * All rights reserved. 555714Skris * 655714Skris * Redistribution and use in source and binary forms, with or without 755714Skris * modification, are permitted provided that the following conditions 855714Skris * are met: 955714Skris * 1. Redistributions of source code must retain the above copyright 1055714Skris * notice, this list of conditions and the following disclaimer. 1155714Skris * 2. Redistributions in binary form must reproduce the above copyright 1255714Skris * notice, this list of conditions and the following disclaimer in the 1355714Skris * documentation and/or other materials provided with the distribution. 14280304Sjkim * 3. The name of the author may not be used to endorse or promote products 1555714Skris * derived from this software without specific prior written permission. 1655714Skris * 1755714Skris * Alternatively, this software may be distributed under the terms of the 1855714Skris * GNU General Public License ("GPL") version 2 as published by the Free 1955714Skris * Software Foundation. 2055714Skris * 2155714Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2255714Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2355714Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2455714Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2555714Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2755714Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2855714Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2955714Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3055714Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3155714Skris */ 3255714Skris 3355714Skris#include <sys/cdefs.h> 3455714Skris__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_crypto.c 148863 2005-08-08 18:46:36Z sam $"); 3555714Skris 3655714Skris/* 3755714Skris * IEEE 802.11 generic crypto support. 3855714Skris */ 3955714Skris#include <sys/param.h> 4055714Skris#include <sys/mbuf.h> 4155714Skris 4255714Skris#include <sys/socket.h> 4355714Skris 4455714Skris#include <net/if.h> 4555714Skris#include <net/if_media.h> 4655714Skris#include <net/ethernet.h> /* XXX ETHER_HDR_LEN */ 4755714Skris 4855714Skris#include <net80211/ieee80211_var.h> 4955714Skris 5055714Skris/* 5155714Skris * Table of registered cipher modules. 5255714Skris */ 5355714Skrisstatic const struct ieee80211_cipher *ciphers[IEEE80211_CIPHER_MAX]; 5455714Skris 5555714Skrisstatic int _ieee80211_crypto_delkey(struct ieee80211com *, 5655714Skris struct ieee80211_key *); 5755714Skris 5855714Skris/* 5955714Skris * Default "null" key management routines. 60109998Smarkm */ 61280304Sjkimstatic int 62280304Sjkimnull_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k, 63284285Sjkim ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 64280304Sjkim{ 65280304Sjkim if (!(&ic->ic_nw_keys[0] <= k && 66280304Sjkim k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) { 6755714Skris /* 6855714Skris * Not in the global key table, the driver should handle this 69160814Ssimon * by allocating a slot in the h/w key table/cache. In 70280304Sjkim * lieu of that return key slot 0 for any unicast key 7155714Skris * request. We disallow the request if this is a group key. 72280304Sjkim * This default policy does the right thing for legacy hardware 73280304Sjkim * with a 4 key table. It also handles devices that pass 74280304Sjkim * packets through untouched when marked with the WEP bit 75280304Sjkim * and key index 0. 76280304Sjkim */ 77109998Smarkm if (k->wk_flags & IEEE80211_KEY_GROUP) 78280304Sjkim return 0; 79280304Sjkim *keyix = 0; /* NB: use key index 0 for ucast key */ 80280304Sjkim } else { 81280304Sjkim *keyix = k - ic->ic_nw_keys; 82160814Ssimon } 83280304Sjkim *rxkeyix = IEEE80211_KEYIX_NONE; /* XXX maybe *keyix? */ 84280304Sjkim return 1; 85280304Sjkim} 86280304Sjkimstatic int 87280304Sjkimnull_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 88280304Sjkim{ 89280304Sjkim return 1; 90280304Sjkim} 91280304Sjkimstatic int 92280304Sjkimnull_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 93280304Sjkim const u_int8_t mac[IEEE80211_ADDR_LEN]) 94280304Sjkim{ 95280304Sjkim return 1; 96280304Sjkim} 97280304Sjkimstatic void null_key_update(struct ieee80211com *ic) {} 98280304Sjkim 99280304Sjkim/* 100280304Sjkim * Write-arounds for common operations. 101280304Sjkim */ 102280304Sjkimstatic __inline void 103280304Sjkimcipher_detach(struct ieee80211_key *key) 104280304Sjkim{ 105280304Sjkim key->wk_cipher->ic_detach(key); 106280304Sjkim} 107280304Sjkim 108280304Sjkimstatic __inline void * 109280304Sjkimcipher_attach(struct ieee80211com *ic, struct ieee80211_key *key) 110280304Sjkim{ 11155714Skris return key->wk_cipher->ic_attach(ic, key); 11255714Skris} 11355714Skris 114160814Ssimon/* 11555714Skris * Wrappers for driver key management methods. 116280304Sjkim */ 117280304Sjkimstatic __inline int 118280304Sjkimdev_key_alloc(struct ieee80211com *ic, 119280304Sjkim const struct ieee80211_key *key, 120280304Sjkim ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 121280304Sjkim{ 122280304Sjkim return ic->ic_crypto.cs_key_alloc(ic, key, keyix, rxkeyix); 123280304Sjkim} 124280304Sjkim 125280304Sjkimstatic __inline int 126280304Sjkimdev_key_delete(struct ieee80211com *ic, 127284285Sjkim const struct ieee80211_key *key) 128280304Sjkim{ 129280304Sjkim return ic->ic_crypto.cs_key_delete(ic, key); 13055714Skris} 13155714Skris 13255714Skrisstatic __inline int 13355714Skrisdev_key_set(struct ieee80211com *ic, const struct ieee80211_key *key, 134160814Ssimon const u_int8_t mac[IEEE80211_ADDR_LEN]) 135280304Sjkim{ 136280304Sjkim return ic->ic_crypto.cs_key_set(ic, key, mac); 13755714Skris} 138280304Sjkim 139280304Sjkim/* 14055714Skris * Setup crypto support. 141280304Sjkim */ 142280304Sjkimvoid 143280304Sjkimieee80211_crypto_attach(struct ieee80211com *ic) 144280304Sjkim{ 145280304Sjkim struct ieee80211_crypto_state *cs = &ic->ic_crypto; 146280304Sjkim int i; 147280304Sjkim 148280304Sjkim /* NB: we assume everything is pre-zero'd */ 149280304Sjkim cs->cs_def_txkey = IEEE80211_KEYIX_NONE; 150280304Sjkim cs->cs_max_keyix = IEEE80211_WEP_NKID; 151280304Sjkim ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none; 152280304Sjkim for (i = 0; i < IEEE80211_WEP_NKID; i++) 153280304Sjkim ieee80211_crypto_resetkey(ic, &cs->cs_nw_keys[i], 154280304Sjkim IEEE80211_KEYIX_NONE); 155280304Sjkim /* 15655714Skris * Initialize the driver key support routines to noop entries. 15755714Skris * This is useful especially for the cipher test modules. 15855714Skris */ 159160814Ssimon cs->cs_key_alloc = null_key_alloc; 160280304Sjkim cs->cs_key_set = null_key_set; 16155714Skris cs->cs_key_delete = null_key_delete; 162280304Sjkim cs->cs_key_update_begin = null_key_update; 163280304Sjkim cs->cs_key_update_end = null_key_update; 164280304Sjkim} 165280304Sjkim 166280304Sjkim/* 167280304Sjkim * Teardown crypto support. 168280304Sjkim */ 169280304Sjkimvoid 170280304Sjkimieee80211_crypto_detach(struct ieee80211com *ic) 171280304Sjkim{ 172280304Sjkim ieee80211_crypto_delglobalkeys(ic); 173280304Sjkim} 174280304Sjkim 175280304Sjkim/* 176291721Sjkim * Register a crypto cipher module. 177280304Sjkim */ 178280304Sjkimvoid 179280304Sjkimieee80211_crypto_register(const struct ieee80211_cipher *cip) 180291721Sjkim{ 181280304Sjkim if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 182306196Sjkim printf("%s: cipher %s has an invalid cipher index %u\n", 183280304Sjkim __func__, cip->ic_name, cip->ic_cipher); 184280304Sjkim return; 185280304Sjkim } 186280304Sjkim if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 187280304Sjkim printf("%s: cipher %s registered with a different template\n", 188280304Sjkim __func__, cip->ic_name); 189280304Sjkim return; 190280304Sjkim } 191280304Sjkim ciphers[cip->ic_cipher] = cip; 192280304Sjkim} 193280304Sjkim 19455714Skris/* 19555714Skris * Unregister a crypto cipher module. 196 */ 197void 198ieee80211_crypto_unregister(const struct ieee80211_cipher *cip) 199{ 200 if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) { 201 printf("%s: cipher %s has an invalid cipher index %u\n", 202 __func__, cip->ic_name, cip->ic_cipher); 203 return; 204 } 205 if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) { 206 printf("%s: cipher %s registered with a different template\n", 207 __func__, cip->ic_name); 208 return; 209 } 210 /* NB: don't complain about not being registered */ 211 /* XXX disallow if references */ 212 ciphers[cip->ic_cipher] = NULL; 213} 214 215int 216ieee80211_crypto_available(u_int cipher) 217{ 218 return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL; 219} 220 221/* XXX well-known names! */ 222static const char *cipher_modnames[] = { 223 "wlan_wep", /* IEEE80211_CIPHER_WEP */ 224 "wlan_tkip", /* IEEE80211_CIPHER_TKIP */ 225 "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */ 226 "wlan_ccmp", /* IEEE80211_CIPHER_AES_CCM */ 227 "wlan_ckip", /* IEEE80211_CIPHER_CKIP */ 228}; 229 230/* 231 * Establish a relationship between the specified key and cipher 232 * and, if necessary, allocate a hardware index from the driver. 233 * Note that when a fixed key index is required it must be specified 234 * and we blindly assign it w/o consulting the driver (XXX). 235 * 236 * This must be the first call applied to a key; all the other key 237 * routines assume wk_cipher is setup. 238 * 239 * Locking must be handled by the caller using: 240 * ieee80211_key_update_begin(ic); 241 * ieee80211_key_update_end(ic); 242 */ 243int 244ieee80211_crypto_newkey(struct ieee80211com *ic, 245 int cipher, int flags, struct ieee80211_key *key) 246{ 247#define N(a) (sizeof(a) / sizeof(a[0])) 248 const struct ieee80211_cipher *cip; 249 ieee80211_keyix keyix, rxkeyix; 250 void *keyctx; 251 int oflags; 252 253 /* 254 * Validate cipher and set reference to cipher routines. 255 */ 256 if (cipher >= IEEE80211_CIPHER_MAX) { 257 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 258 "%s: invalid cipher %u\n", __func__, cipher); 259 ic->ic_stats.is_crypto_badcipher++; 260 return 0; 261 } 262 cip = ciphers[cipher]; 263 if (cip == NULL) { 264 /* 265 * Auto-load cipher module if we have a well-known name 266 * for it. It might be better to use string names rather 267 * than numbers and craft a module name based on the cipher 268 * name; e.g. wlan_cipher_<cipher-name>. 269 */ 270 if (cipher < N(cipher_modnames)) { 271 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 272 "%s: unregistered cipher %u, load module %s\n", 273 __func__, cipher, cipher_modnames[cipher]); 274 ieee80211_load_module(cipher_modnames[cipher]); 275 /* 276 * If cipher module loaded it should immediately 277 * call ieee80211_crypto_register which will fill 278 * in the entry in the ciphers array. 279 */ 280 cip = ciphers[cipher]; 281 } 282 if (cip == NULL) { 283 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 284 "%s: unable to load cipher %u, module %s\n", 285 __func__, cipher, 286 cipher < N(cipher_modnames) ? 287 cipher_modnames[cipher] : "<unknown>"); 288 ic->ic_stats.is_crypto_nocipher++; 289 return 0; 290 } 291 } 292 293 oflags = key->wk_flags; 294 flags &= IEEE80211_KEY_COMMON; 295 /* 296 * If the hardware does not support the cipher then 297 * fallback to a host-based implementation. 298 */ 299 if ((ic->ic_caps & (1<<cipher)) == 0) { 300 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 301 "%s: no h/w support for cipher %s, falling back to s/w\n", 302 __func__, cip->ic_name); 303 flags |= IEEE80211_KEY_SWCRYPT; 304 } 305 /* 306 * Hardware TKIP with software MIC is an important 307 * combination; we handle it by flagging each key, 308 * the cipher modules honor it. 309 */ 310 if (cipher == IEEE80211_CIPHER_TKIP && 311 (ic->ic_caps & IEEE80211_C_TKIPMIC) == 0) { 312 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 313 "%s: no h/w support for TKIP MIC, falling back to s/w\n", 314 __func__); 315 flags |= IEEE80211_KEY_SWMIC; 316 } 317 318 /* 319 * Bind cipher to key instance. Note we do this 320 * after checking the device capabilities so the 321 * cipher module can optimize space usage based on 322 * whether or not it needs to do the cipher work. 323 */ 324 if (key->wk_cipher != cip || key->wk_flags != flags) { 325again: 326 /* 327 * Fillin the flags so cipher modules can see s/w 328 * crypto requirements and potentially allocate 329 * different state and/or attach different method 330 * pointers. 331 * 332 * XXX this is not right when s/w crypto fallback 333 * fails and we try to restore previous state. 334 */ 335 key->wk_flags = flags; 336 keyctx = cip->ic_attach(ic, key); 337 if (keyctx == NULL) { 338 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 339 "%s: unable to attach cipher %s\n", 340 __func__, cip->ic_name); 341 key->wk_flags = oflags; /* restore old flags */ 342 ic->ic_stats.is_crypto_attachfail++; 343 return 0; 344 } 345 cipher_detach(key); 346 key->wk_cipher = cip; /* XXX refcnt? */ 347 key->wk_private = keyctx; 348 } 349 /* 350 * Commit to requested usage so driver can see the flags. 351 */ 352 key->wk_flags = flags; 353 354 /* 355 * Ask the driver for a key index if we don't have one. 356 * Note that entries in the global key table always have 357 * an index; this means it's safe to call this routine 358 * for these entries just to setup the reference to the 359 * cipher template. Note also that when using software 360 * crypto we also call the driver to give us a key index. 361 */ 362 if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 363 if (!dev_key_alloc(ic, key, &keyix, &rxkeyix)) { 364 /* 365 * Driver has no room; fallback to doing crypto 366 * in the host. We change the flags and start the 367 * procedure over. If we get back here then there's 368 * no hope and we bail. Note that this can leave 369 * the key in a inconsistent state if the caller 370 * continues to use it. 371 */ 372 if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 373 ic->ic_stats.is_crypto_swfallback++; 374 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 375 "%s: no h/w resources for cipher %s, " 376 "falling back to s/w\n", __func__, 377 cip->ic_name); 378 oflags = key->wk_flags; 379 flags |= IEEE80211_KEY_SWCRYPT; 380 if (cipher == IEEE80211_CIPHER_TKIP) 381 flags |= IEEE80211_KEY_SWMIC; 382 goto again; 383 } 384 ic->ic_stats.is_crypto_keyfail++; 385 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 386 "%s: unable to setup cipher %s\n", 387 __func__, cip->ic_name); 388 return 0; 389 } 390 key->wk_keyix = keyix; 391 key->wk_rxkeyix = rxkeyix; 392 } 393 return 1; 394#undef N 395} 396 397/* 398 * Remove the key (no locking, for internal use). 399 */ 400static int 401_ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 402{ 403 ieee80211_keyix keyix; 404 405 KASSERT(key->wk_cipher != NULL, ("No cipher!")); 406 407 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 408 "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n", 409 __func__, key->wk_cipher->ic_name, 410 key->wk_keyix, key->wk_flags, 411 key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 412 413 keyix = key->wk_keyix; 414 if (keyix != IEEE80211_KEYIX_NONE) { 415 /* 416 * Remove hardware entry. 417 */ 418 /* XXX key cache */ 419 if (!dev_key_delete(ic, key)) { 420 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 421 "%s: driver did not delete key index %u\n", 422 __func__, keyix); 423 ic->ic_stats.is_crypto_delkey++; 424 /* XXX recovery? */ 425 } 426 } 427 cipher_detach(key); 428 memset(key, 0, sizeof(*key)); 429 ieee80211_crypto_resetkey(ic, key, IEEE80211_KEYIX_NONE); 430 return 1; 431} 432 433/* 434 * Remove the specified key. 435 */ 436int 437ieee80211_crypto_delkey(struct ieee80211com *ic, struct ieee80211_key *key) 438{ 439 int status; 440 441 ieee80211_key_update_begin(ic); 442 status = _ieee80211_crypto_delkey(ic, key); 443 ieee80211_key_update_end(ic); 444 return status; 445} 446 447/* 448 * Clear the global key table. 449 */ 450void 451ieee80211_crypto_delglobalkeys(struct ieee80211com *ic) 452{ 453 int i; 454 455 ieee80211_key_update_begin(ic); 456 for (i = 0; i < IEEE80211_WEP_NKID; i++) 457 (void) _ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[i]); 458 ieee80211_key_update_end(ic); 459} 460 461/* 462 * Set the contents of the specified key. 463 * 464 * Locking must be handled by the caller using: 465 * ieee80211_key_update_begin(ic); 466 * ieee80211_key_update_end(ic); 467 */ 468int 469ieee80211_crypto_setkey(struct ieee80211com *ic, struct ieee80211_key *key, 470 const u_int8_t macaddr[IEEE80211_ADDR_LEN]) 471{ 472 const struct ieee80211_cipher *cip = key->wk_cipher; 473 474 KASSERT(cip != NULL, ("No cipher!")); 475 476 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 477 "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n", 478 __func__, cip->ic_name, key->wk_keyix, 479 key->wk_flags, ether_sprintf(macaddr), 480 key->wk_keyrsc, key->wk_keytsc, key->wk_keylen); 481 482 /* 483 * Give cipher a chance to validate key contents. 484 * XXX should happen before modifying state. 485 */ 486 if (!cip->ic_setkey(key)) { 487 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 488 "%s: cipher %s rejected key index %u len %u flags 0x%x\n", 489 __func__, cip->ic_name, key->wk_keyix, 490 key->wk_keylen, key->wk_flags); 491 ic->ic_stats.is_crypto_setkey_cipher++; 492 return 0; 493 } 494 if (key->wk_keyix == IEEE80211_KEYIX_NONE) { 495 /* XXX nothing allocated, should not happen */ 496 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 497 "%s: no key index; should not happen!\n", __func__); 498 ic->ic_stats.is_crypto_setkey_nokey++; 499 return 0; 500 } 501 return dev_key_set(ic, key, macaddr); 502} 503 504/* 505 * Add privacy headers appropriate for the specified key. 506 */ 507struct ieee80211_key * 508ieee80211_crypto_encap(struct ieee80211com *ic, 509 struct ieee80211_node *ni, struct mbuf *m) 510{ 511 struct ieee80211_key *k; 512 struct ieee80211_frame *wh; 513 const struct ieee80211_cipher *cip; 514 u_int8_t keyid; 515 516 /* 517 * Multicast traffic always uses the multicast key. 518 * Otherwise if a unicast key is set we use that and 519 * it is always key index 0. When no unicast key is 520 * set we fall back to the default transmit key. 521 */ 522 wh = mtod(m, struct ieee80211_frame *); 523 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 524 ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) { 525 if (ic->ic_def_txkey == IEEE80211_KEYIX_NONE) { 526 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 527 "[%s] no default transmit key (%s) deftxkey %u\n", 528 ether_sprintf(wh->i_addr1), __func__, 529 ic->ic_def_txkey); 530 ic->ic_stats.is_tx_nodefkey++; 531 return NULL; 532 } 533 keyid = ic->ic_def_txkey; 534 k = &ic->ic_nw_keys[ic->ic_def_txkey]; 535 } else { 536 keyid = 0; 537 k = &ni->ni_ucastkey; 538 } 539 cip = k->wk_cipher; 540 return (cip->ic_encap(k, m, keyid<<6) ? k : NULL); 541} 542 543/* 544 * Validate and strip privacy headers (and trailer) for a 545 * received frame that has the WEP/Privacy bit set. 546 */ 547struct ieee80211_key * 548ieee80211_crypto_decap(struct ieee80211com *ic, 549 struct ieee80211_node *ni, struct mbuf *m, int hdrlen) 550{ 551#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN) 552#define IEEE80211_WEP_MINLEN \ 553 (sizeof(struct ieee80211_frame) + \ 554 IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN) 555 struct ieee80211_key *k; 556 struct ieee80211_frame *wh; 557 const struct ieee80211_cipher *cip; 558 const u_int8_t *ivp; 559 u_int8_t keyid; 560 561 /* NB: this minimum size data frame could be bigger */ 562 if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) { 563 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY, 564 "%s: WEP data frame too short, len %u\n", 565 __func__, m->m_pkthdr.len); 566 ic->ic_stats.is_rx_tooshort++; /* XXX need unique stat? */ 567 return NULL; 568 } 569 570 /* 571 * Locate the key. If unicast and there is no unicast 572 * key then we fall back to the key id in the header. 573 * This assumes unicast keys are only configured when 574 * the key id in the header is meaningless (typically 0). 575 */ 576 wh = mtod(m, struct ieee80211_frame *); 577 ivp = mtod(m, const u_int8_t *) + hdrlen; /* XXX contig */ 578 keyid = ivp[IEEE80211_WEP_IVLEN]; 579 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 580 ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) 581 k = &ic->ic_nw_keys[keyid >> 6]; 582 else 583 k = &ni->ni_ucastkey; 584 585 /* 586 * Insure crypto header is contiguous for all decap work. 587 */ 588 cip = k->wk_cipher; 589 if (m->m_len < hdrlen + cip->ic_header && 590 (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) { 591 IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO, 592 "[%s] unable to pullup %s header\n", 593 ether_sprintf(wh->i_addr2), cip->ic_name); 594 ic->ic_stats.is_rx_wepfail++; /* XXX */ 595 return 0; 596 } 597 598 return (cip->ic_decap(k, m, hdrlen) ? k : NULL); 599#undef IEEE80211_WEP_MINLEN 600#undef IEEE80211_WEP_HDRLEN 601} 602