mesh.c 14 KB

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