mesh_mpm.c 27 KB

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