mesh_mpm.c 28 KB

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