ocb.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 - 2023 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. /* XXX: Consider removing the least recently used entry and
  45. * allow new one to be added.
  46. */
  47. if (local->num_sta >= IEEE80211_OCB_MAX_STA_ENTRIES) {
  48. net_info_ratelimited("%s: No room for a new OCB STA entry %pM\n",
  49. sdata->name, addr);
  50. return;
  51. }
  52. ocb_dbg(sdata, "Adding new OCB station %pM\n", addr);
  53. rcu_read_lock();
  54. chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
  55. if (WARN_ON_ONCE(!chanctx_conf)) {
  56. rcu_read_unlock();
  57. return;
  58. }
  59. band = chanctx_conf->def.chan->band;
  60. rcu_read_unlock();
  61. sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
  62. if (!sta)
  63. return;
  64. /* Add only mandatory rates for now */
  65. sband = local->hw.wiphy->bands[band];
  66. sta->sta.deflink.supp_rates[band] = ieee80211_mandatory_rates(sband);
  67. spin_lock(&ifocb->incomplete_lock);
  68. list_add(&sta->list, &ifocb->incomplete_stations);
  69. spin_unlock(&ifocb->incomplete_lock);
  70. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  71. }
  72. static struct sta_info *ieee80211_ocb_finish_sta(struct sta_info *sta)
  73. __acquires(RCU)
  74. {
  75. struct ieee80211_sub_if_data *sdata = sta->sdata;
  76. u8 addr[ETH_ALEN];
  77. memcpy(addr, sta->sta.addr, ETH_ALEN);
  78. ocb_dbg(sdata, "Adding new IBSS station %pM (dev=%s)\n",
  79. addr, sdata->name);
  80. sta_info_move_state(sta, IEEE80211_STA_AUTH);
  81. sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  82. sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  83. rate_control_rate_init(sta);
  84. /* If it fails, maybe we raced another insertion? */
  85. if (sta_info_insert_rcu(sta))
  86. return sta_info_get(sdata, addr);
  87. return sta;
  88. }
  89. static void ieee80211_ocb_housekeeping(struct ieee80211_sub_if_data *sdata)
  90. {
  91. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  92. ocb_dbg(sdata, "Running ocb housekeeping\n");
  93. ieee80211_sta_expire(sdata, IEEE80211_OCB_PEER_INACTIVITY_LIMIT);
  94. mod_timer(&ifocb->housekeeping_timer,
  95. round_jiffies(jiffies + IEEE80211_OCB_HOUSEKEEPING_INTERVAL));
  96. }
  97. void ieee80211_ocb_work(struct ieee80211_sub_if_data *sdata)
  98. {
  99. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  100. struct sta_info *sta;
  101. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  102. if (ifocb->joined != true)
  103. return;
  104. spin_lock_bh(&ifocb->incomplete_lock);
  105. while (!list_empty(&ifocb->incomplete_stations)) {
  106. sta = list_first_entry(&ifocb->incomplete_stations,
  107. struct sta_info, list);
  108. list_del(&sta->list);
  109. spin_unlock_bh(&ifocb->incomplete_lock);
  110. ieee80211_ocb_finish_sta(sta);
  111. rcu_read_unlock();
  112. spin_lock_bh(&ifocb->incomplete_lock);
  113. }
  114. spin_unlock_bh(&ifocb->incomplete_lock);
  115. if (test_and_clear_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags))
  116. ieee80211_ocb_housekeeping(sdata);
  117. }
  118. static void ieee80211_ocb_housekeeping_timer(struct timer_list *t)
  119. {
  120. struct ieee80211_sub_if_data *sdata =
  121. from_timer(sdata, t, u.ocb.housekeeping_timer);
  122. struct ieee80211_local *local = sdata->local;
  123. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  124. set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
  125. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  126. }
  127. void ieee80211_ocb_setup_sdata(struct ieee80211_sub_if_data *sdata)
  128. {
  129. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  130. timer_setup(&ifocb->housekeeping_timer,
  131. ieee80211_ocb_housekeeping_timer, 0);
  132. INIT_LIST_HEAD(&ifocb->incomplete_stations);
  133. spin_lock_init(&ifocb->incomplete_lock);
  134. }
  135. int ieee80211_ocb_join(struct ieee80211_sub_if_data *sdata,
  136. struct ocb_setup *setup)
  137. {
  138. struct ieee80211_chan_req chanreq = { .oper = setup->chandef };
  139. struct ieee80211_local *local = sdata->local;
  140. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  141. u64 changed = BSS_CHANGED_OCB | BSS_CHANGED_BSSID;
  142. int err;
  143. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  144. if (ifocb->joined == true)
  145. return -EINVAL;
  146. sdata->deflink.operating_11g_mode = true;
  147. sdata->deflink.smps_mode = IEEE80211_SMPS_OFF;
  148. sdata->deflink.needed_rx_chains = sdata->local->rx_chains;
  149. err = ieee80211_link_use_channel(&sdata->deflink, &chanreq,
  150. IEEE80211_CHANCTX_SHARED);
  151. if (err)
  152. return err;
  153. ieee80211_bss_info_change_notify(sdata, changed);
  154. ifocb->joined = true;
  155. set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
  156. wiphy_work_queue(local->hw.wiphy, &sdata->work);
  157. netif_carrier_on(sdata->dev);
  158. return 0;
  159. }
  160. int ieee80211_ocb_leave(struct ieee80211_sub_if_data *sdata)
  161. {
  162. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  163. struct ieee80211_local *local = sdata->local;
  164. struct sta_info *sta;
  165. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  166. ifocb->joined = false;
  167. sta_info_flush(sdata, -1);
  168. spin_lock_bh(&ifocb->incomplete_lock);
  169. while (!list_empty(&ifocb->incomplete_stations)) {
  170. sta = list_first_entry(&ifocb->incomplete_stations,
  171. struct sta_info, list);
  172. list_del(&sta->list);
  173. spin_unlock_bh(&ifocb->incomplete_lock);
  174. sta_info_free(local, sta);
  175. spin_lock_bh(&ifocb->incomplete_lock);
  176. }
  177. spin_unlock_bh(&ifocb->incomplete_lock);
  178. netif_carrier_off(sdata->dev);
  179. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  180. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_OCB);
  181. ieee80211_link_release_channel(&sdata->deflink);
  182. skb_queue_purge(&sdata->skb_queue);
  183. del_timer_sync(&sdata->u.ocb.housekeeping_timer);
  184. /* If the timer fired while we waited for it, it will have
  185. * requeued the work. Now the work will be running again
  186. * but will not rearm the timer again because it checks
  187. * whether we are connected to the network or not -- at this
  188. * point we shouldn't be anymore.
  189. */
  190. return 0;
  191. }