mesh_mpm.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. sta->ht_capabilities) {
  573. wpa_msg(wpa_s, MSG_DEBUG, MACSTR
  574. " does not support 40 MHz bandwidth",
  575. MAC2STR(sta->addr));
  576. set_disable_ht40(sta->ht_capabilities, 1);
  577. }
  578. update_ht_state(data, sta);
  579. #endif /* CONFIG_IEEE80211N */
  580. #ifdef CONFIG_IEEE80211AC
  581. copy_sta_vht_capab(data, sta, elems->vht_capabilities);
  582. set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
  583. #endif /* CONFIG_IEEE80211AC */
  584. if (hostapd_get_aid(data, sta) < 0) {
  585. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  586. ap_free_sta(data, sta);
  587. return NULL;
  588. }
  589. /* insert into driver */
  590. os_memset(&params, 0, sizeof(params));
  591. params.supp_rates = sta->supported_rates;
  592. params.supp_rates_len = sta->supported_rates_len;
  593. params.addr = addr;
  594. params.plink_state = sta->plink_state;
  595. params.aid = sta->aid;
  596. params.peer_aid = sta->peer_aid;
  597. params.listen_interval = 100;
  598. params.ht_capabilities = sta->ht_capabilities;
  599. params.vht_capabilities = sta->vht_capabilities;
  600. params.flags |= WPA_STA_WMM;
  601. params.flags_mask |= WPA_STA_AUTHENTICATED;
  602. if (conf->security == MESH_CONF_SEC_NONE) {
  603. params.flags |= WPA_STA_AUTHORIZED;
  604. params.flags |= WPA_STA_AUTHENTICATED;
  605. } else {
  606. sta->flags |= WLAN_STA_MFP;
  607. params.flags |= WPA_STA_MFP;
  608. }
  609. ret = wpa_drv_sta_add(wpa_s, &params);
  610. if (ret) {
  611. wpa_msg(wpa_s, MSG_ERROR,
  612. "Driver failed to insert " MACSTR ": %d",
  613. MAC2STR(addr), ret);
  614. ap_free_sta(data, sta);
  615. return NULL;
  616. }
  617. return sta;
  618. }
  619. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  620. struct ieee802_11_elems *elems)
  621. {
  622. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  623. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  624. struct sta_info *sta;
  625. struct wpa_ssid *ssid = wpa_s->current_ssid;
  626. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  627. if (!sta)
  628. return;
  629. if (ssid && ssid->no_auto_peer &&
  630. (is_zero_ether_addr(data->mesh_required_peer) ||
  631. os_memcmp(data->mesh_required_peer, addr, ETH_ALEN) != 0)) {
  632. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  633. MACSTR " because of no_auto_peer", MAC2STR(addr));
  634. if (data->mesh_pending_auth) {
  635. struct os_reltime age;
  636. const struct ieee80211_mgmt *mgmt;
  637. struct hostapd_frame_info fi;
  638. mgmt = wpabuf_head(data->mesh_pending_auth);
  639. os_reltime_age(&data->mesh_pending_auth_time, &age);
  640. if (age.sec < 2 &&
  641. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  642. wpa_printf(MSG_DEBUG,
  643. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  644. (unsigned int) age.sec,
  645. (unsigned int) age.usec);
  646. os_memset(&fi, 0, sizeof(fi));
  647. ieee802_11_mgmt(
  648. data,
  649. wpabuf_head(data->mesh_pending_auth),
  650. wpabuf_len(data->mesh_pending_auth),
  651. &fi);
  652. }
  653. wpabuf_free(data->mesh_pending_auth);
  654. data->mesh_pending_auth = NULL;
  655. }
  656. return;
  657. }
  658. if (conf->security == MESH_CONF_SEC_NONE) {
  659. if (sta->plink_state < PLINK_OPN_SNT ||
  660. sta->plink_state > PLINK_ESTAB)
  661. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_SNT);
  662. } else {
  663. mesh_rsn_auth_sae_sta(wpa_s, sta);
  664. }
  665. }
  666. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  667. {
  668. struct hostapd_frame_info fi;
  669. os_memset(&fi, 0, sizeof(fi));
  670. fi.datarate = rx_mgmt->datarate;
  671. fi.ssi_signal = rx_mgmt->ssi_signal;
  672. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  673. rx_mgmt->frame_len, &fi);
  674. }
  675. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  676. struct sta_info *sta)
  677. {
  678. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  679. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  680. u8 seq[6] = {};
  681. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  682. MAC2STR(sta->addr));
  683. if (conf->security & MESH_CONF_SEC_AMPE) {
  684. wpa_hexdump_key(MSG_DEBUG, "mesh: MTK", sta->mtk, sta->mtk_len);
  685. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->pairwise_cipher),
  686. sta->addr, 0, 0, seq, sizeof(seq),
  687. sta->mtk, sta->mtk_len);
  688. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK Key RSC",
  689. sta->mgtk_rsc, sizeof(sta->mgtk_rsc));
  690. wpa_hexdump_key(MSG_DEBUG, "mesh: RX MGTK",
  691. sta->mgtk, sta->mgtk_len);
  692. wpa_drv_set_key(wpa_s, wpa_cipher_to_alg(conf->group_cipher),
  693. sta->addr, sta->mgtk_key_id, 0,
  694. sta->mgtk_rsc, sizeof(sta->mgtk_rsc),
  695. sta->mgtk, sta->mgtk_len);
  696. if (sta->igtk_len) {
  697. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK Key RSC",
  698. sta->igtk_rsc, sizeof(sta->igtk_rsc));
  699. wpa_hexdump_key(MSG_DEBUG, "mesh: RX IGTK",
  700. sta->igtk, sta->igtk_len);
  701. wpa_drv_set_key(
  702. wpa_s,
  703. wpa_cipher_to_alg(conf->mgmt_group_cipher),
  704. sta->addr, sta->igtk_key_id, 0,
  705. sta->igtk_rsc, sizeof(sta->igtk_rsc),
  706. sta->igtk, sta->igtk_len);
  707. }
  708. }
  709. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  710. hapd->num_plinks++;
  711. sta->flags |= WLAN_STA_ASSOC;
  712. sta->mesh_sae_pmksa_caching = 0;
  713. eloop_cancel_timeout(peer_add_timer, wpa_s, NULL);
  714. peer_add_timer(wpa_s, NULL);
  715. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  716. /* Send ctrl event */
  717. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  718. MAC2STR(sta->addr));
  719. }
  720. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  721. enum plink_event event, u16 reason)
  722. {
  723. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  724. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  725. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  726. MAC2STR(sta->addr), mplstate[sta->plink_state],
  727. mplevent[event]);
  728. switch (sta->plink_state) {
  729. case PLINK_IDLE:
  730. switch (event) {
  731. case CLS_ACPT:
  732. mesh_mpm_fsm_restart(wpa_s, sta);
  733. break;
  734. case OPN_ACPT:
  735. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPN_RCVD);
  736. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  737. 0);
  738. break;
  739. case REQ_RJCT:
  740. mesh_mpm_send_plink_action(wpa_s, sta,
  741. PLINK_CLOSE, reason);
  742. break;
  743. default:
  744. break;
  745. }
  746. break;
  747. case PLINK_OPN_SNT:
  748. switch (event) {
  749. case OPN_RJCT:
  750. case CNF_RJCT:
  751. if (!reason)
  752. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  753. /* fall-through */
  754. case CLS_ACPT:
  755. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  756. if (!reason)
  757. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  758. eloop_register_timeout(
  759. conf->dot11MeshHoldingTimeout / 1000,
  760. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  761. plink_timer, wpa_s, sta);
  762. mesh_mpm_send_plink_action(wpa_s, sta,
  763. PLINK_CLOSE, reason);
  764. break;
  765. case OPN_ACPT:
  766. /* retry timer is left untouched */
  767. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPN_RCVD);
  768. mesh_mpm_send_plink_action(wpa_s, sta,
  769. PLINK_CONFIRM, 0);
  770. break;
  771. case CNF_ACPT:
  772. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  773. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  774. eloop_register_timeout(
  775. conf->dot11MeshConfirmTimeout / 1000,
  776. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  777. plink_timer, wpa_s, sta);
  778. break;
  779. default:
  780. break;
  781. }
  782. break;
  783. case PLINK_OPN_RCVD:
  784. switch (event) {
  785. case OPN_RJCT:
  786. case CNF_RJCT:
  787. if (!reason)
  788. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  789. /* fall-through */
  790. case CLS_ACPT:
  791. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  792. if (!reason)
  793. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  794. eloop_register_timeout(
  795. conf->dot11MeshHoldingTimeout / 1000,
  796. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  797. plink_timer, wpa_s, sta);
  798. sta->mpm_close_reason = reason;
  799. mesh_mpm_send_plink_action(wpa_s, sta,
  800. PLINK_CLOSE, reason);
  801. break;
  802. case OPN_ACPT:
  803. mesh_mpm_send_plink_action(wpa_s, sta,
  804. PLINK_CONFIRM, 0);
  805. break;
  806. case CNF_ACPT:
  807. if (conf->security & MESH_CONF_SEC_AMPE)
  808. mesh_rsn_derive_mtk(wpa_s, sta);
  809. mesh_mpm_plink_estab(wpa_s, sta);
  810. break;
  811. default:
  812. break;
  813. }
  814. break;
  815. case PLINK_CNF_RCVD:
  816. switch (event) {
  817. case OPN_RJCT:
  818. case CNF_RJCT:
  819. if (!reason)
  820. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  821. /* fall-through */
  822. case CLS_ACPT:
  823. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  824. if (!reason)
  825. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  826. eloop_register_timeout(
  827. conf->dot11MeshHoldingTimeout / 1000,
  828. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  829. plink_timer, wpa_s, sta);
  830. sta->mpm_close_reason = reason;
  831. mesh_mpm_send_plink_action(wpa_s, sta,
  832. PLINK_CLOSE, reason);
  833. break;
  834. case OPN_ACPT:
  835. if (conf->security & MESH_CONF_SEC_AMPE)
  836. mesh_rsn_derive_mtk(wpa_s, sta);
  837. mesh_mpm_plink_estab(wpa_s, sta);
  838. mesh_mpm_send_plink_action(wpa_s, sta,
  839. PLINK_CONFIRM, 0);
  840. break;
  841. default:
  842. break;
  843. }
  844. break;
  845. case PLINK_ESTAB:
  846. switch (event) {
  847. case OPN_RJCT:
  848. case CNF_RJCT:
  849. case CLS_ACPT:
  850. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  851. if (!reason)
  852. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  853. eloop_register_timeout(
  854. conf->dot11MeshHoldingTimeout / 1000,
  855. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  856. plink_timer, wpa_s, sta);
  857. sta->mpm_close_reason = reason;
  858. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  859. " closed with reason %d",
  860. MAC2STR(sta->addr), reason);
  861. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  862. MAC2STR(sta->addr));
  863. hapd->num_plinks--;
  864. mesh_mpm_send_plink_action(wpa_s, sta,
  865. PLINK_CLOSE, reason);
  866. break;
  867. case OPN_ACPT:
  868. mesh_mpm_send_plink_action(wpa_s, sta,
  869. PLINK_CONFIRM, 0);
  870. break;
  871. default:
  872. break;
  873. }
  874. break;
  875. case PLINK_HOLDING:
  876. switch (event) {
  877. case CLS_ACPT:
  878. mesh_mpm_fsm_restart(wpa_s, sta);
  879. break;
  880. case OPN_ACPT:
  881. case CNF_ACPT:
  882. case OPN_RJCT:
  883. case CNF_RJCT:
  884. reason = sta->mpm_close_reason;
  885. mesh_mpm_send_plink_action(wpa_s, sta,
  886. PLINK_CLOSE, reason);
  887. break;
  888. default:
  889. break;
  890. }
  891. break;
  892. default:
  893. wpa_msg(wpa_s, MSG_DEBUG,
  894. "Unsupported MPM event %s for state %s",
  895. mplevent[event], mplstate[sta->plink_state]);
  896. break;
  897. }
  898. }
  899. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  900. const struct ieee80211_mgmt *mgmt, size_t len)
  901. {
  902. u8 action_field;
  903. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  904. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  905. struct sta_info *sta;
  906. u16 plid = 0, llid = 0, aid = 0;
  907. enum plink_event event;
  908. struct ieee802_11_elems elems;
  909. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  910. const u8 *ies;
  911. size_t ie_len;
  912. int ret;
  913. u16 reason = 0;
  914. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  915. return;
  916. action_field = mgmt->u.action.u.slf_prot_action.action;
  917. if (action_field != PLINK_OPEN &&
  918. action_field != PLINK_CONFIRM &&
  919. action_field != PLINK_CLOSE)
  920. return;
  921. ies = mgmt->u.action.u.slf_prot_action.variable;
  922. ie_len = (const u8 *) mgmt + len -
  923. mgmt->u.action.u.slf_prot_action.variable;
  924. /* at least expect mesh id and peering mgmt */
  925. if (ie_len < 2 + 2) {
  926. wpa_printf(MSG_DEBUG,
  927. "MPM: Ignore too short action frame %u ie_len %u",
  928. action_field, (unsigned int) ie_len);
  929. return;
  930. }
  931. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  932. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  933. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  934. WPA_GET_LE16(ies));
  935. ies += 2; /* capability */
  936. ie_len -= 2;
  937. }
  938. if (action_field == PLINK_CONFIRM) {
  939. aid = WPA_GET_LE16(ies);
  940. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", aid);
  941. ies += 2; /* aid */
  942. ie_len -= 2;
  943. }
  944. /* check for mesh peering, mesh id and mesh config IEs */
  945. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  946. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  947. return;
  948. }
  949. if (!elems.peer_mgmt) {
  950. wpa_printf(MSG_DEBUG,
  951. "MPM: No Mesh Peering Management element");
  952. return;
  953. }
  954. if (action_field != PLINK_CLOSE) {
  955. if (!elems.mesh_id || !elems.mesh_config) {
  956. wpa_printf(MSG_DEBUG,
  957. "MPM: No Mesh ID or Mesh Configuration element");
  958. return;
  959. }
  960. if (!matches_local(wpa_s, &elems)) {
  961. wpa_printf(MSG_DEBUG,
  962. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  963. return;
  964. }
  965. }
  966. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  967. elems.peer_mgmt,
  968. elems.peer_mgmt_len,
  969. &peer_mgmt_ie);
  970. if (ret) {
  971. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  972. return;
  973. }
  974. /* the sender's llid is our plid and vice-versa */
  975. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  976. if (peer_mgmt_ie.plid)
  977. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  978. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  979. if (action_field == PLINK_CLOSE)
  980. wpa_printf(MSG_DEBUG, "MPM: close reason=%u",
  981. WPA_GET_LE16(peer_mgmt_ie.reason));
  982. sta = ap_get_sta(hapd, mgmt->sa);
  983. /*
  984. * If this is an open frame from an unknown STA, and this is an
  985. * open mesh, then go ahead and add the peer before proceeding.
  986. */
  987. if (!sta && action_field == PLINK_OPEN &&
  988. (!(mconf->security & MESH_CONF_SEC_AMPE) ||
  989. wpa_auth_pmksa_get(hapd->wpa_auth, mgmt->sa, NULL)))
  990. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  991. if (!sta) {
  992. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  993. return;
  994. }
  995. #ifdef CONFIG_SAE
  996. /* peer is in sae_accepted? */
  997. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  998. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  999. return;
  1000. }
  1001. #endif /* CONFIG_SAE */
  1002. if (!sta->my_lid)
  1003. mesh_mpm_init_link(wpa_s, sta);
  1004. if (mconf->security & MESH_CONF_SEC_AMPE) {
  1005. int res;
  1006. res = mesh_rsn_process_ampe(wpa_s, sta, &elems,
  1007. &mgmt->u.action.category,
  1008. peer_mgmt_ie.chosen_pmk,
  1009. ies, ie_len);
  1010. if (res) {
  1011. wpa_printf(MSG_DEBUG,
  1012. "MPM: RSN process rejected frame (res=%d)",
  1013. res);
  1014. if (action_field == PLINK_OPEN && res == -2) {
  1015. /* AES-SIV decryption failed */
  1016. mesh_mpm_fsm(wpa_s, sta, OPN_RJCT,
  1017. WLAN_REASON_MESH_INVALID_GTK);
  1018. }
  1019. return;
  1020. }
  1021. }
  1022. if (sta->plink_state == PLINK_BLOCKED) {
  1023. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  1024. return;
  1025. }
  1026. /* Now we will figure out the appropriate event... */
  1027. switch (action_field) {
  1028. case PLINK_OPEN:
  1029. if (plink_free_count(hapd) == 0) {
  1030. event = REQ_RJCT;
  1031. reason = WLAN_REASON_MESH_MAX_PEERS;
  1032. wpa_printf(MSG_INFO,
  1033. "MPM: Peer link num over quota(%d)",
  1034. hapd->max_plinks);
  1035. } else if (sta->peer_lid && sta->peer_lid != plid) {
  1036. wpa_printf(MSG_DEBUG,
  1037. "MPM: peer_lid mismatch: 0x%x != 0x%x",
  1038. sta->peer_lid, plid);
  1039. return; /* no FSM event */
  1040. } else {
  1041. sta->peer_lid = plid;
  1042. event = OPN_ACPT;
  1043. }
  1044. break;
  1045. case PLINK_CONFIRM:
  1046. if (plink_free_count(hapd) == 0) {
  1047. event = REQ_RJCT;
  1048. reason = WLAN_REASON_MESH_MAX_PEERS;
  1049. wpa_printf(MSG_INFO,
  1050. "MPM: Peer link num over quota(%d)",
  1051. hapd->max_plinks);
  1052. } else if (sta->my_lid != llid ||
  1053. (sta->peer_lid && sta->peer_lid != plid)) {
  1054. wpa_printf(MSG_DEBUG,
  1055. "MPM: lid mismatch: my_lid: 0x%x != 0x%x or peer_lid: 0x%x != 0x%x",
  1056. sta->my_lid, llid, sta->peer_lid, plid);
  1057. return; /* no FSM event */
  1058. } else {
  1059. if (!sta->peer_lid)
  1060. sta->peer_lid = plid;
  1061. sta->peer_aid = aid;
  1062. event = CNF_ACPT;
  1063. }
  1064. break;
  1065. case PLINK_CLOSE:
  1066. if (sta->plink_state == PLINK_ESTAB)
  1067. /* Do not check for llid or plid. This does not
  1068. * follow the standard but since multiple plinks
  1069. * per cand are not supported, it is necessary in
  1070. * order to avoid a livelock when MP A sees an
  1071. * establish peer link to MP B but MP B does not
  1072. * see it. This can be caused by a timeout in
  1073. * B's peer link establishment or B being
  1074. * restarted.
  1075. */
  1076. event = CLS_ACPT;
  1077. else if (sta->peer_lid != plid) {
  1078. wpa_printf(MSG_DEBUG,
  1079. "MPM: peer_lid mismatch: 0x%x != 0x%x",
  1080. sta->peer_lid, plid);
  1081. return; /* no FSM event */
  1082. } else if (peer_mgmt_ie.plid && sta->my_lid != llid) {
  1083. wpa_printf(MSG_DEBUG,
  1084. "MPM: my_lid mismatch: 0x%x != 0x%x",
  1085. sta->my_lid, llid);
  1086. return; /* no FSM event */
  1087. } else {
  1088. event = CLS_ACPT;
  1089. }
  1090. break;
  1091. default:
  1092. /*
  1093. * This cannot be hit due to the action_field check above, but
  1094. * compilers may not be able to figure that out and can warn
  1095. * about uninitialized event below.
  1096. */
  1097. return;
  1098. }
  1099. mesh_mpm_fsm(wpa_s, sta, event, reason);
  1100. }
  1101. /* called by ap_free_sta */
  1102. void mesh_mpm_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
  1103. {
  1104. if (sta->plink_state == PLINK_ESTAB)
  1105. hapd->num_plinks--;
  1106. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  1107. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  1108. }