mesh_mpm.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  665. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  666. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  667. seq, sizeof(seq),
  668. sta->mgtk, sizeof(sta->mgtk));
  669. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  670. seq, sizeof(seq),
  671. sta->mgtk, sizeof(sta->mgtk));
  672. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  673. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  674. sta->mgtk, sizeof(sta->mgtk));
  675. }
  676. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  677. hapd->num_plinks++;
  678. sta->flags |= WLAN_STA_ASSOC;
  679. sta->mesh_sae_pmksa_caching = 0;
  680. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  681. peer_add_timer(wpa_s, NULL);
  682. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  683. /* Send ctrl event */
  684. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  685. MAC2STR(sta->addr));
  686. }
  687. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  688. enum plink_event event)
  689. {
  690. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  691. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  692. u16 reason = 0;
  693. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  694. MAC2STR(sta->addr), mplstate[sta->plink_state],
  695. mplevent[event]);
  696. switch (sta->plink_state) {
  697. case PLINK_LISTEN:
  698. switch (event) {
  699. case CLS_ACPT:
  700. mesh_mpm_fsm_restart(wpa_s, sta);
  701. break;
  702. case OPN_ACPT:
  703. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  704. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  705. 0);
  706. break;
  707. default:
  708. break;
  709. }
  710. break;
  711. case PLINK_OPEN_SENT:
  712. switch (event) {
  713. case OPN_RJCT:
  714. case CNF_RJCT:
  715. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  716. /* fall-through */
  717. case CLS_ACPT:
  718. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  719. if (!reason)
  720. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  721. eloop_register_timeout(
  722. conf->dot11MeshHoldingTimeout / 1000,
  723. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  724. plink_timer, wpa_s, sta);
  725. mesh_mpm_send_plink_action(wpa_s, sta,
  726. PLINK_CLOSE, reason);
  727. break;
  728. case OPN_ACPT:
  729. /* retry timer is left untouched */
  730. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  731. mesh_mpm_send_plink_action(wpa_s, sta,
  732. PLINK_CONFIRM, 0);
  733. break;
  734. case CNF_ACPT:
  735. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  736. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  737. eloop_register_timeout(
  738. conf->dot11MeshConfirmTimeout / 1000,
  739. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  740. plink_timer, wpa_s, sta);
  741. break;
  742. default:
  743. break;
  744. }
  745. break;
  746. case PLINK_OPEN_RCVD:
  747. switch (event) {
  748. case OPN_RJCT:
  749. case CNF_RJCT:
  750. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  751. /* fall-through */
  752. case CLS_ACPT:
  753. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  754. if (!reason)
  755. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  756. eloop_register_timeout(
  757. conf->dot11MeshHoldingTimeout / 1000,
  758. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  759. plink_timer, wpa_s, sta);
  760. sta->mpm_close_reason = reason;
  761. mesh_mpm_send_plink_action(wpa_s, sta,
  762. PLINK_CLOSE, reason);
  763. break;
  764. case OPN_ACPT:
  765. mesh_mpm_send_plink_action(wpa_s, sta,
  766. PLINK_CONFIRM, 0);
  767. break;
  768. case CNF_ACPT:
  769. if (conf->security & MESH_CONF_SEC_AMPE)
  770. mesh_rsn_derive_mtk(wpa_s, sta);
  771. mesh_mpm_plink_estab(wpa_s, sta);
  772. break;
  773. default:
  774. break;
  775. }
  776. break;
  777. case PLINK_CNF_RCVD:
  778. switch (event) {
  779. case OPN_RJCT:
  780. case CNF_RJCT:
  781. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  782. /* fall-through */
  783. case CLS_ACPT:
  784. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  785. if (!reason)
  786. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  787. eloop_register_timeout(
  788. conf->dot11MeshHoldingTimeout / 1000,
  789. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  790. plink_timer, wpa_s, sta);
  791. sta->mpm_close_reason = reason;
  792. mesh_mpm_send_plink_action(wpa_s, sta,
  793. PLINK_CLOSE, reason);
  794. break;
  795. case OPN_ACPT:
  796. if (conf->security & MESH_CONF_SEC_AMPE)
  797. mesh_rsn_derive_mtk(wpa_s, sta);
  798. mesh_mpm_plink_estab(wpa_s, sta);
  799. mesh_mpm_send_plink_action(wpa_s, sta,
  800. PLINK_CONFIRM, 0);
  801. break;
  802. default:
  803. break;
  804. }
  805. break;
  806. case PLINK_ESTAB:
  807. switch (event) {
  808. case CLS_ACPT:
  809. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  810. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  811. eloop_register_timeout(
  812. conf->dot11MeshHoldingTimeout / 1000,
  813. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  814. plink_timer, wpa_s, sta);
  815. sta->mpm_close_reason = reason;
  816. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  817. " closed with reason %d",
  818. MAC2STR(sta->addr), reason);
  819. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  820. MAC2STR(sta->addr));
  821. hapd->num_plinks--;
  822. mesh_mpm_send_plink_action(wpa_s, sta,
  823. PLINK_CLOSE, reason);
  824. break;
  825. case OPN_ACPT:
  826. mesh_mpm_send_plink_action(wpa_s, sta,
  827. PLINK_CONFIRM, 0);
  828. break;
  829. default:
  830. break;
  831. }
  832. break;
  833. case PLINK_HOLDING:
  834. switch (event) {
  835. case CLS_ACPT:
  836. mesh_mpm_fsm_restart(wpa_s, sta);
  837. break;
  838. case OPN_ACPT:
  839. case CNF_ACPT:
  840. case OPN_RJCT:
  841. case CNF_RJCT:
  842. reason = sta->mpm_close_reason;
  843. mesh_mpm_send_plink_action(wpa_s, sta,
  844. PLINK_CLOSE, reason);
  845. break;
  846. default:
  847. break;
  848. }
  849. break;
  850. default:
  851. wpa_msg(wpa_s, MSG_DEBUG,
  852. "Unsupported MPM event %s for state %s",
  853. mplevent[event], mplstate[sta->plink_state]);
  854. break;
  855. }
  856. }
  857. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  858. const struct ieee80211_mgmt *mgmt, size_t len)
  859. {
  860. u8 action_field;
  861. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  862. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  863. struct sta_info *sta;
  864. u16 plid = 0, llid = 0;
  865. enum plink_event event;
  866. struct ieee802_11_elems elems;
  867. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  868. const u8 *ies;
  869. size_t ie_len;
  870. int ret;
  871. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  872. return;
  873. action_field = mgmt->u.action.u.slf_prot_action.action;
  874. if (action_field != PLINK_OPEN &&
  875. action_field != PLINK_CONFIRM &&
  876. action_field != PLINK_CLOSE)
  877. return;
  878. ies = mgmt->u.action.u.slf_prot_action.variable;
  879. ie_len = (const u8 *) mgmt + len -
  880. mgmt->u.action.u.slf_prot_action.variable;
  881. /* at least expect mesh id and peering mgmt */
  882. if (ie_len < 2 + 2) {
  883. wpa_printf(MSG_DEBUG,
  884. "MPM: Ignore too short action frame %u ie_len %u",
  885. action_field, (unsigned int) ie_len);
  886. return;
  887. }
  888. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  889. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  890. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  891. WPA_GET_LE16(ies));
  892. ies += 2; /* capability */
  893. ie_len -= 2;
  894. }
  895. if (action_field == PLINK_CONFIRM) {
  896. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  897. ies += 2; /* aid */
  898. ie_len -= 2;
  899. }
  900. /* check for mesh peering, mesh id and mesh config IEs */
  901. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  902. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  903. return;
  904. }
  905. if (!elems.peer_mgmt) {
  906. wpa_printf(MSG_DEBUG,
  907. "MPM: No Mesh Peering Management element");
  908. return;
  909. }
  910. if (action_field != PLINK_CLOSE) {
  911. if (!elems.mesh_id || !elems.mesh_config) {
  912. wpa_printf(MSG_DEBUG,
  913. "MPM: No Mesh ID or Mesh Configuration element");
  914. return;
  915. }
  916. if (!matches_local(wpa_s, &elems)) {
  917. wpa_printf(MSG_DEBUG,
  918. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  919. return;
  920. }
  921. }
  922. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  923. elems.peer_mgmt,
  924. elems.peer_mgmt_len,
  925. &peer_mgmt_ie);
  926. if (ret) {
  927. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  928. return;
  929. }
  930. /* the sender's llid is our plid and vice-versa */
  931. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  932. if (peer_mgmt_ie.plid)
  933. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  934. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  935. sta = ap_get_sta(hapd, mgmt->sa);
  936. /*
  937. * If this is an open frame from an unknown STA, and this is an
  938. * open mesh, then go ahead and add the peer before proceeding.
  939. */
  940. if (!sta && action_field == PLINK_OPEN &&
  941. (!(mconf->security & MESH_CONF_SEC_AMPE) ||
  942. wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa)))
  943. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  944. if (!sta) {
  945. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  946. return;
  947. }
  948. #ifdef CONFIG_SAE
  949. /* peer is in sae_accepted? */
  950. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  951. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  952. return;
  953. }
  954. #endif /* CONFIG_SAE */
  955. if (!sta->my_lid)
  956. mesh_mpm_init_link(wpa_s, sta);
  957. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  958. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  959. &mgmt->u.action.category,
  960. peer_mgmt_ie.chosen_pmk,
  961. ies, ie_len)) {
  962. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  963. return;
  964. }
  965. if (sta->plink_state == PLINK_BLOCKED) {
  966. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  967. return;
  968. }
  969. /* Now we will figure out the appropriate event... */
  970. switch (action_field) {
  971. case PLINK_OPEN:
  972. if (plink_free_count(hapd) == 0) {
  973. event = OPN_IGNR;
  974. wpa_printf(MSG_INFO,
  975. "MPM: Peer link num over quota(%d)",
  976. hapd->max_plinks);
  977. } else if (sta->peer_lid && sta->peer_lid != plid) {
  978. event = OPN_IGNR;
  979. } else {
  980. sta->peer_lid = plid;
  981. event = OPN_ACPT;
  982. }
  983. break;
  984. case PLINK_CONFIRM:
  985. if (plink_free_count(hapd) == 0) {
  986. event = CNF_IGNR;
  987. wpa_printf(MSG_INFO,
  988. "MPM: Peer link num over quota(%d)",
  989. hapd->max_plinks);
  990. } else if (sta->my_lid != llid ||
  991. (sta->peer_lid && sta->peer_lid != plid)) {
  992. event = CNF_IGNR;
  993. } else {
  994. if (!sta->peer_lid)
  995. sta->peer_lid = plid;
  996. event = CNF_ACPT;
  997. }
  998. break;
  999. case PLINK_CLOSE:
  1000. if (sta->plink_state == PLINK_ESTAB)
  1001. /* Do not check for llid or plid. This does not
  1002. * follow the standard but since multiple plinks
  1003. * per cand are not supported, it is necessary in
  1004. * order to avoid a livelock when MP A sees an
  1005. * establish peer link to MP B but MP B does not
  1006. * see it. This can be caused by a timeout in
  1007. * B's peer link establishment or B being
  1008. * restarted.
  1009. */
  1010. event = CLS_ACPT;
  1011. else if (sta->peer_lid != plid)
  1012. event = CLS_IGNR;
  1013. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  1014. event = CLS_IGNR;
  1015. else
  1016. event = CLS_ACPT;
  1017. break;
  1018. default:
  1019. /*
  1020. * This cannot be hit due to the action_field check above, but
  1021. * compilers may not be able to figure that out and can warn
  1022. * about uninitialized event below.
  1023. */
  1024. return;
  1025. }
  1026. mesh_mpm_fsm(wpa_s, sta, event);
  1027. }
  1028. /* called by ap_free_sta */
  1029. void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  1030. {
  1031. if (sta->plink_state == PLINK_ESTAB)
  1032. hapd->num_plinks--;
  1033. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  1034. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  1035. }