fst_group.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * FST module - FST group object implementation
  3. * Copyright (c) 2014, Qualcomm Atheros, Inc.
  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 "common/defs.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/ieee802_11_common.h"
  13. #include "drivers/driver.h"
  14. #include "fst/fst_internal.h"
  15. #include "fst/fst_defs.h"
  16. struct dl_list fst_global_groups_list;
  17. static void fst_dump_mb_ies(const char *group_id, const char *ifname,
  18. struct wpabuf *mbies)
  19. {
  20. const u8 *p = wpabuf_head(mbies);
  21. size_t s = wpabuf_len(mbies);
  22. while (s >= 2) {
  23. const struct multi_band_ie *mbie =
  24. (const struct multi_band_ie *) p;
  25. WPA_ASSERT(mbie->eid == WLAN_EID_MULTI_BAND);
  26. WPA_ASSERT(2 + mbie->len >= sizeof(*mbie));
  27. fst_printf(MSG_WARNING,
  28. "%s: %s: mb_ctrl=%u band_id=%u op_class=%u chan=%u bssid="
  29. MACSTR
  30. " beacon_int=%u tsf_offs=[%u %u %u %u %u %u %u %u] mb_cc=0x%02x tmout=%u",
  31. group_id, ifname,
  32. mbie->mb_ctrl, mbie->band_id, mbie->op_class,
  33. mbie->chan, MAC2STR(mbie->bssid), mbie->beacon_int,
  34. mbie->tsf_offs[0], mbie->tsf_offs[1],
  35. mbie->tsf_offs[2], mbie->tsf_offs[3],
  36. mbie->tsf_offs[4], mbie->tsf_offs[5],
  37. mbie->tsf_offs[6], mbie->tsf_offs[7],
  38. mbie->mb_connection_capability,
  39. mbie->fst_session_tmout);
  40. p += 2 + mbie->len;
  41. s -= 2 + mbie->len;
  42. }
  43. }
  44. static void fst_fill_mb_ie(struct wpabuf *buf, const u8 *bssid,
  45. const u8 *own_addr, enum mb_band_id band, u8 channel)
  46. {
  47. struct multi_band_ie *mbie;
  48. size_t len = sizeof(*mbie);
  49. if (own_addr)
  50. len += ETH_ALEN;
  51. mbie = wpabuf_put(buf, len);
  52. os_memset(mbie, 0, len);
  53. mbie->eid = WLAN_EID_MULTI_BAND;
  54. mbie->len = len - 2;
  55. #ifdef HOSTAPD
  56. mbie->mb_ctrl = MB_STA_ROLE_AP;
  57. mbie->mb_connection_capability = MB_CONNECTION_CAPABILITY_AP;
  58. #else /* HOSTAPD */
  59. mbie->mb_ctrl = MB_STA_ROLE_NON_PCP_NON_AP;
  60. mbie->mb_connection_capability = 0;
  61. #endif /* HOSTAPD */
  62. if (bssid)
  63. os_memcpy(mbie->bssid, bssid, ETH_ALEN);
  64. mbie->band_id = band;
  65. mbie->op_class = 0; /* means all */
  66. mbie->chan = channel;
  67. mbie->fst_session_tmout = FST_DEFAULT_SESSION_TIMEOUT_TU;
  68. if (own_addr) {
  69. mbie->mb_ctrl |= MB_CTRL_STA_MAC_PRESENT;
  70. os_memcpy(&mbie[1], own_addr, ETH_ALEN);
  71. }
  72. }
  73. static unsigned fst_fill_iface_mb_ies(struct fst_iface *f, struct wpabuf *buf)
  74. {
  75. const u8 *bssid;
  76. bssid = fst_iface_get_bssid(f);
  77. if (bssid) {
  78. enum hostapd_hw_mode hw_mode;
  79. u8 channel;
  80. if (buf) {
  81. fst_iface_get_channel_info(f, &hw_mode, &channel);
  82. fst_fill_mb_ie(buf, bssid, fst_iface_get_addr(f),
  83. fst_hw_mode_to_band(hw_mode), channel);
  84. }
  85. return 1;
  86. } else {
  87. unsigned bands[MB_BAND_ID_WIFI_60GHZ + 1] = {};
  88. struct hostapd_hw_modes *modes;
  89. enum mb_band_id b;
  90. int num_modes = fst_iface_get_hw_modes(f, &modes);
  91. int ret = 0;
  92. while (num_modes--) {
  93. b = fst_hw_mode_to_band(modes->mode);
  94. modes++;
  95. if (b >= ARRAY_SIZE(bands) || bands[b]++)
  96. continue;
  97. ret++;
  98. if (buf)
  99. fst_fill_mb_ie(buf, NULL, fst_iface_get_addr(f),
  100. b, MB_STA_CHANNEL_ALL);
  101. }
  102. return ret;
  103. }
  104. }
  105. static struct wpabuf * fst_group_create_mb_ie(struct fst_group *g,
  106. struct fst_iface *i)
  107. {
  108. struct wpabuf *buf;
  109. struct fst_iface *f;
  110. unsigned int nof_mbies = 0;
  111. unsigned int nof_ifaces_added = 0;
  112. foreach_fst_group_iface(g, f) {
  113. if (f == i)
  114. continue;
  115. nof_mbies += fst_fill_iface_mb_ies(f, NULL);
  116. }
  117. buf = wpabuf_alloc(nof_mbies *
  118. (sizeof(struct multi_band_ie) + ETH_ALEN));
  119. if (!buf) {
  120. fst_printf_iface(i, MSG_ERROR,
  121. "cannot allocate mem for %u MB IEs",
  122. nof_mbies);
  123. return NULL;
  124. }
  125. /* The list is sorted in descending order by priorities, so MB IEs will
  126. * be arranged in the same order, as required by spec (see corresponding
  127. * comment in.fst_attach().
  128. */
  129. foreach_fst_group_iface(g, f) {
  130. if (f == i)
  131. continue;
  132. fst_fill_iface_mb_ies(f, buf);
  133. ++nof_ifaces_added;
  134. fst_printf_iface(i, MSG_DEBUG, "added to MB IE");
  135. }
  136. if (!nof_ifaces_added) {
  137. wpabuf_free(buf);
  138. buf = NULL;
  139. fst_printf_iface(i, MSG_INFO,
  140. "cannot add MB IE: no backup ifaces");
  141. } else {
  142. fst_dump_mb_ies(fst_group_get_id(g), fst_iface_get_name(i),
  143. buf);
  144. }
  145. return buf;
  146. }
  147. static const u8 * fst_mbie_get_peer_addr(const struct multi_band_ie *mbie)
  148. {
  149. const u8 *peer_addr = NULL;
  150. switch (MB_CTRL_ROLE(mbie->mb_ctrl)) {
  151. case MB_STA_ROLE_AP:
  152. peer_addr = mbie->bssid;
  153. break;
  154. case MB_STA_ROLE_NON_PCP_NON_AP:
  155. if (mbie->mb_ctrl & MB_CTRL_STA_MAC_PRESENT &&
  156. (size_t) 2 + mbie->len >= sizeof(*mbie) + ETH_ALEN)
  157. peer_addr = (const u8 *) &mbie[1];
  158. break;
  159. default:
  160. break;
  161. }
  162. return peer_addr;
  163. }
  164. static struct fst_iface *
  165. fst_group_get_new_iface_by_mbie_and_band_id(struct fst_group *g,
  166. const u8 *mb_ies_buff,
  167. size_t mb_ies_size,
  168. u8 band_id,
  169. u8 *iface_peer_addr)
  170. {
  171. while (mb_ies_size >= 2) {
  172. const struct multi_band_ie *mbie =
  173. (const struct multi_band_ie *) mb_ies_buff;
  174. if (mbie->eid != WLAN_EID_MULTI_BAND ||
  175. (size_t) 2 + mbie->len < sizeof(*mbie))
  176. break;
  177. if (mbie->band_id == band_id) {
  178. struct fst_iface *iface;
  179. foreach_fst_group_iface(g, iface) {
  180. const u8 *peer_addr =
  181. fst_mbie_get_peer_addr(mbie);
  182. if (peer_addr &&
  183. fst_iface_is_connected(iface, peer_addr) &&
  184. band_id == fst_iface_get_band_id(iface)) {
  185. os_memcpy(iface_peer_addr, peer_addr,
  186. ETH_ALEN);
  187. return iface;
  188. }
  189. }
  190. break;
  191. }
  192. mb_ies_buff += 2 + mbie->len;
  193. mb_ies_size -= 2 + mbie->len;
  194. }
  195. return NULL;
  196. }
  197. struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g,
  198. const char *ifname)
  199. {
  200. struct fst_iface *f;
  201. foreach_fst_group_iface(g, f) {
  202. const char *in = fst_iface_get_name(f);
  203. if (os_strncmp(in, ifname, os_strlen(in)) == 0)
  204. return f;
  205. }
  206. return NULL;
  207. }
  208. u8 fst_group_assign_dialog_token(struct fst_group *g)
  209. {
  210. g->dialog_token++;
  211. if (g->dialog_token == 0)
  212. g->dialog_token++;
  213. return g->dialog_token;
  214. }
  215. u32 fst_group_assign_fsts_id(struct fst_group *g)
  216. {
  217. g->fsts_id++;
  218. return g->fsts_id;
  219. }
  220. static Boolean
  221. fst_group_does_iface_appear_in_other_mbies(struct fst_group *g,
  222. struct fst_iface *iface,
  223. struct fst_iface *other,
  224. u8 *peer_addr)
  225. {
  226. struct fst_get_peer_ctx *ctx;
  227. const u8 *addr;
  228. const u8 *iface_addr;
  229. enum mb_band_id iface_band_id;
  230. WPA_ASSERT(g == fst_iface_get_group(iface));
  231. WPA_ASSERT(g == fst_iface_get_group(other));
  232. iface_addr = fst_iface_get_addr(iface);
  233. iface_band_id = fst_iface_get_band_id(iface);
  234. addr = fst_iface_get_peer_first(other, &ctx, TRUE);
  235. for (; addr; addr = fst_iface_get_peer_next(other, &ctx, TRUE)) {
  236. const struct wpabuf *mbies;
  237. u8 other_iface_peer_addr[ETH_ALEN];
  238. struct fst_iface *other_new_iface;
  239. mbies = fst_iface_get_peer_mb_ie(other, addr);
  240. if (!mbies)
  241. continue;
  242. other_new_iface = fst_group_get_new_iface_by_mbie_and_band_id(
  243. g, wpabuf_head(mbies), wpabuf_len(mbies),
  244. iface_band_id, other_iface_peer_addr);
  245. if (other_new_iface == iface &&
  246. os_memcmp(iface_addr, other_iface_peer_addr,
  247. ETH_ALEN) != 0) {
  248. os_memcpy(peer_addr, addr, ETH_ALEN);
  249. return TRUE;
  250. }
  251. }
  252. return FALSE;
  253. }
  254. struct fst_iface *
  255. fst_group_find_new_iface_by_stie(struct fst_group *g,
  256. struct fst_iface *iface,
  257. const u8 *peer_addr,
  258. const struct session_transition_ie *stie,
  259. u8 *iface_peer_addr)
  260. {
  261. struct fst_iface *i;
  262. foreach_fst_group_iface(g, i) {
  263. if (i == iface ||
  264. stie->new_band_id != fst_iface_get_band_id(i))
  265. continue;
  266. if (fst_group_does_iface_appear_in_other_mbies(g, iface, i,
  267. iface_peer_addr))
  268. return i;
  269. break;
  270. }
  271. return NULL;
  272. }
  273. struct fst_iface *
  274. fst_group_get_new_iface_by_stie_and_mbie(
  275. struct fst_group *g, const u8 *mb_ies_buff, size_t mb_ies_size,
  276. const struct session_transition_ie *stie, u8 *iface_peer_addr)
  277. {
  278. return fst_group_get_new_iface_by_mbie_and_band_id(
  279. g, mb_ies_buff, mb_ies_size, stie->new_band_id,
  280. iface_peer_addr);
  281. }
  282. struct fst_group * fst_group_create(const char *group_id)
  283. {
  284. struct fst_group *g;
  285. g = os_zalloc(sizeof(*g));
  286. if (g == NULL) {
  287. fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id);
  288. return NULL;
  289. }
  290. dl_list_init(&g->ifaces);
  291. os_strlcpy(g->group_id, group_id, sizeof(g->group_id));
  292. dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry);
  293. fst_printf_group(g, MSG_DEBUG, "instance created");
  294. foreach_fst_ctrl_call(on_group_created, g);
  295. return g;
  296. }
  297. void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i)
  298. {
  299. struct dl_list *list = &g->ifaces;
  300. struct fst_iface *f;
  301. /*
  302. * Add new interface to the list.
  303. * The list is sorted in descending order by priority to allow
  304. * multiple MB IEs creation according to the spec (see 10.32 Multi-band
  305. * operation, 10.32.1 General), as they should be ordered according to
  306. * priorities.
  307. */
  308. foreach_fst_group_iface(g, f) {
  309. if (fst_iface_get_priority(f) < fst_iface_get_priority(i))
  310. break;
  311. list = &f->group_lentry;
  312. }
  313. dl_list_add(list, &i->group_lentry);
  314. }
  315. void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i)
  316. {
  317. dl_list_del(&i->group_lentry);
  318. }
  319. void fst_group_delete(struct fst_group *group)
  320. {
  321. struct fst_session *s;
  322. dl_list_del(&group->global_groups_lentry);
  323. WPA_ASSERT(dl_list_empty(&group->ifaces));
  324. foreach_fst_ctrl_call(on_group_deleted, group);
  325. fst_printf_group(group, MSG_DEBUG, "instance deleted");
  326. while ((s = fst_session_global_get_first_by_group(group)) != NULL)
  327. fst_session_delete(s);
  328. os_free(group);
  329. }
  330. Boolean fst_group_delete_if_empty(struct fst_group *group)
  331. {
  332. Boolean is_empty = !fst_group_has_ifaces(group) &&
  333. !fst_session_global_get_first_by_group(group);
  334. if (is_empty)
  335. fst_group_delete(group);
  336. return is_empty;
  337. }
  338. void fst_group_update_ie(struct fst_group *g)
  339. {
  340. struct fst_iface *i;
  341. foreach_fst_group_iface(g, i) {
  342. struct wpabuf *mbie = fst_group_create_mb_ie(g, i);
  343. if (!mbie)
  344. fst_printf_iface(i, MSG_WARNING, "cannot create MB IE");
  345. fst_iface_attach_mbie(i, mbie);
  346. fst_iface_set_ies(i, mbie);
  347. fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie);
  348. }
  349. }