fst_group.c 10 KB

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