ibss_rsn.c 21 KB

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