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

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