mesh_mpm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 "common/hw_features_common.h"
  13. #include "ap/hostapd.h"
  14. #include "ap/sta_info.h"
  15. #include "ap/ieee802_11.h"
  16. #include "ap/wpa_auth.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "driver_i.h"
  19. #include "mesh_mpm.h"
  20. #include "mesh_rsn.h"
  21. #include "notify.h"
  22. struct mesh_peer_mgmt_ie {
  23. const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
  24. const u8 *llid; /* Local Link ID (2 octets) */
  25. const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
  26. const u8 *reason; /* Reason Code (conditional, 2 octets) */
  27. const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
  28. };
  29. static void plink_timer(void *eloop_ctx, void *user_data);
  30. enum plink_event {
  31. PLINK_UNDEFINED,
  32. OPN_ACPT,
  33. OPN_RJCT,
  34. CNF_ACPT,
  35. CNF_RJCT,
  36. CLS_ACPT,
  37. REQ_RJCT
  38. };
  39. static const char * const mplstate[] = {
  40. [0] = "UNINITIALIZED",
  41. [PLINK_IDLE] = "IDLE",
  42. [PLINK_OPN_SNT] = "OPN_SNT",
  43. [PLINK_OPN_RCVD] = "OPN_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. [CNF_ACPT] = "CNF_ACPT",
  54. [CNF_RJCT] = "CNF_RJCT",
  55. [CLS_ACPT] = "CLS_ACPT",
  56. [REQ_RJCT] = "REQ_RJCT",
  57. };
  58. static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
  59. u8 action_field,
  60. const u8 *ie, size_t len,
  61. struct mesh_peer_mgmt_ie *mpm_ie)
  62. {
  63. os_memset(mpm_ie, 0, sizeof(*mpm_ie));
  64. /* Remove optional Chosen PMK field at end */
  65. if (len >= SAE_PMKID_LEN) {
  66. mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
  67. len -= SAE_PMKID_LEN;
  68. }
  69. if ((action_field == PLINK_OPEN && len != 4) ||
  70. (action_field == PLINK_CONFIRM && len != 6) ||
  71. (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
  72. wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
  73. return -1;
  74. }
  75. /* required fields */
  76. if (len < 4)
  77. return -1;
  78. mpm_ie->proto_id = ie;
  79. mpm_ie->llid = ie + 2;
  80. ie += 4;
  81. len -= 4;
  82. /* close reason is always present at end for close */
  83. if (action_field == PLINK_CLOSE) {
  84. if (len < 2)
  85. return -1;
  86. mpm_ie->reason = ie + len - 2;
  87. len -= 2;
  88. }
  89. /* Peer Link ID, present for confirm, and possibly close */
  90. if (len >= 2)
  91. mpm_ie->plid = ie;
  92. return 0;
  93. }
  94. static int plink_free_count(struct hostapd_data *hapd)
  95. {
  96. if (hapd->max_plinks > hapd->num_plinks)
  97. return hapd->max_plinks - hapd->num_plinks;
  98. return 0;
  99. }
  100. static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
  101. struct sta_info *sta,
  102. struct ieee802_11_elems *elems)
  103. {
  104. if (!elems->supp_rates) {
  105. wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
  106. MAC2STR(sta->addr));
  107. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  108. }
  109. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  110. sizeof(sta->supported_rates)) {
  111. wpa_msg(wpa_s, MSG_ERROR,
  112. "Invalid supported rates element length " MACSTR
  113. " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
  114. elems->ext_supp_rates_len);
  115. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  116. }
  117. sta->supported_rates_len = merge_byte_arrays(
  118. sta->supported_rates, sizeof(sta->supported_rates),
  119. elems->supp_rates, elems->supp_rates_len,
  120. elems->ext_supp_rates, elems->ext_supp_rates_len);
  121. return WLAN_STATUS_SUCCESS;
  122. }
  123. /* return true if elems from a neighbor match this MBSS */
  124. static Boolean matches_local(struct wpa_supplicant *wpa_s,
  125. struct ieee802_11_elems *elems)
  126. {
  127. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  128. if (elems->mesh_config_len < 5)
  129. return FALSE;
  130. return (mconf->meshid_len == elems->mesh_id_len &&
  131. os_memcmp(mconf->meshid, elems->mesh_id,
  132. elems->mesh_id_len) == 0 &&
  133. mconf->mesh_pp_id == elems->mesh_config[0] &&
  134. mconf->mesh_pm_id == elems->mesh_config[1] &&
  135. mconf->mesh_cc_id == elems->mesh_config[2] &&
  136. mconf->mesh_sp_id == elems->mesh_config[3] &&
  137. mconf->mesh_auth_id == elems->mesh_config[4]);
  138. }
  139. /* check if local link id is already used with another peer */
  140. static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
  141. {
  142. struct sta_info *sta;
  143. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  144. for (sta = hapd->sta_list; sta; sta = sta->next) {
  145. if (sta->my_lid == llid)
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. /* generate an llid for a link and set to initial state */
  151. static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
  152. struct sta_info *sta)
  153. {
  154. u16 llid;
  155. do {
  156. if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
  157. continue;
  158. } while (!llid || llid_in_use(wpa_s, llid));
  159. sta->my_lid = llid;
  160. sta->peer_lid = 0;
  161. sta->peer_aid = 0;
  162. /*
  163. * We do not use wpa_mesh_set_plink_state() here because there is no
  164. * entry in kernel yet.
  165. */
  166. sta->plink_state = PLINK_IDLE;
  167. }
  168. static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
  169. struct sta_info *sta,
  170. enum plink_action_field type,
  171. u16 close_reason)
  172. {
  173. struct wpabuf *buf;
  174. struct hostapd_iface *ifmsh = wpa_s->ifmsh;
  175. struct hostapd_data *bss = ifmsh->bss[0];
  176. struct mesh_conf *conf = ifmsh->mconf;
  177. u8 supp_rates[2 + 2 + 32];
  178. u8 *pos, *cat;
  179. u8 ie_len, add_plid = 0;
  180. int ret;
  181. int ampe = conf->security & MESH_CONF_SEC_AMPE;
  182. size_t buf_len;
  183. if (!sta)
  184. return;
  185. buf_len = 2 + /* capability info */
  186. 2 + /* AID */
  187. 2 + 8 + /* supported rates */
  188. 2 + (32 - 8) +
  189. 2 + 32 + /* mesh ID */
  190. 2 + 7 + /* mesh config */
  191. 2 + 23 + /* peering management */
  192. 2 + 96 + /* AMPE */
  193. 2 + 16; /* MIC */
  194. #ifdef CONFIG_IEEE80211N
  195. if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
  196. buf_len += 2 + 26 + /* HT capabilities */
  197. 2 + 22; /* HT operation */
  198. }
  199. #endif /* CONFIG_IEEE80211N */
  200. #ifdef CONFIG_IEEE80211AC
  201. if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
  202. buf_len += 2 + 12 + /* VHT Capabilities */
  203. 2 + 5; /* VHT Operation */
  204. }
  205. #endif /* CONFIG_IEEE80211AC */
  206. if (type != PLINK_CLOSE)
  207. buf_len += conf->rsn_ie_len; /* RSN IE */
  208. buf = wpabuf_alloc(buf_len);
  209. if (!buf)
  210. return;
  211. cat = wpabuf_mhead_u8(buf);
  212. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  213. wpabuf_put_u8(buf, type);
  214. if (type != PLINK_CLOSE) {
  215. u8 info;
  216. /* capability info */
  217. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  218. /* aid */
  219. if (type == PLINK_CONFIRM)
  220. wpabuf_put_le16(buf, sta->aid);
  221. /* IE: supp + ext. supp rates */
  222. pos = hostapd_eid_supp_rates(bss, supp_rates);
  223. pos = hostapd_eid_ext_supp_rates(bss, pos);
  224. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  225. /* IE: RSN IE */
  226. wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
  227. /* IE: Mesh ID */
  228. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  229. wpabuf_put_u8(buf, conf->meshid_len);
  230. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  231. /* IE: mesh conf */
  232. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  233. wpabuf_put_u8(buf, 7);
  234. wpabuf_put_u8(buf, conf->mesh_pp_id);
  235. wpabuf_put_u8(buf, conf->mesh_pm_id);
  236. wpabuf_put_u8(buf, conf->mesh_cc_id);
  237. wpabuf_put_u8(buf, conf->mesh_sp_id);
  238. wpabuf_put_u8(buf, conf->mesh_auth_id);
  239. info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
  240. /* TODO: Add Connected to Mesh Gate/AS subfields */
  241. wpabuf_put_u8(buf, info);
  242. /* always forwarding & accepting plinks for now */
  243. wpabuf_put_u8(buf, MESH_CAP_ACCEPT_ADDITIONAL_PEER |
  244. MESH_CAP_FORWARDING);
  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, 0);
  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.peer_aid = sta->peer_aid;
  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_OPN_RCVD:
  359. case PLINK_OPN_SNT:
  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_OPN_SNT <= 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_OPN_SNT);
  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_OPN_SNT);
  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. #ifdef CONFIG_IEEE80211N
  543. struct ieee80211_ht_operation *oper;
  544. #endif /* CONFIG_IEEE80211N */
  545. int ret;
  546. if (elems->mesh_config_len >= 7 &&
  547. !(elems->mesh_config[6] & MESH_CAP_ACCEPT_ADDITIONAL_PEER)) {
  548. wpa_msg(wpa_s, MSG_DEBUG,
  549. "mesh: Ignore a crowded peer " MACSTR,
  550. MAC2STR(addr));
  551. return NULL;
  552. }
  553. sta = ap_get_sta(data, addr);
  554. if (!sta) {
  555. sta = ap_sta_add(data, addr);
  556. if (!sta)
  557. return NULL;
  558. }
  559. /* Set WMM by default since Mesh STAs are QoS STAs */
  560. sta->flags |= WLAN_STA_WMM;
  561. /* initialize sta */
  562. if (copy_supp_rates(wpa_s, sta, elems)) {
  563. ap_free_sta(data, sta);
  564. return NULL;
  565. }
  566. if (!sta->my_lid)
  567. mesh_mpm_init_link(wpa_s, sta);
  568. #ifdef CONFIG_IEEE80211N
  569. copy_sta_ht_capab(data, sta, elems->ht_capabilities);
  570. oper = (struct ieee80211_ht_operation *) elems->ht_operation;
  571. if (oper &&
  572. !(oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) &&
  573. sta->ht_capabilities) {
  574. wpa_msg(wpa_s, MSG_DEBUG, MACSTR
  575. " does not support 40 MHz bandwidth",
  576. MAC2STR(sta->addr));
  577. set_disable_ht40(sta->ht_capabilities, 1);
  578. }
  579. update_ht_state(data, sta);
  580. #endif /* CONFIG_IEEE80211N */
  581. #ifdef CONFIG_IEEE80211AC
  582. copy_sta_vht_capab(data, sta, elems->vht_capabilities);
  583. set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
  584. #endif /* CONFIG_IEEE80211AC */
  585. if (hostapd_get_aid(data, sta) < 0) {
  586. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  587. ap_free_sta(data, sta);
  588. return NULL;
  589. }
  590. /* insert into driver */
  591. os_memset(&params, 0, sizeof(params));
  592. params.supp_rates = sta->supported_rates;
  593. params.supp_rates_len = sta->supported_rates_len;
  594. params.addr = addr;
  595. params.plink_state = sta->plink_state;
  596. params.aid = sta->aid;
  597. params.peer_aid = sta->peer_aid;
  598. params.listen_interval = 100;
  599. params.ht_capabilities = sta->ht_capabilities;
  600. params.vht_capabilities = sta->vht_capabilities;
  601. params.flags |= WPA_STA_WMM;
  602. params.flags_mask |= WPA_STA_AUTHENTICATED;
  603. if (conf->security == MESH_CONF_SEC_NONE) {
  604. params.flags |= WPA_STA_AUTHORIZED;
  605. params.flags |= WPA_STA_AUTHENTICATED;
  606. } else {
  607. sta->flags |= WLAN_STA_MFP;
  608. params.flags |= WPA_STA_MFP;
  609. }
  610. ret = wpa_drv_sta_add(wpa_s, &params);
  611. if (ret) {
  612. wpa_msg(wpa_s, MSG_ERROR,
  613. "Driver failed to insert " MACSTR ": %d",
  614. MAC2STR(addr), ret);
  615. ap_free_sta(data, sta);
  616. return NULL;
  617. }
  618. return sta;
  619. }
  620. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  621. struct ieee802_11_elems *elems)
  622. {
  623. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  624. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  625. struct sta_info *sta;
  626. struct wpa_ssid *ssid = wpa_s->current_ssid;
  627. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  628. if (!sta)
  629. return;
  630. if (ssid && ssid->no_auto_peer &&
  631. (is_zero_ether_addr(data->mesh_required_peer) ||
  632. os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
  633. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  634. MACSTR " because of no_auto_peer", MAC2STR(addr));
  635. if (data->mesh_pending_auth) {
  636. struct os_reltime age;
  637. const struct ieee80211_mgmt *mgmt;
  638. struct hostapd_frame_info fi;
  639. mgmt = wpabuf_head(data->mesh_pending_auth);
  640. os_reltime_age(&data->mesh_pending_auth_time, &age);
  641. if (age.sec < 2 &&
  642. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  643. wpa_printf(MSG_DEBUG,
  644. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  645. (unsigned int) age.sec,
  646. (unsigned int) age.usec);
  647. os_memset(&fi, 0, sizeof(fi));
  648. ieee802_11_mgmt(
  649. data,
  650. wpabuf_head(data->mesh_pending_auth),
  651. wpabuf_len(data->mesh_pending_auth),
  652. &fi);
  653. }
  654. wpabuf_free(data->mesh_pending_auth);
  655. data->mesh_pending_auth = NULL;
  656. }
  657. return;
  658. }
  659. if (conf->security == MESH_CONF_SEC_NONE) {
  660. if (sta->plink_state < PLINK_OPN_SNT ||
  661. sta->plink_state > PLINK_ESTAB)
  662. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
  663. } else {
  664. mesh_rsn_auth_sae_sta(wpa_s, sta);
  665. }
  666. }
  667. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  668. {
  669. struct hostapd_frame_info fi;
  670. os_memset(&fi, 0, sizeof(fi));
  671. fi.datarate = rx_mgmt->datarate;
  672. fi.ssi_signal = rx_mgmt->ssi_signal;
  673. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  674. rx_mgmt->frame_len, &fi);
  675. }
  676. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  677. struct sta_info *sta)
  678. {
  679. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  680. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  681. u8 seq[6] = {};
  682. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  683. MAC2STR(sta->addr));
  684. if (conf->security & MESH_CONF_SEC_AMPE) {
  685. wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
  686. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
  687. sta->addr, 0, 0, seq, sizeof(seq),
  688. sta->mtk, sta->mtk_len);
  689. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
  690. sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
  691. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
  692. sta->mgtk, sta->mgtk_len);
  693. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
  694. sta->addr, sta->mgtk_key_id, 0,
  695. sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
  696. sta->mgtk, sta->mgtk_len);
  697. if (sta->igtk_len) {
  698. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
  699. sta->igtk_rsc, sizeof(sta->igtk_rsc));
  700. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
  701. sta->igtk, sta->igtk_len);
  702. wpa_drv_set_key(
  703. wpa_s,
  704. wpa_cipher_to_alg(conf->mgmt_group_cipher),
  705. sta->addr, sta->igtk_key_id, 0,
  706. sta->igtk_rsc, sizeof(sta->igtk_rsc),
  707. sta->igtk, sta->igtk_len);
  708. }
  709. }
  710. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  711. hapd->num_plinks++;
  712. sta->flags |= WLAN_STA_ASSOC;
  713. sta->mesh_sae_pmksa_caching = 0;
  714. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  715. peer_add_timer(wpa_s, NULL);
  716. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  717. /* Send ctrl event */
  718. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  719. MAC2STR(sta->addr));
  720. /* Send D-Bus event */
  721. wpas_notify_mesh_peer_connected(wpa_s, sta->addr);
  722. }
  723. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  724. enum plink_event event, u16 reason)
  725. {
  726. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  727. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  728. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  729. MAC2STR(sta->addr), mplstate[sta->plink_state],
  730. mplevent[event]);
  731. switch (sta->plink_state) {
  732. case PLINK_IDLE:
  733. switch (event) {
  734. case CLS_ACPT:
  735. mesh_mpm_fsm_restart(wpa_s, sta);
  736. break;
  737. case OPN_ACPT:
  738. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
  739. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  740. 0);
  741. break;
  742. case REQ_RJCT:
  743. mesh_mpm_send_plink_action(wpa_s, sta,
  744. PLINK_CLOSE, reason);
  745. break;
  746. default:
  747. break;
  748. }
  749. break;
  750. case PLINK_OPN_SNT:
  751. switch (event) {
  752. case OPN_RJCT:
  753. case CNF_RJCT:
  754. if (!reason)
  755. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  756. /* fall-through */
  757. case CLS_ACPT:
  758. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  759. if (!reason)
  760. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  761. eloop_register_timeout(
  762. conf->dot11MeshHoldingTimeout / 1000,
  763. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  764. plink_timer, wpa_s, sta);
  765. mesh_mpm_send_plink_action(wpa_s, sta,
  766. PLINK_CLOSE, reason);
  767. break;
  768. case OPN_ACPT:
  769. /* retry timer is left untouched */
  770. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
  771. mesh_mpm_send_plink_action(wpa_s, sta,
  772. PLINK_CONFIRM, 0);
  773. break;
  774. case CNF_ACPT:
  775. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  776. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  777. eloop_register_timeout(
  778. conf->dot11MeshConfirmTimeout / 1000,
  779. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  780. plink_timer, wpa_s, sta);
  781. break;
  782. default:
  783. break;
  784. }
  785. break;
  786. case PLINK_OPN_RCVD:
  787. switch (event) {
  788. case OPN_RJCT:
  789. case CNF_RJCT:
  790. if (!reason)
  791. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  792. /* fall-through */
  793. case CLS_ACPT:
  794. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  795. if (!reason)
  796. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  797. eloop_register_timeout(
  798. conf->dot11MeshHoldingTimeout / 1000,
  799. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  800. plink_timer, wpa_s, sta);
  801. sta->mpm_close_reason = reason;
  802. mesh_mpm_send_plink_action(wpa_s, sta,
  803. PLINK_CLOSE, reason);
  804. break;
  805. case OPN_ACPT:
  806. mesh_mpm_send_plink_action(wpa_s, sta,
  807. PLINK_CONFIRM, 0);
  808. break;
  809. case CNF_ACPT:
  810. if (conf->security & MESH_CONF_SEC_AMPE)
  811. mesh_rsn_derive_mtk(wpa_s, sta);
  812. mesh_mpm_plink_estab(wpa_s, sta);
  813. break;
  814. default:
  815. break;
  816. }
  817. break;
  818. case PLINK_CNF_RCVD:
  819. switch (event) {
  820. case OPN_RJCT:
  821. case CNF_RJCT:
  822. if (!reason)
  823. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  824. /* fall-through */
  825. case CLS_ACPT:
  826. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  827. if (!reason)
  828. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  829. eloop_register_timeout(
  830. conf->dot11MeshHoldingTimeout / 1000,
  831. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  832. plink_timer, wpa_s, sta);
  833. sta->mpm_close_reason = reason;
  834. mesh_mpm_send_plink_action(wpa_s, sta,
  835. PLINK_CLOSE, reason);
  836. break;
  837. case OPN_ACPT:
  838. if (conf->security & MESH_CONF_SEC_AMPE)
  839. mesh_rsn_derive_mtk(wpa_s, sta);
  840. mesh_mpm_plink_estab(wpa_s, sta);
  841. mesh_mpm_send_plink_action(wpa_s, sta,
  842. PLINK_CONFIRM, 0);
  843. break;
  844. default:
  845. break;
  846. }
  847. break;
  848. case PLINK_ESTAB:
  849. switch (event) {
  850. case OPN_RJCT:
  851. case CNF_RJCT:
  852. case CLS_ACPT:
  853. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  854. if (!reason)
  855. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  856. eloop_register_timeout(
  857. conf->dot11MeshHoldingTimeout / 1000,
  858. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  859. plink_timer, wpa_s, sta);
  860. sta->mpm_close_reason = reason;
  861. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  862. " closed with reason %d",
  863. MAC2STR(sta->addr), reason);
  864. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  865. MAC2STR(sta->addr));
  866. /* Send D-Bus event */
  867. wpas_notify_mesh_peer_disconnected(wpa_s, sta->addr,
  868. reason);
  869. hapd->num_plinks--;
  870. mesh_mpm_send_plink_action(wpa_s, sta,
  871. PLINK_CLOSE, reason);
  872. break;
  873. case OPN_ACPT:
  874. mesh_mpm_send_plink_action(wpa_s, sta,
  875. PLINK_CONFIRM, 0);
  876. break;
  877. default:
  878. break;
  879. }
  880. break;
  881. case PLINK_HOLDING:
  882. switch (event) {
  883. case CLS_ACPT:
  884. mesh_mpm_fsm_restart(wpa_s, sta);
  885. break;
  886. case OPN_ACPT:
  887. case CNF_ACPT:
  888. case OPN_RJCT:
  889. case CNF_RJCT:
  890. reason = sta->mpm_close_reason;
  891. mesh_mpm_send_plink_action(wpa_s, sta,
  892. PLINK_CLOSE, reason);
  893. break;
  894. default:
  895. break;
  896. }
  897. break;
  898. default:
  899. wpa_msg(wpa_s, MSG_DEBUG,
  900. "Unsupported MPM event %s for state %s",
  901. mplevent[event], mplstate[sta->plink_state]);
  902. break;
  903. }
  904. }
  905. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  906. const struct ieee80211_mgmt *mgmt, size_t len)
  907. {
  908. u8 action_field;
  909. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  910. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  911. struct sta_info *sta;
  912. u16 plid = 0, llid = 0, aid = 0;
  913. enum plink_event event;
  914. struct ieee802_11_elems elems;
  915. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  916. const u8 *ies;
  917. size_t ie_len;
  918. int ret;
  919. u16 reason = 0;
  920. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  921. return;
  922. action_field = mgmt->u.action.u.slf_prot_action.action;
  923. if (action_field != PLINK_OPEN &&
  924. action_field != PLINK_CONFIRM &&
  925. action_field != PLINK_CLOSE)
  926. return;
  927. ies = mgmt->u.action.u.slf_prot_action.variable;
  928. ie_len = (const u8 *) mgmt + len -
  929. mgmt->u.action.u.slf_prot_action.variable;
  930. /* at least expect mesh id and peering mgmt */
  931. if (ie_len < 2 + 2) {
  932. wpa_printf(MSG_DEBUG,
  933. "MPM: Ignore too short action frame %u ie_len %u",
  934. action_field, (unsigned int) ie_len);
  935. return;
  936. }
  937. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  938. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  939. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  940. WPA_GET_LE16(ies));
  941. ies += 2; /* capability */
  942. ie_len -= 2;
  943. }
  944. if (action_field == PLINK_CONFIRM) {
  945. aid = WPA_GET_LE16(ies);
  946. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
  947. ies += 2; /* aid */
  948. ie_len -= 2;
  949. }
  950. /* check for mesh peering, mesh id and mesh config IEs */
  951. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  952. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  953. return;
  954. }
  955. if (!elems.peer_mgmt) {
  956. wpa_printf(MSG_DEBUG,
  957. "MPM: No Mesh Peering Management element");
  958. return;
  959. }
  960. if (action_field != PLINK_CLOSE) {
  961. if (!elems.mesh_id || !elems.mesh_config) {
  962. wpa_printf(MSG_DEBUG,
  963. "MPM: No Mesh ID or Mesh Configuration element");
  964. return;
  965. }
  966. if (!matches_local(wpa_s, &elems)) {
  967. wpa_printf(MSG_DEBUG,
  968. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  969. return;
  970. }
  971. }
  972. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  973. elems.peer_mgmt,
  974. elems.peer_mgmt_len,
  975. &peer_mgmt_ie);
  976. if (ret) {
  977. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  978. return;
  979. }
  980. /* the sender's llid is our plid and vice-versa */
  981. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  982. if (peer_mgmt_ie.plid)
  983. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  984. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  985. if (action_field == PLINK_CLOSE)
  986. wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
  987. WPA_GET_LE16(peer_mgmt_ie.reason));
  988. sta = ap_get_sta(hapd, mgmt->sa);
  989. /*
  990. * If this is an open frame from an unknown STA, and this is an
  991. * open mesh, then go ahead and add the peer before proceeding.
  992. */
  993. if (!sta && action_field == PLINK_OPEN &&
  994. (!(mconf->security & MESH_CONF_SEC_AMPE) ||
  995. wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
  996. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  997. if (!sta) {
  998. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  999. return;
  1000. }
  1001. #ifdef CONFIG_SAE
  1002. /* peer is in sae_accepted? */
  1003. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  1004. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  1005. return;
  1006. }
  1007. #endif /* CONFIG_SAE */
  1008. if (!sta->my_lid)
  1009. mesh_mpm_init_link(wpa_s, sta);
  1010. if (mconf->security & MESH_CONF_SEC_AMPE) {
  1011. int res;
  1012. res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
  1013. &mgmt->u.action.category,
  1014. peer_mgmt_ie.chosen_pmk,
  1015. ies, ie_len);
  1016. if (res) {
  1017. wpa_printf(MSG_DEBUG,
  1018. "MPM: RSN process rejected frame (res=%d)",
  1019. res);
  1020. if (action_field == PLINK_OPEN && res == -2) {
  1021. /* AES-SIV decryption failed */
  1022. mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
  1023. WLAN_REASON_MESH_INVALID_GTK);
  1024. }
  1025. return;
  1026. }
  1027. }
  1028. if (sta->plink_state == PLINK_BLOCKED) {
  1029. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  1030. return;
  1031. }
  1032. /* Now we will figure out the appropriate event... */
  1033. switch (action_field) {
  1034. case PLINK_OPEN:
  1035. if (plink_free_count(hapd) == 0) {
  1036. event = REQ_RJCT;
  1037. reason = WLAN_REASON_MESH_MAX_PEERS;
  1038. wpa_printf(MSG_INFO,
  1039. "MPM: Peer link num over quota(%d)",
  1040. hapd->max_plinks);
  1041. } else if (sta->peer_lid && sta->peer_lid != plid) {
  1042. wpa_printf(MSG_DEBUG,
  1043. "MPM: peer_lid mismatch: 0x%x != 0x%x",
  1044. sta->peer_lid, plid);
  1045. return; /* no FSM event */
  1046. } else {
  1047. sta->peer_lid = plid;
  1048. event = OPN_ACPT;
  1049. }
  1050. break;
  1051. case PLINK_CONFIRM:
  1052. if (plink_free_count(hapd) == 0) {
  1053. event = REQ_RJCT;
  1054. reason = WLAN_REASON_MESH_MAX_PEERS;
  1055. wpa_printf(MSG_INFO,
  1056. "MPM: Peer link num over quota(%d)",
  1057. hapd->max_plinks);
  1058. } else if (sta->my_lid != llid ||
  1059. (sta->peer_lid && sta->peer_lid != plid)) {
  1060. wpa_printf(MSG_DEBUG,
  1061. "MPM: lid mismatch: my_lid: 0x%x != 0x%x or peer_lid: 0x%x != 0x%x",
  1062. sta->my_lid, llid, sta->peer_lid, plid);
  1063. return; /* no FSM event */
  1064. } else {
  1065. if (!sta->peer_lid)
  1066. sta->peer_lid = plid;
  1067. sta->peer_aid = aid;
  1068. event = CNF_ACPT;
  1069. }
  1070. break;
  1071. case PLINK_CLOSE:
  1072. if (sta->plink_state == PLINK_ESTAB)
  1073. /* Do not check for llid or plid. This does not
  1074. * follow the standard but since multiple plinks
  1075. * per cand are not supported, it is necessary in
  1076. * order to avoid a livelock when MP A sees an
  1077. * establish peer link to MP B but MP B does not
  1078. * see it. This can be caused by a timeout in
  1079. * B's peer link establishment or B being
  1080. * restarted.
  1081. */
  1082. event = CLS_ACPT;
  1083. else if (sta->peer_lid != plid) {
  1084. wpa_printf(MSG_DEBUG,
  1085. "MPM: peer_lid mismatch: 0x%x != 0x%x",
  1086. sta->peer_lid, plid);
  1087. return; /* no FSM event */
  1088. } else if (peer_mgmt_ie.plid && sta->my_lid != llid) {
  1089. wpa_printf(MSG_DEBUG,
  1090. "MPM: my_lid mismatch: 0x%x != 0x%x",
  1091. sta->my_lid, llid);
  1092. return; /* no FSM event */
  1093. } else {
  1094. event = CLS_ACPT;
  1095. }
  1096. break;
  1097. default:
  1098. /*
  1099. * This cannot be hit due to the action_field check above, but
  1100. * compilers may not be able to figure that out and can warn
  1101. * about uninitialized event below.
  1102. */
  1103. return;
  1104. }
  1105. mesh_mpm_fsm(wpa_s, sta, event, reason);
  1106. }
  1107. /* called by ap_free_sta */
  1108. void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  1109. {
  1110. if (sta->plink_state == PLINK_ESTAB)
  1111. hapd->num_plinks--;
  1112. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  1113. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  1114. }