ibss_rsn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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 wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
  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);
  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 wpa_drv_send_eapol(wpa_s, addr, ETH_P_EAPOL, data, data_len);
  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 the peer already exists, exit immediately */
  453. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  454. if (peer)
  455. return 0;
  456. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  457. if (peer == NULL)
  458. return -1;
  459. /* Open Authentication: send first Authentication frame */
  460. res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
  461. if (res) {
  462. /*
  463. * The driver may not support Authentication frame exchange in
  464. * IBSS. Ignore authentication and go through EAPOL exchange.
  465. */
  466. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  467. return ibss_rsn_auth_init(ibss_rsn, peer);
  468. } else {
  469. os_get_reltime(&peer->own_auth_tx);
  470. eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
  471. }
  472. return 0;
  473. }
  474. static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
  475. struct ibss_rsn_peer *peer, int reason)
  476. {
  477. int already_started;
  478. if (ibss_rsn == NULL || peer == NULL)
  479. return -1;
  480. already_started = ibss_rsn_is_auth_started(peer);
  481. peer->authentication_status |= reason;
  482. if (already_started) {
  483. wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
  484. "started for peer " MACSTR, MAC2STR(peer->addr));
  485. return 0;
  486. }
  487. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
  488. "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
  489. return ibss_rsn_auth_init(ibss_rsn, peer);
  490. }
  491. void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
  492. {
  493. struct ibss_rsn_peer *peer, *prev;
  494. if (ibss_rsn == NULL)
  495. return;
  496. if (peermac == NULL) {
  497. /* remove all peers */
  498. wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
  499. peer = ibss_rsn->peers;
  500. while (peer) {
  501. prev = peer;
  502. peer = peer->next;
  503. ibss_rsn_free(prev);
  504. ibss_rsn->peers = peer;
  505. }
  506. } else {
  507. /* remove specific peer */
  508. wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
  509. __func__, MAC2STR(peermac));
  510. for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
  511. prev = peer, peer = peer->next) {
  512. if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
  513. if (prev == NULL)
  514. ibss_rsn->peers = peer->next;
  515. else
  516. prev->next = peer->next;
  517. ibss_rsn_free(peer);
  518. wpa_printf(MSG_DEBUG, "%s: Successfully "
  519. "removed a specific peer",
  520. __func__);
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s)
  527. {
  528. struct ibss_rsn *ibss_rsn;
  529. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  530. if (ibss_rsn == NULL)
  531. return NULL;
  532. ibss_rsn->wpa_s = wpa_s;
  533. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr) < 0) {
  534. ibss_rsn_deinit(ibss_rsn);
  535. return NULL;
  536. }
  537. return ibss_rsn;
  538. }
  539. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  540. {
  541. struct ibss_rsn_peer *peer, *prev;
  542. if (ibss_rsn == NULL)
  543. return;
  544. peer = ibss_rsn->peers;
  545. while (peer) {
  546. prev = peer;
  547. peer = peer->next;
  548. ibss_rsn_free(prev);
  549. }
  550. wpa_deinit(ibss_rsn->auth_group);
  551. os_free(ibss_rsn);
  552. }
  553. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  554. {
  555. const struct ieee802_1x_hdr *hdr;
  556. const struct wpa_eapol_key *key;
  557. u16 key_info;
  558. size_t plen;
  559. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  560. if (len < sizeof(*hdr) + sizeof(*key))
  561. return -1;
  562. hdr = (const struct ieee802_1x_hdr *) buf;
  563. key = (const struct wpa_eapol_key *) (hdr + 1);
  564. plen = be_to_host16(hdr->length);
  565. if (hdr->version < EAPOL_VERSION) {
  566. /* TODO: backwards compatibility */
  567. }
  568. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  569. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  570. "not a Key frame", hdr->type);
  571. return -1;
  572. }
  573. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  574. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  575. "invalid (frame size %lu)",
  576. (unsigned long) plen, (unsigned long) len);
  577. return -1;
  578. }
  579. if (key->type != EAPOL_KEY_TYPE_RSN) {
  580. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  581. "discarded", key->type);
  582. return -1;
  583. }
  584. key_info = WPA_GET_BE16(key->key_info);
  585. return !!(key_info & WPA_KEY_INFO_ACK);
  586. }
  587. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  588. struct ibss_rsn_peer *peer,
  589. const u8 *buf, size_t len)
  590. {
  591. int supp;
  592. u8 *tmp;
  593. supp = ibss_rsn_eapol_dst_supp(buf, len);
  594. if (supp < 0)
  595. return -1;
  596. tmp = os_malloc(len);
  597. if (tmp == NULL)
  598. return -1;
  599. os_memcpy(tmp, buf, len);
  600. if (supp) {
  601. peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
  602. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
  603. MACSTR, MAC2STR(peer->addr));
  604. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  605. } else {
  606. if (ibss_rsn_is_auth_started(peer) == 0) {
  607. wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
  608. "Authenticator dropped as " MACSTR " is not "
  609. "authenticated", MAC2STR(peer->addr));
  610. os_free(tmp);
  611. return -1;
  612. }
  613. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
  614. "from "MACSTR, MAC2STR(peer->addr));
  615. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  616. }
  617. os_free(tmp);
  618. return 1;
  619. }
  620. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  621. const u8 *buf, size_t len)
  622. {
  623. struct ibss_rsn_peer *peer;
  624. if (ibss_rsn == NULL)
  625. return -1;
  626. peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
  627. if (peer)
  628. return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
  629. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  630. /*
  631. * Create new IBSS peer based on an EAPOL message from the peer
  632. * Authenticator.
  633. */
  634. peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
  635. if (peer == NULL)
  636. return -1;
  637. /* assume the peer is authenticated already */
  638. wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
  639. MACSTR, MAC2STR(src_addr));
  640. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  641. IBSS_RSN_AUTH_EAPOL_BY_US);
  642. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  643. buf, len);
  644. }
  645. return 0;
  646. }
  647. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  648. {
  649. if (ibss_rsn == NULL)
  650. return;
  651. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  652. }
  653. static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
  654. struct ibss_rsn_peer *peer,
  655. const u8* addr)
  656. {
  657. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
  658. MAC2STR(addr));
  659. if (peer &&
  660. peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
  661. if (peer->own_auth_tx.sec) {
  662. struct os_reltime now, diff;
  663. os_get_reltime(&now);
  664. os_reltime_sub(&now, &peer->own_auth_tx, &diff);
  665. if (diff.sec == 0 && diff.usec < 500000) {
  666. wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
  667. (int) diff.usec);
  668. goto skip_reinit;
  669. }
  670. }
  671. /*
  672. * A peer sent us an Authentication frame even though it already
  673. * started an EAPOL session. We should reinit state machines
  674. * here, but it's much more complicated than just deleting and
  675. * recreating the state machine
  676. */
  677. wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
  678. MACSTR, MAC2STR(addr));
  679. ibss_rsn_stop(ibss_rsn, addr);
  680. peer = NULL;
  681. }
  682. if (!peer) {
  683. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  684. if (!peer)
  685. return;
  686. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
  687. MAC2STR(addr));
  688. }
  689. skip_reinit:
  690. /* reply with an Authentication frame now, before sending an EAPOL */
  691. ibss_rsn_send_auth(ibss_rsn, addr, 2);
  692. /* no need to start another AUTH challenge in the other way.. */
  693. ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
  694. }
  695. void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
  696. size_t len)
  697. {
  698. const struct ieee80211_mgmt *header;
  699. struct ibss_rsn_peer *peer;
  700. size_t auth_length;
  701. header = (const struct ieee80211_mgmt *) auth_frame;
  702. auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
  703. if (ibss_rsn == NULL || len < auth_length)
  704. return;
  705. if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
  706. le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
  707. return;
  708. peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
  709. switch (le_to_host16(header->u.auth.auth_transaction)) {
  710. case 1:
  711. ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
  712. break;
  713. case 2:
  714. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
  715. MACSTR, MAC2STR(header->sa));
  716. if (!peer) {
  717. wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
  718. "unknown STA " MACSTR, MAC2STR(header->sa));
  719. break;
  720. }
  721. /* authentication has been completed */
  722. eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
  723. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
  724. MAC2STR(header->sa));
  725. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  726. IBSS_RSN_AUTH_BY_US);
  727. break;
  728. }
  729. }