fst.c 4.6 KB

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