mesh.c 8.6 KB

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