mesh_mpm.c 27 KB

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