mesh.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "mesh_mpm.h"
  23. #include "mesh.h"
  24. static void wpa_supplicant_mesh_deinit(struct wpa_supplicant *wpa_s)
  25. {
  26. wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
  27. wpa_s->ifmsh = NULL;
  28. wpa_s->current_ssid = NULL;
  29. /* TODO: leave mesh (stop beacon). This will happen on link down
  30. * anyway, so it's not urgent */
  31. }
  32. void wpa_supplicant_mesh_iface_deinit(struct wpa_supplicant *wpa_s,
  33. struct hostapd_iface *ifmsh)
  34. {
  35. if (!ifmsh)
  36. return;
  37. if (ifmsh->mconf) {
  38. mesh_mpm_deinit(wpa_s, ifmsh);
  39. if (ifmsh->mconf->ies) {
  40. ifmsh->mconf->ies = NULL;
  41. /* We cannot free this struct
  42. * because wpa_authenticator on
  43. * hostapd side is also using it
  44. * for now just set to NULL and
  45. * let hostapd code free it.
  46. */
  47. }
  48. os_free(ifmsh->mconf);
  49. ifmsh->mconf = NULL;
  50. }
  51. /* take care of shared data */
  52. hostapd_interface_deinit(ifmsh);
  53. hostapd_interface_free(ifmsh);
  54. }
  55. static struct mesh_conf * mesh_config_create(struct wpa_ssid *ssid)
  56. {
  57. struct mesh_conf *conf;
  58. conf = os_zalloc(sizeof(struct mesh_conf));
  59. if (!conf)
  60. return NULL;
  61. os_memcpy(conf->meshid, ssid->ssid, ssid->ssid_len);
  62. conf->meshid_len = ssid->ssid_len;
  63. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE)
  64. conf->security |= MESH_CONF_SEC_AUTH |
  65. MESH_CONF_SEC_AMPE;
  66. else
  67. conf->security |= MESH_CONF_SEC_NONE;
  68. /* defaults */
  69. conf->mesh_pp_id = MESH_PATH_PROTOCOL_HWMP;
  70. conf->mesh_pm_id = MESH_PATH_METRIC_AIRTIME;
  71. conf->mesh_cc_id = 0;
  72. conf->mesh_sp_id = MESH_SYNC_METHOD_NEIGHBOR_OFFSET;
  73. conf->mesh_auth_id = (conf->security & MESH_CONF_SEC_AUTH) ? 1 : 0;
  74. return conf;
  75. }
  76. static void wpas_mesh_copy_groups(struct hostapd_data *bss,
  77. struct wpa_supplicant *wpa_s)
  78. {
  79. int num_groups;
  80. size_t groups_size;
  81. for (num_groups = 0; wpa_s->conf->sae_groups[num_groups] > 0;
  82. num_groups++)
  83. ;
  84. groups_size = (num_groups + 1) * sizeof(wpa_s->conf->sae_groups[0]);
  85. bss->conf->sae_groups = os_malloc(groups_size);
  86. if (bss->conf->sae_groups)
  87. os_memcpy(bss->conf->sae_groups, wpa_s->conf->sae_groups,
  88. groups_size);
  89. }
  90. static int wpa_supplicant_mesh_init(struct wpa_supplicant *wpa_s,
  91. struct wpa_ssid *ssid)
  92. {
  93. struct hostapd_iface *ifmsh;
  94. struct hostapd_data *bss;
  95. struct hostapd_config *conf;
  96. struct mesh_conf *mconf;
  97. int basic_rates_erp[] = { 10, 20, 55, 60, 110, 120, 240, -1 };
  98. static int default_groups[] = { 19, 20, 21, 25, 26, -1 };
  99. size_t len;
  100. if (!wpa_s->conf->user_mpm) {
  101. /* not much for us to do here */
  102. wpa_msg(wpa_s, MSG_WARNING,
  103. "user_mpm is not enabled in configuration");
  104. return 0;
  105. }
  106. wpa_s->ifmsh = ifmsh = os_zalloc(sizeof(*wpa_s->ifmsh));
  107. if (!ifmsh)
  108. return -ENOMEM;
  109. ifmsh->num_bss = 1;
  110. ifmsh->bss = os_calloc(wpa_s->ifmsh->num_bss,
  111. sizeof(struct hostapd_data *));
  112. if (!ifmsh->bss)
  113. goto out_free;
  114. ifmsh->bss[0] = bss = os_zalloc(sizeof(struct hostapd_data));
  115. if (!bss)
  116. goto out_free;
  117. os_memcpy(bss->own_addr, wpa_s->own_addr, ETH_ALEN);
  118. bss->driver = wpa_s->driver;
  119. bss->drv_priv = wpa_s->drv_priv;
  120. bss->iface = ifmsh;
  121. wpa_s->assoc_freq = ssid->frequency;
  122. wpa_s->current_ssid = ssid;
  123. /* setup an AP config for auth processing */
  124. conf = hostapd_config_defaults();
  125. if (!conf)
  126. goto out_free;
  127. bss->conf = *conf->bss;
  128. bss->conf->start_disabled = 1;
  129. bss->conf->mesh = MESH_ENABLED;
  130. bss->iconf = conf;
  131. ifmsh->conf = conf;
  132. ifmsh->bss[0]->max_plinks = 99;
  133. os_strlcpy(bss->conf->iface, wpa_s->ifname, sizeof(bss->conf->iface));
  134. mconf = mesh_config_create(ssid);
  135. if (!mconf)
  136. goto out_free;
  137. ifmsh->mconf = mconf;
  138. /* need conf->hw_mode for supported rates. */
  139. if (ssid->frequency == 0) {
  140. conf->hw_mode = HOSTAPD_MODE_IEEE80211G;
  141. conf->channel = 1;
  142. } else {
  143. conf->hw_mode = ieee80211_freq_to_chan(ssid->frequency,
  144. &conf->channel);
  145. }
  146. if (conf->hw_mode == NUM_HOSTAPD_MODES) {
  147. wpa_printf(MSG_ERROR, "Unsupported mesh mode frequency: %d MHz",
  148. ssid->frequency);
  149. goto out_free;
  150. }
  151. /*
  152. * XXX: Hack! This is so an MPM which correctly sets the ERP mandatory
  153. * rates as BSSBasicRateSet doesn't reject us. We could add a new
  154. * hw_mode HOSTAPD_MODE_IEEE80211G_ERP, but this is way easier. This
  155. * also makes our BSSBasicRateSet advertised in Beacon frames match the
  156. * one in peering frames, sigh.
  157. */
  158. if (conf->hw_mode == HOSTAPD_MODE_IEEE80211G) {
  159. conf->basic_rates = os_malloc(sizeof(basic_rates_erp));
  160. if (!conf->basic_rates)
  161. goto out_free;
  162. os_memcpy(conf->basic_rates, basic_rates_erp,
  163. sizeof(basic_rates_erp));
  164. }
  165. if (hostapd_setup_interface(ifmsh)) {
  166. wpa_printf(MSG_ERROR,
  167. "Failed to initialize hostapd interface for mesh");
  168. return -1;
  169. }
  170. if (wpa_drv_init_mesh(wpa_s)) {
  171. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh in driver");
  172. return -1;
  173. }
  174. if (mconf->security != MESH_CONF_SEC_NONE) {
  175. if (ssid->passphrase == NULL) {
  176. wpa_printf(MSG_ERROR,
  177. "mesh: Passphrase for SAE not configured");
  178. goto out_free;
  179. }
  180. bss->conf->wpa = ssid->proto;
  181. bss->conf->wpa_key_mgmt = ssid->key_mgmt;
  182. if (wpa_s->conf->sae_groups &&
  183. wpa_s->conf->sae_groups[0] > 0) {
  184. wpas_mesh_copy_groups(bss, wpa_s);
  185. } else {
  186. bss->conf->sae_groups =
  187. os_malloc(sizeof(default_groups));
  188. if (!bss->conf->sae_groups)
  189. goto out_free;
  190. os_memcpy(bss->conf->sae_groups, default_groups,
  191. sizeof(default_groups));
  192. }
  193. len = os_strlen(ssid->passphrase);
  194. bss->conf->ssid.wpa_passphrase =
  195. dup_binstr(ssid->passphrase, len);
  196. }
  197. return 0;
  198. out_free:
  199. wpa_supplicant_mesh_deinit(wpa_s);
  200. return -ENOMEM;
  201. }
  202. void wpa_mesh_notify_peer(struct wpa_supplicant *wpa_s, const u8 *addr,
  203. const u8 *ies, size_t ie_len)
  204. {
  205. struct ieee802_11_elems elems;
  206. wpa_msg(wpa_s, MSG_INFO,
  207. "new peer notification for " MACSTR, MAC2STR(addr));
  208. if (ieee802_11_parse_elems(ies, ie_len, &elems, 0) == ParseFailed) {
  209. wpa_msg(wpa_s, MSG_INFO, "Could not parse beacon from " MACSTR,
  210. MAC2STR(addr));
  211. return;
  212. }
  213. wpa_mesh_new_mesh_peer(wpa_s, addr, &elems);
  214. }
  215. void wpa_supplicant_mesh_add_scan_ie(struct wpa_supplicant *wpa_s,
  216. struct wpabuf **extra_ie)
  217. {
  218. /* EID + 0-length (wildcard) mesh-id */
  219. size_t ielen = 2;
  220. if (wpabuf_resize(extra_ie, ielen) == 0) {
  221. wpabuf_put_u8(*extra_ie, WLAN_EID_MESH_ID);
  222. wpabuf_put_u8(*extra_ie, 0);
  223. }
  224. }
  225. int wpa_supplicant_join_mesh(struct wpa_supplicant *wpa_s,
  226. struct wpa_ssid *ssid)
  227. {
  228. struct wpa_driver_mesh_join_params params;
  229. int ret = 0;
  230. if (!ssid || !ssid->ssid || !ssid->ssid_len || !ssid->frequency) {
  231. ret = -ENOENT;
  232. goto out;
  233. }
  234. wpa_supplicant_mesh_deinit(wpa_s);
  235. os_memset(&params, 0, sizeof(params));
  236. params.meshid = ssid->ssid;
  237. params.meshid_len = ssid->ssid_len;
  238. params.freq = ssid->frequency;
  239. if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
  240. params.flags |= WPA_DRIVER_MESH_FLAG_SAE_AUTH;
  241. params.flags |= WPA_DRIVER_MESH_FLAG_AMPE;
  242. wpa_s->conf->user_mpm = 1;
  243. }
  244. if (wpa_s->conf->user_mpm) {
  245. params.flags |= WPA_DRIVER_MESH_FLAG_USER_MPM;
  246. params.conf.flags &= ~WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
  247. } else {
  248. params.flags |= WPA_DRIVER_MESH_FLAG_DRIVER_MPM;
  249. params.conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
  250. }
  251. if (wpa_supplicant_mesh_init(wpa_s, ssid)) {
  252. wpa_msg(wpa_s, MSG_ERROR, "Failed to init mesh");
  253. ret = -1;
  254. goto out;
  255. }
  256. if (wpa_s->ifmsh) {
  257. params.ies = wpa_s->ifmsh->mconf->ies;
  258. params.ie_len = wpa_s->ifmsh->mconf->ie_len;
  259. params.basic_rates = wpa_s->ifmsh->basic_rates;
  260. }
  261. wpa_msg(wpa_s, MSG_INFO, "joining mesh %s",
  262. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  263. ret = wpa_drv_join_mesh(wpa_s, &params);
  264. if (ret)
  265. wpa_msg(wpa_s, MSG_ERROR, "mesh join error=%d\n", ret);
  266. /* hostapd sets the interface down until we associate */
  267. wpa_drv_set_operstate(wpa_s, 1);
  268. out:
  269. return ret;
  270. }
  271. int wpa_supplicant_leave_mesh(struct wpa_supplicant *wpa_s)
  272. {
  273. int ret = 0;
  274. wpa_msg(wpa_s, MSG_INFO, "leaving mesh");
  275. ret = wpa_drv_leave_mesh(wpa_s);
  276. if (ret)
  277. wpa_msg(wpa_s, MSG_ERROR, "mesh leave error=%d", ret);
  278. wpa_drv_set_operstate(wpa_s, 1);
  279. wpa_supplicant_mesh_deinit(wpa_s);
  280. return ret;
  281. }