mesh_mpm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
  245. MESH_CAP_FORWARDING);
  246. } else { /* Peer closing frame */
  247. /* IE: Mesh ID */
  248. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  249. wpabuf_put_u8(buf, conf->meshid_len);
  250. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  251. }
  252. /* IE: Mesh Peering Management element */
  253. ie_len = 4;
  254. if (ampe)
  255. ie_len += PMKID_LEN;
  256. switch (type) {
  257. case PLINK_OPEN:
  258. break;
  259. case PLINK_CONFIRM:
  260. ie_len += 2;
  261. add_plid = 1;
  262. break;
  263. case PLINK_CLOSE:
  264. ie_len += 2;
  265. add_plid = 1;
  266. ie_len += 2; /* reason code */
  267. break;
  268. }
  269. wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
  270. wpabuf_put_u8(buf, ie_len);
  271. /* peering protocol */
  272. if (ampe)
  273. wpabuf_put_le16(buf, 1);
  274. else
  275. wpabuf_put_le16(buf, 0);
  276. wpabuf_put_le16(buf, sta->my_lid);
  277. if (add_plid)
  278. wpabuf_put_le16(buf, sta->peer_lid);
  279. if (type == PLINK_CLOSE)
  280. wpabuf_put_le16(buf, close_reason);
  281. if (ampe) {
  282. if (sta->sae == NULL) {
  283. wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
  284. goto fail;
  285. }
  286. mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
  287. wpabuf_put(buf, PMKID_LEN));
  288. }
  289. #ifdef CONFIG_IEEE80211N
  290. if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
  291. u8 ht_capa_oper[2 + 26 + 2 + 22];
  292. pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
  293. pos = hostapd_eid_ht_operation(bss, pos);
  294. wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
  295. }
  296. #endif /* CONFIG_IEEE80211N */
  297. #ifdef CONFIG_IEEE80211AC
  298. if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
  299. u8 vht_capa_oper[2 + 12 + 2 + 5];
  300. pos = hostapd_eid_vht_capabilities(bss, vht_capa_oper);
  301. pos = hostapd_eid_vht_operation(bss, pos);
  302. wpabuf_put_data(buf, vht_capa_oper, pos - vht_capa_oper);
  303. }
  304. #endif /* CONFIG_IEEE80211AC */
  305. if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
  306. wpa_msg(wpa_s, MSG_INFO,
  307. "Mesh MPM: failed to add AMPE and MIC IE");
  308. goto fail;
  309. }
  310. wpa_msg(wpa_s, MSG_DEBUG, "Mesh MPM: Sending peering frame type %d to "
  311. MACSTR " (my_lid=0x%x peer_lid=0x%x)",
  312. type, MAC2STR(sta->addr), sta->my_lid, sta->peer_lid);
  313. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
  314. sta->addr, wpa_s->own_addr, wpa_s->own_addr,
  315. wpabuf_head(buf), wpabuf_len(buf), 0);
  316. if (ret < 0)
  317. wpa_msg(wpa_s, MSG_INFO,
  318. "Mesh MPM: failed to send peering frame");
  319. fail:
  320. wpabuf_free(buf);
  321. }
  322. /* configure peering state in ours and driver's station entry */
  323. void wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s,
  324. struct sta_info *sta,
  325. enum mesh_plink_state state)
  326. {
  327. struct hostapd_sta_add_params params;
  328. int ret;
  329. wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " from %s into %s",
  330. MAC2STR(sta->addr), mplstate[sta->plink_state],
  331. mplstate[state]);
  332. sta->plink_state = state;
  333. os_memset(&params, 0, sizeof(params));
  334. params.addr = sta->addr;
  335. params.plink_state = state;
  336. params.set = 1;
  337. ret = wpa_drv_sta_add(wpa_s, &params);
  338. if (ret) {
  339. wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
  340. ": %d", MAC2STR(sta->addr), ret);
  341. }
  342. }
  343. static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
  344. struct sta_info *sta)
  345. {
  346. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  347. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  348. ap_free_sta(hapd, sta);
  349. }
  350. static void plink_timer(void *eloop_ctx, void *user_data)
  351. {
  352. struct wpa_supplicant *wpa_s = eloop_ctx;
  353. struct sta_info *sta = user_data;
  354. u16 reason = 0;
  355. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  356. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  357. switch (sta->plink_state) {
  358. case PLINK_OPEN_RCVD:
  359. case PLINK_OPEN_SENT:
  360. /* retry timer */
  361. if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
  362. eloop_register_timeout(
  363. conf->dot11MeshRetryTimeout / 1000,
  364. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  365. plink_timer, wpa_s, sta);
  366. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  367. sta->mpm_retries++;
  368. break;
  369. }
  370. reason = WLAN_REASON_MESH_MAX_RETRIES;
  371. /* fall through on else */
  372. case PLINK_CNF_RCVD:
  373. /* confirm timer */
  374. if (!reason)
  375. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  376. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  377. eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
  378. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  379. plink_timer, wpa_s, sta);
  380. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  381. break;
  382. case PLINK_HOLDING:
  383. /* holding timer */
  384. if (sta->mesh_sae_pmksa_caching) {
  385. wpa_printf(MSG_DEBUG, "MPM: Peer " MACSTR
  386. " looks like it does not support mesh SAE PMKSA caching, so remove the cached entry for it",
  387. MAC2STR(sta->addr));
  388. wpa_auth_pmksa_remove(hapd->wpa_auth, sta->addr);
  389. }
  390. mesh_mpm_fsm_restart(wpa_s, sta);
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. /* initiate peering with station */
  397. static void
  398. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  399. enum mesh_plink_state next_state)
  400. {
  401. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  402. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  403. eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
  404. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  405. plink_timer, wpa_s, sta);
  406. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  407. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  408. }
  409. static int mesh_mpm_plink_close(struct hostapd_data *hapd, struct sta_info *sta,
  410. void *ctx)
  411. {
  412. struct wpa_supplicant *wpa_s = ctx;
  413. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  414. if (sta) {
  415. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  416. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  417. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  418. MAC2STR(sta->addr));
  419. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  420. return 0;
  421. }
  422. return 1;
  423. }
  424. int mesh_mpm_close_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  425. {
  426. struct hostapd_data *hapd;
  427. struct sta_info *sta;
  428. if (!wpa_s->ifmsh) {
  429. wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
  430. return -1;
  431. }
  432. hapd = wpa_s->ifmsh->bss[0];
  433. sta = ap_get_sta(hapd, addr);
  434. if (!sta) {
  435. wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
  436. return -1;
  437. }
  438. return mesh_mpm_plink_close(hapd, sta, wpa_s) == 0 ? 0 : -1;
  439. }
  440. static void peer_add_timer(void *eloop_ctx, void *user_data)
  441. {
  442. struct wpa_supplicant *wpa_s = eloop_ctx;
  443. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  444. os_memset(hapd->mesh_required_peer, 0, ETH_ALEN);
  445. }
  446. int mesh_mpm_connect_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  447. int duration)
  448. {
  449. struct wpa_ssid *ssid = wpa_s->current_ssid;
  450. struct hostapd_data *hapd;
  451. struct sta_info *sta;
  452. struct mesh_conf *conf;
  453. if (!wpa_s->ifmsh) {
  454. wpa_msg(wpa_s, MSG_INFO, "Mesh is not prepared yet");
  455. return -1;
  456. }
  457. if (!ssid || !ssid->no_auto_peer) {
  458. wpa_msg(wpa_s, MSG_INFO,
  459. "This command is available only with no_auto_peer mesh network");
  460. return -1;
  461. }
  462. hapd = wpa_s->ifmsh->bss[0];
  463. conf = wpa_s->ifmsh->mconf;
  464. sta = ap_get_sta(hapd, addr);
  465. if (!sta) {
  466. wpa_msg(wpa_s, MSG_INFO, "No such mesh peer");
  467. return -1;
  468. }
  469. if ((PLINK_OPEN_SENT <= sta->plink_state &&
  470. sta->plink_state <= PLINK_ESTAB) ||
  471. (sta->sae && sta->sae->state > SAE_NOTHING)) {
  472. wpa_msg(wpa_s, MSG_INFO,
  473. "Specified peer is connecting/connected");
  474. return -1;
  475. }
  476. if (conf->security == MESH_CONF_SEC_NONE) {
  477. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  478. } else {
  479. mesh_rsn_auth_sae_sta(wpa_s, sta);
  480. os_memcpy(hapd->mesh_required_peer, addr, ETH_ALEN);
  481. eloop_register_timeout(duration == -1 ? 10 : duration, 0,
  482. peer_add_timer, wpa_s, NULL);
  483. }
  484. return 0;
  485. }
  486. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  487. {
  488. struct hostapd_data *hapd = ifmsh->bss[0];
  489. /* notify peers we're leaving */
  490. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  491. hapd->num_plinks = 0;
  492. hostapd_free_stas(hapd);
  493. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  494. }
  495. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  496. * ready to start AMPE */
  497. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  498. {
  499. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  500. struct hostapd_sta_add_params params;
  501. struct sta_info *sta;
  502. int ret;
  503. sta = ap_get_sta(data, addr);
  504. if (!sta) {
  505. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  506. return;
  507. }
  508. /* TODO: Should do nothing if this STA is already authenticated, but
  509. * the AP code already sets this flag. */
  510. sta->flags |= WLAN_STA_AUTH;
  511. mesh_rsn_init_ampe_sta(wpa_s, sta);
  512. os_memset(&params, 0, sizeof(params));
  513. params.addr = sta->addr;
  514. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  515. params.set = 1;
  516. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  517. MAC2STR(sta->addr));
  518. ret = wpa_drv_sta_add(wpa_s, &params);
  519. if (ret) {
  520. wpa_msg(wpa_s, MSG_ERROR,
  521. "Driver failed to set " MACSTR ": %d",
  522. MAC2STR(sta->addr), ret);
  523. }
  524. if (!sta->my_lid)
  525. mesh_mpm_init_link(wpa_s, sta);
  526. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  527. }
  528. /*
  529. * Initialize a sta_info structure for a peer and upload it into the driver
  530. * in preparation for beginning authentication or peering. This is done when a
  531. * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
  532. * received from the peer for the first time.
  533. */
  534. static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
  535. const u8 *addr,
  536. struct ieee802_11_elems *elems)
  537. {
  538. struct hostapd_sta_add_params params;
  539. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  540. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  541. struct sta_info *sta;
  542. int ret;
  543. if (elems->mesh_config_len >= 7 &&
  544. !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
  545. wpa_msg(wpa_s, MSG_DEBUG,
  546. "mesh: Ignore a crowded peer " MACSTR,
  547. MAC2STR(addr));
  548. return NULL;
  549. }
  550. sta = ap_get_sta(data, addr);
  551. if (!sta) {
  552. sta = ap_sta_add(data, addr);
  553. if (!sta)
  554. return NULL;
  555. }
  556. /* Set WMM by default since Mesh STAs are QoS STAs */
  557. sta->flags |= WLAN_STA_WMM;
  558. /* initialize sta */
  559. if (copy_supp_rates(wpa_s, sta, elems)) {
  560. ap_free_sta(data, sta);
  561. return NULL;
  562. }
  563. if (!sta->my_lid)
  564. mesh_mpm_init_link(wpa_s, sta);
  565. #ifdef CONFIG_IEEE80211N
  566. copy_sta_ht_capab(data, sta, elems->ht_capabilities);
  567. update_ht_state(data, sta);
  568. #endif /* CONFIG_IEEE80211N */
  569. #ifdef CONFIG_IEEE80211AC
  570. copy_sta_vht_capab(data, sta, elems->vht_capabilities);
  571. set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
  572. #endif /* CONFIG_IEEE80211AC */
  573. if (hostapd_get_aid(data, sta) < 0) {
  574. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  575. ap_free_sta(data, sta);
  576. return NULL;
  577. }
  578. /* insert into driver */
  579. os_memset(&params, 0, sizeof(params));
  580. params.supp_rates = sta->supported_rates;
  581. params.supp_rates_len = sta->supported_rates_len;
  582. params.addr = addr;
  583. params.plink_state = sta->plink_state;
  584. params.aid = sta->aid;
  585. params.listen_interval = 100;
  586. params.ht_capabilities = sta->ht_capabilities;
  587. params.vht_capabilities = sta->vht_capabilities;
  588. params.flags |= WPA_STA_WMM;
  589. params.flags_mask |= WPA_STA_AUTHENTICATED;
  590. if (conf->security == MESH_CONF_SEC_NONE) {
  591. params.flags |= WPA_STA_AUTHORIZED;
  592. params.flags |= WPA_STA_AUTHENTICATED;
  593. } else {
  594. sta->flags |= WLAN_STA_MFP;
  595. params.flags |= WPA_STA_MFP;
  596. }
  597. ret = wpa_drv_sta_add(wpa_s, &params);
  598. if (ret) {
  599. wpa_msg(wpa_s, MSG_ERROR,
  600. "Driver failed to insert " MACSTR ": %d",
  601. MAC2STR(addr), ret);
  602. ap_free_sta(data, sta);
  603. return NULL;
  604. }
  605. return sta;
  606. }
  607. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  608. struct ieee802_11_elems *elems)
  609. {
  610. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  611. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  612. struct sta_info *sta;
  613. struct wpa_ssid *ssid = wpa_s->current_ssid;
  614. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  615. if (!sta)
  616. return;
  617. if (ssid && ssid->no_auto_peer &&
  618. (is_zero_ether_addr(data->mesh_required_peer) ||
  619. os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
  620. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  621. MACSTR " because of no_auto_peer", MAC2STR(addr));
  622. if (data->mesh_pending_auth) {
  623. struct os_reltime age;
  624. const struct ieee80211_mgmt *mgmt;
  625. struct hostapd_frame_info fi;
  626. mgmt = wpabuf_head(data->mesh_pending_auth);
  627. os_reltime_age(&data->mesh_pending_auth_time, &age);
  628. if (age.sec < 2 &&
  629. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  630. wpa_printf(MSG_DEBUG,
  631. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  632. (unsigned int) age.sec,
  633. (unsigned int) age.usec);
  634. os_memset(&fi, 0, sizeof(fi));
  635. ieee802_11_mgmt(
  636. data,
  637. wpabuf_head(data->mesh_pending_auth),
  638. wpabuf_len(data->mesh_pending_auth),
  639. &fi);
  640. }
  641. wpabuf_free(data->mesh_pending_auth);
  642. data->mesh_pending_auth = NULL;
  643. }
  644. return;
  645. }
  646. if (conf->security == MESH_CONF_SEC_NONE) {
  647. if (sta->plink_state < PLINK_OPEN_SENT ||
  648. sta->plink_state > PLINK_ESTAB)
  649. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  650. } else {
  651. mesh_rsn_auth_sae_sta(wpa_s, sta);
  652. }
  653. }
  654. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  655. {
  656. struct hostapd_frame_info fi;
  657. os_memset(&fi, 0, sizeof(fi));
  658. fi.datarate = rx_mgmt->datarate;
  659. fi.ssi_signal = rx_mgmt->ssi_signal;
  660. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  661. rx_mgmt->frame_len, &fi);
  662. }
  663. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  664. struct sta_info *sta)
  665. {
  666. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  667. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  668. u8 seq[6] = {};
  669. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  670. MAC2STR(sta->addr));
  671. if (conf->security & MESH_CONF_SEC_AMPE) {
  672. wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
  673. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
  674. sta->addr, 0, 0, seq, sizeof(seq),
  675. sta->mtk, sta->mtk_len);
  676. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
  677. sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
  678. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
  679. sta->mgtk, sta->mgtk_len);
  680. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
  681. sta->addr, sta->mgtk_key_id, 0,
  682. sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
  683. sta->mgtk, sta->mgtk_len);
  684. if (sta->igtk_len) {
  685. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
  686. sta->igtk_rsc, sizeof(sta->igtk_rsc));
  687. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
  688. sta->igtk, sta->igtk_len);
  689. wpa_drv_set_key(
  690. wpa_s,
  691. wpa_cipher_to_alg(conf->mgmt_group_cipher),
  692. sta->addr, sta->igtk_key_id, 0,
  693. sta->igtk_rsc, sizeof(sta->igtk_rsc),
  694. sta->igtk, sta->igtk_len);
  695. }
  696. }
  697. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  698. hapd->num_plinks++;
  699. sta->flags |= WLAN_STA_ASSOC;
  700. sta->mesh_sae_pmksa_caching = 0;
  701. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  702. peer_add_timer(wpa_s, NULL);
  703. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  704. /* Send ctrl event */
  705. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  706. MAC2STR(sta->addr));
  707. }
  708. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  709. enum plink_event event)
  710. {
  711. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  712. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  713. u16 reason = 0;
  714. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  715. MAC2STR(sta->addr), mplstate[sta->plink_state],
  716. mplevent[event]);
  717. switch (sta->plink_state) {
  718. case PLINK_LISTEN:
  719. switch (event) {
  720. case CLS_ACPT:
  721. mesh_mpm_fsm_restart(wpa_s, sta);
  722. break;
  723. case OPN_ACPT:
  724. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  725. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  726. 0);
  727. break;
  728. default:
  729. break;
  730. }
  731. break;
  732. case PLINK_OPEN_SENT:
  733. switch (event) {
  734. case OPN_RJCT:
  735. case CNF_RJCT:
  736. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  737. /* fall-through */
  738. case CLS_ACPT:
  739. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  740. if (!reason)
  741. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  742. eloop_register_timeout(
  743. conf->dot11MeshHoldingTimeout / 1000,
  744. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  745. plink_timer, wpa_s, sta);
  746. mesh_mpm_send_plink_action(wpa_s, sta,
  747. PLINK_CLOSE, reason);
  748. break;
  749. case OPN_ACPT:
  750. /* retry timer is left untouched */
  751. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  752. mesh_mpm_send_plink_action(wpa_s, sta,
  753. PLINK_CONFIRM, 0);
  754. break;
  755. case CNF_ACPT:
  756. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  757. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  758. eloop_register_timeout(
  759. conf->dot11MeshConfirmTimeout / 1000,
  760. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  761. plink_timer, wpa_s, sta);
  762. break;
  763. default:
  764. break;
  765. }
  766. break;
  767. case PLINK_OPEN_RCVD:
  768. switch (event) {
  769. case OPN_RJCT:
  770. case CNF_RJCT:
  771. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  772. /* fall-through */
  773. case CLS_ACPT:
  774. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  775. if (!reason)
  776. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  777. eloop_register_timeout(
  778. conf->dot11MeshHoldingTimeout / 1000,
  779. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  780. plink_timer, wpa_s, sta);
  781. sta->mpm_close_reason = reason;
  782. mesh_mpm_send_plink_action(wpa_s, sta,
  783. PLINK_CLOSE, reason);
  784. break;
  785. case OPN_ACPT:
  786. mesh_mpm_send_plink_action(wpa_s, sta,
  787. PLINK_CONFIRM, 0);
  788. break;
  789. case CNF_ACPT:
  790. if (conf->security & MESH_CONF_SEC_AMPE)
  791. mesh_rsn_derive_mtk(wpa_s, sta);
  792. mesh_mpm_plink_estab(wpa_s, sta);
  793. break;
  794. default:
  795. break;
  796. }
  797. break;
  798. case PLINK_CNF_RCVD:
  799. switch (event) {
  800. case OPN_RJCT:
  801. case CNF_RJCT:
  802. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  803. /* fall-through */
  804. case CLS_ACPT:
  805. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  806. if (!reason)
  807. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  808. eloop_register_timeout(
  809. conf->dot11MeshHoldingTimeout / 1000,
  810. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  811. plink_timer, wpa_s, sta);
  812. sta->mpm_close_reason = reason;
  813. mesh_mpm_send_plink_action(wpa_s, sta,
  814. PLINK_CLOSE, reason);
  815. break;
  816. case OPN_ACPT:
  817. if (conf->security & MESH_CONF_SEC_AMPE)
  818. mesh_rsn_derive_mtk(wpa_s, sta);
  819. mesh_mpm_plink_estab(wpa_s, sta);
  820. mesh_mpm_send_plink_action(wpa_s, sta,
  821. PLINK_CONFIRM, 0);
  822. break;
  823. default:
  824. break;
  825. }
  826. break;
  827. case PLINK_ESTAB:
  828. switch (event) {
  829. case CLS_ACPT:
  830. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  831. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  832. eloop_register_timeout(
  833. conf->dot11MeshHoldingTimeout / 1000,
  834. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  835. plink_timer, wpa_s, sta);
  836. sta->mpm_close_reason = reason;
  837. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  838. " closed with reason %d",
  839. MAC2STR(sta->addr), reason);
  840. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  841. MAC2STR(sta->addr));
  842. hapd->num_plinks--;
  843. mesh_mpm_send_plink_action(wpa_s, sta,
  844. PLINK_CLOSE, reason);
  845. break;
  846. case OPN_ACPT:
  847. mesh_mpm_send_plink_action(wpa_s, sta,
  848. PLINK_CONFIRM, 0);
  849. break;
  850. default:
  851. break;
  852. }
  853. break;
  854. case PLINK_HOLDING:
  855. switch (event) {
  856. case CLS_ACPT:
  857. mesh_mpm_fsm_restart(wpa_s, sta);
  858. break;
  859. case OPN_ACPT:
  860. case CNF_ACPT:
  861. case OPN_RJCT:
  862. case CNF_RJCT:
  863. reason = sta->mpm_close_reason;
  864. mesh_mpm_send_plink_action(wpa_s, sta,
  865. PLINK_CLOSE, reason);
  866. break;
  867. default:
  868. break;
  869. }
  870. break;
  871. default:
  872. wpa_msg(wpa_s, MSG_DEBUG,
  873. "Unsupported MPM event %s for state %s",
  874. mplevent[event], mplstate[sta->plink_state]);
  875. break;
  876. }
  877. }
  878. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  879. const struct ieee80211_mgmt *mgmt, size_t len)
  880. {
  881. u8 action_field;
  882. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  883. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  884. struct sta_info *sta;
  885. u16 plid = 0, llid = 0;
  886. enum plink_event event;
  887. struct ieee802_11_elems elems;
  888. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  889. const u8 *ies;
  890. size_t ie_len;
  891. int ret;
  892. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  893. return;
  894. action_field = mgmt->u.action.u.slf_prot_action.action;
  895. if (action_field != PLINK_OPEN &&
  896. action_field != PLINK_CONFIRM &&
  897. action_field != PLINK_CLOSE)
  898. return;
  899. ies = mgmt->u.action.u.slf_prot_action.variable;
  900. ie_len = (const u8 *) mgmt + len -
  901. mgmt->u.action.u.slf_prot_action.variable;
  902. /* at least expect mesh id and peering mgmt */
  903. if (ie_len < 2 + 2) {
  904. wpa_printf(MSG_DEBUG,
  905. "MPM: Ignore too short action frame %u ie_len %u",
  906. action_field, (unsigned int) ie_len);
  907. return;
  908. }
  909. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  910. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  911. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  912. WPA_GET_LE16(ies));
  913. ies += 2; /* capability */
  914. ie_len -= 2;
  915. }
  916. if (action_field == PLINK_CONFIRM) {
  917. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  918. ies += 2; /* aid */
  919. ie_len -= 2;
  920. }
  921. /* check for mesh peering, mesh id and mesh config IEs */
  922. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  923. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  924. return;
  925. }
  926. if (!elems.peer_mgmt) {
  927. wpa_printf(MSG_DEBUG,
  928. "MPM: No Mesh Peering Management element");
  929. return;
  930. }
  931. if (action_field != PLINK_CLOSE) {
  932. if (!elems.mesh_id || !elems.mesh_config) {
  933. wpa_printf(MSG_DEBUG,
  934. "MPM: No Mesh ID or Mesh Configuration element");
  935. return;
  936. }
  937. if (!matches_local(wpa_s, &elems)) {
  938. wpa_printf(MSG_DEBUG,
  939. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  940. return;
  941. }
  942. }
  943. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  944. elems.peer_mgmt,
  945. elems.peer_mgmt_len,
  946. &peer_mgmt_ie);
  947. if (ret) {
  948. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  949. return;
  950. }
  951. /* the sender's llid is our plid and vice-versa */
  952. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  953. if (peer_mgmt_ie.plid)
  954. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  955. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  956. sta = ap_get_sta(hapd, mgmt->sa);
  957. /*
  958. * If this is an open frame from an unknown STA, and this is an
  959. * open mesh, then go ahead and add the peer before proceeding.
  960. */
  961. if (!sta && action_field == PLINK_OPEN &&
  962. (!(mconf->security & MESH_CONF_SEC_AMPE) ||
  963. wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa)))
  964. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  965. if (!sta) {
  966. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  967. return;
  968. }
  969. #ifdef CONFIG_SAE
  970. /* peer is in sae_accepted? */
  971. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  972. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  973. return;
  974. }
  975. #endif /* CONFIG_SAE */
  976. if (!sta->my_lid)
  977. mesh_mpm_init_link(wpa_s, sta);
  978. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  979. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  980. &mgmt->u.action.category,
  981. peer_mgmt_ie.chosen_pmk,
  982. ies, ie_len)) {
  983. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  984. return;
  985. }
  986. if (sta->plink_state == PLINK_BLOCKED) {
  987. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  988. return;
  989. }
  990. /* Now we will figure out the appropriate event... */
  991. switch (action_field) {
  992. case PLINK_OPEN:
  993. if (plink_free_count(hapd) == 0) {
  994. event = OPN_IGNR;
  995. wpa_printf(MSG_INFO,
  996. "MPM: Peer link num over quota(%d)",
  997. hapd->max_plinks);
  998. } else if (sta->peer_lid && sta->peer_lid != plid) {
  999. event = OPN_IGNR;
  1000. } else {
  1001. sta->peer_lid = plid;
  1002. event = OPN_ACPT;
  1003. }
  1004. break;
  1005. case PLINK_CONFIRM:
  1006. if (plink_free_count(hapd) == 0) {
  1007. event = CNF_IGNR;
  1008. wpa_printf(MSG_INFO,
  1009. "MPM: Peer link num over quota(%d)",
  1010. hapd->max_plinks);
  1011. } else if (sta->my_lid != llid ||
  1012. (sta->peer_lid && sta->peer_lid != plid)) {
  1013. event = CNF_IGNR;
  1014. } else {
  1015. if (!sta->peer_lid)
  1016. sta->peer_lid = plid;
  1017. event = CNF_ACPT;
  1018. }
  1019. break;
  1020. case PLINK_CLOSE:
  1021. if (sta->plink_state == PLINK_ESTAB)
  1022. /* Do not check for llid or plid. This does not
  1023. * follow the standard but since multiple plinks
  1024. * per cand are not supported, it is necessary in
  1025. * order to avoid a livelock when MP A sees an
  1026. * establish peer link to MP B but MP B does not
  1027. * see it. This can be caused by a timeout in
  1028. * B's peer link establishment or B being
  1029. * restarted.
  1030. */
  1031. event = CLS_ACPT;
  1032. else if (sta->peer_lid != plid)
  1033. event = CLS_IGNR;
  1034. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  1035. event = CLS_IGNR;
  1036. else
  1037. event = CLS_ACPT;
  1038. break;
  1039. default:
  1040. /*
  1041. * This cannot be hit due to the action_field check above, but
  1042. * compilers may not be able to figure that out and can warn
  1043. * about uninitialized event below.
  1044. */
  1045. return;
  1046. }
  1047. mesh_mpm_fsm(wpa_s, sta, event);
  1048. }
  1049. /* called by ap_free_sta */
  1050. void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  1051. {
  1052. if (sta->plink_state == PLINK_ESTAB)
  1053. hapd->num_plinks--;
  1054. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  1055. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  1056. }