2 * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
24 #include <gpxe/ieee80211.h>
25 #include <gpxe/net80211.h>
26 #include <gpxe/sec80211.h>
30 * General secured-network routines required whenever any secure
31 * network support at all is compiled in. This involves things like
32 * installing keys, determining the type of security used by a probed
33 * network, and some small helper functions that take advantage of
34 * static data in this file.
37 /** Mapping from net80211 crypto/secprot types to RSN OUI descriptors */
38 struct descriptor_map
{
39 /** Value of net80211_crypto_alg or net80211_security_proto */
42 /** OUI+type in appropriate byte order, masked to exclude vendor */
46 /** Magic number in @a oui_type showing end of list */
47 #define END_MAGIC 0xFFFFFFFF
49 /** Mapping between net80211 cryptosystems and 802.11i cipher IDs */
50 static struct descriptor_map rsn_cipher_map
[] = {
51 { .net80211_type
= NET80211_CRYPT_WEP
,
52 .oui_type
= IEEE80211_RSN_CTYPE_WEP40
},
54 { .net80211_type
= NET80211_CRYPT_WEP
,
55 .oui_type
= IEEE80211_RSN_CTYPE_WEP104
},
57 { .net80211_type
= NET80211_CRYPT_TKIP
,
58 .oui_type
= IEEE80211_RSN_CTYPE_TKIP
},
60 { .net80211_type
= NET80211_CRYPT_CCMP
,
61 .oui_type
= IEEE80211_RSN_CTYPE_CCMP
},
63 { .net80211_type
= NET80211_CRYPT_UNKNOWN
,
64 .oui_type
= END_MAGIC
},
67 /** Mapping between net80211 handshakers and 802.11i AKM IDs */
68 static struct descriptor_map rsn_akm_map
[] = {
69 { .net80211_type
= NET80211_SECPROT_EAP
,
70 .oui_type
= IEEE80211_RSN_ATYPE_8021X
},
72 { .net80211_type
= NET80211_SECPROT_PSK
,
73 .oui_type
= IEEE80211_RSN_ATYPE_PSK
},
75 { .net80211_type
= NET80211_SECPROT_UNKNOWN
,
76 .oui_type
= END_MAGIC
},
81 * Install 802.11 cryptosystem
83 * @v which Pointer to the cryptosystem structure to install in
84 * @v crypt Cryptosystem ID number
85 * @v key Encryption key to use
86 * @v len Length of encryption key
87 * @v rsc Initial receive sequence counter, if applicable
88 * @ret rc Return status code
90 * The encryption key will not be accessed via the provided pointer
91 * after this function returns, so you may keep it on the stack.
93 * @a which must point to either @c dev->crypto (for the normal case
94 * of installing a unicast cryptosystem) or @c dev->gcrypto (to
95 * install a cryptosystem that will be used only for decrypting
96 * group-source frames).
98 int sec80211_install ( struct net80211_crypto
**which
,
99 enum net80211_crypto_alg crypt
,
100 const void *key
, int len
, const void *rsc
)
102 struct net80211_crypto
*crypto
= *which
;
103 struct net80211_crypto
*tbl_crypto
;
105 /* Remove old crypto if it exists */
109 if ( crypt
== NET80211_CRYPT_NONE
) {
110 DBG ( "802.11-Sec not installing null cryptography\n" );
114 /* Find cryptosystem to use */
115 for_each_table_entry ( tbl_crypto
, NET80211_CRYPTOS
) {
116 if ( tbl_crypto
->algorithm
== crypt
) {
117 crypto
= zalloc ( sizeof ( *crypto
) +
118 tbl_crypto
->priv_len
);
120 DBG ( "802.11-Sec out of memory\n" );
124 memcpy ( crypto
, tbl_crypto
, sizeof ( *crypto
) );
125 crypto
->priv
= ( ( void * ) crypto
+
126 sizeof ( *crypto
) );
132 DBG ( "802.11-Sec no support for cryptosystem %d\n", crypt
);
133 return -( ENOTSUP
| EUNIQ_10
| ( crypt
<< 8 ) );
138 DBG ( "802.11-Sec installing cryptosystem %d as %p with key of "
139 "length %d\n", crypt
, crypto
, len
);
141 return crypto
->init ( crypto
, key
, len
, rsc
);
146 * Determine net80211 crypto or handshaking type value to return for RSN info
148 * @v rsnp Pointer to next descriptor count field in RSN IE
149 * @v rsn_end Pointer to end of RSN IE
150 * @v map Descriptor map to use
151 * @v tbl_start Start of linker table to examine for gPXE support
152 * @v tbl_end End of linker table to examine for gPXE support
153 * @ret rsnp Updated to point to first byte after descriptors
154 * @ret map_ent Descriptor map entry of translation to use
156 * The entries in the linker table must be either net80211_crypto or
157 * net80211_handshaker structures, and @a tbl_stride must be set to
158 * sizeof() the appropriate one.
160 * This function expects @a rsnp to point at a two-byte descriptor
161 * count followed by a list of four-byte cipher or AKM descriptors; it
162 * will return @c NULL if the input packet is malformed, and otherwise
163 * set @a rsnp to the first byte it has not looked at. It will return
164 * the first cipher in the list that is supported by the current build
165 * of gPXE, or the first of all if none are supported.
167 * We play rather fast and loose with type checking, because this
168 * function is only called from two well-defined places in the
169 * RSN-checking code. Don't try to use it for anything else.
171 static struct descriptor_map
* rsn_pick_desc ( u8
**rsnp
, u8
*rsn_end
,
172 struct descriptor_map
*map
,
173 void *tbl_start
, void *tbl_end
)
177 struct descriptor_map
*map_ent
, *map_ret
= NULL
;
180 size_t tbl_stride
= ( map
== rsn_cipher_map
?
181 sizeof ( struct net80211_crypto
) :
182 sizeof ( struct net80211_handshaker
) );
184 if ( map
!= rsn_cipher_map
&& map
!= rsn_akm_map
)
187 /* Determine which types we support */
188 for ( tblp
= tbl_start
; tblp
< tbl_end
; tblp
+= tbl_stride
) {
189 struct net80211_crypto
*crypto
= tblp
;
190 struct net80211_handshaker
*hs
= tblp
;
192 if ( map
== rsn_cipher_map
)
193 ok
|= ( 1 << crypto
->algorithm
);
195 ok
|= ( 1 << hs
->protocol
);
198 /* RSN sanity checks */
199 if ( rsn
+ 2 > rsn_end
) {
200 DBG ( "RSN detect: malformed descriptor count\n" );
204 ndesc
= *( u16
* ) rsn
;
208 DBG ( "RSN detect: no descriptors\n" );
212 /* Determine which net80211 crypto types are listed */
216 if ( rsn
+ 4 > rsn_end
) {
217 DBG ( "RSN detect: malformed descriptor (%d left)\n",
222 desc
= *( u32
* ) rsn
;
225 for ( map_ent
= map
; map_ent
->oui_type
!= END_MAGIC
; map_ent
++ )
226 if ( map_ent
->oui_type
== ( desc
& OUI_TYPE_MASK
) )
229 /* Use first cipher as a fallback */
233 /* Once we find one we support, use it */
234 if ( ok
& ( 1 << map_ent
->net80211_type
) ) {
249 * Find the RSN or WPA information element in the provided beacon frame
251 * @v ie Pointer to first information element to check
252 * @v ie_end Pointer to end of information element space
253 * @ret is_rsn TRUE if returned IE is RSN, FALSE if it's WPA
254 * @ret end Pointer to byte immediately after last byte of data
255 * @ret data Pointer to first byte of data (the `version' field)
257 * If both an RSN and a WPA information element are found, this
258 * function will return the first one seen, which by ordering rules
259 * should always prefer the newer RSN IE.
261 * If no RSN or WPA infomration element is found, returns @c NULL and
262 * leaves @a is_rsn and @a end in an undefined state.
264 * This function will not return a pointer to an information element
265 * that states it extends past the tail of the io_buffer, or whose @a
266 * version field is incorrect.
268 u8
* sec80211_find_rsn ( union ieee80211_ie
*ie
, void *ie_end
,
269 int *is_rsn
, u8
**end
)
273 if ( ! ieee80211_ie_bound ( ie
, ie_end
) )
277 if ( ie
->id
== IEEE80211_IE_VENDOR
&&
278 ie
->vendor
.oui
== IEEE80211_WPA_OUI_VEN
) {
279 DBG ( "RSN detect: old-style WPA IE found\n" );
280 rsn
= &ie
->vendor
.data
[0];
281 *end
= rsn
+ ie
->len
- 4;
283 } else if ( ie
->id
== IEEE80211_IE_RSN
) {
284 DBG ( "RSN detect: 802.11i RSN IE found\n" );
285 rsn
= ( u8
* ) &ie
->rsn
.version
;
286 *end
= rsn
+ ie
->len
;
290 if ( rsn
&& ( *end
> ( u8
* ) ie_end
|| rsn
>= *end
||
291 *( u16
* ) rsn
!= IEEE80211_RSN_VERSION
) ) {
292 DBG ( "RSN detect: malformed RSN IE or unknown "
293 "version, keep trying\n" );
300 ie
= ieee80211_next_ie ( ie
, ie_end
);
304 DBG ( "RSN detect: no RSN IE found\n" );
313 * Detect crypto and AKM types from RSN information element
315 * @v is_rsn If TRUE, IE is a new-style RSN information element
316 * @v start Pointer to first byte of @a version field
317 * @v end Pointer to first byte not in the RSN IE
318 * @ret secprot Security handshaking protocol used by network
319 * @ret crypt Cryptosystem used by network
320 * @ret rc Return status code
322 * If the IE cannot be parsed, returns an error indication and leaves
323 * @a secprot and @a crypt unchanged.
325 int sec80211_detect_ie ( int is_rsn
, u8
*start
, u8
*end
,
326 enum net80211_security_proto
*secprot
,
327 enum net80211_crypto_alg
*crypt
)
329 enum net80211_security_proto sp
;
330 enum net80211_crypto_alg cr
;
331 struct descriptor_map
*map
;
334 /* Set some defaults */
335 cr
= ( is_rsn
? NET80211_CRYPT_CCMP
: NET80211_CRYPT_TKIP
);
336 sp
= NET80211_SECPROT_EAP
;
338 rsn
+= 2; /* version - already checked */
339 rsn
+= 4; /* group cipher - we don't use it here */
344 /* Pick crypto algorithm */
345 map
= rsn_pick_desc ( &rsn
, end
, rsn_cipher_map
,
346 table_start ( NET80211_CRYPTOS
),
347 table_end ( NET80211_CRYPTOS
) );
351 cr
= map
->net80211_type
;
356 /* Pick handshaking algorithm */
357 map
= rsn_pick_desc ( &rsn
, end
, rsn_akm_map
,
358 table_start ( NET80211_HANDSHAKERS
),
359 table_end ( NET80211_HANDSHAKERS
) );
363 sp
= map
->net80211_type
;
366 DBG ( "RSN detect: OK, crypto type %d, secprot type %d\n", cr
, sp
);
372 DBG ( "RSN detect: invalid RSN IE\n" );
378 * Detect the cryptosystem and handshaking protocol used by an 802.11 network
380 * @v iob I/O buffer containing beacon frame
381 * @ret secprot Security handshaking protocol used by network
382 * @ret crypt Cryptosystem used by network
383 * @ret rc Return status code
385 * This function uses weak linkage, as it must be called from generic
386 * contexts but should only be linked in if some encryption is
387 * supported; you must test its address against @c NULL before calling
388 * it. If it does not exist, any network with the PRIVACY bit set in
389 * beacon->capab should be considered unknown.
391 int _sec80211_detect ( struct io_buffer
*iob
,
392 enum net80211_security_proto
*secprot
,
393 enum net80211_crypto_alg
*crypt
)
395 struct ieee80211_frame
*hdr
= iob
->data
;
396 struct ieee80211_beacon
*beacon
=
397 ( struct ieee80211_beacon
* ) hdr
->data
;
401 *crypt
= NET80211_CRYPT_UNKNOWN
;
402 *secprot
= NET80211_SECPROT_UNKNOWN
;
404 /* Find RSN or WPA IE */
405 if ( ! ( rsn
= sec80211_find_rsn ( beacon
->info_element
, iob
->tail
,
406 &is_rsn
, &rsn_end
) ) ) {
407 /* No security IE at all; either WEP or no security. */
408 *secprot
= NET80211_SECPROT_NONE
;
410 if ( beacon
->capability
& IEEE80211_CAPAB_PRIVACY
)
411 *crypt
= NET80211_CRYPT_WEP
;
413 *crypt
= NET80211_CRYPT_NONE
;
418 /* Determine type of security */
419 if ( ( rc
= sec80211_detect_ie ( is_rsn
, rsn
, rsn_end
, secprot
,
423 /* If we get here, the RSN IE was invalid */
425 *crypt
= NET80211_CRYPT_UNKNOWN
;
426 *secprot
= NET80211_SECPROT_UNKNOWN
;
427 DBG ( "Failed to handle RSN IE:\n" );
428 DBG_HD ( rsn
, rsn_end
- rsn
);
434 * Determine RSN descriptor for specified net80211 ID
436 * @v id net80211 ID value
437 * @v rsnie Whether to return a new-format (RSN IE) descriptor
438 * @v map Map to use in translation
439 * @ret desc RSN descriptor, or 0 on error
441 * If @a rsnie is false, returns an old-format (WPA vendor IE)
444 static u32
rsn_get_desc ( unsigned id
, int rsnie
, struct descriptor_map
*map
)
446 u32 vendor
= ( rsnie
? IEEE80211_RSN_OUI
: IEEE80211_WPA_OUI
);
448 for ( ; map
->oui_type
!= END_MAGIC
; map
++ ) {
449 if ( map
->net80211_type
== id
)
450 return map
->oui_type
| vendor
;
457 * Determine RSN descriptor for specified net80211 cryptosystem number
459 * @v crypt Cryptosystem number
460 * @v rsnie Whether to return a new-format (RSN IE) descriptor
461 * @ret desc RSN descriptor
463 * If @a rsnie is false, returns an old-format (WPA vendor IE)
466 u32
sec80211_rsn_get_crypto_desc ( enum net80211_crypto_alg crypt
, int rsnie
)
468 return rsn_get_desc ( crypt
, rsnie
, rsn_cipher_map
);
472 * Determine RSN descriptor for specified net80211 handshaker number
474 * @v secprot Handshaker number
475 * @v rsnie Whether to return a new-format (RSN IE) descriptor
476 * @ret desc RSN descriptor
478 * If @a rsnie is false, returns an old-format (WPA vendor IE)
481 u32
sec80211_rsn_get_akm_desc ( enum net80211_security_proto secprot
,
484 return rsn_get_desc ( secprot
, rsnie
, rsn_akm_map
);
488 * Determine net80211 cryptosystem number from RSN descriptor
490 * @v desc RSN descriptor
491 * @ret crypt net80211 cryptosystem enumeration value
493 enum net80211_crypto_alg
sec80211_rsn_get_net80211_crypt ( u32 desc
)
495 struct descriptor_map
*map
= rsn_cipher_map
;
497 for ( ; map
->oui_type
!= END_MAGIC
; map
++ ) {
498 if ( map
->oui_type
== ( desc
& OUI_TYPE_MASK
) )
502 return map
->net80211_type
;