ibss_rsn.c 23 KB

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