ocb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /* Add only mandatory rates for now */
  69. sband = local->hw.wiphy->bands[band];
  70. sta->sta.supp_rates[band] =
  71. ieee80211_mandatory_rates(sband, scan_width);
  72. spin_lock(&ifocb->incomplete_lock);
  73. list_add(&sta->list, &ifocb->incomplete_stations);
  74. spin_unlock(&ifocb->incomplete_lock);
  75. ieee80211_queue_work(&local->hw, &sdata->work);
  76. }
  77. static struct sta_info *ieee80211_ocb_finish_sta(struct sta_info *sta)
  78. __acquires(RCU)
  79. {
  80. struct ieee80211_sub_if_data *sdata = sta->sdata;
  81. u8 addr[ETH_ALEN];
  82. memcpy(addr, sta->sta.addr, ETH_ALEN);
  83. ocb_dbg(sdata, "Adding new IBSS station %pM (dev=%s)\n",
  84. addr, sdata->name);
  85. sta_info_move_state(sta, IEEE80211_STA_AUTH);
  86. sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  87. sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  88. rate_control_rate_init(sta);
  89. /* If it fails, maybe we raced another insertion? */
  90. if (sta_info_insert_rcu(sta))
  91. return sta_info_get(sdata, addr);
  92. return sta;
  93. }
  94. static void ieee80211_ocb_housekeeping(struct ieee80211_sub_if_data *sdata)
  95. {
  96. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  97. ocb_dbg(sdata, "Running ocb housekeeping\n");
  98. ieee80211_sta_expire(sdata, IEEE80211_OCB_PEER_INACTIVITY_LIMIT);
  99. mod_timer(&ifocb->housekeeping_timer,
  100. round_jiffies(jiffies + IEEE80211_OCB_HOUSEKEEPING_INTERVAL));
  101. }
  102. void ieee80211_ocb_work(struct ieee80211_sub_if_data *sdata)
  103. {
  104. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  105. struct sta_info *sta;
  106. if (ifocb->joined != true)
  107. return;
  108. sdata_lock(sdata);
  109. spin_lock_bh(&ifocb->incomplete_lock);
  110. while (!list_empty(&ifocb->incomplete_stations)) {
  111. sta = list_first_entry(&ifocb->incomplete_stations,
  112. struct sta_info, list);
  113. list_del(&sta->list);
  114. spin_unlock_bh(&ifocb->incomplete_lock);
  115. ieee80211_ocb_finish_sta(sta);
  116. rcu_read_unlock();
  117. spin_lock_bh(&ifocb->incomplete_lock);
  118. }
  119. spin_unlock_bh(&ifocb->incomplete_lock);
  120. if (test_and_clear_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags))
  121. ieee80211_ocb_housekeeping(sdata);
  122. sdata_unlock(sdata);
  123. }
  124. static void ieee80211_ocb_housekeeping_timer(struct timer_list *t)
  125. {
  126. struct ieee80211_sub_if_data *sdata =
  127. from_timer(sdata, t, u.ocb.housekeeping_timer);
  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. timer_setup(&ifocb->housekeeping_timer,
  137. ieee80211_ocb_housekeeping_timer, 0);
  138. INIT_LIST_HEAD(&ifocb->incomplete_stations);
  139. spin_lock_init(&ifocb->incomplete_lock);
  140. }
  141. int ieee80211_ocb_join(struct ieee80211_sub_if_data *sdata,
  142. struct ocb_setup *setup)
  143. {
  144. struct ieee80211_local *local = sdata->local;
  145. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  146. u32 changed = BSS_CHANGED_OCB | BSS_CHANGED_BSSID;
  147. int err;
  148. if (ifocb->joined == true)
  149. return -EINVAL;
  150. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  151. sdata->smps_mode = IEEE80211_SMPS_OFF;
  152. sdata->needed_rx_chains = sdata->local->rx_chains;
  153. mutex_lock(&sdata->local->mtx);
  154. err = ieee80211_vif_use_channel(sdata, &setup->chandef,
  155. IEEE80211_CHANCTX_SHARED);
  156. mutex_unlock(&sdata->local->mtx);
  157. if (err)
  158. return err;
  159. ieee80211_bss_info_change_notify(sdata, changed);
  160. ifocb->joined = true;
  161. set_bit(OCB_WORK_HOUSEKEEPING, &ifocb->wrkq_flags);
  162. ieee80211_queue_work(&local->hw, &sdata->work);
  163. netif_carrier_on(sdata->dev);
  164. return 0;
  165. }
  166. int ieee80211_ocb_leave(struct ieee80211_sub_if_data *sdata)
  167. {
  168. struct ieee80211_if_ocb *ifocb = &sdata->u.ocb;
  169. struct ieee80211_local *local = sdata->local;
  170. struct sta_info *sta;
  171. ifocb->joined = false;
  172. sta_info_flush(sdata);
  173. spin_lock_bh(&ifocb->incomplete_lock);
  174. while (!list_empty(&ifocb->incomplete_stations)) {
  175. sta = list_first_entry(&ifocb->incomplete_stations,
  176. struct sta_info, list);
  177. list_del(&sta->list);
  178. spin_unlock_bh(&ifocb->incomplete_lock);
  179. sta_info_free(local, sta);
  180. spin_lock_bh(&ifocb->incomplete_lock);
  181. }
  182. spin_unlock_bh(&ifocb->incomplete_lock);
  183. netif_carrier_off(sdata->dev);
  184. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  185. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_OCB);
  186. mutex_lock(&sdata->local->mtx);
  187. ieee80211_vif_release_channel(sdata);
  188. mutex_unlock(&sdata->local->mtx);
  189. skb_queue_purge(&sdata->skb_queue);
  190. del_timer_sync(&sdata->u.ocb.housekeeping_timer);
  191. /* If the timer fired while we waited for it, it will have
  192. * requeued the work. Now the work will be running again
  193. * but will not rearm the timer again because it checks
  194. * whether we are connected to the network or not -- at this
  195. * point we shouldn't be anymore.
  196. */
  197. return 0;
  198. }