ibss_rsn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. os_free(ctx);
  177. return -1;
  178. }
  179. wpa_sm_set_own_addr(peer->supp, own_addr);
  180. wpa_sm_set_param(peer->supp, WPA_PARAM_RSN_ENABLED, 1);
  181. wpa_sm_set_param(peer->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  182. wpa_sm_set_param(peer->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  183. wpa_sm_set_param(peer->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  184. wpa_sm_set_param(peer->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  185. wpa_sm_set_pmk(peer->supp, psk, PMK_LEN, NULL, NULL);
  186. peer->supp_ie_len = sizeof(peer->supp_ie);
  187. if (wpa_sm_set_assoc_wpa_ie_default(peer->supp, peer->supp_ie,
  188. &peer->supp_ie_len) < 0) {
  189. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  190. " failed");
  191. return -1;
  192. }
  193. wpa_sm_notify_assoc(peer->supp, peer->addr);
  194. return 0;
  195. }
  196. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  197. const char *txt)
  198. {
  199. if (addr)
  200. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  201. MAC2STR(addr), txt);
  202. else
  203. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  204. }
  205. static const u8 * auth_get_psk(void *ctx, const u8 *addr,
  206. const u8 *p2p_dev_addr, const u8 *prev_psk,
  207. size_t *psk_len)
  208. {
  209. struct ibss_rsn *ibss_rsn = ctx;
  210. if (psk_len)
  211. *psk_len = PMK_LEN;
  212. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  213. __func__, MAC2STR(addr), prev_psk);
  214. if (prev_psk)
  215. return NULL;
  216. return ibss_rsn->psk;
  217. }
  218. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  219. size_t data_len, int encrypt)
  220. {
  221. struct ibss_rsn *ibss_rsn = ctx;
  222. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  223. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  224. "encrypt=%d)",
  225. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  226. if (wpa_s->l2)
  227. return l2_packet_send(wpa_s->l2, addr, ETH_P_EAPOL, data,
  228. data_len);
  229. return -1;
  230. }
  231. static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  232. const u8 *addr, int idx, u8 *key, size_t key_len)
  233. {
  234. struct ibss_rsn *ibss_rsn = ctx;
  235. u8 seq[6];
  236. os_memset(seq, 0, sizeof(seq));
  237. if (addr) {
  238. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
  239. " key_idx=%d)",
  240. __func__, alg, MAC2STR(addr), idx);
  241. } else {
  242. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
  243. __func__, alg, idx);
  244. }
  245. wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
  246. if (idx == 0) {
  247. if (addr) {
  248. struct ibss_rsn_peer *peer;
  249. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  250. if (peer) {
  251. peer->authentication_status |=
  252. IBSS_RSN_SET_PTK_AUTH;
  253. ibss_check_rsn_completed(peer);
  254. }
  255. }
  256. /*
  257. * In IBSS RSN, the pairwise key from the 4-way handshake
  258. * initiated by the peer with highest MAC address is used.
  259. */
  260. if (addr == NULL ||
  261. os_memcmp(ibss_rsn->wpa_s->own_addr, addr, ETH_ALEN) < 0) {
  262. wpa_printf(MSG_DEBUG, "AUTH: Do not use this PTK");
  263. return 0;
  264. }
  265. }
  266. return wpa_drv_set_key(ibss_rsn->wpa_s, alg, addr, idx,
  267. 1, seq, 6, key, key_len);
  268. }
  269. static void ibss_rsn_disconnect(void *ctx, const u8 *addr, u16 reason)
  270. {
  271. struct ibss_rsn *ibss_rsn = ctx;
  272. wpa_drv_sta_deauth(ibss_rsn->wpa_s, addr, reason);
  273. }
  274. static int auth_for_each_sta(void *ctx, int (*cb)(struct wpa_state_machine *sm,
  275. void *ctx),
  276. void *cb_ctx)
  277. {
  278. struct ibss_rsn *ibss_rsn = ctx;
  279. struct ibss_rsn_peer *peer;
  280. wpa_printf(MSG_DEBUG, "AUTH: for_each_sta");
  281. for (peer = ibss_rsn->peers; peer; peer = peer->next) {
  282. if (peer->auth && cb(peer->auth, cb_ctx))
  283. return 1;
  284. }
  285. return 0;
  286. }
  287. static void ibss_set_sta_authorized(struct ibss_rsn *ibss_rsn,
  288. struct ibss_rsn_peer *peer, int authorized)
  289. {
  290. int res;
  291. if (authorized) {
  292. res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
  293. WPA_STA_AUTHORIZED,
  294. WPA_STA_AUTHORIZED, ~0);
  295. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " authorizing port",
  296. MAC2STR(peer->addr));
  297. } else {
  298. res = wpa_drv_sta_set_flags(ibss_rsn->wpa_s, peer->addr,
  299. 0, 0, ~WPA_STA_AUTHORIZED);
  300. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " unauthorizing port",
  301. MAC2STR(peer->addr));
  302. }
  303. if (res && errno != ENOENT) {
  304. wpa_printf(MSG_DEBUG, "Could not set station " MACSTR " flags "
  305. "for kernel driver (errno=%d)",
  306. MAC2STR(peer->addr), errno);
  307. }
  308. }
  309. static void auth_set_eapol(void *ctx, const u8 *addr,
  310. wpa_eapol_variable var, int value)
  311. {
  312. struct ibss_rsn *ibss_rsn = ctx;
  313. struct ibss_rsn_peer *peer = ibss_rsn_get_peer(ibss_rsn, addr);
  314. if (peer == NULL)
  315. return;
  316. switch (var) {
  317. case WPA_EAPOL_authorized:
  318. ibss_set_sta_authorized(ibss_rsn, peer, value);
  319. break;
  320. default:
  321. /* do not handle any other event */
  322. wpa_printf(MSG_DEBUG, "AUTH: eapol event not handled %d", var);
  323. break;
  324. }
  325. }
  326. static int ibss_rsn_auth_init_group(struct ibss_rsn *ibss_rsn,
  327. const u8 *own_addr, struct wpa_ssid *ssid)
  328. {
  329. struct wpa_auth_config conf;
  330. static const struct wpa_auth_callbacks cb = {
  331. .logger = auth_logger,
  332. .set_eapol = auth_set_eapol,
  333. .send_eapol = auth_send_eapol,
  334. .get_psk = auth_get_psk,
  335. .set_key = auth_set_key,
  336. .for_each_sta = auth_for_each_sta,
  337. .disconnect = ibss_rsn_disconnect,
  338. };
  339. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  340. os_memset(&conf, 0, sizeof(conf));
  341. conf.wpa = 2;
  342. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  343. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  344. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  345. conf.wpa_group = WPA_CIPHER_CCMP;
  346. conf.eapol_version = 2;
  347. conf.wpa_group_rekey = ssid->group_rekey ? ssid->group_rekey : 600;
  348. conf.wpa_group_update_count = 4;
  349. conf.wpa_pairwise_update_count = 4;
  350. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb, ibss_rsn);
  351. if (ibss_rsn->auth_group == NULL) {
  352. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  353. return -1;
  354. }
  355. wpa_init_keys(ibss_rsn->auth_group);
  356. return 0;
  357. }
  358. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  359. struct ibss_rsn_peer *peer)
  360. {
  361. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
  362. if (peer->auth == NULL) {
  363. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  364. return -1;
  365. }
  366. /* TODO: get peer RSN IE with Probe Request */
  367. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  368. (u8 *) "\x30\x14\x01\x00"
  369. "\x00\x0f\xac\x04"
  370. "\x01\x00\x00\x0f\xac\x04"
  371. "\x01\x00\x00\x0f\xac\x02"
  372. "\x00\x00", 22, NULL, 0, NULL, 0) !=
  373. WPA_IE_OK) {
  374. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  375. return -1;
  376. }
  377. if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
  378. return -1;
  379. if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
  380. return -1;
  381. return 0;
  382. }
  383. static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
  384. {
  385. struct ieee80211_mgmt auth;
  386. const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
  387. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  388. if (wpa_s->driver->send_frame == NULL)
  389. return -1;
  390. os_memset(&auth, 0, sizeof(auth));
  391. auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  392. WLAN_FC_STYPE_AUTH);
  393. os_memcpy(auth.da, da, ETH_ALEN);
  394. os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
  395. os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
  396. auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  397. auth.u.auth.auth_transaction = host_to_le16(seq);
  398. auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  399. wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
  400. seq, MAC2STR(da));
  401. return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
  402. auth_length, 0);
  403. }
  404. static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
  405. {
  406. return peer->authentication_status &
  407. (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
  408. }
  409. static struct ibss_rsn_peer *
  410. ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
  411. {
  412. struct ibss_rsn_peer *peer;
  413. if (ibss_rsn == NULL)
  414. return NULL;
  415. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  416. if (peer) {
  417. wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
  418. " already running", MAC2STR(addr));
  419. return peer;
  420. }
  421. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
  422. MAC2STR(addr));
  423. peer = os_zalloc(sizeof(*peer));
  424. if (peer == NULL) {
  425. wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
  426. return NULL;
  427. }
  428. peer->ibss_rsn = ibss_rsn;
  429. os_memcpy(peer->addr, addr, ETH_ALEN);
  430. peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
  431. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
  432. ibss_rsn->psk) < 0) {
  433. ibss_rsn_free(peer);
  434. return NULL;
  435. }
  436. peer->next = ibss_rsn->peers;
  437. ibss_rsn->peers = peer;
  438. return peer;
  439. }
  440. static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
  441. {
  442. struct ibss_rsn_peer *peer = eloop_ctx;
  443. /*
  444. * Assume peer does not support Authentication exchange or the frame was
  445. * lost somewhere - start EAPOL Authenticator.
  446. */
  447. wpa_printf(MSG_DEBUG,
  448. "RSN: Timeout on waiting Authentication frame response from "
  449. MACSTR " - start authenticator", MAC2STR(peer->addr));
  450. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  451. ibss_rsn_auth_init(peer->ibss_rsn, peer);
  452. }
  453. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  454. {
  455. struct ibss_rsn_peer *peer;
  456. int res;
  457. if (!ibss_rsn)
  458. return -1;
  459. /* if the peer already exists, exit immediately */
  460. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  461. if (peer)
  462. return 0;
  463. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  464. if (peer == NULL)
  465. return -1;
  466. /* Open Authentication: send first Authentication frame */
  467. res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
  468. if (res) {
  469. /*
  470. * The driver may not support Authentication frame exchange in
  471. * IBSS. Ignore authentication and go through EAPOL exchange.
  472. */
  473. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  474. return ibss_rsn_auth_init(ibss_rsn, peer);
  475. } else {
  476. os_get_reltime(&peer->own_auth_tx);
  477. eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
  478. }
  479. return 0;
  480. }
  481. static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
  482. struct ibss_rsn_peer *peer, int reason)
  483. {
  484. int already_started;
  485. if (ibss_rsn == NULL || peer == NULL)
  486. return -1;
  487. already_started = ibss_rsn_is_auth_started(peer);
  488. peer->authentication_status |= reason;
  489. if (already_started) {
  490. wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
  491. "started for peer " MACSTR, MAC2STR(peer->addr));
  492. return 0;
  493. }
  494. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
  495. "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
  496. return ibss_rsn_auth_init(ibss_rsn, peer);
  497. }
  498. void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
  499. {
  500. struct ibss_rsn_peer *peer, *prev;
  501. if (ibss_rsn == NULL)
  502. return;
  503. if (peermac == NULL) {
  504. /* remove all peers */
  505. wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
  506. peer = ibss_rsn->peers;
  507. while (peer) {
  508. prev = peer;
  509. peer = peer->next;
  510. ibss_rsn_free(prev);
  511. ibss_rsn->peers = peer;
  512. }
  513. } else {
  514. /* remove specific peer */
  515. wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
  516. __func__, MAC2STR(peermac));
  517. for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
  518. prev = peer, peer = peer->next) {
  519. if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
  520. if (prev == NULL)
  521. ibss_rsn->peers = peer->next;
  522. else
  523. prev->next = peer->next;
  524. ibss_rsn_free(peer);
  525. wpa_printf(MSG_DEBUG, "%s: Successfully "
  526. "removed a specific peer",
  527. __func__);
  528. break;
  529. }
  530. }
  531. }
  532. }
  533. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s,
  534. struct wpa_ssid *ssid)
  535. {
  536. struct ibss_rsn *ibss_rsn;
  537. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  538. if (ibss_rsn == NULL)
  539. return NULL;
  540. ibss_rsn->wpa_s = wpa_s;
  541. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr, ssid) < 0) {
  542. ibss_rsn_deinit(ibss_rsn);
  543. return NULL;
  544. }
  545. return ibss_rsn;
  546. }
  547. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  548. {
  549. struct ibss_rsn_peer *peer, *prev;
  550. if (ibss_rsn == NULL)
  551. return;
  552. peer = ibss_rsn->peers;
  553. while (peer) {
  554. prev = peer;
  555. peer = peer->next;
  556. ibss_rsn_free(prev);
  557. }
  558. if (ibss_rsn->auth_group)
  559. wpa_deinit(ibss_rsn->auth_group);
  560. os_free(ibss_rsn);
  561. }
  562. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  563. {
  564. const struct ieee802_1x_hdr *hdr;
  565. const struct wpa_eapol_key *key;
  566. u16 key_info;
  567. size_t plen;
  568. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  569. if (len < sizeof(*hdr) + sizeof(*key))
  570. return -1;
  571. hdr = (const struct ieee802_1x_hdr *) buf;
  572. key = (const struct wpa_eapol_key *) (hdr + 1);
  573. plen = be_to_host16(hdr->length);
  574. if (hdr->version < EAPOL_VERSION) {
  575. /* TODO: backwards compatibility */
  576. }
  577. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  578. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  579. "not a Key frame", hdr->type);
  580. return -1;
  581. }
  582. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  583. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  584. "invalid (frame size %lu)",
  585. (unsigned long) plen, (unsigned long) len);
  586. return -1;
  587. }
  588. if (key->type != EAPOL_KEY_TYPE_RSN) {
  589. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  590. "discarded", key->type);
  591. return -1;
  592. }
  593. key_info = WPA_GET_BE16(key->key_info);
  594. return !!(key_info & WPA_KEY_INFO_ACK);
  595. }
  596. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  597. struct ibss_rsn_peer *peer,
  598. const u8 *buf, size_t len)
  599. {
  600. int supp;
  601. u8 *tmp;
  602. supp = ibss_rsn_eapol_dst_supp(buf, len);
  603. if (supp < 0)
  604. return -1;
  605. tmp = os_memdup(buf, len);
  606. if (tmp == NULL)
  607. return -1;
  608. if (supp) {
  609. peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
  610. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
  611. MACSTR, MAC2STR(peer->addr));
  612. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  613. } else {
  614. if (ibss_rsn_is_auth_started(peer) == 0) {
  615. wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
  616. "Authenticator dropped as " MACSTR " is not "
  617. "authenticated", MAC2STR(peer->addr));
  618. os_free(tmp);
  619. return -1;
  620. }
  621. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
  622. "from "MACSTR, MAC2STR(peer->addr));
  623. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  624. }
  625. os_free(tmp);
  626. return 1;
  627. }
  628. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  629. const u8 *buf, size_t len)
  630. {
  631. struct ibss_rsn_peer *peer;
  632. if (ibss_rsn == NULL)
  633. return -1;
  634. peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
  635. if (peer)
  636. return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
  637. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  638. /*
  639. * Create new IBSS peer based on an EAPOL message from the peer
  640. * Authenticator.
  641. */
  642. peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
  643. if (peer == NULL)
  644. return -1;
  645. /* assume the peer is authenticated already */
  646. wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
  647. MACSTR, MAC2STR(src_addr));
  648. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  649. IBSS_RSN_AUTH_EAPOL_BY_US);
  650. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  651. buf, len);
  652. }
  653. return 0;
  654. }
  655. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  656. {
  657. if (ibss_rsn == NULL)
  658. return;
  659. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  660. }
  661. static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
  662. struct ibss_rsn_peer *peer,
  663. const u8* addr)
  664. {
  665. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
  666. MAC2STR(addr));
  667. if (peer &&
  668. peer->authentication_status & (IBSS_RSN_SET_PTK_SUPP |
  669. IBSS_RSN_SET_PTK_AUTH)) {
  670. /* Clear the TK for this pair to allow recovery from the case
  671. * where the peer STA has restarted and lost its key while we
  672. * still have a pairwise key configured. */
  673. wpa_printf(MSG_DEBUG, "RSN: Clear pairwise key for peer "
  674. MACSTR, MAC2STR(addr));
  675. wpa_drv_set_key(ibss_rsn->wpa_s, WPA_ALG_NONE, addr, 0, 0,
  676. NULL, 0, NULL, 0);
  677. }
  678. if (peer &&
  679. peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
  680. if (peer->own_auth_tx.sec) {
  681. struct os_reltime now, diff;
  682. os_get_reltime(&now);
  683. os_reltime_sub(&now, &peer->own_auth_tx, &diff);
  684. if (diff.sec == 0 && diff.usec < 500000) {
  685. wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
  686. (int) diff.usec);
  687. goto skip_reinit;
  688. }
  689. }
  690. /*
  691. * A peer sent us an Authentication frame even though it already
  692. * started an EAPOL session. We should reinit state machines
  693. * here, but it's much more complicated than just deleting and
  694. * recreating the state machine
  695. */
  696. wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
  697. MACSTR, MAC2STR(addr));
  698. ibss_rsn_stop(ibss_rsn, addr);
  699. peer = NULL;
  700. }
  701. if (!peer) {
  702. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  703. if (!peer)
  704. return;
  705. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
  706. MAC2STR(addr));
  707. }
  708. skip_reinit:
  709. /* reply with an Authentication frame now, before sending an EAPOL */
  710. ibss_rsn_send_auth(ibss_rsn, addr, 2);
  711. /* no need to start another AUTH challenge in the other way.. */
  712. ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
  713. }
  714. void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
  715. size_t len)
  716. {
  717. const struct ieee80211_mgmt *header;
  718. struct ibss_rsn_peer *peer;
  719. size_t auth_length;
  720. header = (const struct ieee80211_mgmt *) auth_frame;
  721. auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
  722. if (ibss_rsn == NULL || len < auth_length)
  723. return;
  724. if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
  725. le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
  726. return;
  727. peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
  728. switch (le_to_host16(header->u.auth.auth_transaction)) {
  729. case 1:
  730. ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
  731. break;
  732. case 2:
  733. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
  734. MACSTR, MAC2STR(header->sa));
  735. if (!peer) {
  736. wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
  737. "unknown STA " MACSTR, MAC2STR(header->sa));
  738. break;
  739. }
  740. /* authentication has been completed */
  741. eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
  742. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
  743. MAC2STR(header->sa));
  744. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  745. IBSS_RSN_AUTH_BY_US);
  746. break;
  747. }
  748. }