ibss_rsn.c 21 KB

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