mesh.c 16 KB

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