mesh_mpm.c 28 KB

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