mesh_mpm.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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->aid);
  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. /* Set WMM by default since Mesh STAs are QoS STAs */
  461. sta->flags |= WLAN_STA_WMM;
  462. /* initialize sta */
  463. if (copy_supp_rates(wpa_s, sta, elems)) {
  464. ap_free_sta(data, sta);
  465. return NULL;
  466. }
  467. mesh_mpm_init_link(wpa_s, sta);
  468. #ifdef CONFIG_IEEE80211N
  469. copy_sta_ht_capab(data, sta, elems->ht_capabilities);
  470. update_ht_state(data, sta);
  471. #endif /* CONFIG_IEEE80211N */
  472. if (hostapd_get_aid(data, sta) < 0) {
  473. wpa_msg(wpa_s, MSG_ERROR, "No AIDs available");
  474. ap_free_sta(data, sta);
  475. return NULL;
  476. }
  477. /* insert into driver */
  478. os_memset(&params, 0, sizeof(params));
  479. params.supp_rates = sta->supported_rates;
  480. params.supp_rates_len = sta->supported_rates_len;
  481. params.addr = addr;
  482. params.plink_state = sta->plink_state;
  483. params.aid = sta->aid;
  484. params.listen_interval = 100;
  485. params.ht_capabilities = sta->ht_capabilities;
  486. params.flags |= WPA_STA_WMM;
  487. params.flags_mask |= WPA_STA_AUTHENTICATED;
  488. if (conf->security == MESH_CONF_SEC_NONE) {
  489. params.flags |= WPA_STA_AUTHORIZED;
  490. params.flags |= WPA_STA_AUTHENTICATED;
  491. } else {
  492. sta->flags |= WLAN_STA_MFP;
  493. params.flags |= WPA_STA_MFP;
  494. }
  495. ret = wpa_drv_sta_add(wpa_s, &params);
  496. if (ret) {
  497. wpa_msg(wpa_s, MSG_ERROR,
  498. "Driver failed to insert " MACSTR ": %d",
  499. MAC2STR(addr), ret);
  500. ap_free_sta(data, sta);
  501. return NULL;
  502. }
  503. return sta;
  504. }
  505. void wpa_mesh_new_mesh_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  506. struct ieee802_11_elems *elems)
  507. {
  508. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  509. struct hostapd_data *data = wpa_s->ifmsh->bss[0];
  510. struct sta_info *sta;
  511. struct wpa_ssid *ssid = wpa_s->current_ssid;
  512. sta = mesh_mpm_add_peer(wpa_s, addr, elems);
  513. if (!sta)
  514. return;
  515. if (ssid && ssid->no_auto_peer) {
  516. wpa_msg(wpa_s, MSG_INFO, "will not initiate new peer link with "
  517. MACSTR " because of no_auto_peer", MAC2STR(addr));
  518. if (data->mesh_pending_auth) {
  519. struct os_reltime age;
  520. const struct ieee80211_mgmt *mgmt;
  521. struct hostapd_frame_info fi;
  522. mgmt = wpabuf_head(data->mesh_pending_auth);
  523. os_reltime_age(&data->mesh_pending_auth_time, &age);
  524. if (age.sec < 2 &&
  525. os_memcmp(mgmt->sa, addr, ETH_ALEN) == 0) {
  526. wpa_printf(MSG_DEBUG,
  527. "mesh: Process pending Authentication frame from %u.%06u seconds ago",
  528. (unsigned int) age.sec,
  529. (unsigned int) age.usec);
  530. os_memset(&fi, 0, sizeof(fi));
  531. ieee802_11_mgmt(
  532. data,
  533. wpabuf_head(data->mesh_pending_auth),
  534. wpabuf_len(data->mesh_pending_auth),
  535. &fi);
  536. }
  537. wpabuf_free(data->mesh_pending_auth);
  538. data->mesh_pending_auth = NULL;
  539. }
  540. return;
  541. }
  542. if (conf->security == MESH_CONF_SEC_NONE)
  543. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_SENT);
  544. else
  545. mesh_rsn_auth_sae_sta(wpa_s, sta);
  546. }
  547. void mesh_mpm_mgmt_rx(struct wpa_supplicant *wpa_s, struct rx_mgmt *rx_mgmt)
  548. {
  549. struct hostapd_frame_info fi;
  550. os_memset(&fi, 0, sizeof(fi));
  551. fi.datarate = rx_mgmt->datarate;
  552. fi.ssi_signal = rx_mgmt->ssi_signal;
  553. ieee802_11_mgmt(wpa_s->ifmsh->bss[0], rx_mgmt->frame,
  554. rx_mgmt->frame_len, &fi);
  555. }
  556. static void mesh_mpm_plink_estab(struct wpa_supplicant *wpa_s,
  557. struct sta_info *sta)
  558. {
  559. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  560. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  561. u8 seq[6] = {};
  562. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR " established",
  563. MAC2STR(sta->addr));
  564. if (conf->security & MESH_CONF_SEC_AMPE) {
  565. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 0, 0,
  566. seq, sizeof(seq), sta->mtk, sizeof(sta->mtk));
  567. wpa_drv_set_key(wpa_s, WPA_ALG_CCMP, sta->addr, 1, 0,
  568. seq, sizeof(seq),
  569. sta->mgtk, sizeof(sta->mgtk));
  570. wpa_drv_set_key(wpa_s, WPA_ALG_IGTK, sta->addr, 4, 0,
  571. seq, sizeof(seq),
  572. sta->mgtk, sizeof(sta->mgtk));
  573. wpa_hexdump_key(MSG_DEBUG, "mtk:", sta->mtk, sizeof(sta->mtk));
  574. wpa_hexdump_key(MSG_DEBUG, "mgtk:",
  575. sta->mgtk, sizeof(sta->mgtk));
  576. }
  577. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_ESTAB);
  578. hapd->num_plinks++;
  579. sta->flags |= WLAN_STA_ASSOC;
  580. eloop_cancel_timeout(plink_timer, wpa_s, sta);
  581. /* Send ctrl event */
  582. wpa_msg_ctrl(wpa_s, MSG_INFO, MESH_PEER_CONNECTED MACSTR,
  583. MAC2STR(sta->addr));
  584. }
  585. static void mesh_mpm_fsm(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  586. enum plink_event event)
  587. {
  588. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  589. struct mesh_conf *conf = wpa_s->ifmsh->mconf;
  590. u16 reason = 0;
  591. wpa_msg(wpa_s, MSG_DEBUG, "MPM " MACSTR " state %s event %s",
  592. MAC2STR(sta->addr), mplstate[sta->plink_state],
  593. mplevent[event]);
  594. switch (sta->plink_state) {
  595. case PLINK_LISTEN:
  596. switch (event) {
  597. case CLS_ACPT:
  598. mesh_mpm_fsm_restart(wpa_s, sta);
  599. break;
  600. case OPN_ACPT:
  601. mesh_mpm_plink_open(wpa_s, sta, PLINK_OPEN_RCVD);
  602. mesh_mpm_send_plink_action(wpa_s, sta, PLINK_CONFIRM,
  603. 0);
  604. break;
  605. default:
  606. break;
  607. }
  608. break;
  609. case PLINK_OPEN_SENT:
  610. switch (event) {
  611. case OPN_RJCT:
  612. case CNF_RJCT:
  613. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  614. /* fall-through */
  615. case CLS_ACPT:
  616. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  617. if (!reason)
  618. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  619. eloop_register_timeout(
  620. conf->dot11MeshHoldingTimeout / 1000,
  621. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  622. plink_timer, wpa_s, sta);
  623. mesh_mpm_send_plink_action(wpa_s, sta,
  624. PLINK_CLOSE, reason);
  625. break;
  626. case OPN_ACPT:
  627. /* retry timer is left untouched */
  628. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_OPEN_RCVD);
  629. mesh_mpm_send_plink_action(wpa_s, sta,
  630. PLINK_CONFIRM, 0);
  631. break;
  632. case CNF_ACPT:
  633. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_CNF_RCVD);
  634. eloop_register_timeout(
  635. conf->dot11MeshConfirmTimeout / 1000,
  636. (conf->dot11MeshConfirmTimeout % 1000) * 1000,
  637. plink_timer, wpa_s, sta);
  638. break;
  639. default:
  640. break;
  641. }
  642. break;
  643. case PLINK_OPEN_RCVD:
  644. switch (event) {
  645. case OPN_RJCT:
  646. case CNF_RJCT:
  647. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  648. /* fall-through */
  649. case CLS_ACPT:
  650. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  651. if (!reason)
  652. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  653. eloop_register_timeout(
  654. conf->dot11MeshHoldingTimeout / 1000,
  655. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  656. plink_timer, wpa_s, sta);
  657. sta->mpm_close_reason = reason;
  658. mesh_mpm_send_plink_action(wpa_s, sta,
  659. PLINK_CLOSE, reason);
  660. break;
  661. case OPN_ACPT:
  662. mesh_mpm_send_plink_action(wpa_s, sta,
  663. PLINK_CONFIRM, 0);
  664. break;
  665. case CNF_ACPT:
  666. if (conf->security & MESH_CONF_SEC_AMPE)
  667. mesh_rsn_derive_mtk(wpa_s, sta);
  668. mesh_mpm_plink_estab(wpa_s, sta);
  669. break;
  670. default:
  671. break;
  672. }
  673. break;
  674. case PLINK_CNF_RCVD:
  675. switch (event) {
  676. case OPN_RJCT:
  677. case CNF_RJCT:
  678. reason = WLAN_REASON_MESH_CONFIG_POLICY_VIOLATION;
  679. /* fall-through */
  680. case CLS_ACPT:
  681. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  682. if (!reason)
  683. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  684. eloop_register_timeout(
  685. conf->dot11MeshHoldingTimeout / 1000,
  686. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  687. plink_timer, wpa_s, sta);
  688. sta->mpm_close_reason = reason;
  689. mesh_mpm_send_plink_action(wpa_s, sta,
  690. PLINK_CLOSE, reason);
  691. break;
  692. case OPN_ACPT:
  693. mesh_mpm_plink_estab(wpa_s, sta);
  694. mesh_mpm_send_plink_action(wpa_s, sta,
  695. PLINK_CONFIRM, 0);
  696. break;
  697. default:
  698. break;
  699. }
  700. break;
  701. case PLINK_ESTAB:
  702. switch (event) {
  703. case CLS_ACPT:
  704. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_HOLDING);
  705. reason = WLAN_REASON_MESH_CLOSE_RCVD;
  706. eloop_register_timeout(
  707. conf->dot11MeshHoldingTimeout / 1000,
  708. (conf->dot11MeshHoldingTimeout % 1000) * 1000,
  709. plink_timer, wpa_s, sta);
  710. sta->mpm_close_reason = reason;
  711. wpa_msg(wpa_s, MSG_INFO, "mesh plink with " MACSTR
  712. " closed with reason %d",
  713. MAC2STR(sta->addr), reason);
  714. wpa_msg_ctrl(wpa_s, MSG_INFO,
  715. MESH_PEER_DISCONNECTED MACSTR,
  716. MAC2STR(sta->addr));
  717. hapd->num_plinks--;
  718. mesh_mpm_send_plink_action(wpa_s, sta,
  719. PLINK_CLOSE, reason);
  720. break;
  721. case OPN_ACPT:
  722. mesh_mpm_send_plink_action(wpa_s, sta,
  723. PLINK_CONFIRM, 0);
  724. break;
  725. default:
  726. break;
  727. }
  728. break;
  729. case PLINK_HOLDING:
  730. switch (event) {
  731. case CLS_ACPT:
  732. mesh_mpm_fsm_restart(wpa_s, sta);
  733. break;
  734. case OPN_ACPT:
  735. case CNF_ACPT:
  736. case OPN_RJCT:
  737. case CNF_RJCT:
  738. reason = sta->mpm_close_reason;
  739. mesh_mpm_send_plink_action(wpa_s, sta,
  740. PLINK_CLOSE, reason);
  741. break;
  742. default:
  743. break;
  744. }
  745. break;
  746. default:
  747. wpa_msg(wpa_s, MSG_DEBUG,
  748. "Unsupported MPM event %s for state %s",
  749. mplevent[event], mplstate[sta->plink_state]);
  750. break;
  751. }
  752. }
  753. void mesh_mpm_action_rx(struct wpa_supplicant *wpa_s,
  754. const struct ieee80211_mgmt *mgmt, size_t len)
  755. {
  756. u8 action_field;
  757. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  758. struct mesh_conf *mconf = wpa_s->ifmsh->mconf;
  759. struct sta_info *sta;
  760. u16 plid = 0, llid = 0;
  761. enum plink_event event;
  762. struct ieee802_11_elems elems;
  763. struct mesh_peer_mgmt_ie peer_mgmt_ie;
  764. const u8 *ies;
  765. size_t ie_len;
  766. int ret;
  767. if (mgmt->u.action.category != WLAN_ACTION_SELF_PROTECTED)
  768. return;
  769. action_field = mgmt->u.action.u.slf_prot_action.action;
  770. if (action_field != PLINK_OPEN &&
  771. action_field != PLINK_CONFIRM &&
  772. action_field != PLINK_CLOSE)
  773. return;
  774. ies = mgmt->u.action.u.slf_prot_action.variable;
  775. ie_len = (const u8 *) mgmt + len -
  776. mgmt->u.action.u.slf_prot_action.variable;
  777. /* at least expect mesh id and peering mgmt */
  778. if (ie_len < 2 + 2) {
  779. wpa_printf(MSG_DEBUG,
  780. "MPM: Ignore too short action frame %u ie_len %u",
  781. action_field, (unsigned int) ie_len);
  782. return;
  783. }
  784. wpa_printf(MSG_DEBUG, "MPM: Received PLINK action %u", action_field);
  785. if (action_field == PLINK_OPEN || action_field == PLINK_CONFIRM) {
  786. wpa_printf(MSG_DEBUG, "MPM: Capability 0x%x",
  787. WPA_GET_LE16(ies));
  788. ies += 2; /* capability */
  789. ie_len -= 2;
  790. }
  791. if (action_field == PLINK_CONFIRM) {
  792. wpa_printf(MSG_DEBUG, "MPM: AID 0x%x", WPA_GET_LE16(ies));
  793. ies += 2; /* aid */
  794. ie_len -= 2;
  795. }
  796. /* check for mesh peering, mesh id and mesh config IEs */
  797. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  798. wpa_printf(MSG_DEBUG, "MPM: Failed to parse PLINK IEs");
  799. return;
  800. }
  801. if (!elems.peer_mgmt) {
  802. wpa_printf(MSG_DEBUG,
  803. "MPM: No Mesh Peering Management element");
  804. return;
  805. }
  806. if (action_field != PLINK_CLOSE) {
  807. if (!elems.mesh_id || !elems.mesh_config) {
  808. wpa_printf(MSG_DEBUG,
  809. "MPM: No Mesh ID or Mesh Configuration element");
  810. return;
  811. }
  812. if (!matches_local(wpa_s, &elems)) {
  813. wpa_printf(MSG_DEBUG,
  814. "MPM: Mesh ID or Mesh Configuration element do not match local MBSS");
  815. return;
  816. }
  817. }
  818. ret = mesh_mpm_parse_peer_mgmt(wpa_s, action_field,
  819. elems.peer_mgmt,
  820. elems.peer_mgmt_len,
  821. &peer_mgmt_ie);
  822. if (ret) {
  823. wpa_printf(MSG_DEBUG, "MPM: Mesh parsing rejected frame");
  824. return;
  825. }
  826. /* the sender's llid is our plid and vice-versa */
  827. plid = WPA_GET_LE16(peer_mgmt_ie.llid);
  828. if (peer_mgmt_ie.plid)
  829. llid = WPA_GET_LE16(peer_mgmt_ie.plid);
  830. wpa_printf(MSG_DEBUG, "MPM: plid=0x%x llid=0x%x", plid, llid);
  831. sta = ap_get_sta(hapd, mgmt->sa);
  832. /*
  833. * If this is an open frame from an unknown STA, and this is an
  834. * open mesh, then go ahead and add the peer before proceeding.
  835. */
  836. if (!sta && action_field == PLINK_OPEN &&
  837. !(mconf->security & MESH_CONF_SEC_AMPE))
  838. sta = mesh_mpm_add_peer(wpa_s, mgmt->sa, &elems);
  839. if (!sta) {
  840. wpa_printf(MSG_DEBUG, "MPM: No STA entry for peer");
  841. return;
  842. }
  843. #ifdef CONFIG_SAE
  844. /* peer is in sae_accepted? */
  845. if (sta->sae && sta->sae->state != SAE_ACCEPTED) {
  846. wpa_printf(MSG_DEBUG, "MPM: SAE not yet accepted for peer");
  847. return;
  848. }
  849. #endif /* CONFIG_SAE */
  850. if (!sta->my_lid)
  851. mesh_mpm_init_link(wpa_s, sta);
  852. if ((mconf->security & MESH_CONF_SEC_AMPE) &&
  853. mesh_rsn_process_ampe(wpa_s, sta, &elems,
  854. &mgmt->u.action.category,
  855. ies, ie_len)) {
  856. wpa_printf(MSG_DEBUG, "MPM: RSN process rejected frame");
  857. return;
  858. }
  859. if (sta->plink_state == PLINK_BLOCKED) {
  860. wpa_printf(MSG_DEBUG, "MPM: PLINK_BLOCKED");
  861. return;
  862. }
  863. /* Now we will figure out the appropriate event... */
  864. switch (action_field) {
  865. case PLINK_OPEN:
  866. if (plink_free_count(hapd) == 0) {
  867. event = OPN_IGNR;
  868. wpa_printf(MSG_INFO,
  869. "MPM: Peer link num over quota(%d)",
  870. hapd->max_plinks);
  871. } else if (sta->peer_lid && sta->peer_lid != plid) {
  872. event = OPN_IGNR;
  873. } else {
  874. sta->peer_lid = plid;
  875. event = OPN_ACPT;
  876. }
  877. break;
  878. case PLINK_CONFIRM:
  879. if (plink_free_count(hapd) == 0) {
  880. event = CNF_IGNR;
  881. wpa_printf(MSG_INFO,
  882. "MPM: Peer link num over quota(%d)",
  883. hapd->max_plinks);
  884. } else if (sta->my_lid != llid ||
  885. (sta->peer_lid && sta->peer_lid != plid)) {
  886. event = CNF_IGNR;
  887. } else {
  888. if (!sta->peer_lid)
  889. sta->peer_lid = plid;
  890. event = CNF_ACPT;
  891. }
  892. break;
  893. case PLINK_CLOSE:
  894. if (sta->plink_state == PLINK_ESTAB)
  895. /* Do not check for llid or plid. This does not
  896. * follow the standard but since multiple plinks
  897. * per cand are not supported, it is necessary in
  898. * order to avoid a livelock when MP A sees an
  899. * establish peer link to MP B but MP B does not
  900. * see it. This can be caused by a timeout in
  901. * B's peer link establishment or B being
  902. * restarted.
  903. */
  904. event = CLS_ACPT;
  905. else if (sta->peer_lid != plid)
  906. event = CLS_IGNR;
  907. else if (peer_mgmt_ie.plid && sta->my_lid != llid)
  908. event = CLS_IGNR;
  909. else
  910. event = CLS_ACPT;
  911. break;
  912. default:
  913. /*
  914. * This cannot be hit due to the action_field check above, but
  915. * compilers may not be able to figure that out and can warn
  916. * about uninitialized event below.
  917. */
  918. return;
  919. }
  920. mesh_mpm_fsm(wpa_s, sta, event);
  921. }
  922. /* called by ap_free_sta */
  923. void mesh_mpm_free_sta(struct sta_info *sta)
  924. {
  925. eloop_cancel_timeout(plink_timer, ELOOP_ALL_CTX, sta);
  926. eloop_cancel_timeout(mesh_auth_timer, ELOOP_ALL_CTX, sta);
  927. }