mesh.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. return conf;
  79. }
  80. static void wpas_mesh_copy_groups(struct hostapd_data *bss,
  81. struct wpa_supplicant *wpa_s)
  82. {
  83. int num_groups;
  84. size_t groups_size;
  85. for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
  86. num_groups++)
  87. ;
  88. groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
  89. bss->conf->sae_groups = os_malloc(groups_size);
  90. if (bss->conf->sae_groups)
  91. os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
  92. groups_size);
  93. }
  94. static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
  95. struct wpa_ssid *ssid)
  96. {
  97. struct hostapd_iface *ifmsh;
  98. struct hostapd_data *bss;
  99. struct hostapd_config *conf;
  100. struct mesh_conf *mconf;
  101. int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
  102. static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
  103. size_t len;
  104. int rate_len;
  105. if (!wpa_s->conf->user_mpm) {
  106. /* not much for us to do here */
  107. wpa_msg(wpa_s, MSG_WARNING,
  108. "user_mpm is not enabled in configuration");
  109. return 0;
  110. }
  111. wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
  112. if (!ifmsh)
  113. return -ENOMEM;
  114. ifmsh->drv_flags = wpa_s->drv_flags;
  115. ifmsh->num_bss = 1;
  116. ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
  117. sizeof(struct hostapd_data *));
  118. if (!ifmsh->bss)
  119. goto out_free;
  120. ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
  121. if (!bss)
  122. goto out_free;
  123. os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
  124. bss->driver = wpa_s->driver;
  125. bss->drv_priv = wpa_s->drv_priv;
  126. bss->iface = ifmsh;
  127. bss->mesh_sta_free_cb = mesh_mpm_free_sta;
  128. wpa_s->assoc_freq = ssid->frequency;
  129. wpa_s->current_ssid = ssid;
  130. /* setup an AP config for auth processing */
  131. conf = hostapd_config_defaults();
  132. if (!conf)
  133. goto out_free;
  134. bss->conf = *conf->bss;
  135. bss->conf->start_disabled = 1;
  136. bss->conf->mesh = MESH_ENABLED;
  137. bss->iconf = conf;
  138. ifmsh->conf = conf;
  139. ifmsh->bss[0]->max_plinks = 99;
  140. os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
  141. mconf = mesh_config_create(ssid);
  142. if (!mconf)
  143. goto out_free;
  144. ifmsh->mconf = mconf;
  145. /* need conf->hw_mode for supported rates. */
  146. if (ssid->frequency == 0) {
  147. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  148. conf->channel = 1;
  149. } else {
  150. conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
  151. &conf->channel);
  152. }
  153. if (conf->hw_mode == NUM_HOSTAPD_MODES) {
  154. wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
  155. ssid->frequency);
  156. goto out_free;
  157. }
  158. if (ssid->mesh_basic_rates == NULL) {
  159. /*
  160. * XXX: Hack! This is so an MPM which correctly sets the ERP
  161. * mandatory rates as BSSBasicRateSet doesn't reject us. We
  162. * could add a new hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but
  163. * this is way easier. This also makes our BSSBasicRateSet
  164. * advertised in beacons match the one in peering frames, sigh.
  165. */
  166. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  167. conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
  168. if (!conf->basic_rates)
  169. goto out_free;
  170. os_memcpy(conf->basic_rates, basic_rates_erp,
  171. sizeof(basic_rates_erp));
  172. }
  173. } else {
  174. rate_len = 0;
  175. while (1) {
  176. if (ssid->mesh_basic_rates[rate_len] < 1)
  177. break;
  178. rate_len++;
  179. }
  180. conf->basic_rates = os_calloc(rate_len + 1, sizeof(int));
  181. if (conf->basic_rates == NULL)
  182. goto out_free;
  183. os_memcpy(conf->basic_rates, ssid->mesh_basic_rates,
  184. rate_len * sizeof(int));
  185. conf->basic_rates[rate_len] = -1;
  186. }
  187. if (hostapd_setup_interface(ifmsh)) {
  188. wpa_printf(MSG_ERROR,
  189. "Failed to initialize hostapd interface for mesh");
  190. return -1;
  191. }
  192. if (wpa_drv_init_mesh(wpa_s)) {
  193. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
  194. return -1;
  195. }
  196. if (mconf->security != MESH_CONF_SEC_NONE) {
  197. if (ssid->passphrase == NULL) {
  198. wpa_printf(MSG_ERROR,
  199. "mesh: Passphrase for SAE not configured");
  200. goto out_free;
  201. }
  202. bss->conf->wpa = ssid->proto;
  203. bss->conf->wpa_key_mgmt = ssid->key_mgmt;
  204. if (wpa_s->conf->sae_groups &&
  205. wpa_s->conf->sae_groups[0] > 0) {
  206. wpas_mesh_copy_groups(bss, wpa_s);
  207. } else {
  208. bss->conf->sae_groups =
  209. os_malloc(sizeof(default_groups));
  210. if (!bss->conf->sae_groups)
  211. goto out_free;
  212. os_memcpy(bss->conf->sae_groups, default_groups,
  213. sizeof(default_groups));
  214. }
  215. len = os_strlen(ssid->passphrase);
  216. bss->conf->ssid.wpa_passphrase =
  217. dup_binstr(ssid->passphrase, len);
  218. wpa_s->mesh_rsn = mesh_rsn_auth_init(wpa_s, mconf);
  219. if (!wpa_s->mesh_rsn)
  220. goto out_free;
  221. }
  222. wpa_supplicant_conf_ap_ht(wpa_s, ssid, conf);
  223. return 0;
  224. out_free:
  225. wpa_supplicant_mesh_deinit(wpa_s);
  226. return -ENOMEM;
  227. }
  228. void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  229. const u8 *ies, size_t ie_len)
  230. {
  231. struct ieee802_11_elems elems;
  232. wpa_msg(wpa_s, MSG_INFO,
  233. "new peer notification for " MACSTR, MAC2STR(addr));
  234. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  235. wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
  236. MAC2STR(addr));
  237. return;
  238. }
  239. wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
  240. }
  241. void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
  242. struct wpabuf **extra_ie)
  243. {
  244. /* EID + 0-length (wildcard) mesh-id */
  245. size_t ielen = 2;
  246. if (wpabuf_resize(extra_ie, ielen) == 0) {
  247. wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
  248. wpabuf_put_u8(*extra_ie, 0);
  249. }
  250. }
  251. int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
  252. struct wpa_ssid *ssid)
  253. {
  254. struct wpa_driver_mesh_join_params params;
  255. int ret = 0;
  256. if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
  257. ret = -ENOENT;
  258. goto out;
  259. }
  260. wpa_supplicant_mesh_deinit(wpa_s);
  261. os_memset(&params, 0, sizeof(params));
  262. params.meshid = ssid->ssid;
  263. params.meshid_len = ssid->ssid_len;
  264. params.freq = ssid->frequency;
  265. #ifdef CONFIG_IEEE80211N
  266. params.ht_mode = ssid->mesh_ht_mode;
  267. #endif /* CONFIG_IEEE80211N */
  268. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
  269. params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
  270. params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
  271. wpa_s->conf->user_mpm = 1;
  272. }
  273. if (wpa_s->conf->user_mpm) {
  274. params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
  275. params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
  276. } else {
  277. params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
  278. params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
  279. }
  280. if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
  281. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
  282. ret = -1;
  283. goto out;
  284. }
  285. if (wpa_s->ifmsh) {
  286. params.ies = wpa_s->ifmsh->mconf->ies;
  287. params.ie_len = wpa_s->ifmsh->mconf->ie_len;
  288. params.basic_rates = wpa_s->ifmsh->basic_rates;
  289. }
  290. wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
  291. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  292. ret = wpa_drv_join_mesh(wpa_s, &params);
  293. if (ret)
  294. wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
  295. /* hostapd sets the interface down until we associate */
  296. wpa_drv_set_operstate(wpa_s, 1);
  297. out:
  298. return ret;
  299. }
  300. int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
  301. {
  302. int ret = 0;
  303. wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
  304. ret = wpa_drv_leave_mesh(wpa_s);
  305. if (ret)
  306. wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
  307. wpa_drv_set_operstate(wpa_s, 1);
  308. wpa_supplicant_mesh_deinit(wpa_s);
  309. return ret;
  310. }