mesh.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * WPA Supplicant - Basic mesh mode routines
  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 "utils/uuid.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/wpa_ctrl.h"
  14. #include "ap/sta_info.h"
  15. #include "ap/hostapd.h"
  16. #include "ap/ieee802_11.h"
  17. #include "config_ssid.h"
  18. #include "config.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "notify.h"
  22. #include "ap.h"
  23. #include "mesh_mpm.h"
  24. #include "mesh_rsn.h"
  25. #include "mesh.h"
  26. static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s)
  27. {
  28. wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
  29. wpa_s->ifmsh = NULL;
  30. wpa_s->current_ssid = NULL;
  31. os_free(wpa_s->mesh_rsn);
  32. wpa_s->mesh_rsn = NULL;
  33. /* TODO: leave mesh (stop beacon). This will happen on link down
  34. * anyway, so it's not urgent */
  35. }
  36. void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
  37. struct hostapd_iface *ifmsh)
  38. {
  39. if (!ifmsh)
  40. return;
  41. if (ifmsh->mconf) {
  42. mesh_mpm_deinit(wpa_s, ifmsh);
  43. if (ifmsh->mconf->rsn_ie) {
  44. ifmsh->mconf->rsn_ie = NULL;
  45. /* We cannot free this struct
  46. * because wpa_authenticator on
  47. * hostapd side is also using it
  48. * for now just set to NULL and
  49. * let hostapd code free it.
  50. */
  51. }
  52. os_free(ifmsh->mconf);
  53. ifmsh->mconf = NULL;
  54. }
  55. /* take care of shared data */
  56. hostapd_interface_deinit(ifmsh);
  57. hostapd_interface_free(ifmsh);
  58. }
  59. static struct mesh_conf * mesh_config_create(struct wpa_supplicant *wpa_s,
  60. struct wpa_ssid *ssid)
  61. {
  62. struct mesh_conf *conf;
  63. int cipher;
  64. conf = os_zalloc(sizeof(struct mesh_conf));
  65. if (!conf)
  66. return NULL;
  67. os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
  68. conf->meshid_len = ssid->ssid_len;
  69. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
  70. conf->security |= MESH_CONF_SEC_AUTH |
  71. MESH_CONF_SEC_AMPE;
  72. else
  73. conf->security |= MESH_CONF_SEC_NONE;
  74. conf->ieee80211w = ssid->ieee80211w;
  75. if (conf->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
  76. if (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)
  77. conf->ieee80211w = wpa_s->conf->pmf;
  78. else
  79. conf->ieee80211w = NO_MGMT_FRAME_PROTECTION;
  80. }
  81. cipher = wpa_pick_pairwise_cipher(ssid->pairwise_cipher, 0);
  82. if (cipher < 0 || cipher == WPA_CIPHER_TKIP) {
  83. wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid pairwise cipher");
  84. os_free(conf);
  85. return NULL;
  86. }
  87. conf->pairwise_cipher = cipher;
  88. cipher = wpa_pick_group_cipher(ssid->group_cipher);
  89. if (cipher < 0 || cipher == WPA_CIPHER_TKIP ||
  90. cipher == WPA_CIPHER_GTK_NOT_USED) {
  91. wpa_msg(wpa_s, MSG_INFO, "mesh: Invalid group cipher");
  92. os_free(conf);
  93. return NULL;
  94. }
  95. conf->group_cipher = cipher;
  96. if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION)
  97. conf->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  98. /* defaults */
  99. conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
  100. conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
  101. conf->mesh_cc_id = 0;
  102. conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
  103. conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
  104. conf->dot11MeshMaxRetries = ssid->dot11MeshMaxRetries;
  105. conf->dot11MeshRetryTimeout = ssid->dot11MeshRetryTimeout;
  106. conf->dot11MeshConfirmTimeout = ssid->dot11MeshConfirmTimeout;
  107. conf->dot11MeshHoldingTimeout = ssid->dot11MeshHoldingTimeout;
  108. return conf;
  109. }
  110. static void wpas_mesh_copy_groups(struct hostapd_data *bss,
  111. struct wpa_supplicant *wpa_s)
  112. {
  113. int num_groups;
  114. size_t groups_size;
  115. for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
  116. num_groups++)
  117. ;
  118. groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
  119. bss->conf->sae_groups = os_malloc(groups_size);
  120. if (bss->conf->sae_groups)
  121. os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
  122. groups_size);
  123. }
  124. static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
  125. struct wpa_ssid *ssid)
  126. {
  127. struct hostapd_iface *ifmsh;
  128. struct hostapd_data *bss;
  129. struct hostapd_config *conf;
  130. struct mesh_conf *mconf;
  131. int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
  132. static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
  133. size_t len;
  134. int rate_len;
  135. if (!wpa_s->conf->user_mpm) {
  136. /* not much for us to do here */
  137. wpa_msg(wpa_s, MSG_WARNING,
  138. "user_mpm is not enabled in configuration");
  139. return 0;
  140. }
  141. wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
  142. if (!ifmsh)
  143. return -ENOMEM;
  144. ifmsh->drv_flags = wpa_s->drv_flags;
  145. ifmsh->num_bss = 1;
  146. ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
  147. sizeof(struct hostapd_data *));
  148. if (!ifmsh->bss)
  149. goto out_free;
  150. ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
  151. if (!bss)
  152. goto out_free;
  153. os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
  154. bss->driver = wpa_s->driver;
  155. bss->drv_priv = wpa_s->drv_priv;
  156. bss->iface = ifmsh;
  157. bss->mesh_sta_free_cb = mesh_mpm_free_sta;
  158. wpa_s->assoc_freq = ssid->frequency;
  159. wpa_s->current_ssid = ssid;
  160. /* setup an AP config for auth processing */
  161. conf = hostapd_config_defaults();
  162. if (!conf)
  163. goto out_free;
  164. bss->conf = *conf->bss;
  165. bss->conf->start_disabled = 1;
  166. bss->conf->mesh = MESH_ENABLED;
  167. bss->conf->ap_max_inactivity = wpa_s->conf->mesh_max_inactivity;
  168. bss->iconf = conf;
  169. ifmsh->conf = conf;
  170. ifmsh->bss[0]->max_plinks = wpa_s->conf->max_peer_links;
  171. ifmsh->bss[0]->dot11RSNASAERetransPeriod =
  172. wpa_s->conf->dot11RSNASAERetransPeriod;
  173. os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
  174. mconf = mesh_config_create(wpa_s, ssid);
  175. if (!mconf)
  176. goto out_free;
  177. ifmsh->mconf = mconf;
  178. /* need conf->hw_mode for supported rates. */
  179. conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency, &conf->channel);
  180. if (conf->hw_mode == NUM_HOSTAPD_MODES) {
  181. wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
  182. ssid->frequency);
  183. goto out_free;
  184. }
  185. if (ssid->ht40)
  186. conf->secondary_channel = ssid->ht40;
  187. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211A && ssid->vht) {
  188. conf->vht_oper_chwidth = ssid->max_oper_chwidth;
  189. switch (conf->vht_oper_chwidth) {
  190. case VHT_CHANWIDTH_80MHZ:
  191. case VHT_CHANWIDTH_80P80MHZ:
  192. ieee80211_freq_to_chan(
  193. ssid->frequency,
  194. &conf->vht_oper_centr_freq_seg0_idx);
  195. conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
  196. break;
  197. case VHT_CHANWIDTH_160MHZ:
  198. ieee80211_freq_to_chan(
  199. ssid->frequency,
  200. &conf->vht_oper_centr_freq_seg0_idx);
  201. conf->vht_oper_centr_freq_seg0_idx += ssid->ht40 * 2;
  202. conf->vht_oper_centr_freq_seg0_idx += 40 / 5;
  203. break;
  204. }
  205. ieee80211_freq_to_chan(ssid->vht_center_freq2,
  206. &conf->vht_oper_centr_freq_seg1_idx);
  207. }
  208. if (ssid->mesh_basic_rates == NULL) {
  209. /*
  210. * XXX: Hack! This is so an MPM which correctly sets the ERP
  211. * mandatory rates as BSSBasicRateSet doesn't reject us. We
  212. * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
  213. * this is way easier. This also makes our BSSBasicRateSet
  214. * advertised in beacons match the one in peering frames, sigh.
  215. */
  216. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  217. conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
  218. if (!conf->basic_rates)
  219. goto out_free;
  220. os_memcpy(conf->basic_rates, basic_rates_erp,
  221. sizeof(basic_rates_erp));
  222. }
  223. } else {
  224. rate_len = 0;
  225. while (1) {
  226. if (ssid->mesh_basic_rates[rate_len] < 1)
  227. break;
  228. rate_len++;
  229. }
  230. conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
  231. if (conf->basic_rates == NULL)
  232. goto out_free;
  233. os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
  234. rate_len * sizeof(int));
  235. conf->basic_rates[rate_len] = -1;
  236. }
  237. if (hostapd_setup_interface(ifmsh)) {
  238. wpa_printf(MSG_ERROR,
  239. "Failed to initialize hostapd interface for mesh");
  240. return -1;
  241. }
  242. if (wpa_drv_init_mesh(wpa_s)) {
  243. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
  244. return -1;
  245. }
  246. if (mconf->security != MESH_CONF_SEC_NONE) {
  247. if (ssid->passphrase == NULL) {
  248. wpa_printf(MSG_ERROR,
  249. "mesh: Passphrase for SAE not configured");
  250. goto out_free;
  251. }
  252. bss->conf->wpa = ssid->proto;
  253. bss->conf->wpa_key_mgmt = ssid->key_mgmt;
  254. if (wpa_s->conf->sae_groups &&
  255. wpa_s->conf->sae_groups[0] > 0) {
  256. wpas_mesh_copy_groups(bss, wpa_s);
  257. } else {
  258. bss->conf->sae_groups =
  259. os_malloc(sizeof(default_groups));
  260. if (!bss->conf->sae_groups)
  261. goto out_free;
  262. os_memcpy(bss->conf->sae_groups, default_groups,
  263. sizeof(default_groups));
  264. }
  265. len = os_strlen(ssid->passphrase);
  266. bss->conf->ssid.wpa_passphrase =
  267. dup_binstr(ssid->passphrase, len);
  268. wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
  269. if (!wpa_s->mesh_rsn)
  270. goto out_free;
  271. }
  272. wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
  273. return 0;
  274. out_free:
  275. wpa_supplicant_mesh_deinit(wpa_s);
  276. return -ENOMEM;
  277. }
  278. void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  279. const u8 *ies, size_t ie_len)
  280. {
  281. struct ieee802_11_elems elems;
  282. wpa_msg(wpa_s, MSG_INFO,
  283. "new peer notification for " MACSTR, MAC2STR(addr));
  284. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  285. wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
  286. MAC2STR(addr));
  287. return;
  288. }
  289. wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
  290. }
  291. void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
  292. struct wpabuf **extra_ie)
  293. {
  294. /* EID + 0-length (wildcard) mesh-id */
  295. size_t ielen = 2;
  296. if (wpabuf_resize(extra_ie, ielen) == 0) {
  297. wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
  298. wpabuf_put_u8(*extra_ie, 0);
  299. }
  300. }
  301. int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
  302. struct wpa_ssid *ssid)
  303. {
  304. struct wpa_driver_mesh_join_params params;
  305. int ret = 0;
  306. if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
  307. ret = -ENOENT;
  308. goto out;
  309. }
  310. wpa_supplicant_mesh_deinit(wpa_s);
  311. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  312. wpa_s->group_cipher = WPA_CIPHER_NONE;
  313. wpa_s->mgmt_group_cipher = 0;
  314. os_memset(&params, 0, sizeof(params));
  315. params.meshid = ssid->ssid;
  316. params.meshid_len = ssid->ssid_len;
  317. ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
  318. wpa_s->mesh_ht_enabled = !!params.freq.ht_enabled;
  319. wpa_s->mesh_vht_enabled = !!params.freq.vht_enabled;
  320. if (params.freq.ht_enabled && params.freq.sec_channel_offset)
  321. ssid->ht40 = params.freq.sec_channel_offset;
  322. if (wpa_s->mesh_vht_enabled) {
  323. ssid->vht = 1;
  324. switch (params.freq.bandwidth) {
  325. case 80:
  326. if (params.freq.center_freq2) {
  327. ssid->max_oper_chwidth = VHT_CHANWIDTH_80P80MHZ;
  328. ssid->vht_center_freq2 =
  329. params.freq.center_freq2;
  330. } else {
  331. ssid->max_oper_chwidth = VHT_CHANWIDTH_80MHZ;
  332. }
  333. break;
  334. case 160:
  335. ssid->max_oper_chwidth = VHT_CHANWIDTH_160MHZ;
  336. break;
  337. default:
  338. ssid->max_oper_chwidth = VHT_CHANWIDTH_USE_HT;
  339. break;
  340. }
  341. }
  342. if (ssid->beacon_int > 0)
  343. params.beacon_int = ssid->beacon_int;
  344. else if (wpa_s->conf->beacon_int > 0)
  345. params.beacon_int = wpa_s->conf->beacon_int;
  346. if (ssid->dtim_period > 0)
  347. params.dtim_period = ssid->dtim_period;
  348. else if (wpa_s->conf->dtim_period > 0)
  349. params.dtim_period = wpa_s->conf->dtim_period;
  350. params.conf.max_peer_links = wpa_s->conf->max_peer_links;
  351. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
  352. params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
  353. params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
  354. wpa_s->conf->user_mpm = 1;
  355. }
  356. if (wpa_s->conf->user_mpm) {
  357. params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
  358. params.conf.auto_plinks = 0;
  359. } else {
  360. params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
  361. params.conf.auto_plinks = 1;
  362. }
  363. params.conf.peer_link_timeout = wpa_s->conf->mesh_max_inactivity;
  364. if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
  365. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
  366. wpa_drv_leave_mesh(wpa_s);
  367. ret = -1;
  368. goto out;
  369. }
  370. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
  371. wpa_s->pairwise_cipher = wpa_s->mesh_rsn->pairwise_cipher;
  372. wpa_s->group_cipher = wpa_s->mesh_rsn->group_cipher;
  373. wpa_s->mgmt_group_cipher = wpa_s->mesh_rsn->mgmt_group_cipher;
  374. }
  375. if (wpa_s->ifmsh) {
  376. params.ies = wpa_s->ifmsh->mconf->rsn_ie;
  377. params.ie_len = wpa_s->ifmsh->mconf->rsn_ie_len;
  378. params.basic_rates = wpa_s->ifmsh->basic_rates;
  379. params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
  380. params.conf.ht_opmode = wpa_s->ifmsh->bss[0]->iface->ht_op_mode;
  381. }
  382. wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
  383. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  384. ret = wpa_drv_join_mesh(wpa_s, &params);
  385. if (ret)
  386. wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d", ret);
  387. /* hostapd sets the interface down until we associate */
  388. wpa_drv_set_operstate(wpa_s, 1);
  389. if (!ret)
  390. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  391. out:
  392. return ret;
  393. }
  394. int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
  395. {
  396. int ret = 0;
  397. wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
  398. /* Need to send peering close messages first */
  399. wpa_supplicant_mesh_deinit(wpa_s);
  400. ret = wpa_drv_leave_mesh(wpa_s);
  401. if (ret)
  402. wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
  403. wpa_drv_set_operstate(wpa_s, 1);
  404. return ret;
  405. }
  406. static int mesh_attr_text(const u8 *ies, size_t ies_len, char *buf, char *end)
  407. {
  408. struct ieee802_11_elems elems;
  409. char *mesh_id, *pos = buf;
  410. u8 *bss_basic_rate_set;
  411. int bss_basic_rate_set_len, ret, i;
  412. if (ieee802_11_parse_elems(ies, ies_len, &elems, 0) == ParseFailed)
  413. return -1;
  414. if (elems.mesh_id_len < 1)
  415. return 0;
  416. mesh_id = os_malloc(elems.mesh_id_len + 1);
  417. if (mesh_id == NULL)
  418. return -1;
  419. os_memcpy(mesh_id, elems.mesh_id, elems.mesh_id_len);
  420. mesh_id[elems.mesh_id_len] = '\0';
  421. ret = os_snprintf(pos, end - pos, "mesh_id=%s\n", mesh_id);
  422. os_free(mesh_id);
  423. if (os_snprintf_error(end - pos, ret))
  424. return pos - buf;
  425. pos += ret;
  426. if (elems.mesh_config_len > 6) {
  427. ret = os_snprintf(pos, end - pos,
  428. "active_path_selection_protocol_id=0x%02x\n"
  429. "active_path_selection_metric_id=0x%02x\n"
  430. "congestion_control_mode_id=0x%02x\n"
  431. "synchronization_method_id=0x%02x\n"
  432. "authentication_protocol_id=0x%02x\n"
  433. "mesh_formation_info=0x%02x\n"
  434. "mesh_capability=0x%02x\n",
  435. elems.mesh_config[0], elems.mesh_config[1],
  436. elems.mesh_config[2], elems.mesh_config[3],
  437. elems.mesh_config[4], elems.mesh_config[5],
  438. elems.mesh_config[6]);
  439. if (os_snprintf_error(end - pos, ret))
  440. return pos - buf;
  441. pos += ret;
  442. }
  443. bss_basic_rate_set = os_malloc(elems.supp_rates_len +
  444. elems.ext_supp_rates_len);
  445. if (bss_basic_rate_set == NULL)
  446. return -1;
  447. bss_basic_rate_set_len = 0;
  448. for (i = 0; i < elems.supp_rates_len; i++) {
  449. if (elems.supp_rates[i] & 0x80) {
  450. bss_basic_rate_set[bss_basic_rate_set_len++] =
  451. (elems.supp_rates[i] & 0x7f) * 5;
  452. }
  453. }
  454. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  455. if (elems.ext_supp_rates[i] & 0x80) {
  456. bss_basic_rate_set[bss_basic_rate_set_len++] =
  457. (elems.ext_supp_rates[i] & 0x7f) * 5;
  458. }
  459. }
  460. if (bss_basic_rate_set_len > 0) {
  461. ret = os_snprintf(pos, end - pos, "bss_basic_rate_set=%d",
  462. bss_basic_rate_set[0]);
  463. if (os_snprintf_error(end - pos, ret))
  464. goto fail;
  465. pos += ret;
  466. for (i = 1; i < bss_basic_rate_set_len; i++) {
  467. ret = os_snprintf(pos, end - pos, " %d",
  468. bss_basic_rate_set[i]);
  469. if (os_snprintf_error(end - pos, ret))
  470. goto fail;
  471. pos += ret;
  472. }
  473. ret = os_snprintf(pos, end - pos, "\n");
  474. if (os_snprintf_error(end - pos, ret))
  475. goto fail;
  476. pos += ret;
  477. }
  478. fail:
  479. os_free(bss_basic_rate_set);
  480. return pos - buf;
  481. }
  482. int wpas_mesh_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  483. char *end)
  484. {
  485. return mesh_attr_text(ies, ies_len, buf, end);
  486. }
  487. static int wpas_mesh_get_ifname(struct wpa_supplicant *wpa_s, char *ifname,
  488. size_t len)
  489. {
  490. char *ifname_ptr = wpa_s->ifname;
  491. int res;
  492. res = os_snprintf(ifname, len, "mesh-%s-%d", ifname_ptr,
  493. wpa_s->mesh_if_idx);
  494. if (os_snprintf_error(len, res) ||
  495. (os_strlen(ifname) >= IFNAMSIZ &&
  496. os_strlen(wpa_s->ifname) < IFNAMSIZ)) {
  497. /* Try to avoid going over the IFNAMSIZ length limit */
  498. res = os_snprintf(ifname, len, "mesh-%d", wpa_s->mesh_if_idx);
  499. if (os_snprintf_error(len, res))
  500. return -1;
  501. }
  502. wpa_s->mesh_if_idx++;
  503. return 0;
  504. }
  505. int wpas_mesh_add_interface(struct wpa_supplicant *wpa_s, char *ifname,
  506. size_t len)
  507. {
  508. struct wpa_interface iface;
  509. struct wpa_supplicant *mesh_wpa_s;
  510. u8 addr[ETH_ALEN];
  511. if (ifname[0] == '\0' && wpas_mesh_get_ifname(wpa_s, ifname, len) < 0)
  512. return -1;
  513. if (wpa_drv_if_add(wpa_s, WPA_IF_MESH, ifname, NULL, NULL, NULL, addr,
  514. NULL) < 0) {
  515. wpa_printf(MSG_ERROR,
  516. "mesh: Failed to create new mesh interface");
  517. return -1;
  518. }
  519. wpa_printf(MSG_INFO, "mesh: Created virtual interface %s addr "
  520. MACSTR, ifname, MAC2STR(addr));
  521. os_memset(&iface, 0, sizeof(iface));
  522. iface.ifname = ifname;
  523. iface.driver = wpa_s->driver->name;
  524. iface.driver_param = wpa_s->conf->driver_param;
  525. iface.ctrl_interface = wpa_s->conf->ctrl_interface;
  526. mesh_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
  527. if (!mesh_wpa_s) {
  528. wpa_printf(MSG_ERROR,
  529. "mesh: Failed to create new wpa_supplicant interface");
  530. wpa_drv_if_remove(wpa_s, WPA_IF_MESH, ifname);
  531. return -1;
  532. }
  533. mesh_wpa_s->mesh_if_created = 1;
  534. return 0;
  535. }
  536. int wpas_mesh_peer_remove(struct wpa_supplicant *wpa_s, const u8 *addr)
  537. {
  538. return mesh_mpm_close_peer(wpa_s, addr);
  539. }
  540. int wpas_mesh_peer_add(struct wpa_supplicant *wpa_s, const u8 *addr,
  541. int duration)
  542. {
  543. return mesh_mpm_connect_peer(wpa_s, addr, duration);
  544. }