ocb.c 6.8 KB

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