Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/net80211/ieee80211_crypto.c
39475 views
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
66
if (!ieee80211_is_key_global(vap, k)) {
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
* Default set of key management types supported by net80211.
160
*
161
* These are supported by software net80211 and announced/
162
* driven by hostapd + wpa_supplicant.
163
*
164
* Drivers doing full supplicant offload must not set
165
* anything here.
166
*
167
* Note that IEEE80211_C_WPA1 and IEEE80211_C_WPA2 are the
168
* "old" style way of drivers announcing key management
169
* capabilities. There are many, many more key management
170
* suites in 802.11-2016 (see 9.4.2.25.3 - AKM suites.)
171
* For now they still need to be set - these flags are checked
172
* when assembling a beacon to reserve space for the WPA
173
* vendor IE (WPA 1) and RSN IE (WPA 2).
174
*/
175
ic->ic_sw_keymgmtcaps = 0;
176
}
177
178
/*
179
* Teardown crypto support.
180
*/
181
void
182
ieee80211_crypto_detach(struct ieee80211com *ic)
183
{
184
}
185
186
/*
187
* Set the supported ciphers for software encryption.
188
*/
189
void
190
ieee80211_crypto_set_supported_software_ciphers(struct ieee80211com *ic,
191
uint32_t cipher_set)
192
{
193
ic->ic_sw_cryptocaps = cipher_set;
194
}
195
196
/*
197
* Set the supported ciphers for hardware encryption.
198
*/
199
void
200
ieee80211_crypto_set_supported_hardware_ciphers(struct ieee80211com *ic,
201
uint32_t cipher_set)
202
{
203
ic->ic_cryptocaps = cipher_set;
204
}
205
206
/*
207
* Set the supported software key management by the driver.
208
*
209
* These are the key management suites that are supported via
210
* the driver via hostapd/wpa_supplicant.
211
*
212
* Key management which is completely offloaded (ie, the supplicant
213
* runs in hardware/firmware) must not be set here.
214
*/
215
void
216
ieee80211_crypto_set_supported_driver_keymgmt(struct ieee80211com *ic,
217
uint32_t keymgmt_set)
218
{
219
220
ic->ic_sw_keymgmtcaps = keymgmt_set;
221
}
222
223
/*
224
* Setup crypto support for a vap.
225
*/
226
void
227
ieee80211_crypto_vattach(struct ieee80211vap *vap)
228
{
229
int i;
230
231
/* NB: we assume everything is pre-zero'd */
232
vap->iv_max_keyix = IEEE80211_WEP_NKID;
233
vap->iv_def_txkey = IEEE80211_KEYIX_NONE;
234
for (i = 0; i < IEEE80211_WEP_NKID; i++)
235
ieee80211_crypto_resetkey(vap, &vap->iv_nw_keys[i],
236
IEEE80211_KEYIX_NONE);
237
/*
238
* Initialize the driver key support routines to noop entries.
239
* This is useful especially for the cipher test modules.
240
*/
241
vap->iv_key_alloc = null_key_alloc;
242
vap->iv_key_set = null_key_set;
243
vap->iv_key_delete = null_key_delete;
244
vap->iv_key_update_begin = null_key_update;
245
vap->iv_key_update_end = null_key_update;
246
}
247
248
/*
249
* Teardown crypto support for a vap.
250
*/
251
void
252
ieee80211_crypto_vdetach(struct ieee80211vap *vap)
253
{
254
ieee80211_crypto_delglobalkeys(vap);
255
}
256
257
/*
258
* Register a crypto cipher module.
259
*/
260
void
261
ieee80211_crypto_register(const struct ieee80211_cipher *cip)
262
{
263
if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
264
net80211_printf("%s: cipher %s has an invalid cipher index %u\n",
265
__func__, cip->ic_name, cip->ic_cipher);
266
return;
267
}
268
if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
269
net80211_printf("%s: cipher %s registered with a different template\n",
270
__func__, cip->ic_name);
271
return;
272
}
273
ciphers[cip->ic_cipher] = cip;
274
}
275
276
/*
277
* Unregister a crypto cipher module.
278
*/
279
void
280
ieee80211_crypto_unregister(const struct ieee80211_cipher *cip)
281
{
282
if (cip->ic_cipher >= IEEE80211_CIPHER_MAX) {
283
net80211_printf("%s: cipher %s has an invalid cipher index %u\n",
284
__func__, cip->ic_name, cip->ic_cipher);
285
return;
286
}
287
if (ciphers[cip->ic_cipher] != NULL && ciphers[cip->ic_cipher] != cip) {
288
net80211_printf("%s: cipher %s registered with a different template\n",
289
__func__, cip->ic_name);
290
return;
291
}
292
/* NB: don't complain about not being registered */
293
/* XXX disallow if references */
294
ciphers[cip->ic_cipher] = NULL;
295
}
296
297
int
298
ieee80211_crypto_available(u_int cipher)
299
{
300
return cipher < IEEE80211_CIPHER_MAX && ciphers[cipher] != NULL;
301
}
302
303
/* XXX well-known names! */
304
static const char *cipher_modnames[IEEE80211_CIPHER_MAX] = {
305
[IEEE80211_CIPHER_WEP] = "wlan_wep",
306
[IEEE80211_CIPHER_TKIP] = "wlan_tkip",
307
[IEEE80211_CIPHER_AES_OCB] = "wlan_aes_ocb",
308
[IEEE80211_CIPHER_AES_CCM] = "wlan_ccmp",
309
[IEEE80211_CIPHER_TKIPMIC] = "#4", /* NB: reserved */
310
[IEEE80211_CIPHER_CKIP] = "wlan_ckip",
311
[IEEE80211_CIPHER_NONE] = "wlan_none",
312
[IEEE80211_CIPHER_AES_CCM_256] = "wlan_ccmp",
313
[IEEE80211_CIPHER_BIP_CMAC_128] = "wlan_bip_cmac",
314
[IEEE80211_CIPHER_BIP_CMAC_256] = "wlan_bip_cmac",
315
[IEEE80211_CIPHER_BIP_GMAC_128] = "wlan_bip_gmac",
316
[IEEE80211_CIPHER_BIP_GMAC_256] = "wlan_bip_gmac",
317
[IEEE80211_CIPHER_AES_GCM_128] = "wlan_gcmp",
318
[IEEE80211_CIPHER_AES_GCM_256] = "wlan_gcmp",
319
};
320
321
/* NB: there must be no overlap between user-supplied and device-owned flags */
322
CTASSERT((IEEE80211_KEY_COMMON & IEEE80211_KEY_DEVICE) == 0);
323
324
/*
325
* Establish a relationship between the specified key and cipher
326
* and, if necessary, allocate a hardware index from the driver.
327
* Note that when a fixed key index is required it must be specified.
328
*
329
* This must be the first call applied to a key; all the other key
330
* routines assume wk_cipher is setup.
331
*
332
* Locking must be handled by the caller using:
333
* ieee80211_key_update_begin(vap);
334
* ieee80211_key_update_end(vap);
335
*/
336
int
337
ieee80211_crypto_newkey(struct ieee80211vap *vap,
338
int cipher, int flags, struct ieee80211_key *key)
339
{
340
struct ieee80211com *ic = vap->iv_ic;
341
const struct ieee80211_cipher *cip;
342
ieee80211_keyix keyix, rxkeyix;
343
void *keyctx;
344
int oflags;
345
346
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
347
"%s: cipher %u flags 0x%x keyix %u\n",
348
__func__, cipher, flags, key->wk_keyix);
349
350
/*
351
* Validate cipher and set reference to cipher routines.
352
*/
353
if (cipher >= IEEE80211_CIPHER_MAX) {
354
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
355
"%s: invalid cipher %u\n", __func__, cipher);
356
vap->iv_stats.is_crypto_badcipher++;
357
return 0;
358
}
359
cip = ciphers[cipher];
360
if (cip == NULL) {
361
/*
362
* Auto-load cipher module if we have a well-known name
363
* for it. It might be better to use string names rather
364
* than numbers and craft a module name based on the cipher
365
* name; e.g. wlan_cipher_<cipher-name>.
366
*/
367
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
368
"%s: unregistered cipher %u, load module %s\n",
369
__func__, cipher, cipher_modnames[cipher]);
370
ieee80211_load_module(cipher_modnames[cipher]);
371
/*
372
* If cipher module loaded it should immediately
373
* call ieee80211_crypto_register which will fill
374
* in the entry in the ciphers array.
375
*/
376
cip = ciphers[cipher];
377
if (cip == NULL) {
378
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
379
"%s: unable to load cipher %u, module %s\n",
380
__func__, cipher, cipher_modnames[cipher]);
381
vap->iv_stats.is_crypto_nocipher++;
382
return 0;
383
}
384
}
385
386
oflags = key->wk_flags;
387
flags &= IEEE80211_KEY_COMMON;
388
/* NB: preserve device attributes */
389
flags |= (oflags & IEEE80211_KEY_DEVICE);
390
/*
391
* If the hardware does not support the cipher then
392
* fallback to a host-based implementation.
393
*/
394
if ((ic->ic_cryptocaps & (1<<cipher)) == 0) {
395
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
396
"%s: no h/w support for cipher %s, falling back to s/w\n",
397
__func__, cip->ic_name);
398
flags |= IEEE80211_KEY_SWCRYPT;
399
}
400
/*
401
* Check if the software cipher is available; if not then
402
* fail it early.
403
*
404
* Some devices do not support all ciphers in software
405
* (for example they don't support a "raw" data path.)
406
*/
407
if ((flags & IEEE80211_KEY_SWCRYPT) &&
408
(ic->ic_sw_cryptocaps & (1<<cipher)) == 0) {
409
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
410
"%s: no s/w support for cipher %s, rejecting\n",
411
__func__, cip->ic_name);
412
vap->iv_stats.is_crypto_swcipherfail++;
413
return (0);
414
}
415
/*
416
* Hardware TKIP with software MIC is an important
417
* combination; we handle it by flagging each key,
418
* the cipher modules honor it.
419
*/
420
if (cipher == IEEE80211_CIPHER_TKIP &&
421
(ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIPMIC) == 0) {
422
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
423
"%s: no h/w support for TKIP MIC, falling back to s/w\n",
424
__func__);
425
flags |= IEEE80211_KEY_SWMIC;
426
}
427
428
/*
429
* Bind cipher to key instance. Note we do this
430
* after checking the device capabilities so the
431
* cipher module can optimize space usage based on
432
* whether or not it needs to do the cipher work.
433
*/
434
if (key->wk_cipher != cip || key->wk_flags != flags) {
435
/*
436
* Fillin the flags so cipher modules can see s/w
437
* crypto requirements and potentially allocate
438
* different state and/or attach different method
439
* pointers.
440
*/
441
key->wk_flags = flags;
442
keyctx = cip->ic_attach(vap, key);
443
if (keyctx == NULL) {
444
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
445
"%s: unable to attach cipher %s\n",
446
__func__, cip->ic_name);
447
key->wk_flags = oflags; /* restore old flags */
448
vap->iv_stats.is_crypto_attachfail++;
449
return 0;
450
}
451
cipher_detach(key);
452
key->wk_cipher = cip; /* XXX refcnt? */
453
key->wk_private = keyctx;
454
}
455
456
/*
457
* Ask the driver for a key index if we don't have one.
458
* Note that entries in the global key table always have
459
* an index; this means it's safe to call this routine
460
* for these entries just to setup the reference to the
461
* cipher template. Note also that when using software
462
* crypto we also call the driver to give us a key index.
463
*/
464
if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
465
if (!dev_key_alloc(vap, key, &keyix, &rxkeyix)) {
466
/*
467
* Unable to setup driver state.
468
*/
469
vap->iv_stats.is_crypto_keyfail++;
470
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
471
"%s: unable to setup cipher %s\n",
472
__func__, cip->ic_name);
473
return 0;
474
}
475
if (key->wk_flags != flags) {
476
/*
477
* Driver overrode flags we setup; typically because
478
* resources were unavailable to handle _this_ key.
479
* Re-attach the cipher context to allow cipher
480
* modules to handle differing requirements.
481
*/
482
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
483
"%s: driver override for cipher %s, flags "
484
"%b -> %b\n", __func__, cip->ic_name,
485
oflags, IEEE80211_KEY_BITS,
486
key->wk_flags, IEEE80211_KEY_BITS);
487
keyctx = cip->ic_attach(vap, key);
488
if (keyctx == NULL) {
489
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
490
"%s: unable to attach cipher %s with "
491
"flags %b\n", __func__, cip->ic_name,
492
key->wk_flags, IEEE80211_KEY_BITS);
493
key->wk_flags = oflags; /* restore old flags */
494
vap->iv_stats.is_crypto_attachfail++;
495
return 0;
496
}
497
cipher_detach(key);
498
key->wk_cipher = cip; /* XXX refcnt? */
499
key->wk_private = keyctx;
500
}
501
key->wk_keyix = keyix;
502
key->wk_rxkeyix = rxkeyix;
503
key->wk_flags |= IEEE80211_KEY_DEVKEY;
504
}
505
return 1;
506
}
507
508
/*
509
* Remove the key (no locking, for internal use).
510
*/
511
static int
512
_ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
513
{
514
KASSERT(key->wk_cipher != NULL, ("No cipher!"));
515
516
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
517
"%s: %s keyix %u flags %b rsc %ju tsc %ju len %u\n",
518
__func__, key->wk_cipher->ic_name,
519
key->wk_keyix, key->wk_flags, IEEE80211_KEY_BITS,
520
key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
521
key->wk_keylen);
522
523
if (key->wk_flags & IEEE80211_KEY_DEVKEY) {
524
/*
525
* Remove hardware entry.
526
*/
527
/* XXX key cache */
528
if (!dev_key_delete(vap, key)) {
529
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
530
"%s: driver did not delete key index %u\n",
531
__func__, key->wk_keyix);
532
vap->iv_stats.is_crypto_delkey++;
533
/* XXX recovery? */
534
}
535
}
536
cipher_detach(key);
537
memset(key, 0, sizeof(*key));
538
ieee80211_crypto_resetkey(vap, key, IEEE80211_KEYIX_NONE);
539
return 1;
540
}
541
542
/*
543
* Remove the specified key.
544
*/
545
int
546
ieee80211_crypto_delkey(struct ieee80211vap *vap, struct ieee80211_key *key)
547
{
548
int status;
549
550
ieee80211_key_update_begin(vap);
551
status = _ieee80211_crypto_delkey(vap, key);
552
ieee80211_key_update_end(vap);
553
return status;
554
}
555
556
/*
557
* Clear the global key table.
558
*/
559
void
560
ieee80211_crypto_delglobalkeys(struct ieee80211vap *vap)
561
{
562
int i;
563
564
ieee80211_key_update_begin(vap);
565
for (i = 0; i < IEEE80211_WEP_NKID; i++)
566
(void) _ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[i]);
567
ieee80211_key_update_end(vap);
568
}
569
570
/*
571
* Set the contents of the specified key.
572
*
573
* Locking must be handled by the caller using:
574
* ieee80211_key_update_begin(vap);
575
* ieee80211_key_update_end(vap);
576
*/
577
int
578
ieee80211_crypto_setkey(struct ieee80211vap *vap, struct ieee80211_key *key)
579
{
580
const struct ieee80211_cipher *cip = key->wk_cipher;
581
582
KASSERT(cip != NULL, ("No cipher!"));
583
584
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
585
"%s: %s keyix %u flags %b mac %s rsc %ju tsc %ju len %u\n",
586
__func__, cip->ic_name, key->wk_keyix,
587
key->wk_flags, IEEE80211_KEY_BITS, ether_sprintf(key->wk_macaddr),
588
key->wk_keyrsc[IEEE80211_NONQOS_TID], key->wk_keytsc,
589
key->wk_keylen);
590
591
if ((key->wk_flags & IEEE80211_KEY_DEVKEY) == 0) {
592
/* XXX nothing allocated, should not happen */
593
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
594
"%s: no device key setup done; should not happen!\n",
595
__func__);
596
vap->iv_stats.is_crypto_setkey_nokey++;
597
return 0;
598
}
599
/*
600
* Give cipher a chance to validate key contents.
601
* XXX should happen before modifying state.
602
*/
603
if (!cip->ic_setkey(key)) {
604
IEEE80211_DPRINTF(vap, IEEE80211_MSG_CRYPTO,
605
"%s: cipher %s rejected key index %u len %u flags %b\n",
606
__func__, cip->ic_name, key->wk_keyix,
607
key->wk_keylen, key->wk_flags, IEEE80211_KEY_BITS);
608
vap->iv_stats.is_crypto_setkey_cipher++;
609
return 0;
610
}
611
return dev_key_set(vap, key);
612
}
613
614
/*
615
* Return index if the key is a WEP key (0..3); -1 otherwise.
616
*
617
* This is different to "get_keyid" which defaults to returning
618
* 0 for unicast keys; it assumes that it won't be used for WEP.
619
*/
620
int
621
ieee80211_crypto_get_key_wepidx(const struct ieee80211vap *vap,
622
const struct ieee80211_key *k)
623
{
624
625
if (ieee80211_is_key_global(vap, k)) {
626
return (k - vap->iv_nw_keys);
627
}
628
return (-1);
629
}
630
631
/*
632
* Note: only supports a single unicast key (0).
633
*/
634
uint8_t
635
ieee80211_crypto_get_keyid(struct ieee80211vap *vap, struct ieee80211_key *k)
636
{
637
if (ieee80211_is_key_global(vap, k)) {
638
return (k - vap->iv_nw_keys);
639
}
640
641
return (0);
642
}
643
644
struct ieee80211_key *
645
ieee80211_crypto_get_txkey(struct ieee80211_node *ni, struct mbuf *m)
646
{
647
struct ieee80211vap *vap = ni->ni_vap;
648
struct ieee80211_frame *wh;
649
650
/*
651
* Multicast traffic always uses the multicast key.
652
*
653
* Historically we would fall back to the default
654
* transmit key if there was no unicast key. This
655
* behaviour was documented up to IEEE Std 802.11-2016,
656
* 12.9.2.2 Per-MSDU/Per-A-MSDU Tx pseudocode, in the
657
* 'else' case but is no longer in later versions of
658
* the standard. Additionally falling back to the
659
* group key for unicast was a security risk.
660
*/
661
wh = mtod(m, struct ieee80211_frame *);
662
if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
663
if (vap->iv_def_txkey == IEEE80211_KEYIX_NONE) {
664
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO,
665
wh->i_addr1,
666
"no default transmit key (%s) deftxkey %u",
667
__func__, vap->iv_def_txkey);
668
vap->iv_stats.is_tx_nodefkey++;
669
return NULL;
670
}
671
return &vap->iv_nw_keys[vap->iv_def_txkey];
672
}
673
674
if (IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
675
return NULL;
676
return &ni->ni_ucastkey;
677
}
678
679
/*
680
* Add privacy headers appropriate for the specified key.
681
*/
682
struct ieee80211_key *
683
ieee80211_crypto_encap(struct ieee80211_node *ni, struct mbuf *m)
684
{
685
struct ieee80211_key *k;
686
const struct ieee80211_cipher *cip;
687
688
if ((k = ieee80211_crypto_get_txkey(ni, m)) != NULL) {
689
cip = k->wk_cipher;
690
return (cip->ic_encap(k, m) ? k : NULL);
691
}
692
693
return NULL;
694
}
695
696
/*
697
* Validate and strip privacy headers (and trailer) for a
698
* received frame that has the WEP/Privacy bit set.
699
*/
700
int
701
ieee80211_crypto_decap(struct ieee80211_node *ni, struct mbuf *m, int hdrlen,
702
struct ieee80211_key **key)
703
{
704
#define IEEE80211_WEP_HDRLEN (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN)
705
#define IEEE80211_WEP_MINLEN \
706
(sizeof(struct ieee80211_frame) + \
707
IEEE80211_WEP_HDRLEN + IEEE80211_WEP_CRCLEN)
708
struct ieee80211vap *vap = ni->ni_vap;
709
struct ieee80211_key *k;
710
struct ieee80211_frame *wh;
711
const struct ieee80211_rx_stats *rxs;
712
const struct ieee80211_cipher *cip;
713
uint8_t keyid;
714
715
/*
716
* Check for hardware decryption and IV stripping.
717
* If the IV is stripped then we definitely can't find a key.
718
* Set the key to NULL but return true; upper layers
719
* will need to handle a NULL key for a successful
720
* decrypt.
721
*/
722
rxs = ieee80211_get_rx_params_ptr(m);
723
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
724
if (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) {
725
/*
726
* Hardware decrypted, IV stripped.
727
* We can't find a key with a stripped IV.
728
* Return successful.
729
*/
730
*key = NULL;
731
return (1);
732
}
733
}
734
735
/* NB: this minimum size data frame could be bigger */
736
if (m->m_pkthdr.len < IEEE80211_WEP_MINLEN) {
737
IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
738
"%s: WEP data frame too short, len %u\n",
739
__func__, m->m_pkthdr.len);
740
vap->iv_stats.is_rx_tooshort++; /* XXX need unique stat? */
741
*key = NULL;
742
return (0);
743
}
744
745
/*
746
* Locate the key. If unicast and there is no unicast
747
* key then we fall back to the key id in the header.
748
* This assumes unicast keys are only configured when
749
* the key id in the header is meaningless (typically 0).
750
*/
751
wh = mtod(m, struct ieee80211_frame *);
752
m_copydata(m, hdrlen + IEEE80211_WEP_IVLEN, sizeof(keyid), &keyid);
753
if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
754
IEEE80211_KEY_UNDEFINED(&ni->ni_ucastkey))
755
k = &vap->iv_nw_keys[keyid >> 6];
756
else
757
k = &ni->ni_ucastkey;
758
759
/*
760
* Ensure crypto header is contiguous and long enough for all
761
* decap work.
762
*/
763
cip = k->wk_cipher;
764
if (m->m_len < hdrlen + cip->ic_header) {
765
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
766
"frame is too short (%d < %u) for crypto decap",
767
cip->ic_name, m->m_len, hdrlen + cip->ic_header);
768
vap->iv_stats.is_rx_tooshort++;
769
*key = NULL;
770
return (0);
771
}
772
773
/*
774
* Attempt decryption.
775
*
776
* If we fail then don't return the key - return NULL
777
* and an error.
778
*/
779
if (cip->ic_decap(k, m, hdrlen)) {
780
/* success */
781
*key = k;
782
return (1);
783
}
784
785
/* Failure */
786
*key = NULL;
787
return (0);
788
#undef IEEE80211_WEP_MINLEN
789
#undef IEEE80211_WEP_HDRLEN
790
}
791
792
/**
793
* @brief Check and remove any post-defragmentation MIC from an MSDU.
794
*
795
* This is called after defragmentation. Crypto types that implement
796
* a MIC/ICV check per MSDU will not implement this function.
797
*
798
* As an example, TKIP decapsulation covers both MIC/ICV checks per
799
* MPDU (the "WEP" ICV) and then a Michael MIC verification on the
800
* defragmented MSDU. Please see 802.11-2020 12.5.2.1.3 (TKIP decapsulation)
801
* for more information.
802
*
803
* @param vap the current VAP
804
* @param k the current key
805
* @param m the mbuf representing the MSDU
806
* @param f set to 1 to force a MSDU MIC check, even if HW decrypted
807
* @returns 0 if error / MIC check failed, 1 if OK
808
*/
809
int
810
ieee80211_crypto_demic(struct ieee80211vap *vap, struct ieee80211_key *k,
811
struct mbuf *m, int force)
812
{
813
const struct ieee80211_cipher *cip;
814
const struct ieee80211_rx_stats *rxs;
815
struct ieee80211_frame *wh;
816
817
rxs = ieee80211_get_rx_params_ptr(m);
818
wh = mtod(m, struct ieee80211_frame *);
819
820
/*
821
* Handle demic / mic errors from hardware-decrypted offload devices.
822
*/
823
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED)) {
824
if ((rxs->c_pktflags & IEEE80211_RX_F_FAIL_MMIC) != 0) {
825
/*
826
* Hardware has said MMIC failed. We don't care about
827
* whether it was stripped or not.
828
*
829
* Eventually - teach the demic methods in crypto
830
* modules to handle a NULL key and not to dereference
831
* it.
832
*/
833
ieee80211_notify_michael_failure(vap, wh,
834
IEEE80211_KEYIX_NONE);
835
return (0);
836
}
837
838
if ((rxs->c_pktflags &
839
(IEEE80211_RX_F_MIC_STRIP|IEEE80211_RX_F_MMIC_STRIP)) != 0) {
840
/*
841
* Hardware has decrypted and not indicated a
842
* MIC failure and has stripped the MIC.
843
* We may not have a key, so for now just
844
* return OK.
845
*/
846
return (1);
847
}
848
}
849
850
/*
851
* If we don't have a key at this point then we don't
852
* have to demic anything.
853
*/
854
if (k == NULL)
855
return (1);
856
857
cip = k->wk_cipher;
858
return (cip->ic_miclen > 0 ? cip->ic_demic(k, m, force) : 1);
859
}
860
861
static void
862
load_ucastkey(void *arg, struct ieee80211_node *ni)
863
{
864
struct ieee80211vap *vap = ni->ni_vap;
865
struct ieee80211_key *k;
866
867
if (vap->iv_state != IEEE80211_S_RUN)
868
return;
869
k = &ni->ni_ucastkey;
870
if (k->wk_flags & IEEE80211_KEY_DEVKEY)
871
dev_key_set(vap, k);
872
}
873
874
/*
875
* Re-load all keys known to the 802.11 layer that may
876
* have hardware state backing them. This is used by
877
* drivers on resume to push keys down into the device.
878
*/
879
void
880
ieee80211_crypto_reload_keys(struct ieee80211com *ic)
881
{
882
struct ieee80211vap *vap;
883
int i;
884
885
/*
886
* Keys in the global key table of each vap.
887
*/
888
/* NB: used only during resume so don't lock for now */
889
TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
890
if (vap->iv_state != IEEE80211_S_RUN)
891
continue;
892
for (i = 0; i < IEEE80211_WEP_NKID; i++) {
893
const struct ieee80211_key *k = &vap->iv_nw_keys[i];
894
if (k->wk_flags & IEEE80211_KEY_DEVKEY)
895
dev_key_set(vap, k);
896
}
897
}
898
/*
899
* Unicast keys.
900
*/
901
ieee80211_iterate_nodes(&ic->ic_sta, load_ucastkey, NULL);
902
}
903
904
/*
905
* Set the default key index for WEP, or KEYIX_NONE for no default TX key.
906
*
907
* This should be done as part of a key update block (iv_key_update_begin /
908
* iv_key_update_end.)
909
*/
910
void
911
ieee80211_crypto_set_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
912
{
913
914
/* XXX TODO: assert we're in a key update block */
915
916
vap->iv_update_deftxkey(vap, kid);
917
}
918
919
/**
920
* @brief Calculate the AAD required for this frame for AES-GCM/AES-CCM.
921
*
922
* The contents are described in 802.11-2020 12.5.3.3.3 (Construct AAD)
923
* under AES-CCM and are shared with AES-GCM as covered in 12.5.5.3.3
924
* (Construct AAD) (AES-GCM).
925
*
926
* NOTE: the first two bytes are a 16 bit big-endian length, which are used
927
* by AES-CCM as part of the Adata field (RFC 3610, section 2.2
928
* (Authentication)) to indicate the length of the Adata field itself.
929
* Since this is small and fits in 0xfeff bytes, the length field
930
* uses the two byte big endian option.
931
*
932
* AES-GCM doesn't require the length at the beginning and will need to
933
* skip it.
934
*
935
* TODO: net80211 currently doesn't support negotiating SPP (Signaling
936
* and Payload Protected A-MSDUs) and thus bit 7 of the QoS control field
937
* is always masked.
938
*
939
* TODO: net80211 currently doesn't support DMG (802.11ad) so bit 7
940
* (A-MSDU present) and bit 8 (A-MSDU type) are always masked.
941
*
942
* @param wh 802.11 frame to calculate the AAD over
943
* @param aad AAD (additional authentication data) buffer
944
* @param len The AAD buffer length in bytes.
945
* @returns The number of AAD payload bytes (ignoring the first two
946
* bytes, which are the AAD payload length in big-endian).
947
*/
948
uint16_t
949
ieee80211_crypto_init_aad(const struct ieee80211_frame *wh, uint8_t *aad,
950
int len)
951
{
952
uint16_t aad_len;
953
954
memset(aad, 0, len);
955
956
/*
957
* AAD for PV0 MPDUs:
958
*
959
* FC with bits 4..6 and 11..13 masked to zero; 14 is always one
960
* A1 | A2 | A3
961
* SC with bits 4..15 (seq#) masked to zero
962
* A4 (if present)
963
* QC (if present)
964
*/
965
aad[0] = 0; /* AAD length >> 8 */
966
/* NB: aad[1] set below */
967
aad[2] = wh->i_fc[0] & 0x8f; /* see above for bitfields */
968
aad[3] = wh->i_fc[1] & 0xc7; /* see above for bitfields */
969
/* mask aad[3] b7 if frame is data frame w/ QoS control field */
970
if (IEEE80211_IS_QOS_ANY(wh))
971
aad[3] &= 0x7f;
972
973
/* NB: we know 3 addresses are contiguous */
974
memcpy(aad + 4, wh->i_addr1, 3 * IEEE80211_ADDR_LEN);
975
aad[22] = wh->i_seq[0] & IEEE80211_SEQ_FRAG_MASK;
976
aad[23] = 0; /* all bits masked */
977
/*
978
* Construct variable-length portion of AAD based
979
* on whether this is a 4-address frame/QOS frame.
980
* We always zero-pad to 32 bytes before running it
981
* through the cipher.
982
*/
983
if (IEEE80211_IS_DSTODS(wh)) {
984
IEEE80211_ADDR_COPY(aad + 24,
985
((const struct ieee80211_frame_addr4 *)wh)->i_addr4);
986
if (IEEE80211_IS_QOS_ANY(wh)) {
987
const struct ieee80211_qosframe_addr4 *qwh4 =
988
(const struct ieee80211_qosframe_addr4 *) wh;
989
/* TODO: SPP A-MSDU / A-MSDU present bit */
990
aad[30] = qwh4->i_qos[0] & 0x0f;/* just priority bits */
991
aad[31] = 0;
992
aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN + 2;
993
} else {
994
*(uint16_t *)&aad[30] = 0;
995
aad_len = aad[1] = 22 + IEEE80211_ADDR_LEN;
996
}
997
} else {
998
if (IEEE80211_IS_QOS_ANY(wh)) {
999
const struct ieee80211_qosframe *qwh =
1000
(const struct ieee80211_qosframe*) wh;
1001
/* TODO: SPP A-MSDU / A-MSDU present bit */
1002
aad[24] = qwh->i_qos[0] & 0x0f; /* just priority bits */
1003
aad[25] = 0;
1004
aad_len = aad[1] = 22 + 2;
1005
} else {
1006
*(uint16_t *)&aad[24] = 0;
1007
aad_len = aad[1] = 22;
1008
}
1009
*(uint16_t *)&aad[26] = 0;
1010
*(uint32_t *)&aad[28] = 0;
1011
}
1012
1013
return (aad_len);
1014
}
1015
1016