]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211_crypto.c
Split ieee80211_notify_erp into locked and unlocked variants
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211_crypto.c
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2008 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 "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, const 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 = k - vap->iv_nw_keys;
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         const uint8_t mac[IEEE80211_ADDR_LEN])
94 {
95         return 1;
96 }
97 static void null_key_update(struct ieee80211vap *vap) {}
98
99 /*
100  * Write-arounds for common operations.
101  */
102 static __inline void
103 cipher_detach(struct ieee80211_key *key)
104 {
105         key->wk_cipher->ic_detach(key);
106 }
107
108 static __inline void *
109 cipher_attach(struct ieee80211vap *vap, struct ieee80211_key *key)
110 {
111         return key->wk_cipher->ic_attach(vap, key);
112 }
113
114 /* 
115  * Wrappers for driver key management methods.
116  */
117 static __inline int
118 dev_key_alloc(struct ieee80211vap *vap,
119         const struct ieee80211_key *key,
120         ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
121 {
122         return vap->iv_key_alloc(vap, key, keyix, rxkeyix);
123 }
124
125 static __inline int
126 dev_key_delete(struct ieee80211vap *vap,
127         const struct ieee80211_key *key)
128 {
129         return vap->iv_key_delete(vap, key);
130 }
131
132 static __inline int
133 dev_key_set(struct ieee80211vap *vap, const struct ieee80211_key *key)
134 {
135         return vap->iv_key_set(vap, key, key->wk_macaddr);
136 }
137
138 /*
139  * Setup crypto support for a device/shared instance.
140  */
141 void
142 ieee80211_crypto_attach(struct ieee80211com *ic)
143 {
144         /* NB: we assume everything is pre-zero'd */
145         ciphers[IEEE80211_CIPHER_NONE] = &ieee80211_cipher_none;
146 }
147
148 /*
149  * Teardown crypto support.
150  */
151 void
152 ieee80211_crypto_detach(struct ieee80211com *ic)
153 {
154 }
155
156 /*
157  * Setup crypto support for a vap.
158  */
159 void
160 ieee80211_crypto_vattach(struct ieee80211vap *vap)
161 {
162         int i;
163
164         /* NB: we assume everything is pre-zero'd */
165         vap->iv_max_keyix = IEEE80211_WEP_NKID;
166         vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
167         for (i = 0; i < IEEE80211_WEP_NKID; i++)
168                 ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
169                         IEEE80211_KEYIX_NONE);
170         /*
171          * Initialize the driver key support routines to noop entries.
172          * This is useful especially for the cipher test modules.
173          */
174         vap->iv_key_alloc = null_key_alloc;
175         vap->iv_key_set = null_key_set;
176         vap->iv_key_delete = null_key_delete;
177         vap->iv_key_update_begin = null_key_update;
178         vap->iv_key_update_end = null_key_update;
179 }
180
181 /*
182  * Teardown crypto support for a vap.
183  */
184 void
185 ieee80211_crypto_vdetach(struct ieee80211vap *vap)
186 {
187         ieee80211_crypto_delglobalkeys(vap);
188 }
189
190 /*
191  * Register a crypto cipher module.
192  */
193 void
194 ieee80211_crypto_register(const struct ieee80211_cipher *cip)
195 {
196         if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
197                 printf("%s: cipher %s has an invalid cipher index %u\n",
198                         __func__, cip->ic_name, cip->ic_cipher);
199                 return;
200         }
201         if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
202                 printf("%s: cipher %s registered with a different template\n",
203                         __func__, cip->ic_name);
204                 return;
205         }
206         ciphers[cip->ic_cipher] = cip;
207 }
208
209 /*
210  * Unregister a crypto cipher module.
211  */
212 void
213 ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
214 {
215         if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
216                 printf("%s: cipher %s has an invalid cipher index %u\n",
217                         __func__, cip->ic_name, cip->ic_cipher);
218                 return;
219         }
220         if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
221                 printf("%s: cipher %s registered with a different template\n",
222                         __func__, cip->ic_name);
223                 return;
224         }
225         /* NB: don't complain about not being registered */
226         /* XXX disallow if references */
227         ciphers[cip->ic_cipher] = NULL;
228 }
229
230 int
231 ieee80211_crypto_available(u_int cipher)
232 {
233         return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
234 }
235
236 /* XXX well-known names! */
237 static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
238         "wlan_wep",     /* IEEE80211_CIPHER_WEP */
239         "wlan_tkip",    /* IEEE80211_CIPHER_TKIP */
240         "wlan_aes_ocb", /* IEEE80211_CIPHER_AES_OCB */
241         "wlan_ccmp",    /* IEEE80211_CIPHER_AES_CCM */
242         "#4",           /* reserved */
243         "wlan_ckip",    /* IEEE80211_CIPHER_CKIP */
244         "wlan_none",    /* IEEE80211_CIPHER_NONE */
245 };
246
247 /*
248  * Establish a relationship between the specified key and cipher
249  * and, if necessary, allocate a hardware index from the driver.
250  * Note that when a fixed key index is required it must be specified
251  * and we blindly assign it w/o consulting the driver (XXX).
252  *
253  * This must be the first call applied to a key; all the other key
254  * routines assume wk_cipher is setup.
255  *
256  * Locking must be handled by the caller using:
257  *      ieee80211_key_update_begin(vap);
258  *      ieee80211_key_update_end(vap);
259  */
260 int
261 ieee80211_crypto_newkey(struct ieee80211vap *vap,
262         int cipher, int flags, struct ieee80211_key *key)
263 {
264         struct ieee80211com *ic = vap->iv_ic;
265         const struct ieee80211_cipher *cip;
266         ieee80211_keyix keyix, rxkeyix;
267         void *keyctx;
268         int oflags;
269
270         /*
271          * Validate cipher and set reference to cipher routines.
272          */
273         if (cipher >= IEEE80211_CIPHER_MAX) {
274                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
275                     "%s: invalid cipher %u\n", __func__, cipher);
276                 vap->iv_stats.is_crypto_badcipher++;
277                 return 0;
278         }
279         cip = ciphers[cipher];
280         if (cip == NULL) {
281                 /*
282                  * Auto-load cipher module if we have a well-known name
283                  * for it.  It might be better to use string names rather
284                  * than numbers and craft a module name based on the cipher
285                  * name; e.g. wlan_cipher_<cipher-name>.
286                  */
287                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
288                     "%s: unregistered cipher %u, load module %s\n",
289                     __func__, cipher, cipher_modnames[cipher]);
290                 ieee80211_load_module(cipher_modnames[cipher]);
291                 /*
292                  * If cipher module loaded it should immediately
293                  * call ieee80211_crypto_register which will fill
294                  * in the entry in the ciphers array.
295                  */
296                 cip = ciphers[cipher];
297                 if (cip == NULL) {
298                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
299                             "%s: unable to load cipher %u, module %s\n",
300                             __func__, cipher, cipher_modnames[cipher]);
301                         vap->iv_stats.is_crypto_nocipher++;
302                         return 0;
303                 }
304         }
305
306         oflags = key->wk_flags;
307         flags &= IEEE80211_KEY_COMMON;
308         /*
309          * If the hardware does not support the cipher then
310          * fallback to a host-based implementation.
311          */
312         if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
313                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
314                     "%s: no h/w support for cipher %s, falling back to s/w\n",
315                     __func__, cip->ic_name);
316                 flags |= IEEE80211_KEY_SWCRYPT;
317         }
318         /*
319          * Hardware TKIP with software MIC is an important
320          * combination; we handle it by flagging each key,
321          * the cipher modules honor it.
322          */
323         if (cipher == IEEE80211_CIPHER_TKIP &&
324             (ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
325                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
326                     "%s: no h/w support for TKIP MIC, falling back to s/w\n",
327                     __func__);
328                 flags |= IEEE80211_KEY_SWMIC;
329         }
330
331         /*
332          * Bind cipher to key instance.  Note we do this
333          * after checking the device capabilities so the
334          * cipher module can optimize space usage based on
335          * whether or not it needs to do the cipher work.
336          */
337         if (key->wk_cipher != cip || key->wk_flags != flags) {
338 again:
339                 /*
340                  * Fillin the flags so cipher modules can see s/w
341                  * crypto requirements and potentially allocate
342                  * different state and/or attach different method
343                  * pointers.
344                  *
345                  * XXX this is not right when s/w crypto fallback
346                  *     fails and we try to restore previous state.
347                  */
348                 key->wk_flags = flags;
349                 keyctx = cip->ic_attach(vap, key);
350                 if (keyctx == NULL) {
351                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
352                                 "%s: unable to attach cipher %s\n",
353                                 __func__, cip->ic_name);
354                         key->wk_flags = oflags; /* restore old flags */
355                         vap->iv_stats.is_crypto_attachfail++;
356                         return 0;
357                 }
358                 cipher_detach(key);
359                 key->wk_cipher = cip;           /* XXX refcnt? */
360                 key->wk_private = keyctx;
361         }
362         /*
363          * Commit to requested usage so driver can see the flags.
364          */
365         key->wk_flags = flags;
366
367         /*
368          * Ask the driver for a key index if we don't have one.
369          * Note that entries in the global key table always have
370          * an index; this means it's safe to call this routine
371          * for these entries just to setup the reference to the
372          * cipher template.  Note also that when using software
373          * crypto we also call the driver to give us a key index.
374          */
375         if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
376                 if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
377                         /*
378                          * Driver has no room; fallback to doing crypto
379                          * in the host.  We change the flags and start the
380                          * procedure over.  If we get back here then there's
381                          * no hope and we bail.  Note that this can leave
382                          * the key in a inconsistent state if the caller
383                          * continues to use it.
384                          */
385                         if ((key->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
386                                 vap->iv_stats.is_crypto_swfallback++;
387                                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
388                                     "%s: no h/w resources for cipher %s, "
389                                     "falling back to s/w\n", __func__,
390                                     cip->ic_name);
391                                 oflags = key->wk_flags;
392                                 flags |= IEEE80211_KEY_SWCRYPT;
393                                 if (cipher == IEEE80211_CIPHER_TKIP)
394                                         flags |= IEEE80211_KEY_SWMIC;
395                                 goto again;
396                         }
397                         vap->iv_stats.is_crypto_keyfail++;
398                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
399                             "%s: unable to setup cipher %s\n",
400                             __func__, cip->ic_name);
401                         return 0;
402                 }
403                 key->wk_keyix = keyix;
404                 key->wk_rxkeyix = rxkeyix;
405         }
406         return 1;
407 }
408
409 /*
410  * Remove the key (no locking, for internal use).
411  */
412 static int
413 _ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
414 {
415         ieee80211_keyix keyix;
416
417         KASSERT(key->wk_cipher != NULL, ("No cipher!"));
418
419         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
420             "%s: %s keyix %u flags 0x%x rsc %ju tsc %ju len %u\n",
421             __func__, key->wk_cipher->ic_name,
422             key->wk_keyix, key->wk_flags,
423             key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
424             key->wk_keylen);
425
426         keyix = key->wk_keyix;
427         if (keyix != IEEE80211_KEYIX_NONE) {
428                 /*
429                  * Remove hardware entry.
430                  */
431                 /* XXX key cache */
432                 if (!dev_key_delete(vap, key)) {
433                         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
434                             "%s: driver did not delete key index %u\n",
435                             __func__, keyix);
436                         vap->iv_stats.is_crypto_delkey++;
437                         /* XXX recovery? */
438                 }
439         }
440         cipher_detach(key);
441         memset(key, 0, sizeof(*key));
442         ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
443         return 1;
444 }
445
446 /*
447  * Remove the specified key.
448  */
449 int
450 ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
451 {
452         int status;
453
454         ieee80211_key_update_begin(vap);
455         status = _ieee80211_crypto_delkey(vap, key);
456         ieee80211_key_update_end(vap);
457         return status;
458 }
459
460 /*
461  * Clear the global key table.
462  */
463 void
464 ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
465 {
466         int i;
467
468         ieee80211_key_update_begin(vap);
469         for (i = 0; i < IEEE80211_WEP_NKID; i++)
470                 (void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
471         ieee80211_key_update_end(vap);
472 }
473
474 /*
475  * Set the contents of the specified key.
476  *
477  * Locking must be handled by the caller using:
478  *      ieee80211_key_update_begin(vap);
479  *      ieee80211_key_update_end(vap);
480  */
481 int
482 ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
483 {
484         const struct ieee80211_cipher *cip = key->wk_cipher;
485
486         KASSERT(cip != NULL, ("No cipher!"));
487
488         IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
489             "%s: %s keyix %u flags 0x%x mac %s rsc %ju tsc %ju len %u\n",
490             __func__, cip->ic_name, key->wk_keyix,
491             key->wk_flags, ether_sprintf(key->wk_macaddr),
492             key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
493             key->wk_keylen);
494
495         /*
496          * Give cipher a chance to validate key contents.
497          * XXX should happen before modifying state.
498          */
499         if (!cip->ic_setkey(key)) {
500                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
501                     "%s: cipher %s rejected key index %u len %u flags 0x%x\n",
502                     __func__, cip->ic_name, key->wk_keyix,
503                     key->wk_keylen, key->wk_flags);
504                 vap->iv_stats.is_crypto_setkey_cipher++;
505                 return 0;
506         }
507         if (key->wk_keyix == IEEE80211_KEYIX_NONE) {
508                 /* XXX nothing allocated, should not happen */
509                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
510                     "%s: no key index; should not happen!\n", __func__);
511                 vap->iv_stats.is_crypto_setkey_nokey++;
512                 return 0;
513         }
514         return dev_key_set(vap, key);
515 }
516
517 /*
518  * Add privacy headers appropriate for the specified key.
519  */
520 struct ieee80211_key *
521 ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
522 {
523         struct ieee80211vap *vap = ni->ni_vap;
524         struct ieee80211_key *k;
525         struct ieee80211_frame *wh;
526         const struct ieee80211_cipher *cip;
527         uint8_t keyid;
528
529         /*
530          * Multicast traffic always uses the multicast key.
531          * Otherwise if a unicast key is set we use that and
532          * it is always key index 0.  When no unicast key is
533          * set we fall back to the default transmit key.
534          */
535         wh = mtod(m, struct ieee80211_frame *);
536         if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
537             IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey)) {
538                 if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
539                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
540                             wh->i_addr1,
541                             "no default transmit key (%s) deftxkey %u",
542                             __func__, vap->iv_def_txkey);
543                         vap->iv_stats.is_tx_nodefkey++;
544                         return NULL;
545                 }
546                 keyid = vap->iv_def_txkey;
547                 k = &vap->iv_nw_keys[vap->iv_def_txkey];
548         } else {
549                 keyid = 0;
550                 k = &ni->ni_ucastkey;
551         }
552         cip = k->wk_cipher;
553         return (cip->ic_encap(k, m, keyid<<6) ? k : NULL);
554 }
555
556 /*
557  * Validate and strip privacy headers (and trailer) for a
558  * received frame that has the WEP/Privacy bit set.
559  */
560 struct ieee80211_key *
561 ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen)
562 {
563 #define IEEE80211_WEP_HDRLEN    (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
564 #define IEEE80211_WEP_MINLEN \
565         (sizeof(struct ieee80211_frame) + \
566         IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
567         struct ieee80211vap *vap = ni->ni_vap;
568         struct ieee80211_key *k;
569         struct ieee80211_frame *wh;
570         const struct ieee80211_cipher *cip;
571         uint8_t keyid;
572
573         /* NB: this minimum size data frame could be bigger */
574         if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
575                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
576                         "%s: WEP data frame too short, len %u\n",
577                         __func__, m->m_pkthdr.len);
578                 vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */
579                 return NULL;
580         }
581
582         /*
583          * Locate the key. If unicast and there is no unicast
584          * key then we fall back to the key id in the header.
585          * This assumes unicast keys are only configured when
586          * the key id in the header is meaningless (typically 0).
587          */
588         wh = mtod(m, struct ieee80211_frame *);
589         m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
590         if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
591             IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
592                 k = &vap->iv_nw_keys[keyid >> 6];
593         else
594                 k = &ni->ni_ucastkey;
595
596         /*
597          * Insure crypto header is contiguous for all decap work.
598          */
599         cip = k->wk_cipher;
600         if (m->m_len < hdrlen + cip->ic_header &&
601             (m = m_pullup(m, hdrlen + cip->ic_header)) == NULL) {
602                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
603                     "unable to pullup %s header", cip->ic_name);
604                 vap->iv_stats.is_rx_wepfail++;  /* XXX */
605                 return NULL;
606         }
607
608         return (cip->ic_decap(k, m, hdrlen) ? k : NULL);
609 #undef IEEE80211_WEP_MINLEN
610 #undef IEEE80211_WEP_HDRLEN
611 }
612
613 static void
614 load_ucastkey(void *arg, struct ieee80211_node *ni)
615 {
616         struct ieee80211vap *vap = ni->ni_vap;
617         struct ieee80211_key *k;
618
619         if (vap->iv_state != IEEE80211_S_RUN)
620                 return;
621         k = &ni->ni_ucastkey;
622         if (k->wk_keyix != IEEE80211_KEYIX_NONE)
623                 dev_key_set(vap, k);
624 }
625
626 /*
627  * Re-load all keys known to the 802.11 layer that may
628  * have hardware state backing them.  This is used by
629  * drivers on resume to push keys down into the device.
630  */
631 void
632 ieee80211_crypto_reload_keys(struct ieee80211com *ic)
633 {
634         struct ieee80211vap *vap;
635         int i;
636
637         /*
638          * Keys in the global key table of each vap.
639          */
640         /* NB: used only during resume so don't lock for now */
641         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
642                 if (vap->iv_state != IEEE80211_S_RUN)
643                         continue;
644                 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
645                         const struct ieee80211_key *k = &vap->iv_nw_keys[i];
646                         if (k->wk_keyix != IEEE80211_KEYIX_NONE)
647                                 dev_key_set(vap, k);
648                 }
649         }
650         /*
651          * Unicast keys.
652          */
653         ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
654 }