mesh_mpm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. struct wpa_ssid *ssid = wpa_s->current_ssid;
  409. int ret = 0;
  410. sta = ap_get_sta(data, addr);
  411. if (!sta) {
  412. sta = ap_sta_add(data, addr);
  413. if (!sta)
  414. return;
  415. }
  416. /* initialize sta */
  417. if (copy_supp_rates(wpa_s, sta, elems))
  418. return;
  419. mesh_mpm_init_link(wpa_s, sta);
  420. /* insert into driver */
  421. os_memset(&params, 0, sizeof(params));
  422. params.supp_rates = sta->supported_rates;
  423. params.supp_rates_len = sta->supported_rates_len;
  424. params.addr = addr;
  425. params.plink_state = sta->plink_state;
  426. params.aid = sta->peer_lid;
  427. params.listen_interval = 100;
  428. /* TODO: HT capabilities */
  429. params.flags |= WPA_STA_WMM;
  430. params.flags_mask |= WPA_STA_AUTHENTICATED;
  431. if (conf->security == MESH_CONF_SEC_NONE) {
  432. params.flags |= WPA_STA_AUTHORIZED;
  433. params.flags |= WPA_STA_AUTHENTICATED;
  434. } else {
  435. sta->flags |= WLAN_STA_MFP;
  436. params.flags |= WPA_STA_MFP;
  437. }
  438. ret = wpa_drv_sta_add(wpa_s, &params);
  439. if (ret) {
  440. wpa_msg(wpa_s, MSG_ERROR,
  441. "Driver failed to insert " MACSTR ": %d",
  442. MAC2STR(addr), ret);
  443. return;
  444. }
  445. if (ssid && ssid->no_auto_peer) {
  446. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  447. MACSTR " because of no_auto_peer", MAC2STR(addr));
  448. return;
  449. }
  450. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  451. }
  452. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  453. {
  454. struct hostapd_frame_info fi;
  455. os_memset(&fi, 0, sizeof(fi));
  456. fi.datarate = rx_mgmt->datarate;
  457. fi.ssi_signal = rx_mgmt->ssi_signal;
  458. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  459. rx_mgmt->frame_len, &fi);
  460. }
  461. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  462. struct sta_info *sta)
  463. {
  464. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  465. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  466. MAC2STR(sta->addr));
  467. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  468. hapd->num_plinks++;
  469. sta->flags |= WLAN_STA_ASSOC;
  470. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  471. /* Send ctrl event */
  472. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  473. MAC2STR(sta->addr));
  474. }
  475. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  476. enum plink_event event)
  477. {
  478. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  479. u16 reason = 0;
  480. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  481. MAC2STR(sta->addr), mplstate[sta->plink_state],
  482. mplevent[event]);
  483. switch (sta->plink_state) {
  484. case PLINK_LISTEN:
  485. switch (event) {
  486. case CLS_ACPT:
  487. mesh_mpm_fsm_restart(wpa_s, sta);
  488. break;
  489. case OPN_ACPT:
  490. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  491. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  492. 0);
  493. break;
  494. default:
  495. break;
  496. }
  497. break;
  498. case PLINK_OPEN_SENT:
  499. switch (event) {
  500. case OPN_RJCT:
  501. case CNF_RJCT:
  502. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  503. /* fall-through */
  504. case CLS_ACPT:
  505. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  506. if (!reason)
  507. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  508. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  509. plink_timer, wpa_s, sta);
  510. mesh_mpm_send_plink_action(wpa_s, sta,
  511. PLINK_CLOSE, reason);
  512. break;
  513. case OPN_ACPT:
  514. /* retry timer is left untouched */
  515. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  516. mesh_mpm_send_plink_action(wpa_s, sta,
  517. PLINK_CONFIRM, 0);
  518. break;
  519. case CNF_ACPT:
  520. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  521. eloop_register_timeout(dot11MeshConfirmTimeout, 0,
  522. plink_timer, wpa_s, sta);
  523. break;
  524. default:
  525. break;
  526. }
  527. break;
  528. case PLINK_OPEN_RCVD:
  529. switch (event) {
  530. case OPN_RJCT:
  531. case CNF_RJCT:
  532. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  533. /* fall-through */
  534. case CLS_ACPT:
  535. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  536. if (!reason)
  537. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  538. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  539. plink_timer, wpa_s, sta);
  540. sta->mpm_close_reason = reason;
  541. mesh_mpm_send_plink_action(wpa_s, sta,
  542. PLINK_CLOSE, reason);
  543. break;
  544. case OPN_ACPT:
  545. mesh_mpm_send_plink_action(wpa_s, sta,
  546. PLINK_CONFIRM, 0);
  547. break;
  548. case CNF_ACPT:
  549. mesh_mpm_plink_estab(wpa_s, sta);
  550. break;
  551. default:
  552. break;
  553. }
  554. break;
  555. case PLINK_CNF_RCVD:
  556. switch (event) {
  557. case OPN_RJCT:
  558. case CNF_RJCT:
  559. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  560. /* fall-through */
  561. case CLS_ACPT:
  562. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  563. if (!reason)
  564. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  565. eloop_register_timeout(dot11MeshHoldingTimeout, 0,
  566. plink_timer, wpa_s, sta);
  567. sta->mpm_close_reason = reason;
  568. mesh_mpm_send_plink_action(wpa_s, sta,
  569. PLINK_CLOSE, reason);
  570. break;
  571. case OPN_ACPT:
  572. mesh_mpm_plink_estab(wpa_s, sta);
  573. mesh_mpm_send_plink_action(wpa_s, sta,
  574. PLINK_CONFIRM, 0);
  575. break;
  576. default:
  577. break;
  578. }
  579. break;
  580. case PLINK_ESTAB:
  581. switch (event) {
  582. case CLS_ACPT:
  583. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  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. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  589. " closed with reason %d",
  590. MAC2STR(sta->addr), reason);
  591. wpa_msg_ctrl(wpa_s, MSG_INFO,
  592. MESH_PEER_DISCONNECTED MACSTR,
  593. MAC2STR(sta->addr));
  594. hapd->num_plinks--;
  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. default:
  603. break;
  604. }
  605. break;
  606. case PLINK_HOLDING:
  607. switch (event) {
  608. case CLS_ACPT:
  609. mesh_mpm_fsm_restart(wpa_s, sta);
  610. break;
  611. case OPN_ACPT:
  612. case CNF_ACPT:
  613. case OPN_RJCT:
  614. case CNF_RJCT:
  615. reason = sta->mpm_close_reason;
  616. mesh_mpm_send_plink_action(wpa_s, sta,
  617. PLINK_CLOSE, reason);
  618. break;
  619. default:
  620. break;
  621. }
  622. break;
  623. default:
  624. wpa_msg(wpa_s, MSG_DEBUG,
  625. "Unsupported MPM event %s for state %s",
  626. mplevent[event], mplstate[sta->plink_state]);
  627. break;
  628. }
  629. }
  630. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  631. const struct ieee80211_mgmt *mgmt, size_t len)
  632. {
  633. u8 action_field;
  634. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  635. struct sta_info *sta;
  636. u16 plid = 0, llid = 0;
  637. enum plink_event event;
  638. struct ieee802_11_elems elems;
  639. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  640. const u8 *ies;
  641. size_t ie_len;
  642. int ret;
  643. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  644. return;
  645. action_field = mgmt->u.action.u.slf_prot_action.action;
  646. ies = mgmt->u.action.u.slf_prot_action.variable;
  647. ie_len = (const u8 *) mgmt + len -
  648. mgmt->u.action.u.slf_prot_action.variable;
  649. /* at least expect mesh id and peering mgmt */
  650. if (ie_len < 2 + 2)
  651. return;
  652. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  653. ies += 2; /* capability */
  654. ie_len -= 2;
  655. }
  656. if (action_field == PLINK_CONFIRM) {
  657. ies += 2; /* aid */
  658. ie_len -= 2;
  659. }
  660. /* check for mesh peering, mesh id and mesh config IEs */
  661. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed)
  662. return;
  663. if (!elems.peer_mgmt)
  664. return;
  665. if ((action_field != PLINK_CLOSE) &&
  666. (!elems.mesh_id || !elems.mesh_config))
  667. return;
  668. if (action_field != PLINK_CLOSE && !matches_local(wpa_s, &elems))
  669. return;
  670. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  671. elems.peer_mgmt,
  672. elems.peer_mgmt_len,
  673. &peer_mgmt_ie);
  674. if (ret)
  675. return;
  676. /* the sender's llid is our plid and vice-versa */
  677. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  678. if (peer_mgmt_ie.plid)
  679. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  680. sta = ap_get_sta(hapd, mgmt->sa);
  681. if (!sta)
  682. return;
  683. if (!sta->my_lid)
  684. mesh_mpm_init_link(wpa_s, sta);
  685. if (sta->plink_state == PLINK_BLOCKED)
  686. return;
  687. /* Now we will figure out the appropriate event... */
  688. switch (action_field) {
  689. case PLINK_OPEN:
  690. if (!plink_free_count(hapd) ||
  691. (sta->peer_lid && sta->peer_lid != plid)) {
  692. event = OPN_IGNR;
  693. } else {
  694. sta->peer_lid = plid;
  695. event = OPN_ACPT;
  696. }
  697. break;
  698. case PLINK_CONFIRM:
  699. if (!plink_free_count(hapd) ||
  700. sta->my_lid != llid ||
  701. (sta->peer_lid && sta->peer_lid != plid)) {
  702. event = CNF_IGNR;
  703. } else {
  704. if (!sta->peer_lid)
  705. sta->peer_lid = plid;
  706. event = CNF_ACPT;
  707. }
  708. break;
  709. case PLINK_CLOSE:
  710. if (sta->plink_state == PLINK_ESTAB)
  711. /* Do not check for llid or plid. This does not
  712. * follow the standard but since multiple plinks
  713. * per cand are not supported, it is necessary in
  714. * order to avoid a livelock when MP A sees an
  715. * establish peer link to MP B but MP B does not
  716. * see it. This can be caused by a timeout in
  717. * B's peer link establishment or B being
  718. * restarted.
  719. */
  720. event = CLS_ACPT;
  721. else if (sta->peer_lid != plid)
  722. event = CLS_IGNR;
  723. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  724. event = CLS_IGNR;
  725. else
  726. event = CLS_ACPT;
  727. break;
  728. default:
  729. wpa_msg(wpa_s, MSG_ERROR,
  730. "Mesh plink: unknown frame subtype");
  731. return;
  732. }
  733. mesh_mpm_fsm(wpa_s, sta, event);
  734. }