mesh_mpm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. /*
  436. * Initialize a sta_info structure for a peer and upload it into the driver
  437. * in preparation for beginning authentication or peering. This is done when a
  438. * Beacon (secure or open mesh) or a peering open frame (for open mesh) is
  439. * received from the peer for the first time.
  440. */
  441. static struct sta_info * mesh_mpm_add_peer(struct wpa_supplicant *wpa_s,
  442. const u8 *addr,
  443. struct ieee802_11_elems *elems)
  444. {
  445. struct hostapd_sta_add_params params;
  446. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  447. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  448. struct sta_info *sta;
  449. int ret;
  450. sta = ap_get_sta(data, addr);
  451. if (!sta) {
  452. sta = ap_sta_add(data, addr);
  453. if (!sta)
  454. return NULL;
  455. }
  456. /* initialize sta */
  457. if (copy_supp_rates(wpa_s, sta, elems)) {
  458. ap_free_sta(data, sta);
  459. return NULL;
  460. }
  461. mesh_mpm_init_link(wpa_s, sta);
  462. #ifdef CONFIG_IEEE80211N
  463. copy_sta_ht_capab(data, sta, elems->ht_capabilities,
  464. elems->ht_capabilities_len);
  465. update_ht_state(data, sta);
  466. #endif /* CONFIG_IEEE80211N */
  467. /* insert into driver */
  468. os_memset(&params, 0, sizeof(params));
  469. params.supp_rates = sta->supported_rates;
  470. params.supp_rates_len = sta->supported_rates_len;
  471. params.addr = addr;
  472. params.plink_state = sta->plink_state;
  473. params.aid = sta->peer_lid;
  474. params.listen_interval = 100;
  475. params.ht_capabilities = sta->ht_capabilities;
  476. params.flags |= WPA_STA_WMM;
  477. params.flags_mask |= WPA_STA_AUTHENTICATED;
  478. if (conf->security == MESH_CONF_SEC_NONE) {
  479. params.flags |= WPA_STA_AUTHORIZED;
  480. params.flags |= WPA_STA_AUTHENTICATED;
  481. } else {
  482. sta->flags |= WLAN_STA_MFP;
  483. params.flags |= WPA_STA_MFP;
  484. }
  485. ret = wpa_drv_sta_add(wpa_s, &params);
  486. if (ret) {
  487. wpa_msg(wpa_s, MSG_ERROR,
  488. "Driver failed to insert " MACSTR ": %d",
  489. MAC2STR(addr), ret);
  490. ap_free_sta(data, sta);
  491. return NULL;
  492. }
  493. return sta;
  494. }
  495. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  496. struct ieee802_11_elems *elems)
  497. {
  498. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  499. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  500. struct sta_info *sta;
  501. struct wpa_ssid *ssid = wpa_s->current_ssid;
  502. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  503. if (!sta)
  504. return;
  505. if (ssid && ssid->no_auto_peer) {
  506. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  507. MACSTR " because of no_auto_peer", MAC2STR(addr));
  508. if (data->mesh_pending_auth) {
  509. struct os_reltime age;
  510. const struct ieee80211_mgmt *mgmt;
  511. struct hostapd_frame_info fi;
  512. mgmt = wpabuf_head(data->mesh_pending_auth);
  513. os_reltime_age(&data->mesh_pending_auth_time, &age);
  514. if (age.sec < 2 &&
  515. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  516. wpa_printf(MSG_DEBUG,
  517. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  518. (unsigned int) age.sec,
  519. (unsigned int) age.usec);
  520. os_memset(&fi, 0, sizeof(fi));
  521. ieee802_11_mgmt(
  522. data,
  523. wpabuf_head(data->mesh_pending_auth),
  524. wpabuf_len(data->mesh_pending_auth),
  525. &fi);
  526. }
  527. wpabuf_free(data->mesh_pending_auth);
  528. data->mesh_pending_auth = NULL;
  529. }
  530. return;
  531. }
  532. if (conf->security == MESH_CONF_SEC_NONE)
  533. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  534. else
  535. mesh_rsn_auth_sae_sta(wpa_s, sta);
  536. }
  537. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  538. {
  539. struct hostapd_frame_info fi;
  540. os_memset(&fi, 0, sizeof(fi));
  541. fi.datarate = rx_mgmt->datarate;
  542. fi.ssi_signal = rx_mgmt->ssi_signal;
  543. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  544. rx_mgmt->frame_len, &fi);
  545. }
  546. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  547. struct sta_info *sta)
  548. {
  549. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  550. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  551. u8 seq[6] = {};
  552. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  553. MAC2STR(sta->addr));
  554. if (conf->security & MESH_CONF_SEC_AMPE) {
  555. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  556. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  557. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  558. seq, sizeof(seq),
  559. sta->mgtk, sizeof(sta->mgtk));
  560. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  561. seq, sizeof(seq),
  562. sta->mgtk, sizeof(sta->mgtk));
  563. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  564. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  565. sta->mgtk, sizeof(sta->mgtk));
  566. }
  567. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  568. hapd->num_plinks++;
  569. sta->flags |= WLAN_STA_ASSOC;
  570. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  571. /* Send ctrl event */
  572. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  573. MAC2STR(sta->addr));
  574. }
  575. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  576. enum plink_event event)
  577. {
  578. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  579. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  580. u16 reason = 0;
  581. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  582. MAC2STR(sta->addr), mplstate[sta->plink_state],
  583. mplevent[event]);
  584. switch (sta->plink_state) {
  585. case PLINK_LISTEN:
  586. switch (event) {
  587. case CLS_ACPT:
  588. mesh_mpm_fsm_restart(wpa_s, sta);
  589. break;
  590. case OPN_ACPT:
  591. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  592. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  593. 0);
  594. break;
  595. default:
  596. break;
  597. }
  598. break;
  599. case PLINK_OPEN_SENT:
  600. switch (event) {
  601. case OPN_RJCT:
  602. case CNF_RJCT:
  603. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  604. /* fall-through */
  605. case CLS_ACPT:
  606. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  607. if (!reason)
  608. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  609. eloop_register_timeout(
  610. conf->dot11MeshHoldingTimeout / 1000,
  611. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  612. plink_timer, wpa_s, sta);
  613. mesh_mpm_send_plink_action(wpa_s, sta,
  614. PLINK_CLOSE, reason);
  615. break;
  616. case OPN_ACPT:
  617. /* retry timer is left untouched */
  618. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  619. mesh_mpm_send_plink_action(wpa_s, sta,
  620. PLINK_CONFIRM, 0);
  621. break;
  622. case CNF_ACPT:
  623. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  624. eloop_register_timeout(
  625. conf->dot11MeshConfirmTimeout / 1000,
  626. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  627. plink_timer, wpa_s, sta);
  628. break;
  629. default:
  630. break;
  631. }
  632. break;
  633. case PLINK_OPEN_RCVD:
  634. switch (event) {
  635. case OPN_RJCT:
  636. case CNF_RJCT:
  637. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  638. /* fall-through */
  639. case CLS_ACPT:
  640. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  641. if (!reason)
  642. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  643. eloop_register_timeout(
  644. conf->dot11MeshHoldingTimeout / 1000,
  645. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  646. plink_timer, wpa_s, sta);
  647. sta->mpm_close_reason = reason;
  648. mesh_mpm_send_plink_action(wpa_s, sta,
  649. PLINK_CLOSE, reason);
  650. break;
  651. case OPN_ACPT:
  652. mesh_mpm_send_plink_action(wpa_s, sta,
  653. PLINK_CONFIRM, 0);
  654. break;
  655. case CNF_ACPT:
  656. if (conf->security & MESH_CONF_SEC_AMPE)
  657. mesh_rsn_derive_mtk(wpa_s, sta);
  658. mesh_mpm_plink_estab(wpa_s, sta);
  659. break;
  660. default:
  661. break;
  662. }
  663. break;
  664. case PLINK_CNF_RCVD:
  665. switch (event) {
  666. case OPN_RJCT:
  667. case CNF_RJCT:
  668. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  669. /* fall-through */
  670. case CLS_ACPT:
  671. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  672. if (!reason)
  673. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  674. eloop_register_timeout(
  675. conf->dot11MeshHoldingTimeout / 1000,
  676. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  677. plink_timer, wpa_s, sta);
  678. sta->mpm_close_reason = reason;
  679. mesh_mpm_send_plink_action(wpa_s, sta,
  680. PLINK_CLOSE, reason);
  681. break;
  682. case OPN_ACPT:
  683. mesh_mpm_plink_estab(wpa_s, sta);
  684. mesh_mpm_send_plink_action(wpa_s, sta,
  685. PLINK_CONFIRM, 0);
  686. break;
  687. default:
  688. break;
  689. }
  690. break;
  691. case PLINK_ESTAB:
  692. switch (event) {
  693. case CLS_ACPT:
  694. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  695. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  696. eloop_register_timeout(
  697. conf->dot11MeshHoldingTimeout / 1000,
  698. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  699. plink_timer, wpa_s, sta);
  700. sta->mpm_close_reason = reason;
  701. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  702. " closed with reason %d",
  703. MAC2STR(sta->addr), reason);
  704. wpa_msg_ctrl(wpa_s, MSG_INFO,
  705. MESH_PEER_DISCONNECTED MACSTR,
  706. MAC2STR(sta->addr));
  707. hapd->num_plinks--;
  708. mesh_mpm_send_plink_action(wpa_s, sta,
  709. PLINK_CLOSE, reason);
  710. break;
  711. case OPN_ACPT:
  712. mesh_mpm_send_plink_action(wpa_s, sta,
  713. PLINK_CONFIRM, 0);
  714. break;
  715. default:
  716. break;
  717. }
  718. break;
  719. case PLINK_HOLDING:
  720. switch (event) {
  721. case CLS_ACPT:
  722. mesh_mpm_fsm_restart(wpa_s, sta);
  723. break;
  724. case OPN_ACPT:
  725. case CNF_ACPT:
  726. case OPN_RJCT:
  727. case CNF_RJCT:
  728. reason = sta->mpm_close_reason;
  729. mesh_mpm_send_plink_action(wpa_s, sta,
  730. PLINK_CLOSE, reason);
  731. break;
  732. default:
  733. break;
  734. }
  735. break;
  736. default:
  737. wpa_msg(wpa_s, MSG_DEBUG,
  738. "Unsupported MPM event %s for state %s",
  739. mplevent[event], mplstate[sta->plink_state]);
  740. break;
  741. }
  742. }
  743. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  744. const struct ieee80211_mgmt *mgmt, size_t len)
  745. {
  746. u8 action_field;
  747. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  748. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  749. struct sta_info *sta;
  750. u16 plid = 0, llid = 0;
  751. enum plink_event event;
  752. struct ieee802_11_elems elems;
  753. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  754. const u8 *ies;
  755. size_t ie_len;
  756. int ret;
  757. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  758. return;
  759. action_field = mgmt->u.action.u.slf_prot_action.action;
  760. if (action_field != PLINK_OPEN &&
  761. action_field != PLINK_CONFIRM &&
  762. action_field != PLINK_CLOSE)
  763. return;
  764. ies = mgmt->u.action.u.slf_prot_action.variable;
  765. ie_len = (const u8 *) mgmt + len -
  766. mgmt->u.action.u.slf_prot_action.variable;
  767. /* at least expect mesh id and peering mgmt */
  768. if (ie_len < 2 + 2) {
  769. wpa_printf(MSG_DEBUG,
  770. "MPM: Ignore too short action frame %u ie_len %u",
  771. action_field, (unsigned int) ie_len);
  772. return;
  773. }
  774. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  775. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  776. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  777. WPA_GET_LE16(ies));
  778. ies += 2; /* capability */
  779. ie_len -= 2;
  780. }
  781. if (action_field == PLINK_CONFIRM) {
  782. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  783. ies += 2; /* aid */
  784. ie_len -= 2;
  785. }
  786. /* check for mesh peering, mesh id and mesh config IEs */
  787. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  788. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  789. return;
  790. }
  791. if (!elems.peer_mgmt) {
  792. wpa_printf(MSG_DEBUG,
  793. "MPM: No Mesh Peering Management element");
  794. return;
  795. }
  796. if (action_field != PLINK_CLOSE) {
  797. if (!elems.mesh_id || !elems.mesh_config) {
  798. wpa_printf(MSG_DEBUG,
  799. "MPM: No Mesh ID or Mesh Configuration element");
  800. return;
  801. }
  802. if (!matches_local(wpa_s, &elems)) {
  803. wpa_printf(MSG_DEBUG,
  804. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  805. return;
  806. }
  807. }
  808. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  809. elems.peer_mgmt,
  810. elems.peer_mgmt_len,
  811. &peer_mgmt_ie);
  812. if (ret) {
  813. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  814. return;
  815. }
  816. /* the sender's llid is our plid and vice-versa */
  817. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  818. if (peer_mgmt_ie.plid)
  819. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  820. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  821. sta = ap_get_sta(hapd, mgmt->sa);
  822. /*
  823. * If this is an open frame from an unknown STA, and this is an
  824. * open mesh, then go ahead and add the peer before proceeding.
  825. */
  826. if (!sta && action_field == PLINK_OPEN &&
  827. !(mconf->security & MESH_CONF_SEC_AMPE))
  828. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  829. if (!sta) {
  830. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  831. return;
  832. }
  833. #ifdef CONFIG_SAE
  834. /* peer is in sae_accepted? */
  835. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  836. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  837. return;
  838. }
  839. #endif /* CONFIG_SAE */
  840. if (!sta->my_lid)
  841. mesh_mpm_init_link(wpa_s, sta);
  842. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  843. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  844. &mgmt->u.action.category,
  845. ies, ie_len)) {
  846. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  847. return;
  848. }
  849. if (sta->plink_state == PLINK_BLOCKED) {
  850. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  851. return;
  852. }
  853. /* Now we will figure out the appropriate event... */
  854. switch (action_field) {
  855. case PLINK_OPEN:
  856. if (plink_free_count(hapd) == 0) {
  857. event = OPN_IGNR;
  858. wpa_printf(MSG_INFO,
  859. "MPM: Peer link num over quota(%d)",
  860. hapd->max_plinks);
  861. } else if (sta->peer_lid && sta->peer_lid != plid) {
  862. event = OPN_IGNR;
  863. } else {
  864. sta->peer_lid = plid;
  865. event = OPN_ACPT;
  866. }
  867. break;
  868. case PLINK_CONFIRM:
  869. if (plink_free_count(hapd) == 0) {
  870. event = CNF_IGNR;
  871. wpa_printf(MSG_INFO,
  872. "MPM: Peer link num over quota(%d)",
  873. hapd->max_plinks);
  874. } else if (sta->my_lid != llid ||
  875. (sta->peer_lid && sta->peer_lid != plid)) {
  876. event = CNF_IGNR;
  877. } else {
  878. if (!sta->peer_lid)
  879. sta->peer_lid = plid;
  880. event = CNF_ACPT;
  881. }
  882. break;
  883. case PLINK_CLOSE:
  884. if (sta->plink_state == PLINK_ESTAB)
  885. /* Do not check for llid or plid. This does not
  886. * follow the standard but since multiple plinks
  887. * per cand are not supported, it is necessary in
  888. * order to avoid a livelock when MP A sees an
  889. * establish peer link to MP B but MP B does not
  890. * see it. This can be caused by a timeout in
  891. * B's peer link establishment or B being
  892. * restarted.
  893. */
  894. event = CLS_ACPT;
  895. else if (sta->peer_lid != plid)
  896. event = CLS_IGNR;
  897. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  898. event = CLS_IGNR;
  899. else
  900. event = CLS_ACPT;
  901. break;
  902. default:
  903. /*
  904. * This cannot be hit due to the action_field check above, but
  905. * compilers may not be able to figure that out and can warn
  906. * about uninitialized event below.
  907. */
  908. return;
  909. }
  910. mesh_mpm_fsm(wpa_s, sta, event);
  911. }
  912. /* called by ap_free_sta */
  913. void mesh_mpm_free_sta(struct sta_info *sta)
  914. {
  915. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  916. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  917. }