mesh_mpm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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;
  21. const u8 *llid;
  22. const u8 *plid;
  23. const u8 *reason;
  24. const u8 *pmk;
  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. [PLINK_LISTEN] = "LISTEN",
  40. [PLINK_OPEN_SENT] = "OPEN_SENT",
  41. [PLINK_OPEN_RCVD] = "OPEN_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. [OPN_IGNR] = "OPN_IGNR",
  52. [CNF_ACPT] = "CNF_ACPT",
  53. [CNF_RJCT] = "CNF_RJCT",
  54. [CNF_IGNR] = "CNF_IGNR",
  55. [CLS_ACPT] = "CLS_ACPT",
  56. [CLS_IGNR] = "CLS_IGNR"
  57. };
  58. static int mesh_mpm_parse_peer_mgmt(struct wpa_supplicant *wpa_s,
  59. u8 action_field,
  60. const u8 *ie, size_t len,
  61. struct mesh_peer_mgmt_ie *mpm_ie)
  62. {
  63. os_memset(mpm_ie, 0, sizeof(*mpm_ie));
  64. /* remove optional PMK at end */
  65. if (len >= 16) {
  66. len -= 16;
  67. mpm_ie->pmk = ie + len - 16;
  68. }
  69. if ((action_field == PLINK_OPEN && len != 4) ||
  70. (action_field == PLINK_CONFIRM && len != 6) ||
  71. (action_field == PLINK_CLOSE && len != 6 && len != 8)) {
  72. wpa_msg(wpa_s, MSG_DEBUG, "MPM: Invalid peer mgmt ie");
  73. return -1;
  74. }
  75. /* required fields */
  76. if (len < 4)
  77. return -1;
  78. mpm_ie->proto_id = ie;
  79. mpm_ie->llid = ie + 2;
  80. ie += 4;
  81. len -= 4;
  82. /* close reason is always present at end for close */
  83. if (action_field == PLINK_CLOSE) {
  84. if (len < 2)
  85. return -1;
  86. mpm_ie->reason = ie + len - 2;
  87. len -= 2;
  88. }
  89. /* plid, present for confirm, and possibly close */
  90. if (len)
  91. mpm_ie->plid = ie;
  92. return 0;
  93. }
  94. static int plink_free_count(struct hostapd_data *hapd)
  95. {
  96. if (hapd->max_plinks > hapd->num_plinks)
  97. return hapd->max_plinks - hapd->num_plinks;
  98. return 0;
  99. }
  100. static u16 copy_supp_rates(struct wpa_supplicant *wpa_s,
  101. struct sta_info *sta,
  102. struct ieee802_11_elems *elems)
  103. {
  104. if (!elems->supp_rates) {
  105. wpa_msg(wpa_s, MSG_ERROR, "no supported rates from " MACSTR,
  106. MAC2STR(sta->addr));
  107. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  108. }
  109. if (elems->supp_rates_len + elems->ext_supp_rates_len >
  110. sizeof(sta->supported_rates)) {
  111. wpa_msg(wpa_s, MSG_ERROR,
  112. "Invalid supported rates element length " MACSTR
  113. " %d+%d", MAC2STR(sta->addr), elems->supp_rates_len,
  114. elems->ext_supp_rates_len);
  115. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  116. }
  117. sta->supported_rates_len = merge_byte_arrays(
  118. sta->supported_rates, sizeof(sta->supported_rates),
  119. elems->supp_rates, elems->supp_rates_len,
  120. elems->ext_supp_rates, elems->ext_supp_rates_len);
  121. return WLAN_STATUS_SUCCESS;
  122. }
  123. /* return true if elems from a neighbor match this MBSS */
  124. static Boolean matches_local(struct wpa_supplicant *wpa_s,
  125. struct ieee802_11_elems *elems)
  126. {
  127. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  128. if (elems->mesh_config_len < 5)
  129. return FALSE;
  130. return (mconf->meshid_len == elems->mesh_id_len &&
  131. os_memcmp(mconf->meshid, elems->mesh_id,
  132. elems->mesh_id_len) == 0 &&
  133. mconf->mesh_pp_id == elems->mesh_config[0] &&
  134. mconf->mesh_pm_id == elems->mesh_config[1] &&
  135. mconf->mesh_cc_id == elems->mesh_config[2] &&
  136. mconf->mesh_sp_id == elems->mesh_config[3] &&
  137. mconf->mesh_auth_id == elems->mesh_config[4]);
  138. }
  139. /* check if local link id is already used with another peer */
  140. static Boolean llid_in_use(struct wpa_supplicant *wpa_s, u16 llid)
  141. {
  142. struct sta_info *sta;
  143. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  144. for (sta = hapd->sta_list; sta; sta = sta->next) {
  145. if (sta->my_lid == llid)
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. /* generate an llid for a link and set to initial state */
  151. static void mesh_mpm_init_link(struct wpa_supplicant *wpa_s,
  152. struct sta_info *sta)
  153. {
  154. u16 llid;
  155. do {
  156. if (os_get_random((u8 *) &llid, sizeof(llid)) < 0)
  157. continue;
  158. } while (!llid || llid_in_use(wpa_s, llid));
  159. sta->my_lid = llid;
  160. sta->peer_lid = 0;
  161. sta->plink_state = PLINK_LISTEN;
  162. }
  163. static void mesh_mpm_send_plink_action(struct wpa_supplicant *wpa_s,
  164. struct sta_info *sta,
  165. enum plink_action_field type,
  166. u16 close_reason)
  167. {
  168. struct wpabuf *buf;
  169. struct hostapd_iface *ifmsh = wpa_s->ifmsh;
  170. struct hostapd_data *bss = ifmsh->bss[0];
  171. struct mesh_conf *conf = ifmsh->mconf;
  172. u8 supp_rates[2 + 2 + 32];
  173. #ifdef CONFIG_IEEE80211N
  174. u8 ht_capa_oper[2 + 26 + 2 + 22];
  175. #endif /* CONFIG_IEEE80211N */
  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 &&
  194. wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
  195. buf_len += 2 + 26 + /* HT capabilities */
  196. 2 + 22; /* HT operation */
  197. }
  198. #endif /* CONFIG_IEEE80211N */
  199. buf = wpabuf_alloc(buf_len);
  200. if (!buf)
  201. return;
  202. cat = wpabuf_mhead_u8(buf);
  203. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  204. wpabuf_put_u8(buf, type);
  205. if (type != PLINK_CLOSE) {
  206. /* capability info */
  207. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  208. /* aid */
  209. if (type == PLINK_CONFIRM)
  210. wpabuf_put_le16(buf, sta->peer_lid);
  211. /* IE: supp + ext. supp rates */
  212. pos = hostapd_eid_supp_rates(bss, supp_rates);
  213. pos = hostapd_eid_ext_supp_rates(bss, pos);
  214. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  215. /* IE: Mesh ID */
  216. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  217. wpabuf_put_u8(buf, conf->meshid_len);
  218. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  219. /* IE: mesh conf */
  220. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  221. wpabuf_put_u8(buf, 7);
  222. wpabuf_put_u8(buf, conf->mesh_pp_id);
  223. wpabuf_put_u8(buf, conf->mesh_pm_id);
  224. wpabuf_put_u8(buf, conf->mesh_cc_id);
  225. wpabuf_put_u8(buf, conf->mesh_sp_id);
  226. wpabuf_put_u8(buf, conf->mesh_auth_id);
  227. /* TODO: formation info */
  228. wpabuf_put_u8(buf, 0);
  229. /* always forwarding & accepting plinks for now */
  230. wpabuf_put_u8(buf, 0x1 | 0x8);
  231. } else { /* Peer closing frame */
  232. /* IE: Mesh ID */
  233. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  234. wpabuf_put_u8(buf, conf->meshid_len);
  235. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  236. }
  237. /* IE: Mesh Peering Management element */
  238. ie_len = 4;
  239. if (ampe)
  240. ie_len += PMKID_LEN;
  241. switch (type) {
  242. case PLINK_OPEN:
  243. break;
  244. case PLINK_CONFIRM:
  245. ie_len += 2;
  246. add_plid = 1;
  247. break;
  248. case PLINK_CLOSE:
  249. ie_len += 2;
  250. add_plid = 1;
  251. ie_len += 2; /* reason code */
  252. break;
  253. }
  254. wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
  255. wpabuf_put_u8(buf, ie_len);
  256. /* peering protocol */
  257. if (ampe)
  258. wpabuf_put_le16(buf, 1);
  259. else
  260. wpabuf_put_le16(buf, 0);
  261. wpabuf_put_le16(buf, sta->my_lid);
  262. if (add_plid)
  263. wpabuf_put_le16(buf, sta->peer_lid);
  264. if (type == PLINK_CLOSE)
  265. wpabuf_put_le16(buf, close_reason);
  266. if (ampe) {
  267. if (sta->sae == NULL) {
  268. wpa_msg(wpa_s, MSG_INFO, "Mesh MPM: no SAE session");
  269. goto fail;
  270. }
  271. mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
  272. wpabuf_put(buf, PMKID_LEN));
  273. }
  274. #ifdef CONFIG_IEEE80211N
  275. if (type != PLINK_CLOSE &&
  276. wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
  277. pos = hostapd_eid_ht_capabilities(bss, ht_capa_oper);
  278. pos = hostapd_eid_ht_operation(bss, pos);
  279. wpabuf_put_data(buf, ht_capa_oper, pos - ht_capa_oper);
  280. }
  281. #endif /* CONFIG_IEEE80211N */
  282. if (ampe && mesh_rsn_protect_frame(wpa_s->mesh_rsn, sta, cat, buf)) {
  283. wpa_msg(wpa_s, MSG_INFO,
  284. "Mesh MPM: failed to add AMPE and MIC IE");
  285. goto fail;
  286. }
  287. ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
  288. sta->addr, wpa_s->own_addr, wpa_s->own_addr,
  289. wpabuf_head(buf), wpabuf_len(buf), 0);
  290. if (ret < 0)
  291. wpa_msg(wpa_s, MSG_INFO,
  292. "Mesh MPM: failed to send peering frame");
  293. fail:
  294. wpabuf_free(buf);
  295. }
  296. /* configure peering state in ours and driver's station entry */
  297. static void
  298. wpa_mesh_set_plink_state(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  299. enum mesh_plink_state state)
  300. {
  301. struct hostapd_sta_add_params params;
  302. int ret;
  303. sta->plink_state = state;
  304. os_memset(&params, 0, sizeof(params));
  305. params.addr = sta->addr;
  306. params.plink_state = state;
  307. params.set = 1;
  308. wpa_msg(wpa_s, MSG_DEBUG, "MPM set " MACSTR " into %s",
  309. MAC2STR(sta->addr), mplstate[state]);
  310. ret = wpa_drv_sta_add(wpa_s, &params);
  311. if (ret) {
  312. wpa_msg(wpa_s, MSG_ERROR, "Driver failed to set " MACSTR
  313. ": %d", MAC2STR(sta->addr), ret);
  314. }
  315. }
  316. static void mesh_mpm_fsm_restart(struct wpa_supplicant *wpa_s,
  317. struct sta_info *sta)
  318. {
  319. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  320. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  321. if (sta->mpm_close_reason == WLAN_REASON_MESH_CLOSE_RCVD) {
  322. ap_free_sta(hapd, sta);
  323. return;
  324. }
  325. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_LISTEN);
  326. sta->my_lid = sta->peer_lid = sta->mpm_close_reason = 0;
  327. sta->mpm_retries = 0;
  328. }
  329. static void plink_timer(void *eloop_ctx, void *user_data)
  330. {
  331. struct wpa_supplicant *wpa_s = eloop_ctx;
  332. struct sta_info *sta = user_data;
  333. u16 reason = 0;
  334. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  335. switch (sta->plink_state) {
  336. case PLINK_OPEN_RCVD:
  337. case PLINK_OPEN_SENT:
  338. /* retry timer */
  339. if (sta->mpm_retries < conf->dot11MeshMaxRetries) {
  340. eloop_register_timeout(
  341. conf->dot11MeshRetryTimeout / 1000,
  342. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  343. plink_timer, wpa_s, sta);
  344. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  345. sta->mpm_retries++;
  346. break;
  347. }
  348. reason = WLAN_REASON_MESH_MAX_RETRIES;
  349. /* fall through on else */
  350. case PLINK_CNF_RCVD:
  351. /* confirm timer */
  352. if (!reason)
  353. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  354. sta->plink_state = PLINK_HOLDING;
  355. eloop_register_timeout(conf->dot11MeshHoldingTimeout / 1000,
  356. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  357. plink_timer, wpa_s, sta);
  358. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  359. break;
  360. case PLINK_HOLDING:
  361. /* holding timer */
  362. mesh_mpm_fsm_restart(wpa_s, sta);
  363. break;
  364. default:
  365. break;
  366. }
  367. }
  368. /* initiate peering with station */
  369. static void
  370. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  371. enum mesh_plink_state next_state)
  372. {
  373. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  374. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  375. eloop_register_timeout(conf->dot11MeshRetryTimeout / 1000,
  376. (conf->dot11MeshRetryTimeout % 1000) * 1000,
  377. plink_timer, wpa_s, sta);
  378. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  379. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  380. }
  381. int mesh_mpm_plink_close(struct hostapd_data *hapd,
  382. struct sta_info *sta, void *ctx)
  383. {
  384. struct wpa_supplicant *wpa_s = ctx;
  385. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  386. if (sta) {
  387. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  388. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  389. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  390. MAC2STR(sta->addr));
  391. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  392. return 0;
  393. }
  394. return 1;
  395. }
  396. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  397. {
  398. struct hostapd_data *hapd = ifmsh->bss[0];
  399. /* notify peers we're leaving */
  400. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  401. hapd->num_plinks = 0;
  402. hostapd_free_stas(hapd);
  403. }
  404. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  405. * ready to start AMPE */
  406. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  407. {
  408. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  409. struct hostapd_sta_add_params params;
  410. struct sta_info *sta;
  411. int ret;
  412. sta = ap_get_sta(data, addr);
  413. if (!sta) {
  414. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  415. return;
  416. }
  417. /* TODO: Should do nothing if this STA is already authenticated, but
  418. * the AP code already sets this flag. */
  419. sta->flags |= WLAN_STA_AUTH;
  420. mesh_rsn_init_ampe_sta(wpa_s, sta);
  421. os_memset(&params, 0, sizeof(params));
  422. params.addr = sta->addr;
  423. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  424. params.set = 1;
  425. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  426. MAC2STR(sta->addr));
  427. ret = wpa_drv_sta_add(wpa_s, &params);
  428. if (ret) {
  429. wpa_msg(wpa_s, MSG_ERROR,
  430. "Driver failed to set " MACSTR ": %d",
  431. MAC2STR(sta->addr), ret);
  432. }
  433. if (!sta->my_lid)
  434. mesh_mpm_init_link(wpa_s, sta);
  435. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  436. }
  437. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  438. struct ieee802_11_elems *elems)
  439. {
  440. struct hostapd_sta_add_params params;
  441. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  442. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  443. struct sta_info *sta;
  444. struct wpa_ssid *ssid = wpa_s->current_ssid;
  445. int ret = 0;
  446. sta = ap_get_sta(data, addr);
  447. if (!sta) {
  448. sta = ap_sta_add(data, addr);
  449. if (!sta)
  450. return;
  451. }
  452. /* initialize sta */
  453. if (copy_supp_rates(wpa_s, sta, elems))
  454. return;
  455. mesh_mpm_init_link(wpa_s, sta);
  456. #ifdef CONFIG_IEEE80211N
  457. copy_sta_ht_capab(data, sta, elems->ht_capabilities,
  458. elems->ht_capabilities_len);
  459. update_ht_state(data, sta);
  460. #endif /* CONFIG_IEEE80211N */
  461. /* insert into driver */
  462. os_memset(&params, 0, sizeof(params));
  463. params.supp_rates = sta->supported_rates;
  464. params.supp_rates_len = sta->supported_rates_len;
  465. params.addr = addr;
  466. params.plink_state = sta->plink_state;
  467. params.aid = sta->peer_lid;
  468. params.listen_interval = 100;
  469. params.ht_capabilities = sta->ht_capabilities;
  470. params.flags |= WPA_STA_WMM;
  471. params.flags_mask |= WPA_STA_AUTHENTICATED;
  472. if (conf->security == MESH_CONF_SEC_NONE) {
  473. params.flags |= WPA_STA_AUTHORIZED;
  474. params.flags |= WPA_STA_AUTHENTICATED;
  475. } else {
  476. sta->flags |= WLAN_STA_MFP;
  477. params.flags |= WPA_STA_MFP;
  478. }
  479. ret = wpa_drv_sta_add(wpa_s, &params);
  480. if (ret) {
  481. wpa_msg(wpa_s, MSG_ERROR,
  482. "Driver failed to insert " MACSTR ": %d",
  483. MAC2STR(addr), ret);
  484. return;
  485. }
  486. if (ssid && ssid->no_auto_peer) {
  487. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  488. MACSTR " because of no_auto_peer", MAC2STR(addr));
  489. return;
  490. }
  491. if (conf->security == MESH_CONF_SEC_NONE)
  492. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  493. else
  494. mesh_rsn_auth_sae_sta(wpa_s, sta);
  495. }
  496. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  497. {
  498. struct hostapd_frame_info fi;
  499. os_memset(&fi, 0, sizeof(fi));
  500. fi.datarate = rx_mgmt->datarate;
  501. fi.ssi_signal = rx_mgmt->ssi_signal;
  502. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  503. rx_mgmt->frame_len, &fi);
  504. }
  505. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  506. struct sta_info *sta)
  507. {
  508. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  509. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  510. u8 seq[6] = {};
  511. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  512. MAC2STR(sta->addr));
  513. if (conf->security & MESH_CONF_SEC_AMPE) {
  514. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  515. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  516. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  517. seq, sizeof(seq),
  518. sta->mgtk, sizeof(sta->mgtk));
  519. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  520. seq, sizeof(seq),
  521. sta->mgtk, sizeof(sta->mgtk));
  522. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  523. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  524. sta->mgtk, sizeof(sta->mgtk));
  525. }
  526. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  527. hapd->num_plinks++;
  528. sta->flags |= WLAN_STA_ASSOC;
  529. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  530. /* Send ctrl event */
  531. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  532. MAC2STR(sta->addr));
  533. }
  534. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  535. enum plink_event event)
  536. {
  537. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  538. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  539. u16 reason = 0;
  540. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  541. MAC2STR(sta->addr), mplstate[sta->plink_state],
  542. mplevent[event]);
  543. switch (sta->plink_state) {
  544. case PLINK_LISTEN:
  545. switch (event) {
  546. case CLS_ACPT:
  547. mesh_mpm_fsm_restart(wpa_s, sta);
  548. break;
  549. case OPN_ACPT:
  550. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  551. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  552. 0);
  553. break;
  554. default:
  555. break;
  556. }
  557. break;
  558. case PLINK_OPEN_SENT:
  559. switch (event) {
  560. case OPN_RJCT:
  561. case CNF_RJCT:
  562. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  563. /* fall-through */
  564. case CLS_ACPT:
  565. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  566. if (!reason)
  567. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  568. eloop_register_timeout(
  569. conf->dot11MeshHoldingTimeout / 1000,
  570. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  571. plink_timer, wpa_s, sta);
  572. mesh_mpm_send_plink_action(wpa_s, sta,
  573. PLINK_CLOSE, reason);
  574. break;
  575. case OPN_ACPT:
  576. /* retry timer is left untouched */
  577. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  578. mesh_mpm_send_plink_action(wpa_s, sta,
  579. PLINK_CONFIRM, 0);
  580. break;
  581. case CNF_ACPT:
  582. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  583. eloop_register_timeout(
  584. conf->dot11MeshConfirmTimeout / 1000,
  585. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  586. plink_timer, wpa_s, sta);
  587. break;
  588. default:
  589. break;
  590. }
  591. break;
  592. case PLINK_OPEN_RCVD:
  593. switch (event) {
  594. case OPN_RJCT:
  595. case CNF_RJCT:
  596. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  597. /* fall-through */
  598. case CLS_ACPT:
  599. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  600. if (!reason)
  601. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  602. eloop_register_timeout(
  603. conf->dot11MeshHoldingTimeout / 1000,
  604. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  605. plink_timer, wpa_s, sta);
  606. sta->mpm_close_reason = reason;
  607. mesh_mpm_send_plink_action(wpa_s, sta,
  608. PLINK_CLOSE, reason);
  609. break;
  610. case OPN_ACPT:
  611. mesh_mpm_send_plink_action(wpa_s, sta,
  612. PLINK_CONFIRM, 0);
  613. break;
  614. case CNF_ACPT:
  615. if (conf->security & MESH_CONF_SEC_AMPE)
  616. mesh_rsn_derive_mtk(wpa_s, sta);
  617. mesh_mpm_plink_estab(wpa_s, sta);
  618. break;
  619. default:
  620. break;
  621. }
  622. break;
  623. case PLINK_CNF_RCVD:
  624. switch (event) {
  625. case OPN_RJCT:
  626. case CNF_RJCT:
  627. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  628. /* fall-through */
  629. case CLS_ACPT:
  630. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  631. if (!reason)
  632. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  633. eloop_register_timeout(
  634. conf->dot11MeshHoldingTimeout / 1000,
  635. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  636. plink_timer, wpa_s, sta);
  637. sta->mpm_close_reason = reason;
  638. mesh_mpm_send_plink_action(wpa_s, sta,
  639. PLINK_CLOSE, reason);
  640. break;
  641. case OPN_ACPT:
  642. mesh_mpm_plink_estab(wpa_s, sta);
  643. mesh_mpm_send_plink_action(wpa_s, sta,
  644. PLINK_CONFIRM, 0);
  645. break;
  646. default:
  647. break;
  648. }
  649. break;
  650. case PLINK_ESTAB:
  651. switch (event) {
  652. case CLS_ACPT:
  653. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  654. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  655. eloop_register_timeout(
  656. conf->dot11MeshHoldingTimeout / 1000,
  657. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  658. plink_timer, wpa_s, sta);
  659. sta->mpm_close_reason = reason;
  660. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  661. " closed with reason %d",
  662. MAC2STR(sta->addr), reason);
  663. wpa_msg_ctrl(wpa_s, MSG_INFO,
  664. MESH_PEER_DISCONNECTED MACSTR,
  665. MAC2STR(sta->addr));
  666. hapd->num_plinks--;
  667. mesh_mpm_send_plink_action(wpa_s, sta,
  668. PLINK_CLOSE, reason);
  669. break;
  670. case OPN_ACPT:
  671. mesh_mpm_send_plink_action(wpa_s, sta,
  672. PLINK_CONFIRM, 0);
  673. break;
  674. default:
  675. break;
  676. }
  677. break;
  678. case PLINK_HOLDING:
  679. switch (event) {
  680. case CLS_ACPT:
  681. mesh_mpm_fsm_restart(wpa_s, sta);
  682. break;
  683. case OPN_ACPT:
  684. case CNF_ACPT:
  685. case OPN_RJCT:
  686. case CNF_RJCT:
  687. reason = sta->mpm_close_reason;
  688. mesh_mpm_send_plink_action(wpa_s, sta,
  689. PLINK_CLOSE, reason);
  690. break;
  691. default:
  692. break;
  693. }
  694. break;
  695. default:
  696. wpa_msg(wpa_s, MSG_DEBUG,
  697. "Unsupported MPM event %s for state %s",
  698. mplevent[event], mplstate[sta->plink_state]);
  699. break;
  700. }
  701. }
  702. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  703. const struct ieee80211_mgmt *mgmt, size_t len)
  704. {
  705. u8 action_field;
  706. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  707. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  708. struct sta_info *sta;
  709. u16 plid = 0, llid = 0;
  710. enum plink_event event;
  711. struct ieee802_11_elems elems;
  712. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  713. const u8 *ies;
  714. size_t ie_len;
  715. int ret;
  716. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  717. return;
  718. action_field = mgmt->u.action.u.slf_prot_action.action;
  719. if (action_field != PLINK_OPEN &&
  720. action_field != PLINK_CONFIRM &&
  721. action_field != PLINK_CLOSE)
  722. return;
  723. ies = mgmt->u.action.u.slf_prot_action.variable;
  724. ie_len = (const u8 *) mgmt + len -
  725. mgmt->u.action.u.slf_prot_action.variable;
  726. /* at least expect mesh id and peering mgmt */
  727. if (ie_len < 2 + 2) {
  728. wpa_printf(MSG_DEBUG,
  729. "MPM: Ignore too short action frame %u ie_len %u",
  730. action_field, (unsigned int) ie_len);
  731. return;
  732. }
  733. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  734. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  735. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  736. WPA_GET_LE16(ies));
  737. ies += 2; /* capability */
  738. ie_len -= 2;
  739. }
  740. if (action_field == PLINK_CONFIRM) {
  741. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  742. ies += 2; /* aid */
  743. ie_len -= 2;
  744. }
  745. /* check for mesh peering, mesh id and mesh config IEs */
  746. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  747. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  748. return;
  749. }
  750. if (!elems.peer_mgmt) {
  751. wpa_printf(MSG_DEBUG,
  752. "MPM: No Mesh Peering Management element");
  753. return;
  754. }
  755. if (action_field != PLINK_CLOSE) {
  756. if (!elems.mesh_id || !elems.mesh_config) {
  757. wpa_printf(MSG_DEBUG,
  758. "MPM: No Mesh ID or Mesh Configuration element");
  759. return;
  760. }
  761. if (!matches_local(wpa_s, &elems)) {
  762. wpa_printf(MSG_DEBUG,
  763. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  764. return;
  765. }
  766. }
  767. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  768. elems.peer_mgmt,
  769. elems.peer_mgmt_len,
  770. &peer_mgmt_ie);
  771. if (ret) {
  772. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  773. return;
  774. }
  775. /* the sender's llid is our plid and vice-versa */
  776. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  777. if (peer_mgmt_ie.plid)
  778. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  779. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  780. sta = ap_get_sta(hapd, mgmt->sa);
  781. if (!sta) {
  782. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  783. return;
  784. }
  785. #ifdef CONFIG_SAE
  786. /* peer is in sae_accepted? */
  787. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  788. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  789. return;
  790. }
  791. #endif /* CONFIG_SAE */
  792. if (!sta->my_lid)
  793. mesh_mpm_init_link(wpa_s, sta);
  794. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  795. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  796. &mgmt->u.action.category,
  797. ies, ie_len)) {
  798. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  799. return;
  800. }
  801. if (sta->plink_state == PLINK_BLOCKED) {
  802. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  803. return;
  804. }
  805. /* Now we will figure out the appropriate event... */
  806. switch (action_field) {
  807. case PLINK_OPEN:
  808. if (!plink_free_count(hapd) ||
  809. (sta->peer_lid && sta->peer_lid != plid)) {
  810. event = OPN_IGNR;
  811. } else {
  812. sta->peer_lid = plid;
  813. event = OPN_ACPT;
  814. }
  815. break;
  816. case PLINK_CONFIRM:
  817. if (!plink_free_count(hapd) ||
  818. sta->my_lid != llid ||
  819. (sta->peer_lid && sta->peer_lid != plid)) {
  820. event = CNF_IGNR;
  821. } else {
  822. if (!sta->peer_lid)
  823. sta->peer_lid = plid;
  824. event = CNF_ACPT;
  825. }
  826. break;
  827. case PLINK_CLOSE:
  828. if (sta->plink_state == PLINK_ESTAB)
  829. /* Do not check for llid or plid. This does not
  830. * follow the standard but since multiple plinks
  831. * per cand are not supported, it is necessary in
  832. * order to avoid a livelock when MP A sees an
  833. * establish peer link to MP B but MP B does not
  834. * see it. This can be caused by a timeout in
  835. * B's peer link establishment or B being
  836. * restarted.
  837. */
  838. event = CLS_ACPT;
  839. else if (sta->peer_lid != plid)
  840. event = CLS_IGNR;
  841. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  842. event = CLS_IGNR;
  843. else
  844. event = CLS_ACPT;
  845. break;
  846. default:
  847. /*
  848. * This cannot be hit due to the action_field check above, but
  849. * compilers may not be able to figure that out and can warn
  850. * about uninitialized event below.
  851. */
  852. return;
  853. }
  854. mesh_mpm_fsm(wpa_s, sta, event);
  855. }
  856. /* called by ap_free_sta */
  857. void mesh_mpm_free_sta(struct sta_info *sta)
  858. {
  859. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  860. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  861. }