mesh_mpm.c 21 KB

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