mesh_mpm.c 31 KB

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