mesh_mpm.c 33 KB

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