ocb.c 6.7 KB

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