mesh_mpm.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 "ap/wpa_auth.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "driver_i.h"
  18. #include "mesh_mpm.h"
  19. #include "mesh_rsn.h"
  20. struct mesh_peer_mgmt_ie {
  21. const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
  22. const u8 *llid; /* Local Link ID (2 octets) */
  23. const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
  24. const u8 *reason; /* Reason Code (conditional, 2 octets) */
  25. const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
  26. };
  27. static void plink_timer(void *eloop_ctx, void *user_data);
  28. enum plink_event {
  29. PLINK_UNDEFINED,
  30. OPN_ACPT,
  31. OPN_RJCT,
  32. OPN_IGNR,
  33. CNF_ACPT,
  34. CNF_RJCT,
  35. CNF_IGNR,
  36. CLS_ACPT,
  37. CLS_IGNR
  38. };
  39. static const char * const mplstate[] = {
  40. [0] = "UNINITIALIZED",
  41. [PLINK_LISTEN] = "LISTEN",
  42. [PLINK_OPEN_SENT] = "OPEN_SENT",
  43. [PLINK_OPEN_RCVD] = "OPEN_RCVD",
  44. [PLINK_CNF_RCVD] = "CNF_RCVD",
  45. [PLINK_ESTAB] = "ESTAB",
  46. [PLINK_HOLDING] = "HOLDING",
  47. [PLINK_BLOCKED] = "BLOCKED"
  48. };
  49. static const char * const mplevent[] = {
  50. [PLINK_UNDEFINED] = "UNDEFINED",
  51. [OPN_ACPT] = "OPN_ACPT",
  52. [OPN_RJCT] = "OPN_RJCT",
  53. [OPN_IGNR] = "OPN_IGNR",
  54. [CNF_ACPT] = "CNF_ACPT",
  55. [CNF_RJCT] = "CNF_RJCT",
  56. [CNF_IGNR] = "CNF_IGNR",
  57. [CLS_ACPT] = "CLS_ACPT",
  58. [CLS_IGNR] = "CLS_IGNR"
  59. };
  60. static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
  61. u8 action_field,
  62. const u8 *ie, size_t len,
  63. struct mesh_peer_mgmt_ie *mpm_ie)
  64. {
  65. os_memset(mpm_ie, 0, sizeof(*mpm_ie));
  66. /* Remove optional Chosen PMK field at end */
  67. if (len >= SAE_PMKID_LEN) {
  68. mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
  69. len -= SAE_PMKID_LEN;
  70. }
  71. if ((action_field == PLINK_OPEN && len != 4) ||
  72. (action_field == PLINK_CONFIRM && len != 6) ||
  73. (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
  74. wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
  75. return -1;
  76. }
  77. /* required fields */
  78. if (len < 4)
  79. return -1;
  80. mpm_ie->proto_id = ie;
  81. mpm_ie->llid = ie + 2;
  82. ie += 4;
  83. len -= 4;
  84. /* close reason is always present at end for close */
  85. if (action_field == PLINK_CLOSE) {
  86. if (len < 2)
  87. return -1;
  88. mpm_ie->reason = ie + len - 2;
  89. len -= 2;
  90. }
  91. /* Peer Link ID, present for confirm, and possibly close */
  92. if (len >= 2)
  93. mpm_ie->plid = ie;
  94. return 0;
  95. }
  96. static int plink_free_count(struct hostapd_data *hapd)
  97. {
  98. if (hapd->max_plinks > hapd->num_plinks)
  99. return hapd->max_plinks - hapd->num_plinks;
  100. return 0;
  101. }
  102. static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
  103. struct sta_info *sta,
  104. struct ieee802_11_elems *elems)
  105. {
  106. if (!elems->supp_rates) {
  107. wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
  108. MAC2STR(sta->addr));
  109. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  110. }
  111. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  112. sizeof(sta->supported_rates)) {
  113. wpa_msg(wpa_s, MSG_ERROR,
  114. "Invalid supported rates element length " MACSTR
  115. " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
  116. elems->ext_supp_rates_len);
  117. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  118. }
  119. sta->supported_rates_len = merge_byte_arrays(
  120. sta->supported_rates, sizeof(sta->supported_rates),
  121. elems->supp_rates, elems->supp_rates_len,
  122. elems->ext_supp_rates, elems->ext_supp_rates_len);
  123. return WLAN_STATUS_SUCCESS;
  124. }
  125. /* return true if elems from a neighbor match this MBSS */
  126. static Boolean matches_local(struct wpa_supplicant *wpa_s,
  127. struct ieee802_11_elems *elems)
  128. {
  129. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  130. if (elems->mesh_config_len < 5)
  131. return FALSE;
  132. return (mconf->meshid_len == elems->mesh_id_len &&
  133. os_memcmp(mconf->meshid, elems->mesh_id,
  134. elems->mesh_id_len) == 0 &&
  135. mconf->mesh_pp_id == elems->mesh_config[0] &&
  136. mconf->mesh_pm_id == elems->mesh_config[1] &&
  137. mconf->mesh_cc_id == elems->mesh_config[2] &&
  138. mconf->mesh_sp_id == elems->mesh_config[3] &&
  139. mconf->mesh_auth_id == elems->mesh_config[4]);
  140. }
  141. /* check if local link id is already used with another peer */
  142. static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
  143. {
  144. struct sta_info *sta;
  145. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  146. for (sta = hapd->sta_list; sta; sta = sta->next) {
  147. if (sta->my_lid == llid)
  148. return TRUE;
  149. }
  150. return FALSE;
  151. }
  152. /* generate an llid for a link and set to initial state */
  153. static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
  154. struct sta_info *sta)
  155. {
  156. u16 llid;
  157. do {
  158. if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
  159. continue;
  160. } while (!llid || llid_in_use(wpa_s, llid));
  161. sta->my_lid = llid;
  162. sta->peer_lid = 0;
  163. /*
  164. * We do not use wpa_mesh_set_plink_state() here because there is no
  165. * entry in kernel yet.
  166. */
  167. sta->plink_state = PLINK_LISTEN;
  168. }
  169. static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
  170. struct sta_info *sta,
  171. enum plink_action_field type,
  172. u16 close_reason)
  173. {
  174. struct wpabuf *buf;
  175. struct hostapd_iface *ifmsh = wpa_s->ifmsh;
  176. struct hostapd_data *bss = ifmsh->bss[0];
  177. struct mesh_conf *conf = ifmsh->mconf;
  178. u8 supp_rates[2 + 2 + 32];
  179. u8 *pos, *cat;
  180. u8 ie_len, add_plid = 0;
  181. int ret;
  182. int ampe = conf->security & MESH_CONF_SEC_AMPE;
  183. size_t buf_len;
  184. if (!sta)
  185. return;
  186. buf_len = 2 + /* capability info */
  187. 2 + /* AID */
  188. 2 + 8 + /* supported rates */
  189. 2 + (32 - 8) +
  190. 2 + 32 + /* mesh ID */
  191. 2 + 7 + /* mesh config */
  192. 2 + 23 + /* peering management */
  193. 2 + 96 + /* AMPE */
  194. 2 + 16; /* MIC */
  195. #ifdef CONFIG_IEEE80211N
  196. if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
  197. buf_len += 2 + 26 + /* HT capabilities */
  198. 2 + 22; /* HT operation */
  199. }
  200. #endif /* CONFIG_IEEE80211N */
  201. #ifdef CONFIG_IEEE80211AC
  202. if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
  203. buf_len += 2 + 12 + /* VHT Capabilities */
  204. 2 + 5; /* VHT Operation */
  205. }
  206. #endif /* CONFIG_IEEE80211AC */
  207. if (type != PLINK_CLOSE)
  208. buf_len += conf->rsn_ie_len; /* RSN IE */
  209. buf = wpabuf_alloc(buf_len);
  210. if (!buf)
  211. return;
  212. cat = wpabuf_mhead_u8(buf);
  213. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  214. wpabuf_put_u8(buf, type);
  215. if (type != PLINK_CLOSE) {
  216. u8 info;
  217. /* capability info */
  218. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  219. /* aid */
  220. if (type == PLINK_CONFIRM)
  221. wpabuf_put_le16(buf, sta->aid);
  222. /* IE: supp + ext. supp rates */
  223. pos = hostapd_eid_supp_rates(bss, supp_rates);
  224. pos = hostapd_eid_ext_supp_rates(bss, pos);
  225. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  226. /* IE: RSN IE */
  227. wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
  228. /* IE: Mesh ID */
  229. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  230. wpabuf_put_u8(buf, conf->meshid_len);
  231. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  232. /* IE: mesh conf */
  233. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  234. wpabuf_put_u8(buf, 7);
  235. wpabuf_put_u8(buf, conf->mesh_pp_id);
  236. wpabuf_put_u8(buf, conf->mesh_pm_id);
  237. wpabuf_put_u8(buf, conf->mesh_cc_id);
  238. wpabuf_put_u8(buf, conf->mesh_sp_id);
  239. wpabuf_put_u8(buf, conf->mesh_auth_id);
  240. info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
  241. /* TODO: Add Connected to Mesh Gate/AS subfields */
  242. wpabuf_put_u8(buf, info);
  243. /* always forwarding & accepting plinks for now */
  244. wpabuf_put_u8(buf, 0x1 | 0x8);
  245. } else { /* Peer closing frame */
  246. /* IE: Mesh ID */
  247. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  248. wpabuf_put_u8(buf, conf->meshid_len);
  249. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  250. }
  251. /* IE: Mesh Peering Management element */
  252. ie_len = 4;
  253. if (ampe)
  254. ie_len += PMKID_LEN;
  255. switch (type) {
  256. case PLINK_OPEN:
  257. break;
  258. case PLINK_CONFIRM:
  259. ie_len += 2;
  260. add_plid = 1;
  261. break;
  262. case PLINK_CLOSE:
  263. ie_len += 2;
  264. add_plid = 1;
  265. ie_len += 2; /* reason code */
  266. break;
  267. }
  268. wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
  269. wpabuf_put_u8(buf, ie_len);
  270. /* peering protocol */
  271. if (ampe)
  272. wpabuf_put_le16(buf, 1);
  273. else
  274. wpabuf_put_le16(buf, 0);
  275. wpabuf_put_le16(buf, sta->my_lid);
  276. if (add_plid)
  277. wpabuf_put_le16(buf, sta->peer_lid);
  278. if (type == PLINK_CLOSE)
  279. wpabuf_put_le16(buf, close_reason);
  280. if (ampe) {
  281. if (sta->sae == NULL) {
  282. wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
  283. goto fail;
  284. }
  285. mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
  286. wpabuf_put(buf, PMKID_LEN));
  287. }
  288. #ifdef CONFIG_IEEE80211N
  289. if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
  290. u8 ht_capa_oper[2 + 26 + 2 + 22];
  291. pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
  292. pos = hostapd_eid_ht_operation(bss, pos);
  293. wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
  294. }
  295. #endif /* CONFIG_IEEE80211N */
  296. #ifdef CONFIG_IEEE80211AC
  297. if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
  298. u8 vht_capa_oper[2 + 12 + 2 + 5];
  299. pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper);
  300. pos = hostapd_eid_vht_operation(bss, pos);
  301. wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
  302. }
  303. #endif /* CONFIG_IEEE80211AC */
  304. if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
  305. wpa_msg(wpa_s, MSG_INFO,
  306. "Mesh MPM: failed to add AMPE and MIC IE");
  307. goto fail;
  308. }
  309. wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
  310. MACSTR " (my_lid=0x%x peer_lid=0x%x)",
  311. type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
  312. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
  313. sta->addr, wpa_s->own_addr, wpa_s->own_addr,
  314. wpabuf_head(buf), wpabuf_len(buf), 0);
  315. if (ret < 0)
  316. wpa_msg(wpa_s, MSG_INFO,
  317. "Mesh MPM: failed to send peering frame");
  318. fail:
  319. wpabuf_free(buf);
  320. }
  321. /* configure peering state in ours and driver's station entry */
  322. void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
  323. struct sta_info *sta,
  324. enum mesh_plink_state state)
  325. {
  326. struct hostapd_sta_add_params params;
  327. int ret;
  328. wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
  329. MAC2STR(sta->addr), mplstate[sta->plink_state],
  330. mplstate[state]);
  331. sta->plink_state = state;
  332. os_memset(&params, 0, sizeof(params));
  333. params.addr = sta->addr;
  334. params.plink_state = state;
  335. params.set = 1;
  336. ret = wpa_drv_sta_add(wpa_s, &params);
  337. if (ret) {
  338. wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
  339. ": %d", MAC2STR(sta->addr), ret);
  340. }
  341. }
  342. static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
  343. struct sta_info *sta)
  344. {
  345. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  346. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  347. ap_free_sta(hapd, sta);
  348. }
  349. static void plink_timer(void *eloop_ctx, void *user_data)
  350. {
  351. struct wpa_supplicant *wpa_s = eloop_ctx;
  352. struct sta_info *sta = user_data;
  353. u16 reason = 0;
  354. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  355. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  356. switch (sta->plink_state) {
  357. case PLINK_OPEN_RCVD:
  358. case PLINK_OPEN_SENT:
  359. /* retry timer */
  360. if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
  361. eloop_register_timeout(
  362. conf->dot11MeshRetryTimeout / 1000,
  363. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  364. plink_timer, wpa_s, sta);
  365. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  366. sta->mpm_retries++;
  367. break;
  368. }
  369. reason = WLAN_REASON_MESH_MAX_RETRIES;
  370. /* fall through on else */
  371. case PLINK_CNF_RCVD:
  372. /* confirm timer */
  373. if (!reason)
  374. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  375. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  376. eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
  377. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  378. plink_timer, wpa_s, sta);
  379. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  380. break;
  381. case PLINK_HOLDING:
  382. /* holding timer */
  383. if (sta->mesh_sae_pmksa_caching) {
  384. wpa_printf(MSG_DEBUG, "MPM: Peer " MACSTR
  385. " looks like it does not support mesh SAE PMKSA caching, so remove the cached entry for it",
  386. MAC2STR(sta->addr));
  387. wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
  388. }
  389. mesh_mpm_fsm_restart(wpa_s, sta);
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. /* initiate peering with station */
  396. static void
  397. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  398. enum mesh_plink_state next_state)
  399. {
  400. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  401. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  402. eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
  403. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  404. plink_timer, wpa_s, sta);
  405. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  406. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  407. }
  408. static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
  409. void *ctx)
  410. {
  411. struct wpa_supplicant *wpa_s = ctx;
  412. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  413. if (sta) {
  414. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  415. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  416. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  417. MAC2STR(sta->addr));
  418. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  419. return 0;
  420. }
  421. return 1;
  422. }
  423. int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  424. {
  425. struct hostapd_data *hapd;
  426. struct sta_info *sta;
  427. if (!wpa_s->ifmsh) {
  428. wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
  429. return -1;
  430. }
  431. hapd = wpa_s->ifmsh->bss[0];
  432. sta = ap_get_sta(hapd, addr);
  433. if (!sta) {
  434. wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
  435. return -1;
  436. }
  437. return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
  438. }
  439. static void peer_add_timer(void *eloop_ctx, void *user_data)
  440. {
  441. struct wpa_supplicant *wpa_s = eloop_ctx;
  442. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  443. os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
  444. }
  445. int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  446. int duration)
  447. {
  448. struct wpa_ssid *ssid = wpa_s->current_ssid;
  449. struct hostapd_data *hapd;
  450. struct sta_info *sta;
  451. struct mesh_conf *conf;
  452. if (!wpa_s->ifmsh) {
  453. wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
  454. return -1;
  455. }
  456. if (!ssid || !ssid->no_auto_peer) {
  457. wpa_msg(wpa_s, MSG_INFO,
  458. "This command is available only with no_auto_peer mesh network");
  459. return -1;
  460. }
  461. hapd = wpa_s->ifmsh->bss[0];
  462. conf = wpa_s->ifmsh->mconf;
  463. sta = ap_get_sta(hapd, addr);
  464. if (!sta) {
  465. wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
  466. return -1;
  467. }
  468. if ((PLINK_OPEN_SENT <= sta->plink_state &&
  469. sta->plink_state <= PLINK_ESTAB) ||
  470. (sta->sae && sta->sae->state > SAE_NOTHING)) {
  471. wpa_msg(wpa_s, MSG_INFO,
  472. "Specified peer is connecting/connected");
  473. return -1;
  474. }
  475. if (conf->security == MESH_CONF_SEC_NONE) {
  476. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  477. } else {
  478. mesh_rsn_auth_sae_sta(wpa_s, sta);
  479. os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
  480. eloop_register_timeout(duration == -1 ? 10 : duration, 0,
  481. peer_add_timer, wpa_s, NULL);
  482. }
  483. return 0;
  484. }
  485. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  486. {
  487. struct hostapd_data *hapd = ifmsh->bss[0];
  488. /* notify peers we're leaving */
  489. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  490. hapd->num_plinks = 0;
  491. hostapd_free_stas(hapd);
  492. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  493. }
  494. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  495. * ready to start AMPE */
  496. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  497. {
  498. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  499. struct hostapd_sta_add_params params;
  500. struct sta_info *sta;
  501. int ret;
  502. sta = ap_get_sta(data, addr);
  503. if (!sta) {
  504. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  505. return;
  506. }
  507. /* TODO: Should do nothing if this STA is already authenticated, but
  508. * the AP code already sets this flag. */
  509. sta->flags |= WLAN_STA_AUTH;
  510. mesh_rsn_init_ampe_sta(wpa_s, sta);
  511. os_memset(&params, 0, sizeof(params));
  512. params.addr = sta->addr;
  513. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  514. params.set = 1;
  515. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  516. MAC2STR(sta->addr));
  517. ret = wpa_drv_sta_add(wpa_s, &params);
  518. if (ret) {
  519. wpa_msg(wpa_s, MSG_ERROR,
  520. "Driver failed to set " MACSTR ": %d",
  521. MAC2STR(sta->addr), ret);
  522. }
  523. if (!sta->my_lid)
  524. mesh_mpm_init_link(wpa_s, sta);
  525. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  526. }
  527. /*
  528. * Initialize a sta_info structure for a peer and upload it into the driver
  529. * in preparation for beginning authentication or peering. This is done when a
  530. * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
  531. * received from the peer for the first time.
  532. */
  533. static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
  534. const u8 *addr,
  535. struct ieee802_11_elems *elems)
  536. {
  537. struct hostapd_sta_add_params params;
  538. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  539. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  540. struct sta_info *sta;
  541. int ret;
  542. sta = ap_get_sta(data, addr);
  543. if (!sta) {
  544. sta = ap_sta_add(data, addr);
  545. if (!sta)
  546. return NULL;
  547. }
  548. /* Set WMM by default since Mesh STAs are QoS STAs */
  549. sta->flags |= WLAN_STA_WMM;
  550. /* initialize sta */
  551. if (copy_supp_rates(wpa_s, sta, elems)) {
  552. ap_free_sta(data, sta);
  553. return NULL;
  554. }
  555. if (!sta->my_lid)
  556. mesh_mpm_init_link(wpa_s, sta);
  557. #ifdef CONFIG_IEEE80211N
  558. copy_sta_ht_capab(data, sta, elems->ht_capabilities);
  559. update_ht_state(data, sta);
  560. #endif /* CONFIG_IEEE80211N */
  561. #ifdef CONFIG_IEEE80211AC
  562. copy_sta_vht_capab(data, sta, elems->vht_capabilities);
  563. set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
  564. #endif /* CONFIG_IEEE80211AC */
  565. if (hostapd_get_aid(data, sta) < 0) {
  566. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  567. ap_free_sta(data, sta);
  568. return NULL;
  569. }
  570. /* insert into driver */
  571. os_memset(&params, 0, sizeof(params));
  572. params.supp_rates = sta->supported_rates;
  573. params.supp_rates_len = sta->supported_rates_len;
  574. params.addr = addr;
  575. params.plink_state = sta->plink_state;
  576. params.aid = sta->aid;
  577. params.listen_interval = 100;
  578. params.ht_capabilities = sta->ht_capabilities;
  579. params.vht_capabilities = sta->vht_capabilities;
  580. params.flags |= WPA_STA_WMM;
  581. params.flags_mask |= WPA_STA_AUTHENTICATED;
  582. if (conf->security == MESH_CONF_SEC_NONE) {
  583. params.flags |= WPA_STA_AUTHORIZED;
  584. params.flags |= WPA_STA_AUTHENTICATED;
  585. } else {
  586. sta->flags |= WLAN_STA_MFP;
  587. params.flags |= WPA_STA_MFP;
  588. }
  589. ret = wpa_drv_sta_add(wpa_s, &params);
  590. if (ret) {
  591. wpa_msg(wpa_s, MSG_ERROR,
  592. "Driver failed to insert " MACSTR ": %d",
  593. MAC2STR(addr), ret);
  594. ap_free_sta(data, sta);
  595. return NULL;
  596. }
  597. return sta;
  598. }
  599. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  600. struct ieee802_11_elems *elems)
  601. {
  602. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  603. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  604. struct sta_info *sta;
  605. struct wpa_ssid *ssid = wpa_s->current_ssid;
  606. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  607. if (!sta)
  608. return;
  609. if (ssid && ssid->no_auto_peer &&
  610. (is_zero_ether_addr(data->mesh_required_peer) ||
  611. os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
  612. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  613. MACSTR " because of no_auto_peer", MAC2STR(addr));
  614. if (data->mesh_pending_auth) {
  615. struct os_reltime age;
  616. const struct ieee80211_mgmt *mgmt;
  617. struct hostapd_frame_info fi;
  618. mgmt = wpabuf_head(data->mesh_pending_auth);
  619. os_reltime_age(&data->mesh_pending_auth_time, &age);
  620. if (age.sec < 2 &&
  621. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  622. wpa_printf(MSG_DEBUG,
  623. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  624. (unsigned int) age.sec,
  625. (unsigned int) age.usec);
  626. os_memset(&fi, 0, sizeof(fi));
  627. ieee802_11_mgmt(
  628. data,
  629. wpabuf_head(data->mesh_pending_auth),
  630. wpabuf_len(data->mesh_pending_auth),
  631. &fi);
  632. }
  633. wpabuf_free(data->mesh_pending_auth);
  634. data->mesh_pending_auth = NULL;
  635. }
  636. return;
  637. }
  638. if (conf->security == MESH_CONF_SEC_NONE) {
  639. if (sta->plink_state < PLINK_OPEN_SENT ||
  640. sta->plink_state > PLINK_ESTAB)
  641. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  642. } else {
  643. mesh_rsn_auth_sae_sta(wpa_s, sta);
  644. }
  645. }
  646. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  647. {
  648. struct hostapd_frame_info fi;
  649. os_memset(&fi, 0, sizeof(fi));
  650. fi.datarate = rx_mgmt->datarate;
  651. fi.ssi_signal = rx_mgmt->ssi_signal;
  652. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  653. rx_mgmt->frame_len, &fi);
  654. }
  655. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  656. struct sta_info *sta)
  657. {
  658. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  659. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  660. u8 seq[6] = {};
  661. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  662. MAC2STR(sta->addr));
  663. if (conf->security & MESH_CONF_SEC_AMPE) {
  664. wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
  665. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
  666. sta->addr, 0, 0, seq, sizeof(seq),
  667. sta->mtk, sta->mtk_len);
  668. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
  669. sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
  670. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
  671. sta->mgtk, sta->mgtk_len);
  672. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
  673. sta->addr, sta->mgtk_key_id, 0,
  674. sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
  675. sta->mgtk, sta->mgtk_len);
  676. if (sta->igtk_len) {
  677. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
  678. sta->igtk_rsc, sizeof(sta->igtk_rsc));
  679. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
  680. sta->igtk, sta->igtk_len);
  681. wpa_drv_set_key(
  682. wpa_s,
  683. wpa_cipher_to_alg(conf->mgmt_group_cipher),
  684. sta->addr, sta->igtk_key_id, 0,
  685. sta->igtk_rsc, sizeof(sta->igtk_rsc),
  686. sta->igtk, sta->igtk_len);
  687. }
  688. }
  689. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  690. hapd->num_plinks++;
  691. sta->flags |= WLAN_STA_ASSOC;
  692. sta->mesh_sae_pmksa_caching = 0;
  693. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  694. peer_add_timer(wpa_s, NULL);
  695. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  696. /* Send ctrl event */
  697. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  698. MAC2STR(sta->addr));
  699. }
  700. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  701. enum plink_event event)
  702. {
  703. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  704. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  705. u16 reason = 0;
  706. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  707. MAC2STR(sta->addr), mplstate[sta->plink_state],
  708. mplevent[event]);
  709. switch (sta->plink_state) {
  710. case PLINK_LISTEN:
  711. switch (event) {
  712. case CLS_ACPT:
  713. mesh_mpm_fsm_restart(wpa_s, sta);
  714. break;
  715. case OPN_ACPT:
  716. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  717. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  718. 0);
  719. break;
  720. default:
  721. break;
  722. }
  723. break;
  724. case PLINK_OPEN_SENT:
  725. switch (event) {
  726. case OPN_RJCT:
  727. case CNF_RJCT:
  728. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  729. /* fall-through */
  730. case CLS_ACPT:
  731. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  732. if (!reason)
  733. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  734. eloop_register_timeout(
  735. conf->dot11MeshHoldingTimeout / 1000,
  736. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  737. plink_timer, wpa_s, sta);
  738. mesh_mpm_send_plink_action(wpa_s, sta,
  739. PLINK_CLOSE, reason);
  740. break;
  741. case OPN_ACPT:
  742. /* retry timer is left untouched */
  743. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  744. mesh_mpm_send_plink_action(wpa_s, sta,
  745. PLINK_CONFIRM, 0);
  746. break;
  747. case CNF_ACPT:
  748. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  749. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  750. eloop_register_timeout(
  751. conf->dot11MeshConfirmTimeout / 1000,
  752. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  753. plink_timer, wpa_s, sta);
  754. break;
  755. default:
  756. break;
  757. }
  758. break;
  759. case PLINK_OPEN_RCVD:
  760. switch (event) {
  761. case OPN_RJCT:
  762. case CNF_RJCT:
  763. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  764. /* fall-through */
  765. case CLS_ACPT:
  766. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  767. if (!reason)
  768. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  769. eloop_register_timeout(
  770. conf->dot11MeshHoldingTimeout / 1000,
  771. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  772. plink_timer, wpa_s, sta);
  773. sta->mpm_close_reason = reason;
  774. mesh_mpm_send_plink_action(wpa_s, sta,
  775. PLINK_CLOSE, reason);
  776. break;
  777. case OPN_ACPT:
  778. mesh_mpm_send_plink_action(wpa_s, sta,
  779. PLINK_CONFIRM, 0);
  780. break;
  781. case CNF_ACPT:
  782. if (conf->security & MESH_CONF_SEC_AMPE)
  783. mesh_rsn_derive_mtk(wpa_s, sta);
  784. mesh_mpm_plink_estab(wpa_s, sta);
  785. break;
  786. default:
  787. break;
  788. }
  789. break;
  790. case PLINK_CNF_RCVD:
  791. switch (event) {
  792. case OPN_RJCT:
  793. case CNF_RJCT:
  794. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  795. /* fall-through */
  796. case CLS_ACPT:
  797. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  798. if (!reason)
  799. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  800. eloop_register_timeout(
  801. conf->dot11MeshHoldingTimeout / 1000,
  802. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  803. plink_timer, wpa_s, sta);
  804. sta->mpm_close_reason = reason;
  805. mesh_mpm_send_plink_action(wpa_s, sta,
  806. PLINK_CLOSE, reason);
  807. break;
  808. case OPN_ACPT:
  809. if (conf->security & MESH_CONF_SEC_AMPE)
  810. mesh_rsn_derive_mtk(wpa_s, sta);
  811. mesh_mpm_plink_estab(wpa_s, sta);
  812. mesh_mpm_send_plink_action(wpa_s, sta,
  813. PLINK_CONFIRM, 0);
  814. break;
  815. default:
  816. break;
  817. }
  818. break;
  819. case PLINK_ESTAB:
  820. switch (event) {
  821. case CLS_ACPT:
  822. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  823. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  824. eloop_register_timeout(
  825. conf->dot11MeshHoldingTimeout / 1000,
  826. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  827. plink_timer, wpa_s, sta);
  828. sta->mpm_close_reason = reason;
  829. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  830. " closed with reason %d",
  831. MAC2STR(sta->addr), reason);
  832. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  833. MAC2STR(sta->addr));
  834. hapd->num_plinks--;
  835. mesh_mpm_send_plink_action(wpa_s, sta,
  836. PLINK_CLOSE, reason);
  837. break;
  838. case OPN_ACPT:
  839. mesh_mpm_send_plink_action(wpa_s, sta,
  840. PLINK_CONFIRM, 0);
  841. break;
  842. default:
  843. break;
  844. }
  845. break;
  846. case PLINK_HOLDING:
  847. switch (event) {
  848. case CLS_ACPT:
  849. mesh_mpm_fsm_restart(wpa_s, sta);
  850. break;
  851. case OPN_ACPT:
  852. case CNF_ACPT:
  853. case OPN_RJCT:
  854. case CNF_RJCT:
  855. reason = sta->mpm_close_reason;
  856. mesh_mpm_send_plink_action(wpa_s, sta,
  857. PLINK_CLOSE, reason);
  858. break;
  859. default:
  860. break;
  861. }
  862. break;
  863. default:
  864. wpa_msg(wpa_s, MSG_DEBUG,
  865. "Unsupported MPM event %s for state %s",
  866. mplevent[event], mplstate[sta->plink_state]);
  867. break;
  868. }
  869. }
  870. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  871. const struct ieee80211_mgmt *mgmt, size_t len)
  872. {
  873. u8 action_field;
  874. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  875. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  876. struct sta_info *sta;
  877. u16 plid = 0, llid = 0;
  878. enum plink_event event;
  879. struct ieee802_11_elems elems;
  880. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  881. const u8 *ies;
  882. size_t ie_len;
  883. int ret;
  884. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  885. return;
  886. action_field = mgmt->u.action.u.slf_prot_action.action;
  887. if (action_field != PLINK_OPEN &&
  888. action_field != PLINK_CONFIRM &&
  889. action_field != PLINK_CLOSE)
  890. return;
  891. ies = mgmt->u.action.u.slf_prot_action.variable;
  892. ie_len = (const u8 *) mgmt + len -
  893. mgmt->u.action.u.slf_prot_action.variable;
  894. /* at least expect mesh id and peering mgmt */
  895. if (ie_len < 2 + 2) {
  896. wpa_printf(MSG_DEBUG,
  897. "MPM: Ignore too short action frame %u ie_len %u",
  898. action_field, (unsigned int) ie_len);
  899. return;
  900. }
  901. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  902. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  903. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  904. WPA_GET_LE16(ies));
  905. ies += 2; /* capability */
  906. ie_len -= 2;
  907. }
  908. if (action_field == PLINK_CONFIRM) {
  909. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  910. ies += 2; /* aid */
  911. ie_len -= 2;
  912. }
  913. /* check for mesh peering, mesh id and mesh config IEs */
  914. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  915. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  916. return;
  917. }
  918. if (!elems.peer_mgmt) {
  919. wpa_printf(MSG_DEBUG,
  920. "MPM: No Mesh Peering Management element");
  921. return;
  922. }
  923. if (action_field != PLINK_CLOSE) {
  924. if (!elems.mesh_id || !elems.mesh_config) {
  925. wpa_printf(MSG_DEBUG,
  926. "MPM: No Mesh ID or Mesh Configuration element");
  927. return;
  928. }
  929. if (!matches_local(wpa_s, &elems)) {
  930. wpa_printf(MSG_DEBUG,
  931. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  932. return;
  933. }
  934. }
  935. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  936. elems.peer_mgmt,
  937. elems.peer_mgmt_len,
  938. &peer_mgmt_ie);
  939. if (ret) {
  940. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  941. return;
  942. }
  943. /* the sender's llid is our plid and vice-versa */
  944. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  945. if (peer_mgmt_ie.plid)
  946. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  947. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  948. sta = ap_get_sta(hapd, mgmt->sa);
  949. /*
  950. * If this is an open frame from an unknown STA, and this is an
  951. * open mesh, then go ahead and add the peer before proceeding.
  952. */
  953. if (!sta && action_field == PLINK_OPEN &&
  954. (!(mconf->security & MESH_CONF_SEC_AMPE) ||
  955. wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa)))
  956. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  957. if (!sta) {
  958. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  959. return;
  960. }
  961. #ifdef CONFIG_SAE
  962. /* peer is in sae_accepted? */
  963. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  964. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  965. return;
  966. }
  967. #endif /* CONFIG_SAE */
  968. if (!sta->my_lid)
  969. mesh_mpm_init_link(wpa_s, sta);
  970. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  971. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  972. &mgmt->u.action.category,
  973. peer_mgmt_ie.chosen_pmk,
  974. ies, ie_len)) {
  975. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  976. return;
  977. }
  978. if (sta->plink_state == PLINK_BLOCKED) {
  979. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  980. return;
  981. }
  982. /* Now we will figure out the appropriate event... */
  983. switch (action_field) {
  984. case PLINK_OPEN:
  985. if (plink_free_count(hapd) == 0) {
  986. event = OPN_IGNR;
  987. wpa_printf(MSG_INFO,
  988. "MPM: Peer link num over quota(%d)",
  989. hapd->max_plinks);
  990. } else if (sta->peer_lid && sta->peer_lid != plid) {
  991. event = OPN_IGNR;
  992. } else {
  993. sta->peer_lid = plid;
  994. event = OPN_ACPT;
  995. }
  996. break;
  997. case PLINK_CONFIRM:
  998. if (plink_free_count(hapd) == 0) {
  999. event = CNF_IGNR;
  1000. wpa_printf(MSG_INFO,
  1001. "MPM: Peer link num over quota(%d)",
  1002. hapd->max_plinks);
  1003. } else if (sta->my_lid != llid ||
  1004. (sta->peer_lid && sta->peer_lid != plid)) {
  1005. event = CNF_IGNR;
  1006. } else {
  1007. if (!sta->peer_lid)
  1008. sta->peer_lid = plid;
  1009. event = CNF_ACPT;
  1010. }
  1011. break;
  1012. case PLINK_CLOSE:
  1013. if (sta->plink_state == PLINK_ESTAB)
  1014. /* Do not check for llid or plid. This does not
  1015. * follow the standard but since multiple plinks
  1016. * per cand are not supported, it is necessary in
  1017. * order to avoid a livelock when MP A sees an
  1018. * establish peer link to MP B but MP B does not
  1019. * see it. This can be caused by a timeout in
  1020. * B's peer link establishment or B being
  1021. * restarted.
  1022. */
  1023. event = CLS_ACPT;
  1024. else if (sta->peer_lid != plid)
  1025. event = CLS_IGNR;
  1026. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  1027. event = CLS_IGNR;
  1028. else
  1029. event = CLS_ACPT;
  1030. break;
  1031. default:
  1032. /*
  1033. * This cannot be hit due to the action_field check above, but
  1034. * compilers may not be able to figure that out and can warn
  1035. * about uninitialized event below.
  1036. */
  1037. return;
  1038. }
  1039. mesh_mpm_fsm(wpa_s, sta, event);
  1040. }
  1041. /* called by ap_free_sta */
  1042. void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  1043. {
  1044. if (sta->plink_state == PLINK_ESTAB)
  1045. hapd->num_plinks--;
  1046. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  1047. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  1048. }