ibss_rsn.c 12 KB

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