mesh_mpm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * WPA Supplicant - Basic mesh peer management
  3. * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "ap/hostapd.h"
  13. #include "ap/sta_info.h"
  14. #include "ap/ieee802_11.h"
  15. #include "wpa_supplicant_i.h"
  16. #include "driver_i.h"
  17. #include "mesh_mpm.h"
  18. #include "mesh_rsn.h"
  19. struct mesh_peer_mgmt_ie {
  20. const u8 *proto_id;
  21. const u8 *llid;
  22. const u8 *plid;
  23. const u8 *reason;
  24. const u8 *pmk;
  25. };
  26. static void plink_timer(void *eloop_ctx, void *user_data);
  27. enum plink_event {
  28. PLINK_UNDEFINED,
  29. OPN_ACPT,
  30. OPN_RJCT,
  31. OPN_IGNR,
  32. CNF_ACPT,
  33. CNF_RJCT,
  34. CNF_IGNR,
  35. CLS_ACPT,
  36. CLS_IGNR
  37. };
  38. static const char * const mplstate[] = {
  39. [PLINK_LISTEN] = "LISTEN",
  40. [PLINK_OPEN_SENT] = "OPEN_SENT",
  41. [PLINK_OPEN_RCVD] = "OPEN_RCVD",
  42. [PLINK_CNF_RCVD] = "CNF_RCVD",
  43. [PLINK_ESTAB] = "ESTAB",
  44. [PLINK_HOLDING] = "HOLDING",
  45. [PLINK_BLOCKED] = "BLOCKED"
  46. };
  47. static const char * const mplevent[] = {
  48. [PLINK_UNDEFINED] = "UNDEFINED",
  49. [OPN_ACPT] = "OPN_ACPT",
  50. [OPN_RJCT] = "OPN_RJCT",
  51. [OPN_IGNR] = "OPN_IGNR",
  52. [CNF_ACPT] = "CNF_ACPT",
  53. [CNF_RJCT] = "CNF_RJCT",
  54. [CNF_IGNR] = "CNF_IGNR",
  55. [CLS_ACPT] = "CLS_ACPT",
  56. [CLS_IGNR] = "CLS_IGNR"
  57. };
  58. static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
  59. u8 action_field,
  60. const u8 *ie, size_t len,
  61. struct mesh_peer_mgmt_ie *mpm_ie)
  62. {
  63. os_memset(mpm_ie, 0, sizeof(*mpm_ie));
  64. /* remove optional PMK at end */
  65. if (len >= 16) {
  66. len -= 16;
  67. mpm_ie->pmk = ie + len - 16;
  68. }
  69. if ((action_field == PLINK_OPEN && len != 4) ||
  70. (action_field == PLINK_CONFIRM && len != 6) ||
  71. (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
  72. wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
  73. return -1;
  74. }
  75. /* required fields */
  76. if (len < 4)
  77. return -1;
  78. mpm_ie->proto_id = ie;
  79. mpm_ie->llid = ie + 2;
  80. ie += 4;
  81. len -= 4;
  82. /* close reason is always present at end for close */
  83. if (action_field == PLINK_CLOSE) {
  84. if (len < 2)
  85. return -1;
  86. mpm_ie->reason = ie + len - 2;
  87. len -= 2;
  88. }
  89. /* plid, present for confirm, and possibly close */
  90. if (len)
  91. mpm_ie->plid = ie;
  92. return 0;
  93. }
  94. static int plink_free_count(struct hostapd_data *hapd)
  95. {
  96. if (hapd->max_plinks > hapd->num_plinks)
  97. return hapd->max_plinks - hapd->num_plinks;
  98. return 0;
  99. }
  100. static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
  101. struct sta_info *sta,
  102. struct ieee802_11_elems *elems)
  103. {
  104. if (!elems->supp_rates) {
  105. wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
  106. MAC2STR(sta->addr));
  107. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  108. }
  109. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  110. sizeof(sta->supported_rates)) {
  111. wpa_msg(wpa_s, MSG_ERROR,
  112. "Invalid supported rates element length " MACSTR
  113. " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
  114. elems->ext_supp_rates_len);
  115. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  116. }
  117. sta->supported_rates_len = merge_byte_arrays(
  118. sta->supported_rates, sizeof(sta->supported_rates),
  119. elems->supp_rates, elems->supp_rates_len,
  120. elems->ext_supp_rates, elems->ext_supp_rates_len);
  121. return WLAN_STATUS_SUCCESS;
  122. }
  123. /* return true if elems from a neighbor match this MBSS */
  124. static Boolean matches_local(struct wpa_supplicant *wpa_s,
  125. struct ieee802_11_elems *elems)
  126. {
  127. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  128. if (elems->mesh_config_len < 5)
  129. return FALSE;
  130. return (mconf->meshid_len == elems->mesh_id_len &&
  131. os_memcmp(mconf->meshid, elems->mesh_id,
  132. elems->mesh_id_len) == 0 &&
  133. mconf->mesh_pp_id == elems->mesh_config[0] &&
  134. mconf->mesh_pm_id == elems->mesh_config[1] &&
  135. mconf->mesh_cc_id == elems->mesh_config[2] &&
  136. mconf->mesh_sp_id == elems->mesh_config[3] &&
  137. mconf->mesh_auth_id == elems->mesh_config[4]);
  138. }
  139. /* check if local link id is already used with another peer */
  140. static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
  141. {
  142. struct sta_info *sta;
  143. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  144. for (sta = hapd->sta_list; sta; sta = sta->next) {
  145. if (sta->my_lid == llid)
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. /* generate an llid for a link and set to initial state */
  151. static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
  152. struct sta_info *sta)
  153. {
  154. u16 llid;
  155. do {
  156. if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
  157. continue;
  158. } while (!llid || llid_in_use(wpa_s, llid));
  159. sta->my_lid = llid;
  160. sta->peer_lid = 0;
  161. sta->plink_state = PLINK_LISTEN;
  162. }
  163. static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
  164. struct sta_info *sta,
  165. enum plink_action_field type,
  166. u16 close_reason)
  167. {
  168. struct wpabuf *buf;
  169. struct hostapd_iface *ifmsh = wpa_s->ifmsh;
  170. struct hostapd_data *bss = ifmsh->bss[0];
  171. struct mesh_conf *conf = ifmsh->mconf;
  172. u8 supp_rates[2 + 2 + 32];
  173. #ifdef CONFIG_IEEE80211N
  174. u8 ht_capa_oper[2 + 26 + 2 + 22];
  175. #endif /* CONFIG_IEEE80211N */
  176. u8 *pos, *cat;
  177. u8 ie_len, add_plid = 0;
  178. int ret;
  179. int ampe = conf->security & MESH_CONF_SEC_AMPE;
  180. size_t buf_len;
  181. if (!sta)
  182. return;
  183. buf_len = 2 + /* capability info */
  184. 2 + /* AID */
  185. 2 + 8 + /* supported rates */
  186. 2 + (32 - 8) +
  187. 2 + 32 + /* mesh ID */
  188. 2 + 7 + /* mesh config */
  189. 2 + 23 + /* peering management */
  190. 2 + 96 + /* AMPE */
  191. 2 + 16; /* MIC */
  192. #ifdef CONFIG_IEEE80211N
  193. if (type != PLINK_CLOSE &&
  194. wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
  195. buf_len += 2 + 26 + /* HT capabilities */
  196. 2 + 22; /* HT operation */
  197. }
  198. #endif /* CONFIG_IEEE80211N */
  199. buf = wpabuf_alloc(buf_len);
  200. if (!buf)
  201. return;
  202. cat = wpabuf_mhead_u8(buf);
  203. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  204. wpabuf_put_u8(buf, type);
  205. if (type != PLINK_CLOSE) {
  206. /* capability info */
  207. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  208. /* aid */
  209. if (type == PLINK_CONFIRM)
  210. wpabuf_put_le16(buf, sta->peer_lid);
  211. /* IE: supp + ext. supp rates */
  212. pos = hostapd_eid_supp_rates(bss, supp_rates);
  213. pos = hostapd_eid_ext_supp_rates(bss, pos);
  214. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  215. /* IE: Mesh ID */
  216. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  217. wpabuf_put_u8(buf, conf->meshid_len);
  218. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  219. /* IE: mesh conf */
  220. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  221. wpabuf_put_u8(buf, 7);
  222. wpabuf_put_u8(buf, conf->mesh_pp_id);
  223. wpabuf_put_u8(buf, conf->mesh_pm_id);
  224. wpabuf_put_u8(buf, conf->mesh_cc_id);
  225. wpabuf_put_u8(buf, conf->mesh_sp_id);
  226. wpabuf_put_u8(buf, conf->mesh_auth_id);
  227. /* TODO: formation info */
  228. wpabuf_put_u8(buf, 0);
  229. /* always forwarding & accepting plinks for now */
  230. wpabuf_put_u8(buf, 0x1 | 0x8);
  231. } else { /* Peer closing frame */
  232. /* IE: Mesh ID */
  233. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  234. wpabuf_put_u8(buf, conf->meshid_len);
  235. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  236. }
  237. /* IE: Mesh Peering Management element */
  238. ie_len = 4;
  239. if (ampe)
  240. ie_len += PMKID_LEN;
  241. switch (type) {
  242. case PLINK_OPEN:
  243. break;
  244. case PLINK_CONFIRM:
  245. ie_len += 2;
  246. add_plid = 1;
  247. break;
  248. case PLINK_CLOSE:
  249. ie_len += 2;
  250. add_plid = 1;
  251. ie_len += 2; /* reason code */
  252. break;
  253. }
  254. wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
  255. wpabuf_put_u8(buf, ie_len);
  256. /* peering protocol */
  257. if (ampe)
  258. wpabuf_put_le16(buf, 1);
  259. else
  260. wpabuf_put_le16(buf, 0);
  261. wpabuf_put_le16(buf, sta->my_lid);
  262. if (add_plid)
  263. wpabuf_put_le16(buf, sta->peer_lid);
  264. if (type == PLINK_CLOSE)
  265. wpabuf_put_le16(buf, close_reason);
  266. if (ampe)
  267. mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
  268. wpabuf_put(buf, PMKID_LEN));
  269. #ifdef CONFIG_IEEE80211N
  270. if (type != PLINK_CLOSE &&
  271. wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
  272. pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
  273. pos = hostapd_eid_ht_operation(bss, pos);
  274. wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
  275. }
  276. #endif /* CONFIG_IEEE80211N */
  277. if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
  278. wpa_msg(wpa_s, MSG_INFO,
  279. "Mesh MPM: failed to add AMPE and MIC IE");
  280. goto fail;
  281. }
  282. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
  283. sta->addr, wpa_s->own_addr, wpa_s->own_addr,
  284. wpabuf_head(buf), wpabuf_len(buf), 0);
  285. if (ret < 0)
  286. wpa_msg(wpa_s, MSG_INFO,
  287. "Mesh MPM: failed to send peering frame");
  288. fail:
  289. wpabuf_free(buf);
  290. }
  291. /* configure peering state in ours and driver's station entry */
  292. static void
  293. wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  294. enum mesh_plink_state state)
  295. {
  296. struct hostapd_sta_add_params params;
  297. int ret;
  298. sta->plink_state = state;
  299. os_memset(&params, 0, sizeof(params));
  300. params.addr = sta->addr;
  301. params.plink_state = state;
  302. params.set = 1;
  303. wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
  304. MAC2STR(sta->addr), mplstate[state]);
  305. ret = wpa_drv_sta_add(wpa_s, &params);
  306. if (ret) {
  307. wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
  308. ": %d", MAC2STR(sta->addr), ret);
  309. }
  310. }
  311. static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
  312. struct sta_info *sta)
  313. {
  314. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  315. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  316. if (sta->mpm_close_reason == WLAN_REASON_MESH_CLOSE_RCVD) {
  317. ap_free_sta(hapd, sta);
  318. return;
  319. }
  320. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_LISTEN);
  321. sta->my_lid = sta->peer_lid = sta->mpm_close_reason = 0;
  322. sta->mpm_retries = 0;
  323. }
  324. static void plink_timer(void *eloop_ctx, void *user_data)
  325. {
  326. struct wpa_supplicant *wpa_s = eloop_ctx;
  327. struct sta_info *sta = user_data;
  328. u16 reason = 0;
  329. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  330. switch (sta->plink_state) {
  331. case PLINK_OPEN_RCVD:
  332. case PLINK_OPEN_SENT:
  333. /* retry timer */
  334. if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
  335. eloop_register_timeout(
  336. conf->dot11MeshRetryTimeout / 1000,
  337. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  338. plink_timer, wpa_s, sta);
  339. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  340. sta->mpm_retries++;
  341. break;
  342. }
  343. reason = WLAN_REASON_MESH_MAX_RETRIES;
  344. /* fall through on else */
  345. case PLINK_CNF_RCVD:
  346. /* confirm timer */
  347. if (!reason)
  348. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  349. sta->plink_state = PLINK_HOLDING;
  350. eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
  351. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  352. plink_timer, wpa_s, sta);
  353. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  354. break;
  355. case PLINK_HOLDING:
  356. /* holding timer */
  357. mesh_mpm_fsm_restart(wpa_s, sta);
  358. break;
  359. default:
  360. break;
  361. }
  362. }
  363. /* initiate peering with station */
  364. static void
  365. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  366. enum mesh_plink_state next_state)
  367. {
  368. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  369. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  370. eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
  371. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  372. plink_timer, wpa_s, sta);
  373. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  374. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  375. }
  376. int mesh_mpm_plink_close(struct hostapd_data *hapd,
  377. struct sta_info *sta, void *ctx)
  378. {
  379. struct wpa_supplicant *wpa_s = ctx;
  380. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  381. if (sta) {
  382. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  383. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  384. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  385. MAC2STR(sta->addr));
  386. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  387. return 0;
  388. }
  389. return 1;
  390. }
  391. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  392. {
  393. struct hostapd_data *hapd = ifmsh->bss[0];
  394. /* notify peers we're leaving */
  395. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  396. hapd->num_plinks = 0;
  397. hostapd_free_stas(hapd);
  398. }
  399. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  400. * ready to start AMPE */
  401. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  402. {
  403. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  404. struct hostapd_sta_add_params params;
  405. struct sta_info *sta;
  406. int ret;
  407. sta = ap_get_sta(data, addr);
  408. if (!sta) {
  409. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  410. return;
  411. }
  412. /* TODO: Should do nothing if this STA is already authenticated, but
  413. * the AP code already sets this flag. */
  414. sta->flags |= WLAN_STA_AUTH;
  415. mesh_rsn_init_ampe_sta(wpa_s, sta);
  416. os_memset(&params, 0, sizeof(params));
  417. params.addr = sta->addr;
  418. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  419. params.set = 1;
  420. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  421. MAC2STR(sta->addr));
  422. ret = wpa_drv_sta_add(wpa_s, &params);
  423. if (ret) {
  424. wpa_msg(wpa_s, MSG_ERROR,
  425. "Driver failed to set " MACSTR ": %d",
  426. MAC2STR(sta->addr), ret);
  427. }
  428. if (!sta->my_lid)
  429. mesh_mpm_init_link(wpa_s, sta);
  430. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  431. }
  432. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  433. struct ieee802_11_elems *elems)
  434. {
  435. struct hostapd_sta_add_params params;
  436. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  437. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  438. struct sta_info *sta;
  439. struct wpa_ssid *ssid = wpa_s->current_ssid;
  440. int ret = 0;
  441. sta = ap_get_sta(data, addr);
  442. if (!sta) {
  443. sta = ap_sta_add(data, addr);
  444. if (!sta)
  445. return;
  446. }
  447. /* initialize sta */
  448. if (copy_supp_rates(wpa_s, sta, elems))
  449. return;
  450. mesh_mpm_init_link(wpa_s, sta);
  451. #ifdef CONFIG_IEEE80211N
  452. copy_sta_ht_capab(data, sta, elems->ht_capabilities,
  453. elems->ht_capabilities_len);
  454. update_ht_state(data, sta);
  455. #endif /* CONFIG_IEEE80211N */
  456. /* insert into driver */
  457. os_memset(&params, 0, sizeof(params));
  458. params.supp_rates = sta->supported_rates;
  459. params.supp_rates_len = sta->supported_rates_len;
  460. params.addr = addr;
  461. params.plink_state = sta->plink_state;
  462. params.aid = sta->peer_lid;
  463. params.listen_interval = 100;
  464. params.ht_capabilities = sta->ht_capabilities;
  465. params.flags |= WPA_STA_WMM;
  466. params.flags_mask |= WPA_STA_AUTHENTICATED;
  467. if (conf->security == MESH_CONF_SEC_NONE) {
  468. params.flags |= WPA_STA_AUTHORIZED;
  469. params.flags |= WPA_STA_AUTHENTICATED;
  470. } else {
  471. sta->flags |= WLAN_STA_MFP;
  472. params.flags |= WPA_STA_MFP;
  473. }
  474. ret = wpa_drv_sta_add(wpa_s, &params);
  475. if (ret) {
  476. wpa_msg(wpa_s, MSG_ERROR,
  477. "Driver failed to insert " MACSTR ": %d",
  478. MAC2STR(addr), ret);
  479. return;
  480. }
  481. if (ssid && ssid->no_auto_peer) {
  482. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  483. MACSTR " because of no_auto_peer", MAC2STR(addr));
  484. return;
  485. }
  486. if (conf->security == MESH_CONF_SEC_NONE)
  487. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  488. else
  489. mesh_rsn_auth_sae_sta(wpa_s, sta);
  490. }
  491. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  492. {
  493. struct hostapd_frame_info fi;
  494. os_memset(&fi, 0, sizeof(fi));
  495. fi.datarate = rx_mgmt->datarate;
  496. fi.ssi_signal = rx_mgmt->ssi_signal;
  497. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  498. rx_mgmt->frame_len, &fi);
  499. }
  500. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  501. struct sta_info *sta)
  502. {
  503. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  504. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  505. u8 seq[6] = {};
  506. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  507. MAC2STR(sta->addr));
  508. if (conf->security & MESH_CONF_SEC_AMPE) {
  509. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  510. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  511. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  512. seq, sizeof(seq),
  513. sta->mgtk, sizeof(sta->mgtk));
  514. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  515. seq, sizeof(seq),
  516. sta->mgtk, sizeof(sta->mgtk));
  517. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  518. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  519. sta->mgtk, sizeof(sta->mgtk));
  520. }
  521. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  522. hapd->num_plinks++;
  523. sta->flags |= WLAN_STA_ASSOC;
  524. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  525. /* Send ctrl event */
  526. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  527. MAC2STR(sta->addr));
  528. }
  529. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  530. enum plink_event event)
  531. {
  532. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  533. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  534. u16 reason = 0;
  535. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  536. MAC2STR(sta->addr), mplstate[sta->plink_state],
  537. mplevent[event]);
  538. switch (sta->plink_state) {
  539. case PLINK_LISTEN:
  540. switch (event) {
  541. case CLS_ACPT:
  542. mesh_mpm_fsm_restart(wpa_s, sta);
  543. break;
  544. case OPN_ACPT:
  545. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  546. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  547. 0);
  548. break;
  549. default:
  550. break;
  551. }
  552. break;
  553. case PLINK_OPEN_SENT:
  554. switch (event) {
  555. case OPN_RJCT:
  556. case CNF_RJCT:
  557. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  558. /* fall-through */
  559. case CLS_ACPT:
  560. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  561. if (!reason)
  562. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  563. eloop_register_timeout(
  564. conf->dot11MeshHoldingTimeout / 1000,
  565. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  566. plink_timer, wpa_s, sta);
  567. mesh_mpm_send_plink_action(wpa_s, sta,
  568. PLINK_CLOSE, reason);
  569. break;
  570. case OPN_ACPT:
  571. /* retry timer is left untouched */
  572. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  573. mesh_mpm_send_plink_action(wpa_s, sta,
  574. PLINK_CONFIRM, 0);
  575. break;
  576. case CNF_ACPT:
  577. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  578. eloop_register_timeout(
  579. conf->dot11MeshConfirmTimeout / 1000,
  580. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  581. plink_timer, wpa_s, sta);
  582. break;
  583. default:
  584. break;
  585. }
  586. break;
  587. case PLINK_OPEN_RCVD:
  588. switch (event) {
  589. case OPN_RJCT:
  590. case CNF_RJCT:
  591. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  592. /* fall-through */
  593. case CLS_ACPT:
  594. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  595. if (!reason)
  596. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  597. eloop_register_timeout(
  598. conf->dot11MeshHoldingTimeout / 1000,
  599. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  600. plink_timer, wpa_s, sta);
  601. sta->mpm_close_reason = reason;
  602. mesh_mpm_send_plink_action(wpa_s, sta,
  603. PLINK_CLOSE, reason);
  604. break;
  605. case OPN_ACPT:
  606. mesh_mpm_send_plink_action(wpa_s, sta,
  607. PLINK_CONFIRM, 0);
  608. break;
  609. case CNF_ACPT:
  610. if (conf->security & MESH_CONF_SEC_AMPE)
  611. mesh_rsn_derive_mtk(wpa_s, sta);
  612. mesh_mpm_plink_estab(wpa_s, sta);
  613. break;
  614. default:
  615. break;
  616. }
  617. break;
  618. case PLINK_CNF_RCVD:
  619. switch (event) {
  620. case OPN_RJCT:
  621. case CNF_RJCT:
  622. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  623. /* fall-through */
  624. case CLS_ACPT:
  625. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  626. if (!reason)
  627. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  628. eloop_register_timeout(
  629. conf->dot11MeshHoldingTimeout / 1000,
  630. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  631. plink_timer, wpa_s, sta);
  632. sta->mpm_close_reason = reason;
  633. mesh_mpm_send_plink_action(wpa_s, sta,
  634. PLINK_CLOSE, reason);
  635. break;
  636. case OPN_ACPT:
  637. mesh_mpm_plink_estab(wpa_s, sta);
  638. mesh_mpm_send_plink_action(wpa_s, sta,
  639. PLINK_CONFIRM, 0);
  640. break;
  641. default:
  642. break;
  643. }
  644. break;
  645. case PLINK_ESTAB:
  646. switch (event) {
  647. case CLS_ACPT:
  648. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  649. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  650. eloop_register_timeout(
  651. conf->dot11MeshHoldingTimeout / 1000,
  652. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  653. plink_timer, wpa_s, sta);
  654. sta->mpm_close_reason = reason;
  655. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  656. " closed with reason %d",
  657. MAC2STR(sta->addr), reason);
  658. wpa_msg_ctrl(wpa_s, MSG_INFO,
  659. MESH_PEER_DISCONNECTED MACSTR,
  660. MAC2STR(sta->addr));
  661. hapd->num_plinks--;
  662. mesh_mpm_send_plink_action(wpa_s, sta,
  663. PLINK_CLOSE, reason);
  664. break;
  665. case OPN_ACPT:
  666. mesh_mpm_send_plink_action(wpa_s, sta,
  667. PLINK_CONFIRM, 0);
  668. break;
  669. default:
  670. break;
  671. }
  672. break;
  673. case PLINK_HOLDING:
  674. switch (event) {
  675. case CLS_ACPT:
  676. mesh_mpm_fsm_restart(wpa_s, sta);
  677. break;
  678. case OPN_ACPT:
  679. case CNF_ACPT:
  680. case OPN_RJCT:
  681. case CNF_RJCT:
  682. reason = sta->mpm_close_reason;
  683. mesh_mpm_send_plink_action(wpa_s, sta,
  684. PLINK_CLOSE, reason);
  685. break;
  686. default:
  687. break;
  688. }
  689. break;
  690. default:
  691. wpa_msg(wpa_s, MSG_DEBUG,
  692. "Unsupported MPM event %s for state %s",
  693. mplevent[event], mplstate[sta->plink_state]);
  694. break;
  695. }
  696. }
  697. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  698. const struct ieee80211_mgmt *mgmt, size_t len)
  699. {
  700. u8 action_field;
  701. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  702. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  703. struct sta_info *sta;
  704. u16 plid = 0, llid = 0;
  705. enum plink_event event;
  706. struct ieee802_11_elems elems;
  707. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  708. const u8 *ies;
  709. size_t ie_len;
  710. int ret;
  711. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  712. return;
  713. action_field = mgmt->u.action.u.slf_prot_action.action;
  714. ies = mgmt->u.action.u.slf_prot_action.variable;
  715. ie_len = (const u8 *) mgmt + len -
  716. mgmt->u.action.u.slf_prot_action.variable;
  717. /* at least expect mesh id and peering mgmt */
  718. if (ie_len < 2 + 2)
  719. return;
  720. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  721. ies += 2; /* capability */
  722. ie_len -= 2;
  723. }
  724. if (action_field == PLINK_CONFIRM) {
  725. ies += 2; /* aid */
  726. ie_len -= 2;
  727. }
  728. /* check for mesh peering, mesh id and mesh config IEs */
  729. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed)
  730. return;
  731. if (!elems.peer_mgmt)
  732. return;
  733. if ((action_field != PLINK_CLOSE) &&
  734. (!elems.mesh_id || !elems.mesh_config))
  735. return;
  736. if (action_field != PLINK_CLOSE && !matches_local(wpa_s, &elems))
  737. return;
  738. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  739. elems.peer_mgmt,
  740. elems.peer_mgmt_len,
  741. &peer_mgmt_ie);
  742. if (ret)
  743. return;
  744. /* the sender's llid is our plid and vice-versa */
  745. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  746. if (peer_mgmt_ie.plid)
  747. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  748. sta = ap_get_sta(hapd, mgmt->sa);
  749. if (!sta)
  750. return;
  751. #ifdef CONFIG_SAE
  752. /* peer is in sae_accepted? */
  753. if (sta->sae && sta->sae->state != SAE_ACCEPTED)
  754. return;
  755. #endif /* CONFIG_SAE */
  756. if (!sta->my_lid)
  757. mesh_mpm_init_link(wpa_s, sta);
  758. if (mconf->security & MESH_CONF_SEC_AMPE)
  759. if (mesh_rsn_process_ampe(wpa_s, sta, &elems,
  760. &mgmt->u.action.category,
  761. ies, ie_len))
  762. return;
  763. if (sta->plink_state == PLINK_BLOCKED)
  764. return;
  765. /* Now we will figure out the appropriate event... */
  766. switch (action_field) {
  767. case PLINK_OPEN:
  768. if (!plink_free_count(hapd) ||
  769. (sta->peer_lid && sta->peer_lid != plid)) {
  770. event = OPN_IGNR;
  771. } else {
  772. sta->peer_lid = plid;
  773. event = OPN_ACPT;
  774. }
  775. break;
  776. case PLINK_CONFIRM:
  777. if (!plink_free_count(hapd) ||
  778. sta->my_lid != llid ||
  779. (sta->peer_lid && sta->peer_lid != plid)) {
  780. event = CNF_IGNR;
  781. } else {
  782. if (!sta->peer_lid)
  783. sta->peer_lid = plid;
  784. event = CNF_ACPT;
  785. }
  786. break;
  787. case PLINK_CLOSE:
  788. if (sta->plink_state == PLINK_ESTAB)
  789. /* Do not check for llid or plid. This does not
  790. * follow the standard but since multiple plinks
  791. * per cand are not supported, it is necessary in
  792. * order to avoid a livelock when MP A sees an
  793. * establish peer link to MP B but MP B does not
  794. * see it. This can be caused by a timeout in
  795. * B's peer link establishment or B being
  796. * restarted.
  797. */
  798. event = CLS_ACPT;
  799. else if (sta->peer_lid != plid)
  800. event = CLS_IGNR;
  801. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  802. event = CLS_IGNR;
  803. else
  804. event = CLS_ACPT;
  805. break;
  806. default:
  807. wpa_msg(wpa_s, MSG_ERROR,
  808. "Mesh plink: unknown frame subtype");
  809. return;
  810. }
  811. mesh_mpm_fsm(wpa_s, sta, event);
  812. }
  813. /* called by ap_free_sta */
  814. void mesh_mpm_free_sta(struct sta_info *sta)
  815. {
  816. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  817. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  818. }