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 14KB

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