mesh_mpm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. size_t buf_len;
  186. if (!sta)
  187. return;
  188. buf_len = 2 + /* capability info */
  189. 2 + /* AID */
  190. 2 + 8 + /* supported rates */
  191. 2 + (32 - 8) +
  192. 2 + 32 + /* mesh ID */
  193. 2 + 7 + /* mesh config */
  194. 2 + 23 + /* peering management */
  195. 2 + 96 + /* AMPE */
  196. 2 + 16; /* MIC */
  197. #ifdef CONFIG_IEEE80211N
  198. if (type != PLINK_CLOSE &&
  199. wpa_s->current_ssid->mesh_ht_mode > CHAN_NO_HT) {
  200. buf_len += 2 + 26 + /* HT capabilities */
  201. 2 + 22; /* HT operation */
  202. }
  203. #endif /* CONFIG_IEEE80211N */
  204. buf = wpabuf_alloc(buf_len);
  205. if (!buf)
  206. return;
  207. cat = wpabuf_mhead_u8(buf);
  208. wpabuf_put_u8(buf, WLAN_ACTION_SELF_PROTECTED);
  209. wpabuf_put_u8(buf, type);
  210. if (type != PLINK_CLOSE) {
  211. /* capability info */
  212. wpabuf_put_le16(buf, ampe ? IEEE80211_CAP_PRIVACY : 0);
  213. /* aid */
  214. if (type == PLINK_CONFIRM)
  215. wpabuf_put_le16(buf, sta->peer_lid);
  216. /* IE: supp + ext. supp rates */
  217. pos = hostapd_eid_supp_rates(bss, supp_rates);
  218. pos = hostapd_eid_ext_supp_rates(bss, pos);
  219. wpabuf_put_data(buf, supp_rates, pos - supp_rates);
  220. /* IE: Mesh ID */
  221. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  222. wpabuf_put_u8(buf, conf->meshid_len);
  223. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  224. /* IE: mesh conf */
  225. wpabuf_put_u8(buf, WLAN_EID_MESH_CONFIG);
  226. wpabuf_put_u8(buf, 7);
  227. wpabuf_put_u8(buf, conf->mesh_pp_id);
  228. wpabuf_put_u8(buf, conf->mesh_pm_id);
  229. wpabuf_put_u8(buf, conf->mesh_cc_id);
  230. wpabuf_put_u8(buf, conf->mesh_sp_id);
  231. wpabuf_put_u8(buf, conf->mesh_auth_id);
  232. /* TODO: formation info */
  233. wpabuf_put_u8(buf, 0);
  234. /* always forwarding & accepting plinks for now */
  235. wpabuf_put_u8(buf, 0x1 | 0x8);
  236. } else { /* Peer closing frame */
  237. /* IE: Mesh ID */
  238. wpabuf_put_u8(buf, WLAN_EID_MESH_ID);
  239. wpabuf_put_u8(buf, conf->meshid_len);
  240. wpabuf_put_data(buf, conf->meshid, conf->meshid_len);
  241. }
  242. /* IE: Mesh Peering Management element */
  243. ie_len = 4;
  244. if (ampe)
  245. ie_len += PMKID_LEN;
  246. switch (type) {
  247. case PLINK_OPEN:
  248. break;
  249. case PLINK_CONFIRM:
  250. ie_len += 2;
  251. add_plid = 1;
  252. break;
  253. case PLINK_CLOSE:
  254. ie_len += 2;
  255. add_plid = 1;
  256. ie_len += 2; /* reason code */
  257. break;
  258. }
  259. wpabuf_put_u8(buf, WLAN_EID_PEER_MGMT);
  260. wpabuf_put_u8(buf, ie_len);
  261. /* peering protocol */
  262. if (ampe)
  263. wpabuf_put_le16(buf, 1);
  264. else
  265. wpabuf_put_le16(buf, 0);
  266. wpabuf_put_le16(buf, sta->my_lid);
  267. if (add_plid)
  268. wpabuf_put_le16(buf, sta->peer_lid);
  269. if (type == PLINK_CLOSE)
  270. wpabuf_put_le16(buf, close_reason);
  271. if (ampe)
  272. mesh_rsn_get_pmkid(wpa_s->mesh_rsn, sta,
  273. wpabuf_put(buf, PMKID_LEN));
  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. switch (sta->plink_state) {
  335. case PLINK_OPEN_RCVD:
  336. case PLINK_OPEN_SENT:
  337. /* retry timer */
  338. if (sta->mpm_retries < dot11MeshMaxRetries) {
  339. eloop_register_timeout(dot11MeshRetryTimeout, 0,
  340. plink_timer, wpa_s, sta);
  341. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  342. sta->mpm_retries++;
  343. break;
  344. }
  345. reason = WLAN_REASON_MESH_MAX_RETRIES;
  346. /* fall through on else */
  347. case PLINK_CNF_RCVD:
  348. /* confirm timer */
  349. if (!reason)
  350. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  351. sta->plink_state = PLINK_HOLDING;
  352. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  353. plink_timer, wpa_s, sta);
  354. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  355. break;
  356. case PLINK_HOLDING:
  357. /* holding timer */
  358. mesh_mpm_fsm_restart(wpa_s, sta);
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. /* initiate peering with station */
  365. static void
  366. mesh_mpm_plink_open(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  367. enum mesh_plink_state next_state)
  368. {
  369. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  370. eloop_register_timeout(dot11MeshRetryTimeout, 0, plink_timer,
  371. wpa_s, sta);
  372. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_OPEN, 0);
  373. wpa_mesh_set_plink_state(wpa_s, sta, next_state);
  374. }
  375. int mesh_mpm_plink_close(struct hostapd_data *hapd,
  376. struct sta_info *sta, void *ctx)
  377. {
  378. struct wpa_supplicant *wpa_s = ctx;
  379. int reason = WLAN_REASON_MESH_PEERING_CANCELLED;
  380. if (sta) {
  381. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  382. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CLOSE, reason);
  383. wpa_printf(MSG_DEBUG, "MPM closing plink sta=" MACSTR,
  384. MAC2STR(sta->addr));
  385. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  386. return 0;
  387. }
  388. return 1;
  389. }
  390. void mesh_mpm_deinit(struct wpa_supplicant *wpa_s, struct hostapd_iface *ifmsh)
  391. {
  392. struct hostapd_data *hapd = ifmsh->bss[0];
  393. /* notify peers we're leaving */
  394. ap_for_each_sta(hapd, mesh_mpm_plink_close, wpa_s);
  395. hapd->num_plinks = 0;
  396. hostapd_free_stas(hapd);
  397. }
  398. /* for mesh_rsn to indicate this peer has completed authentication, and we're
  399. * ready to start AMPE */
  400. void mesh_mpm_auth_peer(struct wpa_supplicant *wpa_s, const u8 *addr)
  401. {
  402. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  403. struct hostapd_sta_add_params params;
  404. struct sta_info *sta;
  405. int ret;
  406. sta = ap_get_sta(data, addr);
  407. if (!sta) {
  408. wpa_msg(wpa_s, MSG_DEBUG, "no such mesh peer");
  409. return;
  410. }
  411. /* TODO: Should do nothing if this STA is already authenticated, but
  412. * the AP code already sets this flag. */
  413. sta->flags |= WLAN_STA_AUTH;
  414. mesh_rsn_init_ampe_sta(wpa_s, sta);
  415. os_memset(&params, 0, sizeof(params));
  416. params.addr = sta->addr;
  417. params.flags = WPA_STA_AUTHENTICATED | WPA_STA_AUTHORIZED;
  418. params.set = 1;
  419. wpa_msg(wpa_s, MSG_DEBUG, "MPM authenticating " MACSTR,
  420. MAC2STR(sta->addr));
  421. ret = wpa_drv_sta_add(wpa_s, &params);
  422. if (ret) {
  423. wpa_msg(wpa_s, MSG_ERROR,
  424. "Driver failed to set " MACSTR ": %d",
  425. MAC2STR(sta->addr), ret);
  426. }
  427. if (!sta->my_lid)
  428. mesh_mpm_init_link(wpa_s, sta);
  429. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  430. }
  431. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  432. struct ieee802_11_elems *elems)
  433. {
  434. struct hostapd_sta_add_params params;
  435. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  436. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  437. struct sta_info *sta;
  438. struct wpa_ssid *ssid = wpa_s->current_ssid;
  439. int ret = 0;
  440. sta = ap_get_sta(data, addr);
  441. if (!sta) {
  442. sta = ap_sta_add(data, addr);
  443. if (!sta)
  444. return;
  445. }
  446. /* initialize sta */
  447. if (copy_supp_rates(wpa_s, sta, elems))
  448. return;
  449. mesh_mpm_init_link(wpa_s, sta);
  450. #ifdef CONFIG_IEEE80211N
  451. copy_sta_ht_capab(data, sta, elems->ht_capabilities,
  452. elems->ht_capabilities_len);
  453. update_ht_state(data, sta);
  454. #endif /* CONFIG_IEEE80211N */
  455. /* insert into driver */
  456. os_memset(&params, 0, sizeof(params));
  457. params.supp_rates = sta->supported_rates;
  458. params.supp_rates_len = sta->supported_rates_len;
  459. params.addr = addr;
  460. params.plink_state = sta->plink_state;
  461. params.aid = sta->peer_lid;
  462. params.listen_interval = 100;
  463. params.ht_capabilities = sta->ht_capabilities;
  464. params.flags |= WPA_STA_WMM;
  465. params.flags_mask |= WPA_STA_AUTHENTICATED;
  466. if (conf->security == MESH_CONF_SEC_NONE) {
  467. params.flags |= WPA_STA_AUTHORIZED;
  468. params.flags |= WPA_STA_AUTHENTICATED;
  469. } else {
  470. sta->flags |= WLAN_STA_MFP;
  471. params.flags |= WPA_STA_MFP;
  472. }
  473. ret = wpa_drv_sta_add(wpa_s, &params);
  474. if (ret) {
  475. wpa_msg(wpa_s, MSG_ERROR,
  476. "Driver failed to insert " MACSTR ": %d",
  477. MAC2STR(addr), ret);
  478. return;
  479. }
  480. if (ssid && ssid->no_auto_peer) {
  481. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  482. MACSTR " because of no_auto_peer", MAC2STR(addr));
  483. return;
  484. }
  485. if (conf->security == MESH_CONF_SEC_NONE)
  486. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  487. else
  488. mesh_rsn_auth_sae_sta(wpa_s, sta);
  489. }
  490. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  491. {
  492. struct hostapd_frame_info fi;
  493. os_memset(&fi, 0, sizeof(fi));
  494. fi.datarate = rx_mgmt->datarate;
  495. fi.ssi_signal = rx_mgmt->ssi_signal;
  496. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  497. rx_mgmt->frame_len, &fi);
  498. }
  499. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  500. struct sta_info *sta)
  501. {
  502. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  503. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  504. u8 seq[6] = {};
  505. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  506. MAC2STR(sta->addr));
  507. if (conf->security & MESH_CONF_SEC_AMPE) {
  508. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  509. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  510. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  511. seq, sizeof(seq),
  512. sta->mgtk, sizeof(sta->mgtk));
  513. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  514. seq, sizeof(seq),
  515. sta->mgtk, sizeof(sta->mgtk));
  516. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  517. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  518. sta->mgtk, sizeof(sta->mgtk));
  519. }
  520. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  521. hapd->num_plinks++;
  522. sta->flags |= WLAN_STA_ASSOC;
  523. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  524. /* Send ctrl event */
  525. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  526. MAC2STR(sta->addr));
  527. }
  528. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  529. enum plink_event event)
  530. {
  531. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  532. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  533. u16 reason = 0;
  534. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  535. MAC2STR(sta->addr), mplstate[sta->plink_state],
  536. mplevent[event]);
  537. switch (sta->plink_state) {
  538. case PLINK_LISTEN:
  539. switch (event) {
  540. case CLS_ACPT:
  541. mesh_mpm_fsm_restart(wpa_s, sta);
  542. break;
  543. case OPN_ACPT:
  544. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  545. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  546. 0);
  547. break;
  548. default:
  549. break;
  550. }
  551. break;
  552. case PLINK_OPEN_SENT:
  553. switch (event) {
  554. case OPN_RJCT:
  555. case CNF_RJCT:
  556. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  557. /* fall-through */
  558. case CLS_ACPT:
  559. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  560. if (!reason)
  561. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  562. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  563. plink_timer, wpa_s, sta);
  564. mesh_mpm_send_plink_action(wpa_s, sta,
  565. PLINK_CLOSE, reason);
  566. break;
  567. case OPN_ACPT:
  568. /* retry timer is left untouched */
  569. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  570. mesh_mpm_send_plink_action(wpa_s, sta,
  571. PLINK_CONFIRM, 0);
  572. break;
  573. case CNF_ACPT:
  574. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  575. eloop_register_timeout(dot11MeshConfirmTimeout, 0,
  576. plink_timer, wpa_s, sta);
  577. break;
  578. default:
  579. break;
  580. }
  581. break;
  582. case PLINK_OPEN_RCVD:
  583. switch (event) {
  584. case OPN_RJCT:
  585. case CNF_RJCT:
  586. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  587. /* fall-through */
  588. case CLS_ACPT:
  589. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  590. if (!reason)
  591. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  592. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  593. plink_timer, wpa_s, sta);
  594. sta->mpm_close_reason = reason;
  595. mesh_mpm_send_plink_action(wpa_s, sta,
  596. PLINK_CLOSE, reason);
  597. break;
  598. case OPN_ACPT:
  599. mesh_mpm_send_plink_action(wpa_s, sta,
  600. PLINK_CONFIRM, 0);
  601. break;
  602. case CNF_ACPT:
  603. if (conf->security & MESH_CONF_SEC_AMPE)
  604. mesh_rsn_derive_mtk(wpa_s, sta);
  605. mesh_mpm_plink_estab(wpa_s, sta);
  606. break;
  607. default:
  608. break;
  609. }
  610. break;
  611. case PLINK_CNF_RCVD:
  612. switch (event) {
  613. case OPN_RJCT:
  614. case CNF_RJCT:
  615. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  616. /* fall-through */
  617. case CLS_ACPT:
  618. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  619. if (!reason)
  620. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  621. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  622. plink_timer, wpa_s, sta);
  623. sta->mpm_close_reason = reason;
  624. mesh_mpm_send_plink_action(wpa_s, sta,
  625. PLINK_CLOSE, reason);
  626. break;
  627. case OPN_ACPT:
  628. mesh_mpm_plink_estab(wpa_s, sta);
  629. mesh_mpm_send_plink_action(wpa_s, sta,
  630. PLINK_CONFIRM, 0);
  631. break;
  632. default:
  633. break;
  634. }
  635. break;
  636. case PLINK_ESTAB:
  637. switch (event) {
  638. case CLS_ACPT:
  639. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  640. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  641. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  642. plink_timer, wpa_s, sta);
  643. sta->mpm_close_reason = reason;
  644. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  645. " closed with reason %d",
  646. MAC2STR(sta->addr), reason);
  647. wpa_msg_ctrl(wpa_s, MSG_INFO,
  648. MESH_PEER_DISCONNECTED MACSTR,
  649. MAC2STR(sta->addr));
  650. hapd->num_plinks--;
  651. mesh_mpm_send_plink_action(wpa_s, sta,
  652. PLINK_CLOSE, reason);
  653. break;
  654. case OPN_ACPT:
  655. mesh_mpm_send_plink_action(wpa_s, sta,
  656. PLINK_CONFIRM, 0);
  657. break;
  658. default:
  659. break;
  660. }
  661. break;
  662. case PLINK_HOLDING:
  663. switch (event) {
  664. case CLS_ACPT:
  665. mesh_mpm_fsm_restart(wpa_s, sta);
  666. break;
  667. case OPN_ACPT:
  668. case CNF_ACPT:
  669. case OPN_RJCT:
  670. case CNF_RJCT:
  671. reason = sta->mpm_close_reason;
  672. mesh_mpm_send_plink_action(wpa_s, sta,
  673. PLINK_CLOSE, reason);
  674. break;
  675. default:
  676. break;
  677. }
  678. break;
  679. default:
  680. wpa_msg(wpa_s, MSG_DEBUG,
  681. "Unsupported MPM event %s for state %s",
  682. mplevent[event], mplstate[sta->plink_state]);
  683. break;
  684. }
  685. }
  686. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  687. const struct ieee80211_mgmt *mgmt, size_t len)
  688. {
  689. u8 action_field;
  690. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  691. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  692. struct sta_info *sta;
  693. u16 plid = 0, llid = 0;
  694. enum plink_event event;
  695. struct ieee802_11_elems elems;
  696. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  697. const u8 *ies;
  698. size_t ie_len;
  699. int ret;
  700. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  701. return;
  702. action_field = mgmt->u.action.u.slf_prot_action.action;
  703. ies = mgmt->u.action.u.slf_prot_action.variable;
  704. ie_len = (const u8 *) mgmt + len -
  705. mgmt->u.action.u.slf_prot_action.variable;
  706. /* at least expect mesh id and peering mgmt */
  707. if (ie_len < 2 + 2)
  708. return;
  709. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  710. ies += 2; /* capability */
  711. ie_len -= 2;
  712. }
  713. if (action_field == PLINK_CONFIRM) {
  714. ies += 2; /* aid */
  715. ie_len -= 2;
  716. }
  717. /* check for mesh peering, mesh id and mesh config IEs */
  718. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed)
  719. return;
  720. if (!elems.peer_mgmt)
  721. return;
  722. if ((action_field != PLINK_CLOSE) &&
  723. (!elems.mesh_id || !elems.mesh_config))
  724. return;
  725. if (action_field != PLINK_CLOSE && !matches_local(wpa_s, &elems))
  726. return;
  727. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  728. elems.peer_mgmt,
  729. elems.peer_mgmt_len,
  730. &peer_mgmt_ie);
  731. if (ret)
  732. return;
  733. /* the sender's llid is our plid and vice-versa */
  734. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  735. if (peer_mgmt_ie.plid)
  736. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  737. sta = ap_get_sta(hapd, mgmt->sa);
  738. if (!sta)
  739. return;
  740. #ifdef CONFIG_SAE
  741. /* peer is in sae_accepted? */
  742. if (sta->sae && sta->sae->state != SAE_ACCEPTED)
  743. return;
  744. #endif /* CONFIG_SAE */
  745. if (!sta->my_lid)
  746. mesh_mpm_init_link(wpa_s, sta);
  747. if (mconf->security & MESH_CONF_SEC_AMPE)
  748. if (mesh_rsn_process_ampe(wpa_s, sta, &elems,
  749. &mgmt->u.action.category,
  750. ies, ie_len))
  751. return;
  752. if (sta->plink_state == PLINK_BLOCKED)
  753. return;
  754. /* Now we will figure out the appropriate event... */
  755. switch (action_field) {
  756. case PLINK_OPEN:
  757. if (!plink_free_count(hapd) ||
  758. (sta->peer_lid && sta->peer_lid != plid)) {
  759. event = OPN_IGNR;
  760. } else {
  761. sta->peer_lid = plid;
  762. event = OPN_ACPT;
  763. }
  764. break;
  765. case PLINK_CONFIRM:
  766. if (!plink_free_count(hapd) ||
  767. sta->my_lid != llid ||
  768. (sta->peer_lid && sta->peer_lid != plid)) {
  769. event = CNF_IGNR;
  770. } else {
  771. if (!sta->peer_lid)
  772. sta->peer_lid = plid;
  773. event = CNF_ACPT;
  774. }
  775. break;
  776. case PLINK_CLOSE:
  777. if (sta->plink_state == PLINK_ESTAB)
  778. /* Do not check for llid or plid. This does not
  779. * follow the standard but since multiple plinks
  780. * per cand are not supported, it is necessary in
  781. * order to avoid a livelock when MP A sees an
  782. * establish peer link to MP B but MP B does not
  783. * see it. This can be caused by a timeout in
  784. * B's peer link establishment or B being
  785. * restarted.
  786. */
  787. event = CLS_ACPT;
  788. else if (sta->peer_lid != plid)
  789. event = CLS_IGNR;
  790. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  791. event = CLS_IGNR;
  792. else
  793. event = CLS_ACPT;
  794. break;
  795. default:
  796. wpa_msg(wpa_s, MSG_ERROR,
  797. "Mesh plink: unknown frame subtype");
  798. return;
  799. }
  800. mesh_mpm_fsm(wpa_s, sta, event);
  801. }
  802. /* called by ap_free_sta */
  803. void mesh_mpm_free_sta(struct sta_info *sta)
  804. {
  805. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  806. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  807. }