ocb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OCB mode implementation
  4. *
  5. * Copyright: (c) 2014 Czech Technical University in Prague
  6. * (c) 2014 Volkswagen Group Research
  7. * Copyright (C) 2022 - 2024 Intel Corporation
  8. * Author: Rostislav Lisovy <rostislav.lisovy@fel.cvut.cz>
  9. * Funded by: Volkswagen Group Research
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/mac80211.h>
  18. #include <linux/unaligned.h>
  19. #include "ieee80211_i.h"
  20. #include "driver-ops.h"
  21. #include "rate.h"
  22. #define IEEE80211_OCB_HOUSEKEEPING_INTERVAL (60 * HZ)
  23. #define IEEE80211_OCB_PEER_INACTIVITY_LIMIT (240 * HZ)
  24. #define IEEE80211_OCB_MAX_STA_ENTRIES 128
  25. /**
  26. * enum ocb_deferred_task_flags - mac80211 OCB deferred tasks
  27. * @OCB_WORK_HOUSEKEEPING: run the periodic OCB housekeeping tasks
  28. *
  29. * These flags are used in @wrkq_flags field of &struct ieee80211_if_ocb
  30. */
  31. enum ocb_deferred_task_flags {
  32. OCB_WORK_HOUSEKEEPING,
  33. };
  34. void ieee80211_ocb_rx_no_sta(struct ieee80211_sub_if_data *sdata,
  35. const u8 *bssid, const u8 *addr,
  36. u32 supp_rates)
  37. {
  38. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  39. struct ieee80211_local *local = sdata->local;
  40. struct ieee80211_chanctx_conf *chanctx_conf;
  41. struct ieee80211_supported_band *sband;
  42. struct sta_info *sta;
  43. int band;
  44. if (!ifocb->joined)
  45. return;
  46. /* XXX: Consider removing the least recently used entry and
  47. * allow new one to be added.
  48. */
  49. if (local->num_sta >= IEEE80211_OCB_MAX_STA_ENTRIES) {
  50. net_info_ratelimited("%s: No room for a new OCB STA entry %pM\n",
  51. sdata->name, addr);
  52. return;
  53. }
  54. ocb_dbg(sdata, "Adding new OCB station %pM\n", addr);
  55. rcu_read_lock();
  56. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  57. if (WARN_ON_ONCE(!chanctx_conf)) {
  58. rcu_read_unlock();
  59. return;
  60. }
  61. band = chanctx_conf->def.chan->band;
  62. rcu_read_unlock();
  63. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  64. if (!sta)
  65. return;
  66. /* Add only mandatory rates for now */
  67. sband = local->hw.wiphy->bands[band];
  68. sta->sta.deflink.supp_rates[band] = ieee80211_mandatory_rates(sband);
  69. spin_lock(&ifocb->incomplete_lock);
  70. list_add(&sta->list, &ifocb->incomplete_stations);
  71. spin_unlock(&ifocb->incomplete_lock);
  72. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  73. }
  74. static struct sta_info *ieee80211_ocb_finish_sta(struct sta_info *sta)
  75. __acquires(RCU)
  76. {
  77. struct ieee80211_sub_if_data *sdata = sta->sdata;
  78. u8 addr[ETH_ALEN];
  79. memcpy(addr, sta->sta.addr, ETH_ALEN);
  80. ocb_dbg(sdata, "Adding new IBSS station %pM (dev=%s)\n",
  81. addr, sdata->name);
  82. sta_info_move_state(sta, IEEE80211_STA_AUTH);
  83. sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  84. sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  85. rate_control_rate_init(&sta->deflink);
  86. /* If it fails, maybe we raced another insertion? */
  87. if (sta_info_insert_rcu(sta))
  88. return sta_info_get(sdata, addr);
  89. return sta;
  90. }
  91. static void ieee80211_ocb_housekeeping(struct ieee80211_sub_if_data *sdata)
  92. {
  93. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  94. ocb_dbg(sdata, "Running ocb housekeeping\n");
  95. ieee80211_sta_expire(sdata, IEEE80211_OCB_PEER_INACTIVITY_LIMIT);
  96. mod_timer(&ifocb->housekeeping_timer,
  97. round_jiffies(jiffies + IEEE80211_OCB_HOUSEKEEPING_INTERVAL));
  98. }
  99. void ieee80211_ocb_work(struct ieee80211_sub_if_data *sdata)
  100. {
  101. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  102. struct sta_info *sta;
  103. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  104. if (ifocb->joined != true)
  105. return;
  106. spin_lock_bh(&ifocb->incomplete_lock);
  107. while (!list_empty(&ifocb->incomplete_stations)) {
  108. sta = list_first_entry(&ifocb->incomplete_stations,
  109. struct sta_info, list);
  110. list_del(&sta->list);
  111. spin_unlock_bh(&ifocb->incomplete_lock);
  112. ieee80211_ocb_finish_sta(sta);
  113. rcu_read_unlock();
  114. spin_lock_bh(&ifocb->incomplete_lock);
  115. }
  116. spin_unlock_bh(&ifocb->incomplete_lock);
  117. if (test_and_clear_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags))
  118. ieee80211_ocb_housekeeping(sdata);
  119. }
  120. static void ieee80211_ocb_housekeeping_timer(struct timer_list *t)
  121. {
  122. struct ieee80211_sub_if_data *sdata =
  123. timer_container_of(sdata, t, u.ocb.housekeeping_timer);
  124. struct ieee80211_local *local = sdata->local;
  125. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  126. set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
  127. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  128. }
  129. void ieee80211_ocb_setup_sdata(struct ieee80211_sub_if_data *sdata)
  130. {
  131. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  132. timer_setup(&ifocb->housekeeping_timer,
  133. ieee80211_ocb_housekeeping_timer, 0);
  134. INIT_LIST_HEAD(&ifocb->incomplete_stations);
  135. spin_lock_init(&ifocb->incomplete_lock);
  136. }
  137. int ieee80211_ocb_join(struct ieee80211_sub_if_data *sdata,
  138. struct ocb_setup *setup)
  139. {
  140. struct ieee80211_chan_req chanreq = { .oper = setup->chandef };
  141. struct ieee80211_local *local = sdata->local;
  142. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  143. u64 changed = BSS_CHANGED_OCB | BSS_CHANGED_BSSID;
  144. int err;
  145. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  146. if (ifocb->joined == true)
  147. return -EINVAL;
  148. sdata->deflink.operating_11g_mode = true;
  149. sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
  150. sdata->deflink.needed_rx_chains = sdata->local->rx_chains;
  151. err = ieee80211_link_use_channel(&sdata->deflink, &chanreq,
  152. IEEE80211_CHANCTX_SHARED);
  153. if (err)
  154. return err;
  155. ieee80211_bss_info_change_notify(sdata, changed);
  156. ifocb->joined = true;
  157. set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
  158. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  159. netif_carrier_on(sdata->dev);
  160. return 0;
  161. }
  162. int ieee80211_ocb_leave(struct ieee80211_sub_if_data *sdata)
  163. {
  164. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  165. struct ieee80211_local *local = sdata->local;
  166. struct sta_info *sta;
  167. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  168. ifocb->joined = false;
  169. sta_info_flush(sdata, -1);
  170. spin_lock_bh(&ifocb->incomplete_lock);
  171. while (!list_empty(&ifocb->incomplete_stations)) {
  172. sta = list_first_entry(&ifocb->incomplete_stations,
  173. struct sta_info, list);
  174. list_del(&sta->list);
  175. spin_unlock_bh(&ifocb->incomplete_lock);
  176. sta_info_free(local, sta);
  177. spin_lock_bh(&ifocb->incomplete_lock);
  178. }
  179. spin_unlock_bh(&ifocb->incomplete_lock);
  180. netif_carrier_off(sdata->dev);
  181. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  182. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_OCB);
  183. ieee80211_link_release_channel(&sdata->deflink);
  184. skb_queue_purge(&sdata->skb_queue);
  185. timer_delete_sync(&sdata->u.ocb.housekeeping_timer);
  186. /* If the timer fired while we waited for it, it will have
  187. * requeued the work. Now the work will be running again
  188. * but will not rearm the timer again because it checks
  189. * whether we are connected to the network or not -- at this
  190. * point we shouldn't be anymore.
  191. */
  192. return 0;
  193. }