You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sec80211.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  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.
  8. *
  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.
  13. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <ipxe/ieee80211.h>
  24. #include <ipxe/net80211.h>
  25. #include <ipxe/sec80211.h>
  26. /** @file
  27. *
  28. * General secured-network routines required whenever any secure
  29. * network support at all is compiled in. This involves things like
  30. * installing keys, determining the type of security used by a probed
  31. * network, and some small helper functions that take advantage of
  32. * static data in this file.
  33. */
  34. /* Unsupported cryptosystem error numbers */
  35. #define ENOTSUP_WEP __einfo_error ( EINFO_ENOTSUP_WEP )
  36. #define EINFO_ENOTSUP_WEP __einfo_uniqify ( EINFO_ENOTSUP, \
  37. ( 0x10 | NET80211_CRYPT_WEP ), "WEP not supported" )
  38. #define ENOTSUP_TKIP __einfo_error ( EINFO_ENOTSUP_TKIP )
  39. #define EINFO_ENOTSUP_TKIP __einfo_uniqify ( EINFO_ENOTSUP, \
  40. ( 0x10 | NET80211_CRYPT_TKIP ), "TKIP not supported" )
  41. #define ENOTSUP_CCMP __einfo_error ( EINFO_ENOTSUP_CCMP )
  42. #define EINFO_ENOTSUP_CCMP __einfo_uniqify ( EINFO_ENOTSUP, \
  43. ( 0x10 | NET80211_CRYPT_CCMP ), "CCMP not supported" )
  44. #define ENOTSUP_CRYPT( crypt ) \
  45. EUNIQ ( EINFO_ENOTSUP, ( 0x10 | (crypt) ), \
  46. ENOTSUP_WEP, ENOTSUP_TKIP, ENOTSUP_CCMP )
  47. /** Mapping from net80211 crypto/secprot types to RSN OUI descriptors */
  48. struct descriptor_map {
  49. /** Value of net80211_crypto_alg or net80211_security_proto */
  50. u32 net80211_type;
  51. /** OUI+type in appropriate byte order, masked to exclude vendor */
  52. u32 oui_type;
  53. };
  54. /** Magic number in @a oui_type showing end of list */
  55. #define END_MAGIC 0xFFFFFFFF
  56. /** Mapping between net80211 cryptosystems and 802.11i cipher IDs */
  57. static struct descriptor_map rsn_cipher_map[] = {
  58. { .net80211_type = NET80211_CRYPT_WEP,
  59. .oui_type = IEEE80211_RSN_CTYPE_WEP40 },
  60. { .net80211_type = NET80211_CRYPT_WEP,
  61. .oui_type = IEEE80211_RSN_CTYPE_WEP104 },
  62. { .net80211_type = NET80211_CRYPT_TKIP,
  63. .oui_type = IEEE80211_RSN_CTYPE_TKIP },
  64. { .net80211_type = NET80211_CRYPT_CCMP,
  65. .oui_type = IEEE80211_RSN_CTYPE_CCMP },
  66. { .net80211_type = NET80211_CRYPT_UNKNOWN,
  67. .oui_type = END_MAGIC },
  68. };
  69. /** Mapping between net80211 handshakers and 802.11i AKM IDs */
  70. static struct descriptor_map rsn_akm_map[] = {
  71. { .net80211_type = NET80211_SECPROT_EAP,
  72. .oui_type = IEEE80211_RSN_ATYPE_8021X },
  73. { .net80211_type = NET80211_SECPROT_PSK,
  74. .oui_type = IEEE80211_RSN_ATYPE_PSK },
  75. { .net80211_type = NET80211_SECPROT_UNKNOWN,
  76. .oui_type = END_MAGIC },
  77. };
  78. /**
  79. * Install 802.11 cryptosystem
  80. *
  81. * @v which Pointer to the cryptosystem structure to install in
  82. * @v crypt Cryptosystem ID number
  83. * @v key Encryption key to use
  84. * @v len Length of encryption key
  85. * @v rsc Initial receive sequence counter, if applicable
  86. * @ret rc Return status code
  87. *
  88. * The encryption key will not be accessed via the provided pointer
  89. * after this function returns, so you may keep it on the stack.
  90. *
  91. * @a which must point to either @c dev->crypto (for the normal case
  92. * of installing a unicast cryptosystem) or @c dev->gcrypto (to
  93. * install a cryptosystem that will be used only for decrypting
  94. * group-source frames).
  95. */
  96. int sec80211_install ( struct net80211_crypto **which,
  97. enum net80211_crypto_alg crypt,
  98. const void *key, int len, const void *rsc )
  99. {
  100. struct net80211_crypto *crypto = *which;
  101. struct net80211_crypto *tbl_crypto;
  102. /* Remove old crypto if it exists */
  103. free ( *which );
  104. *which = NULL;
  105. if ( crypt == NET80211_CRYPT_NONE ) {
  106. DBG ( "802.11-Sec not installing null cryptography\n" );
  107. return 0;
  108. }
  109. /* Find cryptosystem to use */
  110. for_each_table_entry ( tbl_crypto, NET80211_CRYPTOS ) {
  111. if ( tbl_crypto->algorithm == crypt ) {
  112. crypto = zalloc ( sizeof ( *crypto ) +
  113. tbl_crypto->priv_len );
  114. if ( ! crypto ) {
  115. DBG ( "802.11-Sec out of memory\n" );
  116. return -ENOMEM;
  117. }
  118. memcpy ( crypto, tbl_crypto, sizeof ( *crypto ) );
  119. crypto->priv = ( ( void * ) crypto +
  120. sizeof ( *crypto ) );
  121. break;
  122. }
  123. }
  124. if ( ! crypto ) {
  125. DBG ( "802.11-Sec no support for cryptosystem %d\n", crypt );
  126. return -ENOTSUP_CRYPT ( crypt );
  127. }
  128. *which = crypto;
  129. DBG ( "802.11-Sec installing cryptosystem %d as %p with key of "
  130. "length %d\n", crypt, crypto, len );
  131. return crypto->init ( crypto, key, len, rsc );
  132. }
  133. /**
  134. * Determine net80211 crypto or handshaking type value to return for RSN info
  135. *
  136. * @v rsnp Pointer to next descriptor count field in RSN IE
  137. * @v rsn_end Pointer to end of RSN IE
  138. * @v map Descriptor map to use
  139. * @v tbl_start Start of linker table to examine for iPXE support
  140. * @v tbl_end End of linker table to examine for iPXE support
  141. * @ret rsnp Updated to point to first byte after descriptors
  142. * @ret map_ent Descriptor map entry of translation to use
  143. *
  144. * The entries in the linker table must be either net80211_crypto or
  145. * net80211_handshaker structures, and @a tbl_stride must be set to
  146. * sizeof() the appropriate one.
  147. *
  148. * This function expects @a rsnp to point at a two-byte descriptor
  149. * count followed by a list of four-byte cipher or AKM descriptors; it
  150. * will return @c NULL if the input packet is malformed, and otherwise
  151. * set @a rsnp to the first byte it has not looked at. It will return
  152. * the first cipher in the list that is supported by the current build
  153. * of iPXE, or the first of all if none are supported.
  154. *
  155. * We play rather fast and loose with type checking, because this
  156. * function is only called from two well-defined places in the
  157. * RSN-checking code. Don't try to use it for anything else.
  158. */
  159. static struct descriptor_map * rsn_pick_desc ( u8 **rsnp, u8 *rsn_end,
  160. struct descriptor_map *map,
  161. void *tbl_start, void *tbl_end )
  162. {
  163. int ndesc;
  164. int ok = 0;
  165. struct descriptor_map *map_ent, *map_ret = NULL;
  166. u8 *rsn = *rsnp;
  167. void *tblp;
  168. size_t tbl_stride = ( map == rsn_cipher_map ?
  169. sizeof ( struct net80211_crypto ) :
  170. sizeof ( struct net80211_handshaker ) );
  171. if ( map != rsn_cipher_map && map != rsn_akm_map )
  172. return NULL;
  173. /* Determine which types we support */
  174. for ( tblp = tbl_start; tblp < tbl_end; tblp += tbl_stride ) {
  175. struct net80211_crypto *crypto = tblp;
  176. struct net80211_handshaker *hs = tblp;
  177. if ( map == rsn_cipher_map )
  178. ok |= ( 1 << crypto->algorithm );
  179. else
  180. ok |= ( 1 << hs->protocol );
  181. }
  182. /* RSN sanity checks */
  183. if ( rsn + 2 > rsn_end ) {
  184. DBG ( "RSN detect: malformed descriptor count\n" );
  185. return NULL;
  186. }
  187. ndesc = *( u16 * ) rsn;
  188. rsn += 2;
  189. if ( ! ndesc ) {
  190. DBG ( "RSN detect: no descriptors\n" );
  191. return NULL;
  192. }
  193. /* Determine which net80211 crypto types are listed */
  194. while ( ndesc-- ) {
  195. u32 desc;
  196. if ( rsn + 4 > rsn_end ) {
  197. DBG ( "RSN detect: malformed descriptor (%d left)\n",
  198. ndesc );
  199. return NULL;
  200. }
  201. desc = *( u32 * ) rsn;
  202. rsn += 4;
  203. for ( map_ent = map; map_ent->oui_type != END_MAGIC; map_ent++ )
  204. if ( map_ent->oui_type == ( desc & OUI_TYPE_MASK ) )
  205. break;
  206. /* Use first cipher as a fallback */
  207. if ( ! map_ret )
  208. map_ret = map_ent;
  209. /* Once we find one we support, use it */
  210. if ( ok & ( 1 << map_ent->net80211_type ) ) {
  211. map_ret = map_ent;
  212. break;
  213. }
  214. }
  215. if ( ndesc > 0 )
  216. rsn += 4 * ndesc;
  217. *rsnp = rsn;
  218. return map_ret;
  219. }
  220. /**
  221. * Find the RSN or WPA information element in the provided beacon frame
  222. *
  223. * @v ie Pointer to first information element to check
  224. * @v ie_end Pointer to end of information element space
  225. * @ret is_rsn TRUE if returned IE is RSN, FALSE if it's WPA
  226. * @ret end Pointer to byte immediately after last byte of data
  227. * @ret data Pointer to first byte of data (the `version' field)
  228. *
  229. * If both an RSN and a WPA information element are found, this
  230. * function will return the first one seen, which by ordering rules
  231. * should always prefer the newer RSN IE.
  232. *
  233. * If no RSN or WPA infomration element is found, returns @c NULL and
  234. * leaves @a is_rsn and @a end in an undefined state.
  235. *
  236. * This function will not return a pointer to an information element
  237. * that states it extends past the tail of the io_buffer, or whose @a
  238. * version field is incorrect.
  239. */
  240. u8 * sec80211_find_rsn ( union ieee80211_ie *ie, void *ie_end,
  241. int *is_rsn, u8 **end )
  242. {
  243. u8 *rsn = NULL;
  244. if ( ! ieee80211_ie_bound ( ie, ie_end ) )
  245. return NULL;
  246. while ( ie ) {
  247. if ( ie->id == IEEE80211_IE_VENDOR &&
  248. ie->vendor.oui == IEEE80211_WPA_OUI_VEN ) {
  249. DBG ( "RSN detect: old-style WPA IE found\n" );
  250. rsn = &ie->vendor.data[0];
  251. *end = rsn + ie->len - 4;
  252. *is_rsn = 0;
  253. } else if ( ie->id == IEEE80211_IE_RSN ) {
  254. DBG ( "RSN detect: 802.11i RSN IE found\n" );
  255. rsn = ( u8 * ) &ie->rsn.version;
  256. *end = rsn + ie->len;
  257. *is_rsn = 1;
  258. }
  259. if ( rsn && ( *end > ( u8 * ) ie_end || rsn >= *end ||
  260. *( u16 * ) rsn != IEEE80211_RSN_VERSION ) ) {
  261. DBG ( "RSN detect: malformed RSN IE or unknown "
  262. "version, keep trying\n" );
  263. rsn = NULL;
  264. }
  265. if ( rsn )
  266. break;
  267. ie = ieee80211_next_ie ( ie, ie_end );
  268. }
  269. if ( ! ie ) {
  270. DBG ( "RSN detect: no RSN IE found\n" );
  271. return NULL;
  272. }
  273. return rsn;
  274. }
  275. /**
  276. * Detect crypto and AKM types from RSN information element
  277. *
  278. * @v is_rsn If TRUE, IE is a new-style RSN information element
  279. * @v start Pointer to first byte of @a version field
  280. * @v end Pointer to first byte not in the RSN IE
  281. * @ret secprot Security handshaking protocol used by network
  282. * @ret crypt Cryptosystem used by network
  283. * @ret rc Return status code
  284. *
  285. * If the IE cannot be parsed, returns an error indication and leaves
  286. * @a secprot and @a crypt unchanged.
  287. */
  288. int sec80211_detect_ie ( int is_rsn, u8 *start, u8 *end,
  289. enum net80211_security_proto *secprot,
  290. enum net80211_crypto_alg *crypt )
  291. {
  292. enum net80211_security_proto sp;
  293. enum net80211_crypto_alg cr;
  294. struct descriptor_map *map;
  295. u8 *rsn = start;
  296. /* Set some defaults */
  297. cr = ( is_rsn ? NET80211_CRYPT_CCMP : NET80211_CRYPT_TKIP );
  298. sp = NET80211_SECPROT_EAP;
  299. rsn += 2; /* version - already checked */
  300. rsn += 4; /* group cipher - we don't use it here */
  301. if ( rsn >= end )
  302. goto done;
  303. /* Pick crypto algorithm */
  304. map = rsn_pick_desc ( &rsn, end, rsn_cipher_map,
  305. table_start ( NET80211_CRYPTOS ),
  306. table_end ( NET80211_CRYPTOS ) );
  307. if ( ! map )
  308. goto invalid_rsn;
  309. cr = map->net80211_type;
  310. if ( rsn >= end )
  311. goto done;
  312. /* Pick handshaking algorithm */
  313. map = rsn_pick_desc ( &rsn, end, rsn_akm_map,
  314. table_start ( NET80211_HANDSHAKERS ),
  315. table_end ( NET80211_HANDSHAKERS ) );
  316. if ( ! map )
  317. goto invalid_rsn;
  318. sp = map->net80211_type;
  319. done:
  320. DBG ( "RSN detect: OK, crypto type %d, secprot type %d\n", cr, sp );
  321. *secprot = sp;
  322. *crypt = cr;
  323. return 0;
  324. invalid_rsn:
  325. DBG ( "RSN detect: invalid RSN IE\n" );
  326. return -EINVAL;
  327. }
  328. /**
  329. * Detect the cryptosystem and handshaking protocol used by an 802.11 network
  330. *
  331. * @v iob I/O buffer containing beacon frame
  332. * @ret secprot Security handshaking protocol used by network
  333. * @ret crypt Cryptosystem used by network
  334. * @ret rc Return status code
  335. *
  336. * This function uses weak linkage, as it must be called from generic
  337. * contexts but should only be linked in if some encryption is
  338. * supported; you must test its address against @c NULL before calling
  339. * it. If it does not exist, any network with the PRIVACY bit set in
  340. * beacon->capab should be considered unknown.
  341. */
  342. int sec80211_detect ( struct io_buffer *iob,
  343. enum net80211_security_proto *secprot,
  344. enum net80211_crypto_alg *crypt )
  345. {
  346. struct ieee80211_frame *hdr = iob->data;
  347. struct ieee80211_beacon *beacon =
  348. ( struct ieee80211_beacon * ) hdr->data;
  349. u8 *rsn, *rsn_end;
  350. int is_rsn, rc;
  351. *crypt = NET80211_CRYPT_UNKNOWN;
  352. *secprot = NET80211_SECPROT_UNKNOWN;
  353. /* Find RSN or WPA IE */
  354. if ( ! ( rsn = sec80211_find_rsn ( beacon->info_element, iob->tail,
  355. &is_rsn, &rsn_end ) ) ) {
  356. /* No security IE at all; either WEP or no security. */
  357. *secprot = NET80211_SECPROT_NONE;
  358. if ( beacon->capability & IEEE80211_CAPAB_PRIVACY )
  359. *crypt = NET80211_CRYPT_WEP;
  360. else
  361. *crypt = NET80211_CRYPT_NONE;
  362. return 0;
  363. }
  364. /* Determine type of security */
  365. if ( ( rc = sec80211_detect_ie ( is_rsn, rsn, rsn_end, secprot,
  366. crypt ) ) == 0 )
  367. return 0;
  368. /* If we get here, the RSN IE was invalid */
  369. *crypt = NET80211_CRYPT_UNKNOWN;
  370. *secprot = NET80211_SECPROT_UNKNOWN;
  371. DBG ( "Failed to handle RSN IE:\n" );
  372. DBG_HD ( rsn, rsn_end - rsn );
  373. return rc;
  374. }
  375. /**
  376. * Determine RSN descriptor for specified net80211 ID
  377. *
  378. * @v id net80211 ID value
  379. * @v rsnie Whether to return a new-format (RSN IE) descriptor
  380. * @v map Map to use in translation
  381. * @ret desc RSN descriptor, or 0 on error
  382. *
  383. * If @a rsnie is false, returns an old-format (WPA vendor IE)
  384. * descriptor.
  385. */
  386. static u32 rsn_get_desc ( unsigned id, int rsnie, struct descriptor_map *map )
  387. {
  388. u32 vendor = ( rsnie ? IEEE80211_RSN_OUI : IEEE80211_WPA_OUI );
  389. for ( ; map->oui_type != END_MAGIC; map++ ) {
  390. if ( map->net80211_type == id )
  391. return map->oui_type | vendor;
  392. }
  393. return 0;
  394. }
  395. /**
  396. * Determine RSN descriptor for specified net80211 cryptosystem number
  397. *
  398. * @v crypt Cryptosystem number
  399. * @v rsnie Whether to return a new-format (RSN IE) descriptor
  400. * @ret desc RSN descriptor
  401. *
  402. * If @a rsnie is false, returns an old-format (WPA vendor IE)
  403. * descriptor.
  404. */
  405. u32 sec80211_rsn_get_crypto_desc ( enum net80211_crypto_alg crypt, int rsnie )
  406. {
  407. return rsn_get_desc ( crypt, rsnie, rsn_cipher_map );
  408. }
  409. /**
  410. * Determine RSN descriptor for specified net80211 handshaker number
  411. *
  412. * @v secprot Handshaker number
  413. * @v rsnie Whether to return a new-format (RSN IE) descriptor
  414. * @ret desc RSN descriptor
  415. *
  416. * If @a rsnie is false, returns an old-format (WPA vendor IE)
  417. * descriptor.
  418. */
  419. u32 sec80211_rsn_get_akm_desc ( enum net80211_security_proto secprot,
  420. int rsnie )
  421. {
  422. return rsn_get_desc ( secprot, rsnie, rsn_akm_map );
  423. }
  424. /**
  425. * Determine net80211 cryptosystem number from RSN descriptor
  426. *
  427. * @v desc RSN descriptor
  428. * @ret crypt net80211 cryptosystem enumeration value
  429. */
  430. enum net80211_crypto_alg sec80211_rsn_get_net80211_crypt ( u32 desc )
  431. {
  432. struct descriptor_map *map = rsn_cipher_map;
  433. for ( ; map->oui_type != END_MAGIC; map++ ) {
  434. if ( map->oui_type == ( desc & OUI_TYPE_MASK ) )
  435. break;
  436. }
  437. return map->net80211_type;
  438. }