mesh_mpm.c 26 KB

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