mesh_mpm.c 33 KB

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