fst.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * FST module 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 "utils/eloop.h"
  11. #include "fst/fst.h"
  12. #include "fst/fst_internal.h"
  13. #include "fst/fst_defs.h"
  14. #include "fst/fst_ctrl_iface.h"
  15. struct dl_list fst_global_ctrls_list;
  16. static void fst_ctrl_iface_notify_peer_state_change(struct fst_iface *iface,
  17. Boolean connected,
  18. const u8 *peer_addr)
  19. {
  20. union fst_event_extra extra;
  21. extra.peer_state.connected = connected;
  22. os_strlcpy(extra.peer_state.ifname, fst_iface_get_name(iface),
  23. sizeof(extra.peer_state.ifname));
  24. os_memcpy(extra.peer_state.addr, peer_addr, ETH_ALEN);
  25. foreach_fst_ctrl_call(on_event, EVENT_PEER_STATE_CHANGED,
  26. iface, NULL, &extra);
  27. }
  28. static struct fst_group * fst_find_group_by_iface(const char *ifname)
  29. {
  30. struct fst_group *g;
  31. foreach_fst_group(g) {
  32. if (fst_group_get_iface_by_name(g, ifname))
  33. return g;
  34. }
  35. return NULL;
  36. }
  37. struct fst_iface * fst_attach(const char *ifname, const u8 *own_addr,
  38. const struct fst_wpa_obj *iface_obj,
  39. const struct fst_iface_cfg *cfg)
  40. {
  41. struct fst_group *g;
  42. struct fst_group *group = NULL;
  43. struct fst_iface *iface = NULL;
  44. Boolean new_group = FALSE;
  45. WPA_ASSERT(ifname != NULL);
  46. WPA_ASSERT(iface_obj != NULL);
  47. WPA_ASSERT(cfg != NULL);
  48. g = fst_find_group_by_iface(ifname);
  49. if (g) {
  50. fst_printf(MSG_ERROR,
  51. "%s: iface is already part of an FST group: %s",
  52. ifname, g->group_id);
  53. return NULL;
  54. }
  55. foreach_fst_group(g) {
  56. if (os_strcmp(cfg->group_id, fst_group_get_id(g)) == 0) {
  57. group = g;
  58. break;
  59. }
  60. }
  61. if (!group) {
  62. group = fst_group_create(cfg->group_id);
  63. if (!group) {
  64. fst_printf(MSG_ERROR, "%s: FST group cannot be created",
  65. cfg->group_id);
  66. return NULL;
  67. }
  68. new_group = TRUE;
  69. }
  70. iface = fst_iface_create(group, ifname, own_addr, iface_obj, cfg);
  71. if (!iface) {
  72. fst_printf_group(group, MSG_ERROR, "cannot create iface for %s",
  73. ifname);
  74. if (new_group)
  75. fst_group_delete(group);
  76. return NULL;
  77. }
  78. fst_group_attach_iface(group, iface);
  79. fst_group_update_ie(group);
  80. foreach_fst_ctrl_call(on_iface_added, iface);
  81. fst_printf_iface(iface, MSG_DEBUG,
  82. "iface attached to group %s (prio=%d, llt=%d)",
  83. cfg->group_id, cfg->priority, cfg->llt);
  84. return iface;
  85. }
  86. void fst_detach(struct fst_iface *iface)
  87. {
  88. struct fst_group *group = fst_iface_get_group(iface);
  89. fst_printf_iface(iface, MSG_DEBUG, "iface detached from group %s",
  90. fst_group_get_id(group));
  91. fst_session_global_on_iface_detached(iface);
  92. foreach_fst_ctrl_call(on_iface_removed, iface);
  93. fst_group_detach_iface(group, iface);
  94. fst_iface_delete(iface);
  95. fst_group_update_ie(group);
  96. fst_group_delete_if_empty(group);
  97. }
  98. int fst_global_init(void)
  99. {
  100. dl_list_init(&fst_global_groups_list);
  101. dl_list_init(&fst_global_ctrls_list);
  102. fst_session_global_init();
  103. return 0;
  104. }
  105. void fst_global_deinit(void)
  106. {
  107. struct fst_group *group;
  108. struct fst_ctrl_handle *h;
  109. fst_session_global_deinit();
  110. while ((group = fst_first_group()) != NULL)
  111. fst_group_delete(group);
  112. while ((h = dl_list_first(&fst_global_ctrls_list,
  113. struct fst_ctrl_handle,
  114. global_ctrls_lentry)))
  115. fst_global_del_ctrl(h);
  116. }
  117. struct fst_ctrl_handle * fst_global_add_ctrl(const struct fst_ctrl *ctrl)
  118. {
  119. struct fst_ctrl_handle *h;
  120. if (!ctrl)
  121. return NULL;
  122. h = os_zalloc(sizeof(*h));
  123. if (!h)
  124. return NULL;
  125. if (ctrl->init && ctrl->init()) {
  126. os_free(h);
  127. return NULL;
  128. }
  129. h->ctrl = *ctrl;
  130. dl_list_add_tail(&fst_global_ctrls_list, &h->global_ctrls_lentry);
  131. return h;
  132. }
  133. void fst_global_del_ctrl(struct fst_ctrl_handle *h)
  134. {
  135. dl_list_del(&h->global_ctrls_lentry);
  136. if (h->ctrl.deinit)
  137. h->ctrl.deinit();
  138. os_free(h);
  139. }
  140. void fst_rx_action(struct fst_iface *iface, const struct ieee80211_mgmt *mgmt,
  141. size_t len)
  142. {
  143. if (fst_iface_is_connected(iface, mgmt->sa))
  144. fst_session_on_action_rx(iface, mgmt, len);
  145. else
  146. wpa_printf(MSG_DEBUG,
  147. "FST: Ignore FST Action frame - no FST connection with "
  148. MACSTR, MAC2STR(mgmt->sa));
  149. }
  150. void fst_notify_peer_connected(struct fst_iface *iface, const u8 *addr)
  151. {
  152. if (is_zero_ether_addr(addr))
  153. return;
  154. #ifndef HOSTAPD
  155. fst_group_update_ie(fst_iface_get_group(iface));
  156. #endif /* HOSTAPD */
  157. fst_printf_iface(iface, MSG_DEBUG, MACSTR " became connected",
  158. MAC2STR(addr));
  159. fst_ctrl_iface_notify_peer_state_change(iface, TRUE, addr);
  160. }
  161. void fst_notify_peer_disconnected(struct fst_iface *iface, const u8 *addr)
  162. {
  163. if (is_zero_ether_addr(addr))
  164. return;
  165. #ifndef HOSTAPD
  166. fst_group_update_ie(fst_iface_get_group(iface));
  167. #endif /* HOSTAPD */
  168. fst_printf_iface(iface, MSG_DEBUG, MACSTR " became disconnected",
  169. MAC2STR(addr));
  170. fst_ctrl_iface_notify_peer_state_change(iface, FALSE, addr);
  171. }
  172. Boolean fst_are_ifaces_aggregated(struct fst_iface *iface1,
  173. struct fst_iface *iface2)
  174. {
  175. return fst_iface_get_group(iface1) == fst_iface_get_group(iface2);
  176. }