ibss_rsn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 "rsn_supp/wpa.h"
  18. #include "rsn_supp/wpa_ie.h"
  19. #include "ap/wpa_auth.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "driver_i.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, enum 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, enum 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. wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
  86. "set_tx=%d)",
  87. __func__, alg, MAC2STR(addr), key_idx, set_tx);
  88. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
  89. wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
  90. if (key_idx == 0) {
  91. /*
  92. * In IBSS RSN, the pairwise key from the 4-way handshake
  93. * initiated by the peer with highest MAC address is used.
  94. */
  95. if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
  96. ETH_ALEN) > 0) {
  97. wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
  98. return 0;
  99. }
  100. }
  101. if (is_broadcast_ether_addr(addr) && peer->addr)
  102. addr = peer->addr;
  103. return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
  104. set_tx, seq, seq_len, key, key_len);
  105. }
  106. static void * supp_get_network_ctx(void *ctx)
  107. {
  108. struct ibss_rsn_peer *peer = ctx;
  109. return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
  110. }
  111. static int supp_mlme_setprotection(void *ctx, const u8 *addr,
  112. int protection_type, int key_type)
  113. {
  114. wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
  115. "key_type=%d)",
  116. __func__, MAC2STR(addr), protection_type, key_type);
  117. return 0;
  118. }
  119. static void supp_cancel_auth_timeout(void *ctx)
  120. {
  121. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  122. }
  123. static void supp_deauthenticate(void * ctx, int reason_code)
  124. {
  125. wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
  126. }
  127. int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
  128. const u8 *psk)
  129. {
  130. struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
  131. if (ctx == NULL)
  132. return -1;
  133. ctx->ctx = peer;
  134. ctx->msg_ctx = peer->ibss_rsn->wpa_s;
  135. ctx->set_state = supp_set_state;
  136. ctx->ether_send = supp_ether_send;
  137. ctx->get_beacon_ie = supp_get_beacon_ie;
  138. ctx->alloc_eapol = supp_alloc_eapol;
  139. ctx->set_key = supp_set_key;
  140. ctx->get_network_ctx = supp_get_network_ctx;
  141. ctx->mlme_setprotection = supp_mlme_setprotection;
  142. ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
  143. ctx->deauthenticate = supp_deauthenticate;
  144. peer->supp = wpa_sm_init(ctx);
  145. if (peer->supp == NULL) {
  146. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
  147. return -1;
  148. }
  149. wpa_sm_set_own_addr(peer->supp, own_addr);
  150. wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
  151. wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  152. wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  153. wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  154. wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  155. wpa_sm_set_pmk(peer->supp, psk, PMK_LEN);
  156. peer->supp_ie_len = sizeof(peer->supp_ie);
  157. if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
  158. &peer->supp_ie_len) < 0) {
  159. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  160. " failed");
  161. return -1;
  162. }
  163. wpa_sm_notify_assoc(peer->supp, peer->addr);
  164. return 0;
  165. }
  166. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  167. const char *txt)
  168. {
  169. if (addr)
  170. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  171. MAC2STR(addr), txt);
  172. else
  173. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  174. }
  175. static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
  176. {
  177. struct ibss_rsn *ibss_rsn = ctx;
  178. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  179. __func__, MAC2STR(addr), prev_psk);
  180. if (prev_psk)
  181. return NULL;
  182. return ibss_rsn->psk;
  183. }
  184. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  185. size_t data_len, int encrypt)
  186. {
  187. struct ibss_rsn *ibss_rsn = ctx;
  188. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  189. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  190. "encrypt=%d)",
  191. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  192. if (wpa_s->l2)
  193. return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
  194. data_len);
  195. return wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
  196. }
  197. static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  198. const u8 *addr, int idx, u8 *key, size_t key_len)
  199. {
  200. struct ibss_rsn *ibss_rsn = ctx;
  201. u8 seq[6];
  202. os_memset(seq, 0, sizeof(seq));
  203. if (addr) {
  204. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
  205. " key_idx=%d)",
  206. __func__, alg, MAC2STR(addr), idx);
  207. } else {
  208. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
  209. __func__, alg, idx);
  210. }
  211. wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
  212. if (idx == 0) {
  213. /*
  214. * In IBSS RSN, the pairwise key from the 4-way handshake
  215. * initiated by the peer with highest MAC address is used.
  216. */
  217. if (addr == NULL ||
  218. os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
  219. wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
  220. return 0;
  221. }
  222. }
  223. if (ibss_rsn->init_in_progress && key_len <=
  224. sizeof(ibss_rsn->init_gtk)) {
  225. wpa_printf(MSG_DEBUG, "AUTH: Delay setting of initial TX GTK "
  226. "until RSN IBSS is connected");
  227. ibss_rsn->init_gtk_idx = idx;
  228. os_memcpy(ibss_rsn->init_gtk, key, key_len);
  229. return 0;
  230. }
  231. return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
  232. 1, seq, 6, key, key_len);
  233. }
  234. static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
  235. const u8 *own_addr)
  236. {
  237. struct wpa_auth_config conf;
  238. struct wpa_auth_callbacks cb;
  239. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  240. os_memset(&conf, 0, sizeof(conf));
  241. conf.wpa = 2;
  242. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  243. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  244. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  245. conf.wpa_group = WPA_CIPHER_CCMP;
  246. conf.eapol_version = 2;
  247. os_memset(&cb, 0, sizeof(cb));
  248. cb.ctx = ibss_rsn;
  249. cb.logger = auth_logger;
  250. cb.send_eapol = auth_send_eapol;
  251. cb.get_psk = auth_get_psk;
  252. cb.set_key = auth_set_key;
  253. ibss_rsn->init_in_progress = 1;
  254. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
  255. ibss_rsn->init_in_progress = 0;
  256. if (ibss_rsn->auth_group == NULL) {
  257. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  263. struct ibss_rsn_peer *peer)
  264. {
  265. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr);
  266. if (peer->auth == NULL) {
  267. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  268. return -1;
  269. }
  270. /* TODO: get peer RSN IE with Probe Request */
  271. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  272. (u8 *) "\x30\x14\x01\x00"
  273. "\x00\x0f\xac\x04"
  274. "\x01\x00\x00\x0f\xac\x04"
  275. "\x01\x00\x00\x0f\xac\x02"
  276. "\x00\x00", 22, NULL, 0) !=
  277. WPA_IE_OK) {
  278. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  279. return -1;
  280. }
  281. if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
  282. return -1;
  283. if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
  284. return -1;
  285. return 0;
  286. }
  287. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  288. {
  289. struct ibss_rsn_peer *peer;
  290. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  291. if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0) {
  292. wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator and "
  293. "Supplicant for peer " MACSTR " already "
  294. "running", MAC2STR(addr));
  295. return 0;
  296. }
  297. }
  298. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator and "
  299. "Supplicant for peer " MACSTR, MAC2STR(addr));
  300. peer = os_zalloc(sizeof(*peer));
  301. if (peer == NULL)
  302. return -1;
  303. peer->ibss_rsn = ibss_rsn;
  304. os_memcpy(peer->addr, addr, ETH_ALEN);
  305. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr, ibss_rsn->psk)
  306. < 0) {
  307. ibss_rsn_free(peer);
  308. return -1;
  309. }
  310. if (ibss_rsn_auth_init(ibss_rsn, peer) < 0) {
  311. ibss_rsn_free(peer);
  312. return -1;
  313. }
  314. peer->next = ibss_rsn->peers;
  315. ibss_rsn->peers = peer;
  316. return 0;
  317. }
  318. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
  319. {
  320. struct ibss_rsn *ibss_rsn;
  321. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  322. if (ibss_rsn == NULL)
  323. return NULL;
  324. ibss_rsn->wpa_s = wpa_s;
  325. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
  326. ibss_rsn_deinit(ibss_rsn);
  327. return NULL;
  328. }
  329. return ibss_rsn;
  330. }
  331. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  332. {
  333. struct ibss_rsn_peer *peer, *prev;
  334. if (ibss_rsn == NULL)
  335. return;
  336. peer = ibss_rsn->peers;
  337. while (peer) {
  338. prev = peer;
  339. peer = peer->next;
  340. ibss_rsn_free(prev);
  341. }
  342. wpa_deinit(ibss_rsn->auth_group);
  343. os_free(ibss_rsn);
  344. }
  345. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  346. {
  347. const struct ieee802_1x_hdr *hdr;
  348. const struct wpa_eapol_key *key;
  349. u16 key_info;
  350. size_t plen;
  351. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  352. if (len < sizeof(*hdr) + sizeof(*key))
  353. return -1;
  354. hdr = (const struct ieee802_1x_hdr *) buf;
  355. key = (const struct wpa_eapol_key *) (hdr + 1);
  356. plen = be_to_host16(hdr->length);
  357. if (hdr->version < EAPOL_VERSION) {
  358. /* TODO: backwards compatibility */
  359. }
  360. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  361. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  362. "not a Key frame", hdr->type);
  363. return -1;
  364. }
  365. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  366. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  367. "invalid (frame size %lu)",
  368. (unsigned long) plen, (unsigned long) len);
  369. return -1;
  370. }
  371. if (key->type != EAPOL_KEY_TYPE_RSN) {
  372. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  373. "discarded", key->type);
  374. return -1;
  375. }
  376. key_info = WPA_GET_BE16(key->key_info);
  377. return !!(key_info & WPA_KEY_INFO_ACK);
  378. }
  379. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  380. struct ibss_rsn_peer *peer,
  381. const u8 *buf, size_t len)
  382. {
  383. int supp;
  384. u8 *tmp;
  385. supp = ibss_rsn_eapol_dst_supp(buf, len);
  386. if (supp < 0)
  387. return -1;
  388. tmp = os_malloc(len);
  389. if (tmp == NULL)
  390. return -1;
  391. os_memcpy(tmp, buf, len);
  392. if (supp) {
  393. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant");
  394. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  395. } else {
  396. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator");
  397. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  398. }
  399. os_free(tmp);
  400. return 1;
  401. }
  402. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  403. const u8 *buf, size_t len)
  404. {
  405. struct ibss_rsn_peer *peer;
  406. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  407. if (os_memcmp(src_addr, peer->addr, ETH_ALEN) == 0)
  408. return ibss_rsn_process_rx_eapol(ibss_rsn, peer,
  409. buf, len);
  410. }
  411. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  412. /*
  413. * Create new IBSS peer based on an EAPOL message from the peer
  414. * Authenticator.
  415. */
  416. if (ibss_rsn_start(ibss_rsn, src_addr) < 0)
  417. return -1;
  418. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  419. buf, len);
  420. }
  421. return 0;
  422. }
  423. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  424. {
  425. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  426. }
  427. void ibss_rsn_connected(struct ibss_rsn *ibss_rsn)
  428. {
  429. u8 seq[6];
  430. wpa_printf(MSG_DEBUG, "RSN: IBSS connected notification");
  431. if (ibss_rsn->init_gtk_idx) {
  432. wpa_printf(MSG_DEBUG, "RSN: Set initial IBSS TX GTK");
  433. os_memset(seq, 0, sizeof(seq));
  434. if (wpa_drv_set_key(ibss_rsn->wpa_s, WPA_ALG_CCMP,
  435. broadcast_ether_addr,
  436. ibss_rsn->init_gtk_idx,
  437. 1, seq, sizeof(seq), ibss_rsn->init_gtk,
  438. 16) < 0)
  439. wpa_printf(MSG_INFO, "RSN: Failed to set IBSS TX GTK");
  440. ibss_rsn->init_gtk_idx = 0;
  441. os_memset(ibss_rsn->init_gtk, 0, sizeof(ibss_rsn->init_gtk));
  442. }
  443. }