ibss_rsn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * wpa_supplicant - IBSS RSN
  3. * Copyright (c) 2009-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "common/wpa_ctrl.h"
  11. #include "utils/eloop.h"
  12. #include "l2_packet/l2_packet.h"
  13. #include "rsn_supp/wpa.h"
  14. #include "rsn_supp/wpa_ie.h"
  15. #include "ap/wpa_auth.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "driver_i.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "ibss_rsn.h"
  20. static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx);
  21. static struct ibss_rsn_peer * ibss_rsn_get_peer(struct ibss_rsn *ibss_rsn,
  22. const u8 *addr)
  23. {
  24. struct ibss_rsn_peer *peer;
  25. for (peer = ibss_rsn->peers; peer; peer = peer->next)
  26. if (os_memcmp(addr, peer->addr, ETH_ALEN) == 0)
  27. break;
  28. return peer;
  29. }
  30. static void ibss_rsn_free(struct ibss_rsn_peer *peer)
  31. {
  32. eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
  33. wpa_auth_sta_deinit(peer->auth);
  34. wpa_sm_deinit(peer->supp);
  35. os_free(peer);
  36. }
  37. static void supp_set_state(void *ctx, enum wpa_states state)
  38. {
  39. struct ibss_rsn_peer *peer = ctx;
  40. peer->supp_state = state;
  41. }
  42. static enum wpa_states supp_get_state(void *ctx)
  43. {
  44. struct ibss_rsn_peer *peer = ctx;
  45. return peer->supp_state;
  46. }
  47. static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
  48. size_t len)
  49. {
  50. struct ibss_rsn_peer *peer = ctx;
  51. struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
  52. wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
  53. "len=%lu)",
  54. __func__, MAC2STR(dest), proto, (unsigned long) len);
  55. if (wpa_s->l2)
  56. return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
  57. return -1;
  58. }
  59. static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
  60. u16 data_len, size_t *msg_len, void **data_pos)
  61. {
  62. struct ieee802_1x_hdr *hdr;
  63. wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
  64. __func__, type, data_len);
  65. *msg_len = sizeof(*hdr) + data_len;
  66. hdr = os_malloc(*msg_len);
  67. if (hdr == NULL)
  68. return NULL;
  69. hdr->version = 2;
  70. hdr->type = type;
  71. hdr->length = host_to_be16(data_len);
  72. if (data)
  73. os_memcpy(hdr + 1, data, data_len);
  74. else
  75. os_memset(hdr + 1, 0, data_len);
  76. if (data_pos)
  77. *data_pos = hdr + 1;
  78. return (u8 *) hdr;
  79. }
  80. static int supp_get_beacon_ie(void *ctx)
  81. {
  82. struct ibss_rsn_peer *peer = ctx;
  83. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  84. /* TODO: get correct RSN IE */
  85. return wpa_sm_set_ap_rsn_ie(peer->supp,
  86. (u8 *) "\x30\x14\x01\x00"
  87. "\x00\x0f\xac\x04"
  88. "\x01\x00\x00\x0f\xac\x04"
  89. "\x01\x00\x00\x0f\xac\x02"
  90. "\x00\x00", 22);
  91. }
  92. static void ibss_check_rsn_completed(struct ibss_rsn_peer *peer)
  93. {
  94. struct wpa_supplicant *wpa_s = peer->ibss_rsn->wpa_s;
  95. if ((peer->authentication_status &
  96. (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH)) !=
  97. (IBSS_RSN_SET_PTK_SUPP | IBSS_RSN_SET_PTK_AUTH))
  98. return;
  99. if (peer->authentication_status & IBSS_RSN_REPORTED_PTK)
  100. return;
  101. peer->authentication_status |= IBSS_RSN_REPORTED_PTK;
  102. wpa_msg(wpa_s, MSG_INFO, IBSS_RSN_COMPLETED MACSTR,
  103. MAC2STR(peer->addr));
  104. }
  105. static int supp_set_key(void *ctx, enum wpa_alg alg,
  106. const u8 *addr, int key_idx, int set_tx,
  107. const u8 *seq, size_t seq_len,
  108. const u8 *key, size_t key_len)
  109. {
  110. struct ibss_rsn_peer *peer = ctx;
  111. wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
  112. "set_tx=%d)",
  113. __func__, alg, MAC2STR(addr), key_idx, set_tx);
  114. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
  115. wpa_hexdump_key(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
  116. if (key_idx == 0) {
  117. peer->authentication_status |= IBSS_RSN_SET_PTK_SUPP;
  118. ibss_check_rsn_completed(peer);
  119. /*
  120. * In IBSS RSN, the pairwise key from the 4-way handshake
  121. * initiated by the peer with highest MAC address is used.
  122. */
  123. if (os_memcmp(peer->ibss_rsn->wpa_s->own_addr, peer->addr,
  124. ETH_ALEN) > 0) {
  125. wpa_printf(MSG_DEBUG, "SUPP: Do not use this PTK");
  126. return 0;
  127. }
  128. }
  129. if (is_broadcast_ether_addr(addr))
  130. addr = peer->addr;
  131. return wpa_drv_set_key(peer->ibss_rsn->wpa_s, alg, addr, key_idx,
  132. set_tx, seq, seq_len, key, key_len);
  133. }
  134. static void * supp_get_network_ctx(void *ctx)
  135. {
  136. struct ibss_rsn_peer *peer = ctx;
  137. return wpa_supplicant_get_ssid(peer->ibss_rsn->wpa_s);
  138. }
  139. static int supp_mlme_setprotection(void *ctx, const u8 *addr,
  140. int protection_type, int key_type)
  141. {
  142. wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
  143. "key_type=%d)",
  144. __func__, MAC2STR(addr), protection_type, key_type);
  145. return 0;
  146. }
  147. static void supp_cancel_auth_timeout(void *ctx)
  148. {
  149. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  150. }
  151. static void supp_deauthenticate(void * ctx, int reason_code)
  152. {
  153. wpa_printf(MSG_DEBUG, "SUPP: %s (TODO)", __func__);
  154. }
  155. static int ibss_rsn_supp_init(struct ibss_rsn_peer *peer, const u8 *own_addr,
  156. const u8 *psk)
  157. {
  158. struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
  159. if (ctx == NULL)
  160. return -1;
  161. ctx->ctx = peer;
  162. ctx->msg_ctx = peer->ibss_rsn->wpa_s;
  163. ctx->set_state = supp_set_state;
  164. ctx->get_state = supp_get_state;
  165. ctx->ether_send = supp_ether_send;
  166. ctx->get_beacon_ie = supp_get_beacon_ie;
  167. ctx->alloc_eapol = supp_alloc_eapol;
  168. ctx->set_key = supp_set_key;
  169. ctx->get_network_ctx = supp_get_network_ctx;
  170. ctx->mlme_setprotection = supp_mlme_setprotection;
  171. ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
  172. ctx->deauthenticate = supp_deauthenticate;
  173. peer->supp = wpa_sm_init(ctx);
  174. if (peer->supp == NULL) {
  175. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
  176. return -1;
  177. }
  178. wpa_sm_set_own_addr(peer->supp, own_addr);
  179. wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
  180. wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  181. wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  182. wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  183. wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  184. wpa_sm_set_pmk(peer->supp, psk, PMK_LEN, NULL, NULL);
  185. peer->supp_ie_len = sizeof(peer->supp_ie);
  186. if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
  187. &peer->supp_ie_len) < 0) {
  188. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  189. " failed");
  190. return -1;
  191. }
  192. wpa_sm_notify_assoc(peer->supp, peer->addr);
  193. return 0;
  194. }
  195. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  196. const char *txt)
  197. {
  198. if (addr)
  199. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  200. MAC2STR(addr), txt);
  201. else
  202. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  203. }
  204. static const u8 * auth_get_psk(void *ctx, const u8 *addr,
  205. const u8 *p2p_dev_addr, const u8 *prev_psk)
  206. {
  207. struct ibss_rsn *ibss_rsn = ctx;
  208. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  209. __func__, MAC2STR(addr), prev_psk);
  210. if (prev_psk)
  211. return NULL;
  212. return ibss_rsn->psk;
  213. }
  214. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  215. size_t data_len, int encrypt)
  216. {
  217. struct ibss_rsn *ibss_rsn = ctx;
  218. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  219. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  220. "encrypt=%d)",
  221. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  222. if (wpa_s->l2)
  223. return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
  224. data_len);
  225. return -1;
  226. }
  227. static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  228. const u8 *addr, int idx, u8 *key, size_t key_len)
  229. {
  230. struct ibss_rsn *ibss_rsn = ctx;
  231. u8 seq[6];
  232. os_memset(seq, 0, sizeof(seq));
  233. if (addr) {
  234. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
  235. " key_idx=%d)",
  236. __func__, alg, MAC2STR(addr), idx);
  237. } else {
  238. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
  239. __func__, alg, idx);
  240. }
  241. wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
  242. if (idx == 0) {
  243. if (addr) {
  244. struct ibss_rsn_peer *peer;
  245. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  246. if (peer) {
  247. peer->authentication_status |=
  248. IBSS_RSN_SET_PTK_AUTH;
  249. ibss_check_rsn_completed(peer);
  250. }
  251. }
  252. /*
  253. * In IBSS RSN, the pairwise key from the 4-way handshake
  254. * initiated by the peer with highest MAC address is used.
  255. */
  256. if (addr == NULL ||
  257. os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
  258. wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
  259. return 0;
  260. }
  261. }
  262. return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
  263. 1, seq, 6, key, key_len);
  264. }
  265. static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
  266. {
  267. struct ibss_rsn *ibss_rsn = ctx;
  268. wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
  269. }
  270. static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
  271. void *ctx),
  272. void *cb_ctx)
  273. {
  274. struct ibss_rsn *ibss_rsn = ctx;
  275. struct ibss_rsn_peer *peer;
  276. wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
  277. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  278. if (peer->auth && cb(peer->auth, cb_ctx))
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
  284. struct ibss_rsn_peer *peer, int authorized)
  285. {
  286. int res;
  287. if (authorized) {
  288. res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
  289. WPA_STA_AUTHORIZED,
  290. WPA_STA_AUTHORIZED, ~0);
  291. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
  292. MAC2STR(peer->addr));
  293. } else {
  294. res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
  295. 0, 0, ~WPA_STA_AUTHORIZED);
  296. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
  297. MAC2STR(peer->addr));
  298. }
  299. if (res && errno != ENOENT) {
  300. wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
  301. "for kernel driver (errno=%d)",
  302. MAC2STR(peer->addr), errno);
  303. }
  304. }
  305. static void auth_set_eapol(void *ctx, const u8 *addr,
  306. wpa_eapol_variable var, int value)
  307. {
  308. struct ibss_rsn *ibss_rsn = ctx;
  309. struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
  310. if (peer == NULL)
  311. return;
  312. switch (var) {
  313. case WPA_EAPOL_authorized:
  314. ibss_set_sta_authorized(ibss_rsn, peer, value);
  315. break;
  316. default:
  317. /* do not handle any other event */
  318. wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
  319. break;
  320. }
  321. }
  322. static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
  323. const u8 *own_addr)
  324. {
  325. struct wpa_auth_config conf;
  326. struct wpa_auth_callbacks cb;
  327. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  328. os_memset(&conf, 0, sizeof(conf));
  329. conf.wpa = 2;
  330. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  331. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  332. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  333. conf.wpa_group = WPA_CIPHER_CCMP;
  334. conf.eapol_version = 2;
  335. conf.wpa_group_rekey = 600;
  336. os_memset(&cb, 0, sizeof(cb));
  337. cb.ctx = ibss_rsn;
  338. cb.logger = auth_logger;
  339. cb.set_eapol = auth_set_eapol;
  340. cb.send_eapol = auth_send_eapol;
  341. cb.get_psk = auth_get_psk;
  342. cb.set_key = auth_set_key;
  343. cb.for_each_sta = auth_for_each_sta;
  344. cb.disconnect = ibss_rsn_disconnect;
  345. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
  346. if (ibss_rsn->auth_group == NULL) {
  347. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  348. return -1;
  349. }
  350. wpa_init_keys(ibss_rsn->auth_group);
  351. return 0;
  352. }
  353. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  354. struct ibss_rsn_peer *peer)
  355. {
  356. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
  357. if (peer->auth == NULL) {
  358. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  359. return -1;
  360. }
  361. /* TODO: get peer RSN IE with Probe Request */
  362. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  363. (u8 *) "\x30\x14\x01\x00"
  364. "\x00\x0f\xac\x04"
  365. "\x01\x00\x00\x0f\xac\x04"
  366. "\x01\x00\x00\x0f\xac\x02"
  367. "\x00\x00", 22, NULL, 0) !=
  368. WPA_IE_OK) {
  369. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  370. return -1;
  371. }
  372. if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
  373. return -1;
  374. if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
  375. return -1;
  376. return 0;
  377. }
  378. static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
  379. {
  380. struct ieee80211_mgmt auth;
  381. const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
  382. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  383. if (wpa_s->driver->send_frame == NULL)
  384. return -1;
  385. os_memset(&auth, 0, sizeof(auth));
  386. auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  387. WLAN_FC_STYPE_AUTH);
  388. os_memcpy(auth.da, da, ETH_ALEN);
  389. os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
  390. os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
  391. auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  392. auth.u.auth.auth_transaction = host_to_le16(seq);
  393. auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  394. wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
  395. seq, MAC2STR(da));
  396. return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
  397. auth_length, 0);
  398. }
  399. static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
  400. {
  401. return peer->authentication_status &
  402. (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
  403. }
  404. static struct ibss_rsn_peer *
  405. ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
  406. {
  407. struct ibss_rsn_peer *peer;
  408. if (ibss_rsn == NULL)
  409. return NULL;
  410. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  411. if (peer) {
  412. wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
  413. " already running", MAC2STR(addr));
  414. return peer;
  415. }
  416. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
  417. MAC2STR(addr));
  418. peer = os_zalloc(sizeof(*peer));
  419. if (peer == NULL) {
  420. wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
  421. return NULL;
  422. }
  423. peer->ibss_rsn = ibss_rsn;
  424. os_memcpy(peer->addr, addr, ETH_ALEN);
  425. peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
  426. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
  427. ibss_rsn->psk) < 0) {
  428. ibss_rsn_free(peer);
  429. return NULL;
  430. }
  431. peer->next = ibss_rsn->peers;
  432. ibss_rsn->peers = peer;
  433. return peer;
  434. }
  435. static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
  436. {
  437. struct ibss_rsn_peer *peer = eloop_ctx;
  438. /*
  439. * Assume peer does not support Authentication exchange or the frame was
  440. * lost somewhere - start EAPOL Authenticator.
  441. */
  442. wpa_printf(MSG_DEBUG,
  443. "RSN: Timeout on waiting Authentication frame response from "
  444. MACSTR " - start authenticator", MAC2STR(peer->addr));
  445. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  446. ibss_rsn_auth_init(peer->ibss_rsn, peer);
  447. }
  448. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  449. {
  450. struct ibss_rsn_peer *peer;
  451. int res;
  452. if (!ibss_rsn)
  453. return -1;
  454. /* if the peer already exists, exit immediately */
  455. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  456. if (peer)
  457. return 0;
  458. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  459. if (peer == NULL)
  460. return -1;
  461. /* Open Authentication: send first Authentication frame */
  462. res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
  463. if (res) {
  464. /*
  465. * The driver may not support Authentication frame exchange in
  466. * IBSS. Ignore authentication and go through EAPOL exchange.
  467. */
  468. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  469. return ibss_rsn_auth_init(ibss_rsn, peer);
  470. } else {
  471. os_get_reltime(&peer->own_auth_tx);
  472. eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
  473. }
  474. return 0;
  475. }
  476. static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
  477. struct ibss_rsn_peer *peer, int reason)
  478. {
  479. int already_started;
  480. if (ibss_rsn == NULL || peer == NULL)
  481. return -1;
  482. already_started = ibss_rsn_is_auth_started(peer);
  483. peer->authentication_status |= reason;
  484. if (already_started) {
  485. wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
  486. "started for peer " MACSTR, MAC2STR(peer->addr));
  487. return 0;
  488. }
  489. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
  490. "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
  491. return ibss_rsn_auth_init(ibss_rsn, peer);
  492. }
  493. void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
  494. {
  495. struct ibss_rsn_peer *peer, *prev;
  496. if (ibss_rsn == NULL)
  497. return;
  498. if (peermac == NULL) {
  499. /* remove all peers */
  500. wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
  501. peer = ibss_rsn->peers;
  502. while (peer) {
  503. prev = peer;
  504. peer = peer->next;
  505. ibss_rsn_free(prev);
  506. ibss_rsn->peers = peer;
  507. }
  508. } else {
  509. /* remove specific peer */
  510. wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
  511. __func__, MAC2STR(peermac));
  512. for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
  513. prev = peer, peer = peer->next) {
  514. if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
  515. if (prev == NULL)
  516. ibss_rsn->peers = peer->next;
  517. else
  518. prev->next = peer->next;
  519. ibss_rsn_free(peer);
  520. wpa_printf(MSG_DEBUG, "%s: Successfully "
  521. "removed a specific peer",
  522. __func__);
  523. break;
  524. }
  525. }
  526. }
  527. }
  528. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
  529. {
  530. struct ibss_rsn *ibss_rsn;
  531. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  532. if (ibss_rsn == NULL)
  533. return NULL;
  534. ibss_rsn->wpa_s = wpa_s;
  535. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
  536. ibss_rsn_deinit(ibss_rsn);
  537. return NULL;
  538. }
  539. return ibss_rsn;
  540. }
  541. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  542. {
  543. struct ibss_rsn_peer *peer, *prev;
  544. if (ibss_rsn == NULL)
  545. return;
  546. peer = ibss_rsn->peers;
  547. while (peer) {
  548. prev = peer;
  549. peer = peer->next;
  550. ibss_rsn_free(prev);
  551. }
  552. if (ibss_rsn->auth_group)
  553. wpa_deinit(ibss_rsn->auth_group);
  554. os_free(ibss_rsn);
  555. }
  556. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  557. {
  558. const struct ieee802_1x_hdr *hdr;
  559. const struct wpa_eapol_key *key;
  560. u16 key_info;
  561. size_t plen;
  562. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  563. if (len < sizeof(*hdr) + sizeof(*key))
  564. return -1;
  565. hdr = (const struct ieee802_1x_hdr *) buf;
  566. key = (const struct wpa_eapol_key *) (hdr + 1);
  567. plen = be_to_host16(hdr->length);
  568. if (hdr->version < EAPOL_VERSION) {
  569. /* TODO: backwards compatibility */
  570. }
  571. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  572. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  573. "not a Key frame", hdr->type);
  574. return -1;
  575. }
  576. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  577. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  578. "invalid (frame size %lu)",
  579. (unsigned long) plen, (unsigned long) len);
  580. return -1;
  581. }
  582. if (key->type != EAPOL_KEY_TYPE_RSN) {
  583. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  584. "discarded", key->type);
  585. return -1;
  586. }
  587. key_info = WPA_GET_BE16(key->key_info);
  588. return !!(key_info & WPA_KEY_INFO_ACK);
  589. }
  590. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  591. struct ibss_rsn_peer *peer,
  592. const u8 *buf, size_t len)
  593. {
  594. int supp;
  595. u8 *tmp;
  596. supp = ibss_rsn_eapol_dst_supp(buf, len);
  597. if (supp < 0)
  598. return -1;
  599. tmp = os_malloc(len);
  600. if (tmp == NULL)
  601. return -1;
  602. os_memcpy(tmp, buf, len);
  603. if (supp) {
  604. peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
  605. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
  606. MACSTR, MAC2STR(peer->addr));
  607. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  608. } else {
  609. if (ibss_rsn_is_auth_started(peer) == 0) {
  610. wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
  611. "Authenticator dropped as " MACSTR " is not "
  612. "authenticated", MAC2STR(peer->addr));
  613. os_free(tmp);
  614. return -1;
  615. }
  616. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
  617. "from "MACSTR, MAC2STR(peer->addr));
  618. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  619. }
  620. os_free(tmp);
  621. return 1;
  622. }
  623. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  624. const u8 *buf, size_t len)
  625. {
  626. struct ibss_rsn_peer *peer;
  627. if (ibss_rsn == NULL)
  628. return -1;
  629. peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
  630. if (peer)
  631. return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
  632. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  633. /*
  634. * Create new IBSS peer based on an EAPOL message from the peer
  635. * Authenticator.
  636. */
  637. peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
  638. if (peer == NULL)
  639. return -1;
  640. /* assume the peer is authenticated already */
  641. wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
  642. MACSTR, MAC2STR(src_addr));
  643. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  644. IBSS_RSN_AUTH_EAPOL_BY_US);
  645. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  646. buf, len);
  647. }
  648. return 0;
  649. }
  650. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  651. {
  652. if (ibss_rsn == NULL)
  653. return;
  654. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  655. }
  656. static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
  657. struct ibss_rsn_peer *peer,
  658. const u8* addr)
  659. {
  660. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
  661. MAC2STR(addr));
  662. if (peer &&
  663. peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
  664. if (peer->own_auth_tx.sec) {
  665. struct os_reltime now, diff;
  666. os_get_reltime(&now);
  667. os_reltime_sub(&now, &peer->own_auth_tx, &diff);
  668. if (diff.sec == 0 && diff.usec < 500000) {
  669. wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
  670. (int) diff.usec);
  671. goto skip_reinit;
  672. }
  673. }
  674. /*
  675. * A peer sent us an Authentication frame even though it already
  676. * started an EAPOL session. We should reinit state machines
  677. * here, but it's much more complicated than just deleting and
  678. * recreating the state machine
  679. */
  680. wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
  681. MACSTR, MAC2STR(addr));
  682. ibss_rsn_stop(ibss_rsn, addr);
  683. peer = NULL;
  684. }
  685. if (!peer) {
  686. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  687. if (!peer)
  688. return;
  689. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
  690. MAC2STR(addr));
  691. }
  692. skip_reinit:
  693. /* reply with an Authentication frame now, before sending an EAPOL */
  694. ibss_rsn_send_auth(ibss_rsn, addr, 2);
  695. /* no need to start another AUTH challenge in the other way.. */
  696. ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
  697. }
  698. void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
  699. size_t len)
  700. {
  701. const struct ieee80211_mgmt *header;
  702. struct ibss_rsn_peer *peer;
  703. size_t auth_length;
  704. header = (const struct ieee80211_mgmt *) auth_frame;
  705. auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
  706. if (ibss_rsn == NULL || len < auth_length)
  707. return;
  708. if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
  709. le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
  710. return;
  711. peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
  712. switch (le_to_host16(header->u.auth.auth_transaction)) {
  713. case 1:
  714. ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
  715. break;
  716. case 2:
  717. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
  718. MACSTR, MAC2STR(header->sa));
  719. if (!peer) {
  720. wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
  721. "unknown STA " MACSTR, MAC2STR(header->sa));
  722. break;
  723. }
  724. /* authentication has been completed */
  725. eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
  726. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
  727. MAC2STR(header->sa));
  728. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  729. IBSS_RSN_AUTH_BY_US);
  730. break;
  731. }
  732. }