mesh_mpm.c 26 KB

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