mesh_mpm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 "wpa_supplicant_i.h"
  16. #include "driver_i.h"
  17. #include "mesh_mpm.h"
  18. #include "mesh_rsn.h"
  19. struct mesh_peer_mgmt_ie {
  20. const u8 *proto_id; /* Mesh Peering Protocol Identifier (2 octets) */
  21. const u8 *llid; /* Local Link ID (2 octets) */
  22. const u8 *plid; /* Peer Link ID (conditional, 2 octets) */
  23. const u8 *reason; /* Reason Code (conditional, 2 octets) */
  24. const u8 *chosen_pmk; /* Chosen PMK (optional, 16 octets) */
  25. };
  26. static void plink_timer(void *eloop_ctx, void *user_data);
  27. enum plink_event {
  28. PLINK_UNDEFINED,
  29. OPN_ACPT,
  30. OPN_RJCT,
  31. OPN_IGNR,
  32. CNF_ACPT,
  33. CNF_RJCT,
  34. CNF_IGNR,
  35. CLS_ACPT,
  36. CLS_IGNR
  37. };
  38. static const char * const mplstate[] = {
  39. [0] = "UNINITIALIZED",
  40. [PLINK_LISTEN] = "LISTEN",
  41. [PLINK_OPEN_SENT] = "OPEN_SENT",
  42. [PLINK_OPEN_RCVD] = "OPEN_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. [OPN_IGNR] = "OPN_IGNR",
  53. [CNF_ACPT] = "CNF_ACPT",
  54. [CNF_RJCT] = "CNF_RJCT",
  55. [CNF_IGNR] = "CNF_IGNR",
  56. [CLS_ACPT] = "CLS_ACPT",
  57. [CLS_IGNR] = "CLS_IGNR"
  58. };
  59. static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
  60. u8 action_field,
  61. const u8 *ie, size_t len,
  62. struct mesh_peer_mgmt_ie *mpm_ie)
  63. {
  64. os_memset(mpm_ie, 0, sizeof(*mpm_ie));
  65. /* Remove optional Chosen PMK field at end */
  66. if (len >= SAE_PMKID_LEN) {
  67. mpm_ie->chosen_pmk = ie + len - SAE_PMKID_LEN;
  68. len -= SAE_PMKID_LEN;
  69. }
  70. if ((action_field == PLINK_OPEN && len != 4) ||
  71. (action_field == PLINK_CONFIRM && len != 6) ||
  72. (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
  73. wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
  74. return -1;
  75. }
  76. /* required fields */
  77. if (len < 4)
  78. return -1;
  79. mpm_ie->proto_id = ie;
  80. mpm_ie->llid = ie + 2;
  81. ie += 4;
  82. len -= 4;
  83. /* close reason is always present at end for close */
  84. if (action_field == PLINK_CLOSE) {
  85. if (len < 2)
  86. return -1;
  87. mpm_ie->reason = ie + len - 2;
  88. len -= 2;
  89. }
  90. /* Peer Link ID, present for confirm, and possibly close */
  91. if (len >= 2)
  92. mpm_ie->plid = ie;
  93. return 0;
  94. }
  95. static int plink_free_count(struct hostapd_data *hapd)
  96. {
  97. if (hapd->max_plinks > hapd->num_plinks)
  98. return hapd->max_plinks - hapd->num_plinks;
  99. return 0;
  100. }
  101. static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
  102. struct sta_info *sta,
  103. struct ieee802_11_elems *elems)
  104. {
  105. if (!elems->supp_rates) {
  106. wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
  107. MAC2STR(sta->addr));
  108. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  109. }
  110. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  111. sizeof(sta->supported_rates)) {
  112. wpa_msg(wpa_s, MSG_ERROR,
  113. "Invalid supported rates element length " MACSTR
  114. " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
  115. elems->ext_supp_rates_len);
  116. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  117. }
  118. sta->supported_rates_len = merge_byte_arrays(
  119. sta->supported_rates, sizeof(sta->supported_rates),
  120. elems->supp_rates, elems->supp_rates_len,
  121. elems->ext_supp_rates, elems->ext_supp_rates_len);
  122. return WLAN_STATUS_SUCCESS;
  123. }
  124. /* return true if elems from a neighbor match this MBSS */
  125. static Boolean matches_local(struct wpa_supplicant *wpa_s,
  126. struct ieee802_11_elems *elems)
  127. {
  128. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  129. if (elems->mesh_config_len < 5)
  130. return FALSE;
  131. return (mconf->meshid_len == elems->mesh_id_len &&
  132. os_memcmp(mconf->meshid, elems->mesh_id,
  133. elems->mesh_id_len) == 0 &&
  134. mconf->mesh_pp_id == elems->mesh_config[0] &&
  135. mconf->mesh_pm_id == elems->mesh_config[1] &&
  136. mconf->mesh_cc_id == elems->mesh_config[2] &&
  137. mconf->mesh_sp_id == elems->mesh_config[3] &&
  138. mconf->mesh_auth_id == elems->mesh_config[4]);
  139. }
  140. /* check if local link id is already used with another peer */
  141. static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
  142. {
  143. struct sta_info *sta;
  144. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  145. for (sta = hapd->sta_list; sta; sta = sta->next) {
  146. if (sta->my_lid == llid)
  147. return TRUE;
  148. }
  149. return FALSE;
  150. }
  151. /* generate an llid for a link and set to initial state */
  152. static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
  153. struct sta_info *sta)
  154. {
  155. u16 llid;
  156. do {
  157. if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
  158. continue;
  159. } while (!llid || llid_in_use(wpa_s, llid));
  160. sta->my_lid = llid;
  161. sta->peer_lid = 0;
  162. /*
  163. * We do not use wpa_mesh_set_plink_state() here because there is no
  164. * entry in kernel yet.
  165. */
  166. sta->plink_state = PLINK_LISTEN;
  167. }
  168. static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
  169. struct sta_info *sta,
  170. enum plink_action_field type,
  171. u16 close_reason)
  172. {
  173. struct wpabuf *buf;
  174. struct hostapd_iface *ifmsh = wpa_s->ifmsh;
  175. struct hostapd_data *bss = ifmsh->bss[0];
  176. struct mesh_conf *conf = ifmsh->mconf;
  177. u8 supp_rates[2 + 2 + 32];
  178. u8 *pos, *cat;
  179. u8 ie_len, add_plid = 0;
  180. int ret;
  181. int ampe = conf->security & MESH_CONF_SEC_AMPE;
  182. size_t buf_len;
  183. if (!sta)
  184. return;
  185. buf_len = 2 + /* capability info */
  186. 2 + /* AID */
  187. 2 + 8 + /* supported rates */
  188. 2 + (32 - 8) +
  189. 2 + 32 + /* mesh ID */
  190. 2 + 7 + /* mesh config */
  191. 2 + 23 + /* peering management */
  192. 2 + 96 + /* AMPE */
  193. 2 + 16; /* MIC */
  194. #ifdef CONFIG_IEEE80211N
  195. if (type != PLINK_CLOSE && wpa_s->mesh_ht_enabled) {
  196. buf_len += 2 + 26 + /* HT capabilities */
  197. 2 + 22; /* HT operation */
  198. }
  199. #endif /* CONFIG_IEEE80211N */
  200. #ifdef CONFIG_IEEE80211AC
  201. if (type != PLINK_CLOSE && wpa_s->mesh_vht_enabled) {
  202. buf_len += 2 + 12 + /* VHT Capabilities */
  203. 2 + 5; /* VHT Operation */
  204. }
  205. #endif /* CONFIG_IEEE80211AC */
  206. if (type != PLINK_CLOSE)
  207. buf_len += conf->rsn_ie_len; /* RSN IE */
  208. buf = wpabuf_alloc(buf_len);
  209. if (!buf)
  210. return;
  211. cat = wpabuf_mhead_u8(buf);
  212. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  213. wpabuf_put_u8(buf, type);
  214. if (type != PLINK_CLOSE) {
  215. u8 info;
  216. /* capability info */
  217. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  218. /* aid */
  219. if (type == PLINK_CONFIRM)
  220. wpabuf_put_le16(buf, sta->aid);
  221. /* IE: supp + ext. supp rates */
  222. pos = hostapd_eid_supp_rates(bss, supp_rates);
  223. pos = hostapd_eid_ext_supp_rates(bss, pos);
  224. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  225. /* IE: RSN IE */
  226. wpabuf_put_data(buf, conf->rsn_ie, conf->rsn_ie_len);
  227. /* IE: Mesh ID */
  228. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  229. wpabuf_put_u8(buf, conf->meshid_len);
  230. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  231. /* IE: mesh conf */
  232. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  233. wpabuf_put_u8(buf, 7);
  234. wpabuf_put_u8(buf, conf->mesh_pp_id);
  235. wpabuf_put_u8(buf, conf->mesh_pm_id);
  236. wpabuf_put_u8(buf, conf->mesh_cc_id);
  237. wpabuf_put_u8(buf, conf->mesh_sp_id);
  238. wpabuf_put_u8(buf, conf->mesh_auth_id);
  239. info = (bss->num_plinks > 63 ? 63 : bss->num_plinks) << 1;
  240. /* TODO: Add Connected to Mesh Gate/AS subfields */
  241. wpabuf_put_u8(buf, info);
  242. /* always forwarding & accepting plinks for now */
  243. wpabuf_put_u8(buf, 0x1 | 0x8);
  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);
  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.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. switch (sta->plink_state) {
  355. case PLINK_OPEN_RCVD:
  356. case PLINK_OPEN_SENT:
  357. /* retry timer */
  358. if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
  359. eloop_register_timeout(
  360. conf->dot11MeshRetryTimeout / 1000,
  361. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  362. plink_timer, wpa_s, sta);
  363. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  364. sta->mpm_retries++;
  365. break;
  366. }
  367. reason = WLAN_REASON_MESH_MAX_RETRIES;
  368. /* fall through on else */
  369. case PLINK_CNF_RCVD:
  370. /* confirm timer */
  371. if (!reason)
  372. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  373. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  374. eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
  375. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  376. plink_timer, wpa_s, sta);
  377. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  378. break;
  379. case PLINK_HOLDING:
  380. /* holding timer */
  381. mesh_mpm_fsm_restart(wpa_s, sta);
  382. break;
  383. default:
  384. break;
  385. }
  386. }
  387. /* initiate peering with station */
  388. static void
  389. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  390. enum mesh_plink_state next_state)
  391. {
  392. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  393. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  394. eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
  395. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  396. plink_timer, wpa_s, sta);
  397. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  398. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  399. }
  400. int mesh_mpm_plink_close(struct hostapd_data *hapd,
  401. struct sta_info *sta, void *ctx)
  402. {
  403. struct wpa_supplicant *wpa_s = ctx;
  404. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  405. if (sta) {
  406. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  407. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  408. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  409. MAC2STR(sta->addr));
  410. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  411. return 0;
  412. }
  413. return 1;
  414. }
  415. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  416. {
  417. struct hostapd_data *hapd = ifmsh->bss[0];
  418. /* notify peers we're leaving */
  419. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  420. hapd->num_plinks = 0;
  421. hostapd_free_stas(hapd);
  422. }
  423. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  424. * ready to start AMPE */
  425. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  426. {
  427. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  428. struct hostapd_sta_add_params params;
  429. struct sta_info *sta;
  430. int ret;
  431. sta = ap_get_sta(data, addr);
  432. if (!sta) {
  433. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  434. return;
  435. }
  436. /* TODO: Should do nothing if this STA is already authenticated, but
  437. * the AP code already sets this flag. */
  438. sta->flags |= WLAN_STA_AUTH;
  439. mesh_rsn_init_ampe_sta(wpa_s, sta);
  440. os_memset(&params, 0, sizeof(params));
  441. params.addr = sta->addr;
  442. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  443. params.set = 1;
  444. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  445. MAC2STR(sta->addr));
  446. ret = wpa_drv_sta_add(wpa_s, &params);
  447. if (ret) {
  448. wpa_msg(wpa_s, MSG_ERROR,
  449. "Driver failed to set " MACSTR ": %d",
  450. MAC2STR(sta->addr), ret);
  451. }
  452. if (!sta->my_lid)
  453. mesh_mpm_init_link(wpa_s, sta);
  454. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  455. }
  456. /*
  457. * Initialize a sta_info structure for a peer and upload it into the driver
  458. * in preparation for beginning authentication or peering. This is done when a
  459. * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
  460. * received from the peer for the first time.
  461. */
  462. static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
  463. const u8 *addr,
  464. struct ieee802_11_elems *elems)
  465. {
  466. struct hostapd_sta_add_params params;
  467. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  468. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  469. struct sta_info *sta;
  470. int ret;
  471. sta = ap_get_sta(data, addr);
  472. if (!sta) {
  473. sta = ap_sta_add(data, addr);
  474. if (!sta)
  475. return NULL;
  476. }
  477. /* Set WMM by default since Mesh STAs are QoS STAs */
  478. sta->flags |= WLAN_STA_WMM;
  479. /* initialize sta */
  480. if (copy_supp_rates(wpa_s, sta, elems)) {
  481. ap_free_sta(data, sta);
  482. return NULL;
  483. }
  484. mesh_mpm_init_link(wpa_s, sta);
  485. #ifdef CONFIG_IEEE80211N
  486. copy_sta_ht_capab(data, sta, elems->ht_capabilities);
  487. update_ht_state(data, sta);
  488. #endif /* CONFIG_IEEE80211N */
  489. #ifdef CONFIG_IEEE80211AC
  490. copy_sta_vht_capab(data, sta, elems->vht_capabilities);
  491. set_sta_vht_opmode(data, sta, elems->vht_opmode_notif);
  492. #endif /* CONFIG_IEEE80211AC */
  493. if (hostapd_get_aid(data, sta) < 0) {
  494. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  495. ap_free_sta(data, sta);
  496. return NULL;
  497. }
  498. /* insert into driver */
  499. os_memset(&params, 0, sizeof(params));
  500. params.supp_rates = sta->supported_rates;
  501. params.supp_rates_len = sta->supported_rates_len;
  502. params.addr = addr;
  503. params.plink_state = sta->plink_state;
  504. params.aid = sta->aid;
  505. params.listen_interval = 100;
  506. params.ht_capabilities = sta->ht_capabilities;
  507. params.vht_capabilities = sta->vht_capabilities;
  508. params.flags |= WPA_STA_WMM;
  509. params.flags_mask |= WPA_STA_AUTHENTICATED;
  510. if (conf->security == MESH_CONF_SEC_NONE) {
  511. params.flags |= WPA_STA_AUTHORIZED;
  512. params.flags |= WPA_STA_AUTHENTICATED;
  513. } else {
  514. sta->flags |= WLAN_STA_MFP;
  515. params.flags |= WPA_STA_MFP;
  516. }
  517. ret = wpa_drv_sta_add(wpa_s, &params);
  518. if (ret) {
  519. wpa_msg(wpa_s, MSG_ERROR,
  520. "Driver failed to insert " MACSTR ": %d",
  521. MAC2STR(addr), ret);
  522. ap_free_sta(data, sta);
  523. return NULL;
  524. }
  525. return sta;
  526. }
  527. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  528. struct ieee802_11_elems *elems)
  529. {
  530. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  531. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  532. struct sta_info *sta;
  533. struct wpa_ssid *ssid = wpa_s->current_ssid;
  534. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  535. if (!sta)
  536. return;
  537. if (ssid && ssid->no_auto_peer) {
  538. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  539. MACSTR " because of no_auto_peer", MAC2STR(addr));
  540. if (data->mesh_pending_auth) {
  541. struct os_reltime age;
  542. const struct ieee80211_mgmt *mgmt;
  543. struct hostapd_frame_info fi;
  544. mgmt = wpabuf_head(data->mesh_pending_auth);
  545. os_reltime_age(&data->mesh_pending_auth_time, &age);
  546. if (age.sec < 2 &&
  547. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  548. wpa_printf(MSG_DEBUG,
  549. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  550. (unsigned int) age.sec,
  551. (unsigned int) age.usec);
  552. os_memset(&fi, 0, sizeof(fi));
  553. ieee802_11_mgmt(
  554. data,
  555. wpabuf_head(data->mesh_pending_auth),
  556. wpabuf_len(data->mesh_pending_auth),
  557. &fi);
  558. }
  559. wpabuf_free(data->mesh_pending_auth);
  560. data->mesh_pending_auth = NULL;
  561. }
  562. return;
  563. }
  564. if (conf->security == MESH_CONF_SEC_NONE)
  565. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  566. else
  567. mesh_rsn_auth_sae_sta(wpa_s, sta);
  568. }
  569. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  570. {
  571. struct hostapd_frame_info fi;
  572. os_memset(&fi, 0, sizeof(fi));
  573. fi.datarate = rx_mgmt->datarate;
  574. fi.ssi_signal = rx_mgmt->ssi_signal;
  575. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  576. rx_mgmt->frame_len, &fi);
  577. }
  578. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  579. struct sta_info *sta)
  580. {
  581. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  582. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  583. u8 seq[6] = {};
  584. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  585. MAC2STR(sta->addr));
  586. if (conf->security & MESH_CONF_SEC_AMPE) {
  587. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  588. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  589. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  590. seq, sizeof(seq),
  591. sta->mgtk, sizeof(sta->mgtk));
  592. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  593. seq, sizeof(seq),
  594. sta->mgtk, sizeof(sta->mgtk));
  595. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  596. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  597. sta->mgtk, sizeof(sta->mgtk));
  598. }
  599. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  600. hapd->num_plinks++;
  601. sta->flags |= WLAN_STA_ASSOC;
  602. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  603. /* Send ctrl event */
  604. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  605. MAC2STR(sta->addr));
  606. }
  607. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  608. enum plink_event event)
  609. {
  610. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  611. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  612. u16 reason = 0;
  613. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  614. MAC2STR(sta->addr), mplstate[sta->plink_state],
  615. mplevent[event]);
  616. switch (sta->plink_state) {
  617. case PLINK_LISTEN:
  618. switch (event) {
  619. case CLS_ACPT:
  620. mesh_mpm_fsm_restart(wpa_s, sta);
  621. break;
  622. case OPN_ACPT:
  623. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  624. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  625. 0);
  626. break;
  627. default:
  628. break;
  629. }
  630. break;
  631. case PLINK_OPEN_SENT:
  632. switch (event) {
  633. case OPN_RJCT:
  634. case CNF_RJCT:
  635. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  636. /* fall-through */
  637. case CLS_ACPT:
  638. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  639. if (!reason)
  640. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  641. eloop_register_timeout(
  642. conf->dot11MeshHoldingTimeout / 1000,
  643. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  644. plink_timer, wpa_s, sta);
  645. mesh_mpm_send_plink_action(wpa_s, sta,
  646. PLINK_CLOSE, reason);
  647. break;
  648. case OPN_ACPT:
  649. /* retry timer is left untouched */
  650. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  651. mesh_mpm_send_plink_action(wpa_s, sta,
  652. PLINK_CONFIRM, 0);
  653. break;
  654. case CNF_ACPT:
  655. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  656. eloop_register_timeout(
  657. conf->dot11MeshConfirmTimeout / 1000,
  658. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  659. plink_timer, wpa_s, sta);
  660. break;
  661. default:
  662. break;
  663. }
  664. break;
  665. case PLINK_OPEN_RCVD:
  666. switch (event) {
  667. case OPN_RJCT:
  668. case CNF_RJCT:
  669. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  670. /* fall-through */
  671. case CLS_ACPT:
  672. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  673. if (!reason)
  674. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  675. eloop_register_timeout(
  676. conf->dot11MeshHoldingTimeout / 1000,
  677. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  678. plink_timer, wpa_s, sta);
  679. sta->mpm_close_reason = reason;
  680. mesh_mpm_send_plink_action(wpa_s, sta,
  681. PLINK_CLOSE, reason);
  682. break;
  683. case OPN_ACPT:
  684. mesh_mpm_send_plink_action(wpa_s, sta,
  685. PLINK_CONFIRM, 0);
  686. break;
  687. case CNF_ACPT:
  688. if (conf->security & MESH_CONF_SEC_AMPE)
  689. mesh_rsn_derive_mtk(wpa_s, sta);
  690. mesh_mpm_plink_estab(wpa_s, sta);
  691. break;
  692. default:
  693. break;
  694. }
  695. break;
  696. case PLINK_CNF_RCVD:
  697. switch (event) {
  698. case OPN_RJCT:
  699. case CNF_RJCT:
  700. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  701. /* fall-through */
  702. case CLS_ACPT:
  703. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  704. if (!reason)
  705. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  706. eloop_register_timeout(
  707. conf->dot11MeshHoldingTimeout / 1000,
  708. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  709. plink_timer, wpa_s, sta);
  710. sta->mpm_close_reason = reason;
  711. mesh_mpm_send_plink_action(wpa_s, sta,
  712. PLINK_CLOSE, reason);
  713. break;
  714. case OPN_ACPT:
  715. mesh_mpm_plink_estab(wpa_s, sta);
  716. mesh_mpm_send_plink_action(wpa_s, sta,
  717. PLINK_CONFIRM, 0);
  718. break;
  719. default:
  720. break;
  721. }
  722. break;
  723. case PLINK_ESTAB:
  724. switch (event) {
  725. case CLS_ACPT:
  726. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  727. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  728. eloop_register_timeout(
  729. conf->dot11MeshHoldingTimeout / 1000,
  730. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  731. plink_timer, wpa_s, sta);
  732. sta->mpm_close_reason = reason;
  733. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  734. " closed with reason %d",
  735. MAC2STR(sta->addr), reason);
  736. wpa_msg(wpa_s, MSG_INFO, MESH_PEER_DISCONNECTED MACSTR,
  737. MAC2STR(sta->addr));
  738. hapd->num_plinks--;
  739. mesh_mpm_send_plink_action(wpa_s, sta,
  740. PLINK_CLOSE, reason);
  741. break;
  742. case OPN_ACPT:
  743. mesh_mpm_send_plink_action(wpa_s, sta,
  744. PLINK_CONFIRM, 0);
  745. break;
  746. default:
  747. break;
  748. }
  749. break;
  750. case PLINK_HOLDING:
  751. switch (event) {
  752. case CLS_ACPT:
  753. mesh_mpm_fsm_restart(wpa_s, sta);
  754. break;
  755. case OPN_ACPT:
  756. case CNF_ACPT:
  757. case OPN_RJCT:
  758. case CNF_RJCT:
  759. reason = sta->mpm_close_reason;
  760. mesh_mpm_send_plink_action(wpa_s, sta,
  761. PLINK_CLOSE, reason);
  762. break;
  763. default:
  764. break;
  765. }
  766. break;
  767. default:
  768. wpa_msg(wpa_s, MSG_DEBUG,
  769. "Unsupported MPM event %s for state %s",
  770. mplevent[event], mplstate[sta->plink_state]);
  771. break;
  772. }
  773. }
  774. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  775. const struct ieee80211_mgmt *mgmt, size_t len)
  776. {
  777. u8 action_field;
  778. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  779. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  780. struct sta_info *sta;
  781. u16 plid = 0, llid = 0;
  782. enum plink_event event;
  783. struct ieee802_11_elems elems;
  784. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  785. const u8 *ies;
  786. size_t ie_len;
  787. int ret;
  788. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  789. return;
  790. action_field = mgmt->u.action.u.slf_prot_action.action;
  791. if (action_field != PLINK_OPEN &&
  792. action_field != PLINK_CONFIRM &&
  793. action_field != PLINK_CLOSE)
  794. return;
  795. ies = mgmt->u.action.u.slf_prot_action.variable;
  796. ie_len = (const u8 *) mgmt + len -
  797. mgmt->u.action.u.slf_prot_action.variable;
  798. /* at least expect mesh id and peering mgmt */
  799. if (ie_len < 2 + 2) {
  800. wpa_printf(MSG_DEBUG,
  801. "MPM: Ignore too short action frame %u ie_len %u",
  802. action_field, (unsigned int) ie_len);
  803. return;
  804. }
  805. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  806. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  807. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  808. WPA_GET_LE16(ies));
  809. ies += 2; /* capability */
  810. ie_len -= 2;
  811. }
  812. if (action_field == PLINK_CONFIRM) {
  813. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  814. ies += 2; /* aid */
  815. ie_len -= 2;
  816. }
  817. /* check for mesh peering, mesh id and mesh config IEs */
  818. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  819. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  820. return;
  821. }
  822. if (!elems.peer_mgmt) {
  823. wpa_printf(MSG_DEBUG,
  824. "MPM: No Mesh Peering Management element");
  825. return;
  826. }
  827. if (action_field != PLINK_CLOSE) {
  828. if (!elems.mesh_id || !elems.mesh_config) {
  829. wpa_printf(MSG_DEBUG,
  830. "MPM: No Mesh ID or Mesh Configuration element");
  831. return;
  832. }
  833. if (!matches_local(wpa_s, &elems)) {
  834. wpa_printf(MSG_DEBUG,
  835. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  836. return;
  837. }
  838. }
  839. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  840. elems.peer_mgmt,
  841. elems.peer_mgmt_len,
  842. &peer_mgmt_ie);
  843. if (ret) {
  844. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  845. return;
  846. }
  847. /* the sender's llid is our plid and vice-versa */
  848. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  849. if (peer_mgmt_ie.plid)
  850. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  851. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  852. sta = ap_get_sta(hapd, mgmt->sa);
  853. /*
  854. * If this is an open frame from an unknown STA, and this is an
  855. * open mesh, then go ahead and add the peer before proceeding.
  856. */
  857. if (!sta && action_field == PLINK_OPEN &&
  858. !(mconf->security & MESH_CONF_SEC_AMPE))
  859. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  860. if (!sta) {
  861. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  862. return;
  863. }
  864. #ifdef CONFIG_SAE
  865. /* peer is in sae_accepted? */
  866. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  867. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  868. return;
  869. }
  870. #endif /* CONFIG_SAE */
  871. if (!sta->my_lid)
  872. mesh_mpm_init_link(wpa_s, sta);
  873. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  874. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  875. &mgmt->u.action.category,
  876. peer_mgmt_ie.chosen_pmk,
  877. ies, ie_len)) {
  878. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  879. return;
  880. }
  881. if (sta->plink_state == PLINK_BLOCKED) {
  882. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  883. return;
  884. }
  885. /* Now we will figure out the appropriate event... */
  886. switch (action_field) {
  887. case PLINK_OPEN:
  888. if (plink_free_count(hapd) == 0) {
  889. event = OPN_IGNR;
  890. wpa_printf(MSG_INFO,
  891. "MPM: Peer link num over quota(%d)",
  892. hapd->max_plinks);
  893. } else if (sta->peer_lid && sta->peer_lid != plid) {
  894. event = OPN_IGNR;
  895. } else {
  896. sta->peer_lid = plid;
  897. event = OPN_ACPT;
  898. }
  899. break;
  900. case PLINK_CONFIRM:
  901. if (plink_free_count(hapd) == 0) {
  902. event = CNF_IGNR;
  903. wpa_printf(MSG_INFO,
  904. "MPM: Peer link num over quota(%d)",
  905. hapd->max_plinks);
  906. } else if (sta->my_lid != llid ||
  907. (sta->peer_lid && sta->peer_lid != plid)) {
  908. event = CNF_IGNR;
  909. } else {
  910. if (!sta->peer_lid)
  911. sta->peer_lid = plid;
  912. event = CNF_ACPT;
  913. }
  914. break;
  915. case PLINK_CLOSE:
  916. if (sta->plink_state == PLINK_ESTAB)
  917. /* Do not check for llid or plid. This does not
  918. * follow the standard but since multiple plinks
  919. * per cand are not supported, it is necessary in
  920. * order to avoid a livelock when MP A sees an
  921. * establish peer link to MP B but MP B does not
  922. * see it. This can be caused by a timeout in
  923. * B's peer link establishment or B being
  924. * restarted.
  925. */
  926. event = CLS_ACPT;
  927. else if (sta->peer_lid != plid)
  928. event = CLS_IGNR;
  929. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  930. event = CLS_IGNR;
  931. else
  932. event = CLS_ACPT;
  933. break;
  934. default:
  935. /*
  936. * This cannot be hit due to the action_field check above, but
  937. * compilers may not be able to figure that out and can warn
  938. * about uninitialized event below.
  939. */
  940. return;
  941. }
  942. mesh_mpm_fsm(wpa_s, sta, event);
  943. }
  944. /* called by ap_free_sta */
  945. void mesh_mpm_free_sta(struct sta_info *sta)
  946. {
  947. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  948. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  949. }