mesh_mpm.c 23 KB

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