fst_group.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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(2U + 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 const u8 * fst_mbie_get_peer_addr_for_band(const struct wpabuf *mbies,
  165. u8 band_id)
  166. {
  167. const u8 *p = wpabuf_head(mbies);
  168. size_t s = wpabuf_len(mbies);
  169. while (s >= 2) {
  170. const struct multi_band_ie *mbie =
  171. (const struct multi_band_ie *) p;
  172. if (mbie->eid != WLAN_EID_MULTI_BAND) {
  173. fst_printf(MSG_INFO, "unexpected eid %d", mbie->eid);
  174. return NULL;
  175. }
  176. if (mbie->len < sizeof(*mbie) - 2 || mbie->len > s - 2) {
  177. fst_printf(MSG_INFO, "invalid mbie len %d",
  178. mbie->len);
  179. return NULL;
  180. }
  181. if (mbie->band_id == band_id)
  182. return fst_mbie_get_peer_addr(mbie);
  183. p += 2 + mbie->len;
  184. s -= 2 + mbie->len;
  185. }
  186. fst_printf(MSG_INFO, "mbie doesn't contain band %d", band_id);
  187. return NULL;
  188. }
  189. struct fst_iface * fst_group_get_iface_by_name(struct fst_group *g,
  190. const char *ifname)
  191. {
  192. struct fst_iface *f;
  193. foreach_fst_group_iface(g, f) {
  194. const char *in = fst_iface_get_name(f);
  195. if (os_strncmp(in, ifname, os_strlen(in)) == 0)
  196. return f;
  197. }
  198. return NULL;
  199. }
  200. u8 fst_group_assign_dialog_token(struct fst_group *g)
  201. {
  202. g->dialog_token++;
  203. if (g->dialog_token == 0)
  204. g->dialog_token++;
  205. return g->dialog_token;
  206. }
  207. u32 fst_group_assign_fsts_id(struct fst_group *g)
  208. {
  209. g->fsts_id++;
  210. return g->fsts_id;
  211. }
  212. /**
  213. * fst_group_get_peer_other_connection_1 - Find peer's "other" connection
  214. * (iface, MAC tuple) by using peer's MB IE on iface.
  215. *
  216. * @iface: iface on which FST Setup Request was received
  217. * @peer_addr: Peer address on iface
  218. * @band_id: "other" connection band id
  219. * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
  220. * "other" iface)
  221. *
  222. * This function parses peer's MB IE on iface. It looks for peer's MAC address
  223. * on band_id (tmp_peer_addr). Next all interfaces are iterated to find an
  224. * interface which correlates with band_id. If such interface is found, peer
  225. * database is iterated to see if tmp_peer_addr is connected over it.
  226. */
  227. static struct fst_iface *
  228. fst_group_get_peer_other_connection_1(struct fst_iface *iface,
  229. const u8 *peer_addr, u8 band_id,
  230. u8 *other_peer_addr)
  231. {
  232. const struct wpabuf *mbies;
  233. struct fst_iface *other_iface;
  234. const u8 *tmp_peer_addr;
  235. /* Get peer's MB IEs on iface */
  236. mbies = fst_iface_get_peer_mb_ie(iface, peer_addr);
  237. if (!mbies)
  238. return NULL;
  239. /* Get peer's MAC address on the "other" interface */
  240. tmp_peer_addr = fst_mbie_get_peer_addr_for_band(mbies, band_id);
  241. if (!tmp_peer_addr) {
  242. fst_printf(MSG_INFO,
  243. "couldn't extract other peer addr from mbies");
  244. return NULL;
  245. }
  246. fst_printf(MSG_DEBUG, "found other peer addr from mbies: " MACSTR,
  247. MAC2STR(tmp_peer_addr));
  248. foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
  249. if (other_iface == iface ||
  250. band_id != fst_iface_get_band_id(other_iface))
  251. continue;
  252. if (fst_iface_is_connected(other_iface, tmp_peer_addr, FALSE)) {
  253. os_memcpy(other_peer_addr, tmp_peer_addr, ETH_ALEN);
  254. return other_iface;
  255. }
  256. }
  257. return NULL;
  258. }
  259. /**
  260. * fst_group_get_peer_other_connection_2 - Find peer's "other" connection
  261. * (iface, MAC tuple) by using MB IEs of other peers.
  262. *
  263. * @iface: iface on which FST Setup Request was received
  264. * @peer_addr: Peer address on iface
  265. * @band_id: "other" connection band id
  266. * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
  267. * "other" iface)
  268. *
  269. * This function iterates all connection (other_iface, cur_peer_addr tuples).
  270. * For each connection, MB IE (of cur_peer_addr on other_iface) is parsed and
  271. * MAC address on iface's band_id is extracted (this_peer_addr).
  272. * this_peer_addr is then compared to peer_addr. A match indicates we have
  273. * found the "other" connection.
  274. */
  275. static struct fst_iface *
  276. fst_group_get_peer_other_connection_2(struct fst_iface *iface,
  277. const u8 *peer_addr, u8 band_id,
  278. u8 *other_peer_addr)
  279. {
  280. u8 this_band_id = fst_iface_get_band_id(iface);
  281. const u8 *cur_peer_addr, *this_peer_addr;
  282. struct fst_get_peer_ctx *ctx;
  283. struct fst_iface *other_iface;
  284. const struct wpabuf *cur_mbie;
  285. foreach_fst_group_iface(fst_iface_get_group(iface), other_iface) {
  286. if (other_iface == iface ||
  287. band_id != fst_iface_get_band_id(other_iface))
  288. continue;
  289. cur_peer_addr = fst_iface_get_peer_first(other_iface, &ctx,
  290. TRUE);
  291. for (; cur_peer_addr;
  292. cur_peer_addr = fst_iface_get_peer_next(other_iface, &ctx,
  293. TRUE)) {
  294. cur_mbie = fst_iface_get_peer_mb_ie(other_iface,
  295. cur_peer_addr);
  296. if (!cur_mbie)
  297. continue;
  298. this_peer_addr = fst_mbie_get_peer_addr_for_band(
  299. cur_mbie, this_band_id);
  300. if (!this_peer_addr)
  301. continue;
  302. if (os_memcmp(this_peer_addr, peer_addr, ETH_ALEN) ==
  303. 0) {
  304. os_memcpy(other_peer_addr, cur_peer_addr,
  305. ETH_ALEN);
  306. return other_iface;
  307. }
  308. }
  309. }
  310. return NULL;
  311. }
  312. /**
  313. * fst_group_get_peer_other_connection - Find peer's "other" connection (iface,
  314. * MAC tuple).
  315. *
  316. * @iface: iface on which FST Setup Request was received
  317. * @peer_addr: Peer address on iface
  318. * @band_id: "other" connection band id
  319. * @other_peer_addr (out): Peer's MAC address on the "other" connection (on the
  320. * "other" iface)
  321. *
  322. * This function is called upon receiving FST Setup Request from some peer who
  323. * has peer_addr on iface. It searches for another connection of the same peer
  324. * on different interface which correlates with band_id. MB IEs received from
  325. * peer (on the two different interfaces) are used to identify same peer.
  326. */
  327. struct fst_iface *
  328. fst_group_get_peer_other_connection(struct fst_iface *iface,
  329. const u8 *peer_addr, u8 band_id,
  330. u8 *other_peer_addr)
  331. {
  332. struct fst_iface *other_iface;
  333. fst_printf(MSG_DEBUG, "%s: %s:" MACSTR ", %d", __func__,
  334. fst_iface_get_name(iface), MAC2STR(peer_addr), band_id);
  335. /*
  336. * Two search methods are used:
  337. * 1. Use peer's MB IE on iface to extract peer's MAC address on
  338. * "other" connection. Then check if such "other" connection exists.
  339. * 2. Iterate peer database, examine each MB IE to see if it points to
  340. * (iface, peer_addr) tuple
  341. */
  342. other_iface = fst_group_get_peer_other_connection_1(iface, peer_addr,
  343. band_id,
  344. other_peer_addr);
  345. if (other_iface) {
  346. fst_printf(MSG_DEBUG, "found by method #1. %s:" MACSTR,
  347. fst_iface_get_name(other_iface),
  348. MAC2STR(other_peer_addr));
  349. return other_iface;
  350. }
  351. other_iface = fst_group_get_peer_other_connection_2(iface, peer_addr,
  352. band_id,
  353. other_peer_addr);
  354. if (other_iface) {
  355. fst_printf(MSG_DEBUG, "found by method #2. %s:" MACSTR,
  356. fst_iface_get_name(other_iface),
  357. MAC2STR(other_peer_addr));
  358. return other_iface;
  359. }
  360. fst_printf(MSG_INFO, "%s: other connection not found", __func__);
  361. return NULL;
  362. }
  363. struct fst_group * fst_group_create(const char *group_id)
  364. {
  365. struct fst_group *g;
  366. g = os_zalloc(sizeof(*g));
  367. if (g == NULL) {
  368. fst_printf(MSG_ERROR, "%s: Cannot alloc group", group_id);
  369. return NULL;
  370. }
  371. dl_list_init(&g->ifaces);
  372. os_strlcpy(g->group_id, group_id, sizeof(g->group_id));
  373. dl_list_add_tail(&fst_global_groups_list, &g->global_groups_lentry);
  374. fst_printf_group(g, MSG_DEBUG, "instance created");
  375. foreach_fst_ctrl_call(on_group_created, g);
  376. return g;
  377. }
  378. void fst_group_attach_iface(struct fst_group *g, struct fst_iface *i)
  379. {
  380. struct dl_list *list = &g->ifaces;
  381. struct fst_iface *f;
  382. /*
  383. * Add new interface to the list.
  384. * The list is sorted in descending order by priority to allow
  385. * multiple MB IEs creation according to the spec (see 10.32 Multi-band
  386. * operation, 10.32.1 General), as they should be ordered according to
  387. * priorities.
  388. */
  389. foreach_fst_group_iface(g, f) {
  390. if (fst_iface_get_priority(f) < fst_iface_get_priority(i))
  391. break;
  392. list = &f->group_lentry;
  393. }
  394. dl_list_add(list, &i->group_lentry);
  395. }
  396. void fst_group_detach_iface(struct fst_group *g, struct fst_iface *i)
  397. {
  398. dl_list_del(&i->group_lentry);
  399. }
  400. void fst_group_delete(struct fst_group *group)
  401. {
  402. struct fst_session *s;
  403. dl_list_del(&group->global_groups_lentry);
  404. WPA_ASSERT(dl_list_empty(&group->ifaces));
  405. foreach_fst_ctrl_call(on_group_deleted, group);
  406. fst_printf_group(group, MSG_DEBUG, "instance deleted");
  407. while ((s = fst_session_global_get_first_by_group(group)) != NULL)
  408. fst_session_delete(s);
  409. os_free(group);
  410. }
  411. Boolean fst_group_delete_if_empty(struct fst_group *group)
  412. {
  413. Boolean is_empty = !fst_group_has_ifaces(group) &&
  414. !fst_session_global_get_first_by_group(group);
  415. if (is_empty)
  416. fst_group_delete(group);
  417. return is_empty;
  418. }
  419. void fst_group_update_ie(struct fst_group *g)
  420. {
  421. struct fst_iface *i;
  422. foreach_fst_group_iface(g, i) {
  423. struct wpabuf *mbie = fst_group_create_mb_ie(g, i);
  424. if (!mbie)
  425. fst_printf_iface(i, MSG_WARNING, "cannot create MB IE");
  426. fst_iface_attach_mbie(i, mbie);
  427. fst_iface_set_ies(i, mbie);
  428. fst_printf_iface(i, MSG_DEBUG, "multi-band IE set to %p", mbie);
  429. }
  430. }