ibss_rsn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * wpa_supplicant - IBSS RSN
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "l2_packet/l2_packet.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "wpa.h"
  19. #include "wpa_ie.h"
  20. #include "../hostapd/wpa.h"
  21. #include "ibss_rsn.h"
  22. static void ibss_rsn_free(struct ibss_rsn_peer *peer)
  23. {
  24. wpa_auth_sta_deinit(peer->auth);
  25. wpa_sm_deinit(peer->supp);
  26. os_free(peer);
  27. }
  28. static void supp_set_state(void *ctx, wpa_states state)
  29. {
  30. struct ibss_rsn_peer *peer = ctx;
  31. peer->supp_state = state;
  32. }
  33. static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
  34. size_t len)
  35. {
  36. struct ibss_rsn_peer *peer = ctx;
  37. struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
  38. wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
  39. "len=%lu)",
  40. __func__, MAC2STR(dest), proto, (unsigned long) len);
  41. if (wpa_s->l2)
  42. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  43. return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
  44. }
  45. static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
  46. u16 data_len, size_t *msg_len, void **data_pos)
  47. {
  48. struct ieee802_1x_hdr *hdr;
  49. wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
  50. __func__, type, data_len);
  51. *msg_len = sizeof(*hdr) + data_len;
  52. hdr = os_malloc(*msg_len);
  53. if (hdr == NULL)
  54. return NULL;
  55. hdr->version = 2;
  56. hdr->type = type;
  57. hdr->length = host_to_be16(data_len);
  58. if (data)
  59. os_memcpy(hdr + 1, data, data_len);
  60. else
  61. os_memset(hdr + 1, 0, data_len);
  62. if (data_pos)
  63. *data_pos = hdr + 1;
  64. return (u8 *) hdr;
  65. }
  66. static int supp_get_beacon_ie(void *ctx)
  67. {
  68. struct ibss_rsn_peer *peer = ctx;
  69. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  70. /* TODO: get correct RSN IE */
  71. return wpa_sm_set_ap_rsn_ie(peer->supp,
  72. (u8 *) "\x30\x14\x01\x00"
  73. "\x00\x0f\xac\x04"
  74. "\x01\x00\x00\x0f\xac\x04"
  75. "\x01\x00\x00\x0f\xac\x02"
  76. "\x00\x00", 22);
  77. }
  78. static int supp_set_key(void *ctx, wpa_alg alg,
  79. const u8 *addr, int key_idx, int set_tx,
  80. const u8 *seq, size_t seq_len,
  81. const u8 *key, size_t key_len)
  82. {
  83. struct ibss_rsn_peer *peer = ctx;
  84. if (key_idx == 0) {
  85. /*
  86. * In IBSS RSN, the pairwise key from the 4-way handshake
  87. * initiated by the peer with highest MAC address is used.
  88. */
  89. if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
  90. ETH_ALEN) > 0)
  91. return 0;
  92. }
  93. wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
  94. "set_tx=%d)",
  95. __func__, alg, MAC2STR(addr), key_idx, set_tx);
  96. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
  97. wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
  98. return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
  99. set_tx, seq, seq_len, key, key_len);
  100. }
  101. static void * supp_get_network_ctx(void *ctx)
  102. {
  103. struct ibss_rsn_peer *peer = ctx;
  104. return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
  105. }
  106. static int supp_mlme_setprotection(void *ctx, const u8 *addr,
  107. int protection_type, int key_type)
  108. {
  109. wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
  110. "key_type=%d)",
  111. __func__, MAC2STR(addr), protection_type, key_type);
  112. return 0;
  113. }
  114. static void supp_cancel_auth_timeout(void *ctx)
  115. {
  116. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  117. }
  118. int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
  119. const u8 *psk)
  120. {
  121. struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
  122. if (ctx == NULL)
  123. return -1;
  124. ctx->ctx = peer;
  125. ctx->msg_ctx = peer->ibss_rsn->wpa_s;
  126. ctx->set_state = supp_set_state;
  127. ctx->ether_send = supp_ether_send;
  128. ctx->get_beacon_ie = supp_get_beacon_ie;
  129. ctx->alloc_eapol = supp_alloc_eapol;
  130. ctx->set_key = supp_set_key;
  131. ctx->get_network_ctx = supp_get_network_ctx;
  132. ctx->mlme_setprotection = supp_mlme_setprotection;
  133. ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
  134. peer->supp = wpa_sm_init(ctx);
  135. if (peer->supp == NULL) {
  136. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
  137. return -1;
  138. }
  139. wpa_sm_set_own_addr(peer->supp, own_addr);
  140. wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
  141. wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  142. wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  143. wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  144. wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  145. wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
  146. peer->supp_ie_len = sizeof(peer->supp_ie);
  147. if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
  148. &peer->supp_ie_len) < 0) {
  149. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  150. " failed");
  151. return -1;
  152. }
  153. wpa_sm_notify_assoc(peer->supp, peer->addr);
  154. return 0;
  155. }
  156. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  157. const char *txt)
  158. {
  159. if (addr)
  160. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  161. MAC2STR(addr), txt);
  162. else
  163. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  164. }
  165. static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
  166. {
  167. struct ibss_rsn *ibss_rsn = ctx;
  168. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  169. __func__, MAC2STR(addr), prev_psk);
  170. if (prev_psk)
  171. return NULL;
  172. return ibss_rsn->psk;
  173. }
  174. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  175. size_t data_len, int encrypt)
  176. {
  177. struct ibss_rsn *ibss_rsn = ctx;
  178. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  179. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  180. "encrypt=%d)",
  181. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  182. if (wpa_s->l2)
  183. return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
  184. data_len);
  185. return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
  186. }
  187. static int auth_set_key(void *ctx, int vlan_id, const char *_alg,
  188. const u8 *addr, int idx, u8 *key,
  189. size_t key_len)
  190. {
  191. struct ibss_rsn *ibss_rsn = ctx;
  192. u8 seq[6];
  193. wpa_alg alg;
  194. os_memset(seq, 0, sizeof(seq));
  195. if (os_strcmp(_alg, "none") == 0)
  196. alg = WPA_ALG_NONE;
  197. else if (os_strcmp(_alg, "WEP") == 0)
  198. alg = WPA_ALG_WEP;
  199. else if (os_strcmp(_alg, "TKIP") == 0)
  200. alg = WPA_ALG_TKIP;
  201. else if (os_strcmp(_alg, "CCMP") == 0)
  202. alg = WPA_ALG_CCMP;
  203. else if (os_strcmp(_alg, "IGTK") == 0)
  204. alg = WPA_ALG_IGTK;
  205. else
  206. return -1;
  207. if (idx == 0) {
  208. /*
  209. * In IBSS RSN, the pairwise key from the 4-way handshake
  210. * initiated by the peer with highest MAC address is used.
  211. */
  212. if (os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0)
  213. return 0;
  214. }
  215. return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
  216. 1, seq, 6, key, key_len);
  217. }
  218. static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
  219. const u8 *own_addr)
  220. {
  221. struct wpa_auth_config conf;
  222. struct wpa_auth_callbacks cb;
  223. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  224. os_memset(&conf, 0, sizeof(conf));
  225. conf.wpa = 2;
  226. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  227. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  228. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  229. conf.wpa_group = WPA_CIPHER_CCMP;
  230. conf.eapol_version = 2;
  231. os_memset(&cb, 0, sizeof(cb));
  232. cb.ctx = ibss_rsn;
  233. cb.logger = auth_logger;
  234. cb.send_eapol = auth_send_eapol;
  235. cb.get_psk = auth_get_psk;
  236. cb.set_key = auth_set_key;
  237. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
  238. if (ibss_rsn->auth_group == NULL) {
  239. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  240. return -1;
  241. }
  242. return 0;
  243. }
  244. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  245. struct ibss_rsn_peer *peer)
  246. {
  247. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
  248. if (peer->auth == NULL) {
  249. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  250. return -1;
  251. }
  252. /* TODO: get peer RSN IE with Probe Request */
  253. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  254. (u8 *) "\x30\x14\x01\x00"
  255. "\x00\x0f\xac\x04"
  256. "\x01\x00\x00\x0f\xac\x04"
  257. "\x01\x00\x00\x0f\xac\x02"
  258. "\x00\x00", 22, NULL, 0) !=
  259. WPA_IE_OK) {
  260. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  261. return -1;
  262. }
  263. wpa_auth_sm_event(peer->auth, WPA_ASSOC);
  264. wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth);
  265. return 0;
  266. }
  267. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  268. {
  269. struct ibss_rsn_peer *peer;
  270. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
  271. "Supplicant for peer " MACSTR, MAC2STR(addr));
  272. peer = os_zalloc(sizeof(*peer));
  273. if (peer == NULL)
  274. return -1;
  275. peer->ibss_rsn = ibss_rsn;
  276. os_memcpy(peer->addr, addr, ETH_ALEN);
  277. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
  278. < 0) {
  279. ibss_rsn_free(peer);
  280. return -1;
  281. }
  282. if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
  283. ibss_rsn_free(peer);
  284. return -1;
  285. }
  286. peer->next = ibss_rsn->peers;
  287. ibss_rsn->peers = peer;
  288. return 0;
  289. }
  290. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
  291. {
  292. struct ibss_rsn *ibss_rsn;
  293. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  294. if (ibss_rsn == NULL)
  295. return NULL;
  296. ibss_rsn->wpa_s = wpa_s;
  297. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
  298. ibss_rsn_deinit(ibss_rsn);
  299. return NULL;
  300. }
  301. return ibss_rsn;
  302. }
  303. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  304. {
  305. struct ibss_rsn_peer *peer, *prev;
  306. if (ibss_rsn == NULL)
  307. return;
  308. peer = ibss_rsn->peers;
  309. while (peer) {
  310. prev = peer;
  311. peer = peer->next;
  312. ibss_rsn_free(prev);
  313. }
  314. wpa_deinit(ibss_rsn->auth_group);
  315. os_free(ibss_rsn);
  316. }
  317. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  318. {
  319. const struct ieee802_1x_hdr *hdr;
  320. const struct wpa_eapol_key *key;
  321. u16 key_info;
  322. size_t plen;
  323. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  324. if (len < sizeof(*hdr) + sizeof(*key))
  325. return -1;
  326. hdr = (const struct ieee802_1x_hdr *) buf;
  327. key = (const struct wpa_eapol_key *) (hdr + 1);
  328. plen = be_to_host16(hdr->length);
  329. if (hdr->version < EAPOL_VERSION) {
  330. /* TODO: backwards compatibility */
  331. }
  332. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  333. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  334. "not a Key frame", hdr->type);
  335. return -1;
  336. }
  337. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  338. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  339. "invalid (frame size %lu)",
  340. (unsigned long) plen, (unsigned long) len);
  341. return -1;
  342. }
  343. if (key->type != EAPOL_KEY_TYPE_RSN) {
  344. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  345. "discarded", key->type);
  346. return -1;
  347. }
  348. key_info = WPA_GET_BE16(key->key_info);
  349. return !!(key_info & WPA_KEY_INFO_ACK);
  350. }
  351. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  352. struct ibss_rsn_peer *peer,
  353. const u8 *buf, size_t len)
  354. {
  355. int supp;
  356. u8 *tmp;
  357. supp = ibss_rsn_eapol_dst_supp(buf, len);
  358. if (supp < 0)
  359. return -1;
  360. tmp = os_malloc(len);
  361. if (tmp == NULL)
  362. return -1;
  363. os_memcpy(tmp, buf, len);
  364. if (supp) {
  365. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
  366. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  367. } else {
  368. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
  369. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  370. }
  371. os_free(tmp);
  372. return 1;
  373. }
  374. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  375. const u8 *buf, size_t len)
  376. {
  377. struct ibss_rsn_peer *peer;
  378. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  379. if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
  380. return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
  381. buf, len);
  382. }
  383. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  384. /*
  385. * Create new IBSS peer based on an EAPOL message from the peer
  386. * Authenticator.
  387. */
  388. if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
  389. return -1;
  390. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  391. buf, len);
  392. }
  393. return 0;
  394. }
  395. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  396. {
  397. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  398. }