ibss_rsn.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. struct wpa_auth_callbacks cb;
  328. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  329. os_memset(&conf, 0, sizeof(conf));
  330. conf.wpa = 2;
  331. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  332. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  333. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  334. conf.wpa_group = WPA_CIPHER_CCMP;
  335. conf.eapol_version = 2;
  336. conf.wpa_group_rekey = ssid->group_rekey ? ssid->group_rekey : 600;
  337. os_memset(&cb, 0, sizeof(cb));
  338. cb.ctx = ibss_rsn;
  339. cb.logger = auth_logger;
  340. cb.set_eapol = auth_set_eapol;
  341. cb.send_eapol = auth_send_eapol;
  342. cb.get_psk = auth_get_psk;
  343. cb.set_key = auth_set_key;
  344. cb.for_each_sta = auth_for_each_sta;
  345. cb.disconnect = ibss_rsn_disconnect;
  346. ibss_rsn->auth_group = wpa_init(own_addr, &conf, &cb);
  347. if (ibss_rsn->auth_group == NULL) {
  348. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  349. return -1;
  350. }
  351. wpa_init_keys(ibss_rsn->auth_group);
  352. return 0;
  353. }
  354. static int ibss_rsn_auth_init(struct ibss_rsn *ibss_rsn,
  355. struct ibss_rsn_peer *peer)
  356. {
  357. peer->auth = wpa_auth_sta_init(ibss_rsn->auth_group, peer->addr, NULL);
  358. if (peer->auth == NULL) {
  359. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  360. return -1;
  361. }
  362. /* TODO: get peer RSN IE with Probe Request */
  363. if (wpa_validate_wpa_ie(ibss_rsn->auth_group, peer->auth,
  364. (u8 *) "\x30\x14\x01\x00"
  365. "\x00\x0f\xac\x04"
  366. "\x01\x00\x00\x0f\xac\x04"
  367. "\x01\x00\x00\x0f\xac\x02"
  368. "\x00\x00", 22, NULL, 0) !=
  369. WPA_IE_OK) {
  370. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  371. return -1;
  372. }
  373. if (wpa_auth_sm_event(peer->auth, WPA_ASSOC))
  374. return -1;
  375. if (wpa_auth_sta_associated(ibss_rsn->auth_group, peer->auth))
  376. return -1;
  377. return 0;
  378. }
  379. static int ibss_rsn_send_auth(struct ibss_rsn *ibss_rsn, const u8 *da, int seq)
  380. {
  381. struct ieee80211_mgmt auth;
  382. const size_t auth_length = IEEE80211_HDRLEN + sizeof(auth.u.auth);
  383. struct wpa_supplicant *wpa_s = ibss_rsn->wpa_s;
  384. if (wpa_s->driver->send_frame == NULL)
  385. return -1;
  386. os_memset(&auth, 0, sizeof(auth));
  387. auth.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  388. WLAN_FC_STYPE_AUTH);
  389. os_memcpy(auth.da, da, ETH_ALEN);
  390. os_memcpy(auth.sa, wpa_s->own_addr, ETH_ALEN);
  391. os_memcpy(auth.bssid, wpa_s->bssid, ETH_ALEN);
  392. auth.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
  393. auth.u.auth.auth_transaction = host_to_le16(seq);
  394. auth.u.auth.status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  395. wpa_printf(MSG_DEBUG, "RSN: IBSS TX Auth frame (SEQ %d) to " MACSTR,
  396. seq, MAC2STR(da));
  397. return wpa_s->driver->send_frame(wpa_s->drv_priv, (u8 *) &auth,
  398. auth_length, 0);
  399. }
  400. static int ibss_rsn_is_auth_started(struct ibss_rsn_peer * peer)
  401. {
  402. return peer->authentication_status &
  403. (IBSS_RSN_AUTH_BY_US | IBSS_RSN_AUTH_EAPOL_BY_US);
  404. }
  405. static struct ibss_rsn_peer *
  406. ibss_rsn_peer_init(struct ibss_rsn *ibss_rsn, const u8 *addr)
  407. {
  408. struct ibss_rsn_peer *peer;
  409. if (ibss_rsn == NULL)
  410. return NULL;
  411. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  412. if (peer) {
  413. wpa_printf(MSG_DEBUG, "RSN: IBSS Supplicant for peer "MACSTR
  414. " already running", MAC2STR(addr));
  415. return peer;
  416. }
  417. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Supplicant for peer "MACSTR,
  418. MAC2STR(addr));
  419. peer = os_zalloc(sizeof(*peer));
  420. if (peer == NULL) {
  421. wpa_printf(MSG_DEBUG, "RSN: Could not allocate memory.");
  422. return NULL;
  423. }
  424. peer->ibss_rsn = ibss_rsn;
  425. os_memcpy(peer->addr, addr, ETH_ALEN);
  426. peer->authentication_status = IBSS_RSN_AUTH_NOT_AUTHENTICATED;
  427. if (ibss_rsn_supp_init(peer, ibss_rsn->wpa_s->own_addr,
  428. ibss_rsn->psk) < 0) {
  429. ibss_rsn_free(peer);
  430. return NULL;
  431. }
  432. peer->next = ibss_rsn->peers;
  433. ibss_rsn->peers = peer;
  434. return peer;
  435. }
  436. static void ibss_rsn_auth_timeout(void *eloop_ctx, void *timeout_ctx)
  437. {
  438. struct ibss_rsn_peer *peer = eloop_ctx;
  439. /*
  440. * Assume peer does not support Authentication exchange or the frame was
  441. * lost somewhere - start EAPOL Authenticator.
  442. */
  443. wpa_printf(MSG_DEBUG,
  444. "RSN: Timeout on waiting Authentication frame response from "
  445. MACSTR " - start authenticator", MAC2STR(peer->addr));
  446. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  447. ibss_rsn_auth_init(peer->ibss_rsn, peer);
  448. }
  449. int ibss_rsn_start(struct ibss_rsn *ibss_rsn, const u8 *addr)
  450. {
  451. struct ibss_rsn_peer *peer;
  452. int res;
  453. if (!ibss_rsn)
  454. return -1;
  455. /* if the peer already exists, exit immediately */
  456. peer = ibss_rsn_get_peer(ibss_rsn, addr);
  457. if (peer)
  458. return 0;
  459. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  460. if (peer == NULL)
  461. return -1;
  462. /* Open Authentication: send first Authentication frame */
  463. res = ibss_rsn_send_auth(ibss_rsn, addr, 1);
  464. if (res) {
  465. /*
  466. * The driver may not support Authentication frame exchange in
  467. * IBSS. Ignore authentication and go through EAPOL exchange.
  468. */
  469. peer->authentication_status |= IBSS_RSN_AUTH_BY_US;
  470. return ibss_rsn_auth_init(ibss_rsn, peer);
  471. } else {
  472. os_get_reltime(&peer->own_auth_tx);
  473. eloop_register_timeout(1, 0, ibss_rsn_auth_timeout, peer, NULL);
  474. }
  475. return 0;
  476. }
  477. static int ibss_rsn_peer_authenticated(struct ibss_rsn *ibss_rsn,
  478. struct ibss_rsn_peer *peer, int reason)
  479. {
  480. int already_started;
  481. if (ibss_rsn == NULL || peer == NULL)
  482. return -1;
  483. already_started = ibss_rsn_is_auth_started(peer);
  484. peer->authentication_status |= reason;
  485. if (already_started) {
  486. wpa_printf(MSG_DEBUG, "RSN: IBSS Authenticator already "
  487. "started for peer " MACSTR, MAC2STR(peer->addr));
  488. return 0;
  489. }
  490. wpa_printf(MSG_DEBUG, "RSN: Starting IBSS Authenticator "
  491. "for now-authenticated peer " MACSTR, MAC2STR(peer->addr));
  492. return ibss_rsn_auth_init(ibss_rsn, peer);
  493. }
  494. void ibss_rsn_stop(struct ibss_rsn *ibss_rsn, const u8 *peermac)
  495. {
  496. struct ibss_rsn_peer *peer, *prev;
  497. if (ibss_rsn == NULL)
  498. return;
  499. if (peermac == NULL) {
  500. /* remove all peers */
  501. wpa_printf(MSG_DEBUG, "%s: Remove all peers", __func__);
  502. peer = ibss_rsn->peers;
  503. while (peer) {
  504. prev = peer;
  505. peer = peer->next;
  506. ibss_rsn_free(prev);
  507. ibss_rsn->peers = peer;
  508. }
  509. } else {
  510. /* remove specific peer */
  511. wpa_printf(MSG_DEBUG, "%s: Remove specific peer " MACSTR,
  512. __func__, MAC2STR(peermac));
  513. for (prev = NULL, peer = ibss_rsn->peers; peer != NULL;
  514. prev = peer, peer = peer->next) {
  515. if (os_memcmp(peermac, peer->addr, ETH_ALEN) == 0) {
  516. if (prev == NULL)
  517. ibss_rsn->peers = peer->next;
  518. else
  519. prev->next = peer->next;
  520. ibss_rsn_free(peer);
  521. wpa_printf(MSG_DEBUG, "%s: Successfully "
  522. "removed a specific peer",
  523. __func__);
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. struct ibss_rsn * ibss_rsn_init(struct wpa_supplicant *wpa_s,
  530. struct wpa_ssid *ssid)
  531. {
  532. struct ibss_rsn *ibss_rsn;
  533. ibss_rsn = os_zalloc(sizeof(*ibss_rsn));
  534. if (ibss_rsn == NULL)
  535. return NULL;
  536. ibss_rsn->wpa_s = wpa_s;
  537. if (ibss_rsn_auth_init_group(ibss_rsn, wpa_s->own_addr, ssid) < 0) {
  538. ibss_rsn_deinit(ibss_rsn);
  539. return NULL;
  540. }
  541. return ibss_rsn;
  542. }
  543. void ibss_rsn_deinit(struct ibss_rsn *ibss_rsn)
  544. {
  545. struct ibss_rsn_peer *peer, *prev;
  546. if (ibss_rsn == NULL)
  547. return;
  548. peer = ibss_rsn->peers;
  549. while (peer) {
  550. prev = peer;
  551. peer = peer->next;
  552. ibss_rsn_free(prev);
  553. }
  554. if (ibss_rsn->auth_group)
  555. wpa_deinit(ibss_rsn->auth_group);
  556. os_free(ibss_rsn);
  557. }
  558. static int ibss_rsn_eapol_dst_supp(const u8 *buf, size_t len)
  559. {
  560. const struct ieee802_1x_hdr *hdr;
  561. const struct wpa_eapol_key *key;
  562. u16 key_info;
  563. size_t plen;
  564. /* TODO: Support other EAPOL packets than just EAPOL-Key */
  565. if (len < sizeof(*hdr) + sizeof(*key))
  566. return -1;
  567. hdr = (const struct ieee802_1x_hdr *) buf;
  568. key = (const struct wpa_eapol_key *) (hdr + 1);
  569. plen = be_to_host16(hdr->length);
  570. if (hdr->version < EAPOL_VERSION) {
  571. /* TODO: backwards compatibility */
  572. }
  573. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  574. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame (type %u) discarded, "
  575. "not a Key frame", hdr->type);
  576. return -1;
  577. }
  578. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  579. wpa_printf(MSG_DEBUG, "RSN: EAPOL frame payload size %lu "
  580. "invalid (frame size %lu)",
  581. (unsigned long) plen, (unsigned long) len);
  582. return -1;
  583. }
  584. if (key->type != EAPOL_KEY_TYPE_RSN) {
  585. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key type (%d) unknown, "
  586. "discarded", key->type);
  587. return -1;
  588. }
  589. key_info = WPA_GET_BE16(key->key_info);
  590. return !!(key_info & WPA_KEY_INFO_ACK);
  591. }
  592. static int ibss_rsn_process_rx_eapol(struct ibss_rsn *ibss_rsn,
  593. struct ibss_rsn_peer *peer,
  594. const u8 *buf, size_t len)
  595. {
  596. int supp;
  597. u8 *tmp;
  598. supp = ibss_rsn_eapol_dst_supp(buf, len);
  599. if (supp < 0)
  600. return -1;
  601. tmp = os_malloc(len);
  602. if (tmp == NULL)
  603. return -1;
  604. os_memcpy(tmp, buf, len);
  605. if (supp) {
  606. peer->authentication_status |= IBSS_RSN_AUTH_EAPOL_BY_PEER;
  607. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Supplicant from "
  608. MACSTR, MAC2STR(peer->addr));
  609. wpa_sm_rx_eapol(peer->supp, peer->addr, tmp, len);
  610. } else {
  611. if (ibss_rsn_is_auth_started(peer) == 0) {
  612. wpa_printf(MSG_DEBUG, "RSN: IBSS EAPOL for "
  613. "Authenticator dropped as " MACSTR " is not "
  614. "authenticated", MAC2STR(peer->addr));
  615. os_free(tmp);
  616. return -1;
  617. }
  618. wpa_printf(MSG_DEBUG, "RSN: IBSS RX EAPOL for Authenticator "
  619. "from "MACSTR, MAC2STR(peer->addr));
  620. wpa_receive(ibss_rsn->auth_group, peer->auth, tmp, len);
  621. }
  622. os_free(tmp);
  623. return 1;
  624. }
  625. int ibss_rsn_rx_eapol(struct ibss_rsn *ibss_rsn, const u8 *src_addr,
  626. const u8 *buf, size_t len)
  627. {
  628. struct ibss_rsn_peer *peer;
  629. if (ibss_rsn == NULL)
  630. return -1;
  631. peer = ibss_rsn_get_peer(ibss_rsn, src_addr);
  632. if (peer)
  633. return ibss_rsn_process_rx_eapol(ibss_rsn, peer, buf, len);
  634. if (ibss_rsn_eapol_dst_supp(buf, len) > 0) {
  635. /*
  636. * Create new IBSS peer based on an EAPOL message from the peer
  637. * Authenticator.
  638. */
  639. peer = ibss_rsn_peer_init(ibss_rsn, src_addr);
  640. if (peer == NULL)
  641. return -1;
  642. /* assume the peer is authenticated already */
  643. wpa_printf(MSG_DEBUG, "RSN: IBSS Not using IBSS Auth for peer "
  644. MACSTR, MAC2STR(src_addr));
  645. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  646. IBSS_RSN_AUTH_EAPOL_BY_US);
  647. return ibss_rsn_process_rx_eapol(ibss_rsn, ibss_rsn->peers,
  648. buf, len);
  649. }
  650. return 0;
  651. }
  652. void ibss_rsn_set_psk(struct ibss_rsn *ibss_rsn, const u8 *psk)
  653. {
  654. if (ibss_rsn == NULL)
  655. return;
  656. os_memcpy(ibss_rsn->psk, psk, PMK_LEN);
  657. }
  658. static void ibss_rsn_handle_auth_1_of_2(struct ibss_rsn *ibss_rsn,
  659. struct ibss_rsn_peer *peer,
  660. const u8* addr)
  661. {
  662. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 1) from " MACSTR,
  663. MAC2STR(addr));
  664. if (peer &&
  665. peer->authentication_status & IBSS_RSN_AUTH_EAPOL_BY_PEER) {
  666. if (peer->own_auth_tx.sec) {
  667. struct os_reltime now, diff;
  668. os_get_reltime(&now);
  669. os_reltime_sub(&now, &peer->own_auth_tx, &diff);
  670. if (diff.sec == 0 && diff.usec < 500000) {
  671. wpa_printf(MSG_DEBUG, "RSN: Skip IBSS reinit since only %u usec from own Auth frame TX",
  672. (int) diff.usec);
  673. goto skip_reinit;
  674. }
  675. }
  676. /*
  677. * A peer sent us an Authentication frame even though it already
  678. * started an EAPOL session. We should reinit state machines
  679. * here, but it's much more complicated than just deleting and
  680. * recreating the state machine
  681. */
  682. wpa_printf(MSG_DEBUG, "RSN: IBSS Reinitializing station "
  683. MACSTR, MAC2STR(addr));
  684. ibss_rsn_stop(ibss_rsn, addr);
  685. peer = NULL;
  686. }
  687. if (!peer) {
  688. peer = ibss_rsn_peer_init(ibss_rsn, addr);
  689. if (!peer)
  690. return;
  691. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth started by peer " MACSTR,
  692. MAC2STR(addr));
  693. }
  694. skip_reinit:
  695. /* reply with an Authentication frame now, before sending an EAPOL */
  696. ibss_rsn_send_auth(ibss_rsn, addr, 2);
  697. /* no need to start another AUTH challenge in the other way.. */
  698. ibss_rsn_peer_authenticated(ibss_rsn, peer, IBSS_RSN_AUTH_EAPOL_BY_US);
  699. }
  700. void ibss_rsn_handle_auth(struct ibss_rsn *ibss_rsn, const u8 *auth_frame,
  701. size_t len)
  702. {
  703. const struct ieee80211_mgmt *header;
  704. struct ibss_rsn_peer *peer;
  705. size_t auth_length;
  706. header = (const struct ieee80211_mgmt *) auth_frame;
  707. auth_length = IEEE80211_HDRLEN + sizeof(header->u.auth);
  708. if (ibss_rsn == NULL || len < auth_length)
  709. return;
  710. if (le_to_host16(header->u.auth.auth_alg) != WLAN_AUTH_OPEN ||
  711. le_to_host16(header->u.auth.status_code) != WLAN_STATUS_SUCCESS)
  712. return;
  713. peer = ibss_rsn_get_peer(ibss_rsn, header->sa);
  714. switch (le_to_host16(header->u.auth.auth_transaction)) {
  715. case 1:
  716. ibss_rsn_handle_auth_1_of_2(ibss_rsn, peer, header->sa);
  717. break;
  718. case 2:
  719. wpa_printf(MSG_DEBUG, "RSN: IBSS RX Auth frame (SEQ 2) from "
  720. MACSTR, MAC2STR(header->sa));
  721. if (!peer) {
  722. wpa_printf(MSG_DEBUG, "RSN: Received Auth seq 2 from "
  723. "unknown STA " MACSTR, MAC2STR(header->sa));
  724. break;
  725. }
  726. /* authentication has been completed */
  727. eloop_cancel_timeout(ibss_rsn_auth_timeout, peer, NULL);
  728. wpa_printf(MSG_DEBUG, "RSN: IBSS Auth completed with " MACSTR,
  729. MAC2STR(header->sa));
  730. ibss_rsn_peer_authenticated(ibss_rsn, peer,
  731. IBSS_RSN_AUTH_BY_US);
  732. break;
  733. }
  734. }