ibss_rsn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
  84. "set_tx=%d)",
  85. __func__, alg, MAC2STR(addr), key_idx, set_tx);
  86. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
  87. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
  88. return 0;
  89. }
  90. static void * supp_get_network_ctx(void *ctx)
  91. {
  92. struct ibss_rsn_peer *peer = ctx;
  93. return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
  94. }
  95. static int supp_mlme_setprotection(void *ctx, const u8 *addr,
  96. int protection_type, int key_type)
  97. {
  98. wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
  99. "key_type=%d)",
  100. __func__, MAC2STR(addr), protection_type, key_type);
  101. return 0;
  102. }
  103. static void supp_cancel_auth_timeout(void *ctx)
  104. {
  105. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  106. }
  107. int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
  108. const u8 *psk)
  109. {
  110. struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
  111. if (ctx == NULL)
  112. return -1;
  113. ctx->ctx = peer;
  114. ctx->set_state = supp_set_state;
  115. ctx->ether_send = supp_ether_send;
  116. ctx->get_beacon_ie = supp_get_beacon_ie;
  117. ctx->alloc_eapol = supp_alloc_eapol;
  118. ctx->set_key = supp_set_key;
  119. ctx->get_network_ctx = supp_get_network_ctx;
  120. ctx->mlme_setprotection = supp_mlme_setprotection;
  121. ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
  122. peer->supp = wpa_sm_init(ctx);
  123. if (peer->supp == NULL) {
  124. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
  125. return -1;
  126. }
  127. wpa_sm_set_own_addr(peer->supp, own_addr);
  128. wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
  129. wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  130. wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  131. wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  132. wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  133. wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
  134. peer->supp_ie_len = sizeof(peer->supp_ie);
  135. if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
  136. &peer->supp_ie_len) < 0) {
  137. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  138. " failed");
  139. return -1;
  140. }
  141. wpa_sm_notify_assoc(peer->supp, peer->addr);
  142. return 0;
  143. }
  144. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  145. const char *txt)
  146. {
  147. if (addr)
  148. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  149. MAC2STR(addr), txt);
  150. else
  151. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  152. }
  153. static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
  154. {
  155. struct ibss_rsn *ibss_rsn = ctx;
  156. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  157. __func__, MAC2STR(addr), prev_psk);
  158. if (prev_psk)
  159. return NULL;
  160. return ibss_rsn->psk;
  161. }
  162. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  163. size_t data_len, int encrypt)
  164. {
  165. struct ibss_rsn *ibss_rsn = ctx;
  166. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  167. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  168. "encrypt=%d)",
  169. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  170. if (wpa_s->l2)
  171. return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
  172. data_len);
  173. return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
  174. }
  175. static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
  176. const u8 *own_addr)
  177. {
  178. struct wpa_auth_config conf;
  179. struct wpa_auth_callbacks cb;
  180. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  181. os_memset(&conf, 0, sizeof(conf));
  182. conf.wpa = 2;
  183. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  184. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  185. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  186. conf.wpa_group = WPA_CIPHER_CCMP;
  187. conf.eapol_version = 2;
  188. os_memset(&cb, 0, sizeof(cb));
  189. cb.ctx = ibss_rsn;
  190. cb.logger = auth_logger;
  191. cb.send_eapol = auth_send_eapol;
  192. cb.get_psk = auth_get_psk;
  193. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
  194. if (ibss_rsn->auth_group == NULL) {
  195. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  201. struct ibss_rsn_peer *peer)
  202. {
  203. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
  204. if (peer->auth == NULL) {
  205. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  206. return -1;
  207. }
  208. /* TODO: get peer RSN IE with Probe Request */
  209. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  210. (u8 *) "\x30\x14\x01\x00"
  211. "\x00\x0f\xac\x04"
  212. "\x01\x00\x00\x0f\xac\x04"
  213. "\x01\x00\x00\x0f\xac\x02"
  214. "\x00\x00", 22, NULL, 0) !=
  215. WPA_IE_OK) {
  216. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  217. return -1;
  218. }
  219. wpa_auth_sm_event(peer->auth, WPA_ASSOC);
  220. wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth);
  221. return 0;
  222. }
  223. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  224. {
  225. struct ibss_rsn_peer *peer;
  226. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
  227. "Supplicant for peer " MACSTR, MAC2STR(addr));
  228. peer = os_zalloc(sizeof(*peer));
  229. if (peer == NULL)
  230. return -1;
  231. peer->ibss_rsn = ibss_rsn;
  232. os_memcpy(peer->addr, addr, ETH_ALEN);
  233. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
  234. < 0) {
  235. ibss_rsn_free(peer);
  236. return -1;
  237. }
  238. if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
  239. ibss_rsn_free(peer);
  240. return -1;
  241. }
  242. peer->next = ibss_rsn->peers;
  243. ibss_rsn->peers = peer;
  244. return 0;
  245. }
  246. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
  247. {
  248. struct ibss_rsn *ibss_rsn;
  249. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  250. if (ibss_rsn == NULL)
  251. return NULL;
  252. ibss_rsn->wpa_s = wpa_s;
  253. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
  254. ibss_rsn_deinit(ibss_rsn);
  255. return NULL;
  256. }
  257. return ibss_rsn;
  258. }
  259. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  260. {
  261. struct ibss_rsn_peer *peer, *prev;
  262. if (ibss_rsn == NULL)
  263. return;
  264. peer = ibss_rsn->peers;
  265. while (peer) {
  266. prev = peer;
  267. peer = peer->next;
  268. ibss_rsn_free(prev);
  269. }
  270. wpa_deinit(ibss_rsn->auth_group);
  271. os_free(ibss_rsn);
  272. }
  273. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  274. {
  275. const struct ieee802_1x_hdr *hdr;
  276. const struct wpa_eapol_key *key;
  277. u16 key_info;
  278. size_t plen;
  279. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  280. if (len < sizeof(*hdr) + sizeof(*key))
  281. return -1;
  282. hdr = (const struct ieee802_1x_hdr *) buf;
  283. key = (const struct wpa_eapol_key *) (hdr + 1);
  284. plen = be_to_host16(hdr->length);
  285. if (hdr->version < EAPOL_VERSION) {
  286. /* TODO: backwards compatibility */
  287. }
  288. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  289. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  290. "not a Key frame", hdr->type);
  291. return -1;
  292. }
  293. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  294. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  295. "invalid (frame size %lu)",
  296. (unsigned long) plen, (unsigned long) len);
  297. return -1;
  298. }
  299. if (key->type != EAPOL_KEY_TYPE_RSN) {
  300. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  301. "discarded", key->type);
  302. return -1;
  303. }
  304. key_info = WPA_GET_BE16(key->key_info);
  305. return !!(key_info & WPA_KEY_INFO_ACK);
  306. }
  307. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  308. struct ibss_rsn_peer *peer,
  309. const u8 *buf, size_t len)
  310. {
  311. int supp;
  312. u8 *tmp;
  313. supp = ibss_rsn_eapol_dst_supp(buf, len);
  314. if (supp < 0)
  315. return -1;
  316. tmp = os_malloc(len);
  317. if (tmp == NULL)
  318. return -1;
  319. os_memcpy(tmp, buf, len);
  320. if (supp) {
  321. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
  322. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  323. } else {
  324. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
  325. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  326. }
  327. os_free(tmp);
  328. return 1;
  329. }
  330. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  331. const u8 *buf, size_t len)
  332. {
  333. struct ibss_rsn_peer *peer;
  334. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  335. if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
  336. return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
  337. buf, len);
  338. }
  339. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  340. /*
  341. * Create new IBSS peer based on an EAPOL message from the peer
  342. * Authenticator.
  343. */
  344. if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
  345. return -1;
  346. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  347. buf, len);
  348. }
  349. return 0;
  350. }
  351. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  352. {
  353. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  354. }