sta_info.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/timer.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include "ieee80211_i.h"
  20. #include "driver-ops.h"
  21. #include "rate.h"
  22. #include "sta_info.h"
  23. #include "debugfs_sta.h"
  24. #include "mesh.h"
  25. /**
  26. * DOC: STA information lifetime rules
  27. *
  28. * STA info structures (&struct sta_info) are managed in a hash table
  29. * for faster lookup and a list for iteration. They are managed using
  30. * RCU, i.e. access to the list and hash table is protected by RCU.
  31. *
  32. * Upon allocating a STA info structure with sta_info_alloc(), the caller owns
  33. * that structure. It must then either destroy it using sta_info_destroy()
  34. * (which is pretty useless) or insert it into the hash table using
  35. * sta_info_insert() which demotes the reference from ownership to a regular
  36. * RCU-protected reference; if the function is called without protection by an
  37. * RCU critical section the reference is instantly invalidated. Note that the
  38. * caller may not do much with the STA info before inserting it, in particular,
  39. * it may not start any mesh peer link management or add encryption keys.
  40. *
  41. * When the insertion fails (sta_info_insert()) returns non-zero), the
  42. * structure will have been freed by sta_info_insert()!
  43. *
  44. * sta entries are added by mac80211 when you establish a link with a
  45. * peer. This means different things for the different type of interfaces
  46. * we support. For a regular station this mean we add the AP sta when we
  47. * receive an assocation response from the AP. For IBSS this occurs when
  48. * we receive a probe response or a beacon from target IBSS network. For
  49. * WDS we add the sta for the peer imediately upon device open. When using
  50. * AP mode we add stations for each respective station upon request from
  51. * userspace through nl80211.
  52. *
  53. * Because there are debugfs entries for each station, and adding those
  54. * must be able to sleep, it is also possible to "pin" a station entry,
  55. * that means it can be removed from the hash table but not be freed.
  56. * See the comment in __sta_info_unlink() for more information, this is
  57. * an internal capability only.
  58. *
  59. * In order to remove a STA info structure, the caller needs to first
  60. * unlink it (sta_info_unlink()) from the list and hash tables and
  61. * then destroy it; sta_info_destroy() will wait for an RCU grace period
  62. * to elapse before actually freeing it. Due to the pinning and the
  63. * possibility of multiple callers trying to remove the same STA info at
  64. * the same time, sta_info_unlink() can clear the STA info pointer it is
  65. * passed to indicate that the STA info is owned by somebody else now.
  66. *
  67. * If sta_info_unlink() did not clear the pointer then the caller owns
  68. * the STA info structure now and is responsible of destroying it with
  69. * a call to sta_info_destroy().
  70. *
  71. * In all other cases, there is no concept of ownership on a STA entry,
  72. * each structure is owned by the global hash table/list until it is
  73. * removed. All users of the structure need to be RCU protected so that
  74. * the structure won't be freed before they are done using it.
  75. */
  76. /* Caller must hold local->sta_lock */
  77. static int sta_info_hash_del(struct ieee80211_local *local,
  78. struct sta_info *sta)
  79. {
  80. struct sta_info *s;
  81. s = local->sta_hash[STA_HASH(sta->sta.addr)];
  82. if (!s)
  83. return -ENOENT;
  84. if (s == sta) {
  85. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  86. s->hnext);
  87. return 0;
  88. }
  89. while (s->hnext && s->hnext != sta)
  90. s = s->hnext;
  91. if (s->hnext) {
  92. rcu_assign_pointer(s->hnext, sta->hnext);
  93. return 0;
  94. }
  95. return -ENOENT;
  96. }
  97. /* protected by RCU */
  98. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  99. const u8 *addr)
  100. {
  101. struct ieee80211_local *local = sdata->local;
  102. struct sta_info *sta;
  103. sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
  104. while (sta) {
  105. if (sta->sdata == sdata &&
  106. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  107. break;
  108. sta = rcu_dereference(sta->hnext);
  109. }
  110. return sta;
  111. }
  112. /*
  113. * Get sta info either from the specified interface
  114. * or from one of its vlans
  115. */
  116. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  117. const u8 *addr)
  118. {
  119. struct ieee80211_local *local = sdata->local;
  120. struct sta_info *sta;
  121. sta = rcu_dereference(local->sta_hash[STA_HASH(addr)]);
  122. while (sta) {
  123. if ((sta->sdata == sdata ||
  124. sta->sdata->bss == sdata->bss) &&
  125. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  126. break;
  127. sta = rcu_dereference(sta->hnext);
  128. }
  129. return sta;
  130. }
  131. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  132. int idx)
  133. {
  134. struct ieee80211_local *local = sdata->local;
  135. struct sta_info *sta;
  136. int i = 0;
  137. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  138. if (sdata != sta->sdata)
  139. continue;
  140. if (i < idx) {
  141. ++i;
  142. continue;
  143. }
  144. return sta;
  145. }
  146. return NULL;
  147. }
  148. /**
  149. * __sta_info_free - internal STA free helper
  150. *
  151. * @local: pointer to the global information
  152. * @sta: STA info to free
  153. *
  154. * This function must undo everything done by sta_info_alloc()
  155. * that may happen before sta_info_insert().
  156. */
  157. static void __sta_info_free(struct ieee80211_local *local,
  158. struct sta_info *sta)
  159. {
  160. if (sta->rate_ctrl) {
  161. rate_control_free_sta(sta);
  162. rate_control_put(sta->rate_ctrl);
  163. }
  164. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  165. printk(KERN_DEBUG "%s: Destroyed STA %pM\n",
  166. wiphy_name(local->hw.wiphy), sta->sta.addr);
  167. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  168. kfree(sta);
  169. }
  170. void sta_info_destroy(struct sta_info *sta)
  171. {
  172. struct ieee80211_local *local;
  173. struct sk_buff *skb;
  174. int i;
  175. might_sleep();
  176. if (!sta)
  177. return;
  178. local = sta->local;
  179. cancel_work_sync(&sta->drv_unblock_wk);
  180. rate_control_remove_sta_debugfs(sta);
  181. ieee80211_sta_debugfs_remove(sta);
  182. #ifdef CONFIG_MAC80211_MESH
  183. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  184. mesh_plink_deactivate(sta);
  185. #endif
  186. /*
  187. * We have only unlinked the key, and actually destroying it
  188. * may mean it is removed from hardware which requires that
  189. * the key->sta pointer is still valid, so flush the key todo
  190. * list here.
  191. *
  192. * ieee80211_key_todo() will synchronize_rcu() so after this
  193. * nothing can reference this sta struct any more.
  194. */
  195. ieee80211_key_todo();
  196. #ifdef CONFIG_MAC80211_MESH
  197. if (ieee80211_vif_is_mesh(&sta->sdata->vif))
  198. del_timer_sync(&sta->plink_timer);
  199. #endif
  200. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  201. local->total_ps_buffered--;
  202. dev_kfree_skb_any(skb);
  203. }
  204. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  205. dev_kfree_skb_any(skb);
  206. for (i = 0; i < STA_TID_NUM; i++) {
  207. struct tid_ampdu_rx *tid_rx;
  208. struct tid_ampdu_tx *tid_tx;
  209. spin_lock_bh(&sta->lock);
  210. tid_rx = sta->ampdu_mlme.tid_rx[i];
  211. /* Make sure timer won't free the tid_rx struct, see below */
  212. if (tid_rx)
  213. tid_rx->shutdown = true;
  214. spin_unlock_bh(&sta->lock);
  215. /*
  216. * Outside spinlock - shutdown is true now so that the timer
  217. * won't free tid_rx, we have to do that now. Can't let the
  218. * timer do it because we have to sync the timer outside the
  219. * lock that it takes itself.
  220. */
  221. if (tid_rx) {
  222. del_timer_sync(&tid_rx->session_timer);
  223. kfree(tid_rx);
  224. }
  225. /*
  226. * No need to do such complications for TX agg sessions, the
  227. * path leading to freeing the tid_tx struct goes via a call
  228. * from the driver, and thus needs to look up the sta struct
  229. * again, which cannot be found when we get here. Hence, we
  230. * just need to delete the timer and free the aggregation
  231. * info; we won't be telling the peer about it then but that
  232. * doesn't matter if we're not talking to it again anyway.
  233. */
  234. tid_tx = sta->ampdu_mlme.tid_tx[i];
  235. if (tid_tx) {
  236. del_timer_sync(&tid_tx->addba_resp_timer);
  237. /*
  238. * STA removed while aggregation session being
  239. * started? Bit odd, but purge frames anyway.
  240. */
  241. skb_queue_purge(&tid_tx->pending);
  242. kfree(tid_tx);
  243. }
  244. }
  245. __sta_info_free(local, sta);
  246. }
  247. /* Caller must hold local->sta_lock */
  248. static void sta_info_hash_add(struct ieee80211_local *local,
  249. struct sta_info *sta)
  250. {
  251. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  252. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  253. }
  254. static void sta_unblock(struct work_struct *wk)
  255. {
  256. struct sta_info *sta;
  257. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  258. if (sta->dead)
  259. return;
  260. if (!test_sta_flags(sta, WLAN_STA_PS_STA))
  261. ieee80211_sta_ps_deliver_wakeup(sta);
  262. else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL))
  263. ieee80211_sta_ps_deliver_poll_response(sta);
  264. }
  265. static int sta_prepare_rate_control(struct ieee80211_local *local,
  266. struct sta_info *sta, gfp_t gfp)
  267. {
  268. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  269. return 0;
  270. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  271. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  272. &sta->sta, gfp);
  273. if (!sta->rate_ctrl_priv) {
  274. rate_control_put(sta->rate_ctrl);
  275. return -ENOMEM;
  276. }
  277. return 0;
  278. }
  279. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  280. u8 *addr, gfp_t gfp)
  281. {
  282. struct ieee80211_local *local = sdata->local;
  283. struct sta_info *sta;
  284. int i;
  285. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  286. if (!sta)
  287. return NULL;
  288. spin_lock_init(&sta->lock);
  289. spin_lock_init(&sta->flaglock);
  290. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  291. memcpy(sta->sta.addr, addr, ETH_ALEN);
  292. sta->local = local;
  293. sta->sdata = sdata;
  294. if (sta_prepare_rate_control(local, sta, gfp)) {
  295. kfree(sta);
  296. return NULL;
  297. }
  298. for (i = 0; i < STA_TID_NUM; i++) {
  299. /* timer_to_tid must be initialized with identity mapping to
  300. * enable session_timer's data differentiation. refer to
  301. * sta_rx_agg_session_timer_expired for useage */
  302. sta->timer_to_tid[i] = i;
  303. /* rx */
  304. sta->ampdu_mlme.tid_state_rx[i] = HT_AGG_STATE_IDLE;
  305. sta->ampdu_mlme.tid_rx[i] = NULL;
  306. /* tx */
  307. sta->ampdu_mlme.tid_state_tx[i] = HT_AGG_STATE_IDLE;
  308. sta->ampdu_mlme.tid_tx[i] = NULL;
  309. sta->ampdu_mlme.addba_req_num[i] = 0;
  310. }
  311. skb_queue_head_init(&sta->ps_tx_buf);
  312. skb_queue_head_init(&sta->tx_filtered);
  313. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  314. sta->last_seq_ctrl[i] = cpu_to_le16(USHORT_MAX);
  315. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  316. printk(KERN_DEBUG "%s: Allocated STA %pM\n",
  317. wiphy_name(local->hw.wiphy), sta->sta.addr);
  318. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  319. #ifdef CONFIG_MAC80211_MESH
  320. sta->plink_state = PLINK_LISTEN;
  321. init_timer(&sta->plink_timer);
  322. #endif
  323. return sta;
  324. }
  325. int sta_info_insert(struct sta_info *sta)
  326. {
  327. struct ieee80211_local *local = sta->local;
  328. struct ieee80211_sub_if_data *sdata = sta->sdata;
  329. struct station_info sinfo;
  330. unsigned long flags;
  331. int err = 0;
  332. /*
  333. * Can't be a WARN_ON because it can be triggered through a race:
  334. * something inserts a STA (on one CPU) without holding the RTNL
  335. * and another CPU turns off the net device.
  336. */
  337. if (unlikely(!ieee80211_sdata_running(sdata))) {
  338. err = -ENETDOWN;
  339. goto out_free;
  340. }
  341. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  342. is_multicast_ether_addr(sta->sta.addr))) {
  343. err = -EINVAL;
  344. goto out_free;
  345. }
  346. spin_lock_irqsave(&local->sta_lock, flags);
  347. /* check if STA exists already */
  348. if (sta_info_get(sdata, sta->sta.addr)) {
  349. spin_unlock_irqrestore(&local->sta_lock, flags);
  350. err = -EEXIST;
  351. goto out_free;
  352. }
  353. list_add(&sta->list, &local->sta_list);
  354. local->sta_generation++;
  355. local->num_sta++;
  356. sta_info_hash_add(local, sta);
  357. /* notify driver */
  358. if (local->ops->sta_notify) {
  359. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  360. sdata = container_of(sdata->bss,
  361. struct ieee80211_sub_if_data,
  362. u.ap);
  363. drv_sta_notify(local, sdata, STA_NOTIFY_ADD, &sta->sta);
  364. sdata = sta->sdata;
  365. }
  366. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  367. printk(KERN_DEBUG "%s: Inserted STA %pM\n",
  368. wiphy_name(local->hw.wiphy), sta->sta.addr);
  369. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  370. spin_unlock_irqrestore(&local->sta_lock, flags);
  371. sinfo.filled = 0;
  372. sinfo.generation = local->sta_generation;
  373. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_ATOMIC);
  374. #ifdef CONFIG_MAC80211_DEBUGFS
  375. /*
  376. * Debugfs entry adding might sleep, so schedule process
  377. * context task for adding entry for STAs that do not yet
  378. * have one.
  379. * NOTE: due to auto-freeing semantics this may only be done
  380. * if the insertion is successful!
  381. */
  382. schedule_work(&local->sta_debugfs_add);
  383. #endif
  384. if (ieee80211_vif_is_mesh(&sdata->vif))
  385. mesh_accept_plinks_update(sdata);
  386. return 0;
  387. out_free:
  388. BUG_ON(!err);
  389. __sta_info_free(local, sta);
  390. return err;
  391. }
  392. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  393. {
  394. /*
  395. * This format has been mandated by the IEEE specifications,
  396. * so this line may not be changed to use the __set_bit() format.
  397. */
  398. bss->tim[aid / 8] |= (1 << (aid % 8));
  399. }
  400. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  401. {
  402. /*
  403. * This format has been mandated by the IEEE specifications,
  404. * so this line may not be changed to use the __clear_bit() format.
  405. */
  406. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  407. }
  408. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  409. struct sta_info *sta)
  410. {
  411. BUG_ON(!bss);
  412. __bss_tim_set(bss, sta->sta.aid);
  413. if (sta->local->ops->set_tim) {
  414. sta->local->tim_in_locked_section = true;
  415. drv_set_tim(sta->local, &sta->sta, true);
  416. sta->local->tim_in_locked_section = false;
  417. }
  418. }
  419. void sta_info_set_tim_bit(struct sta_info *sta)
  420. {
  421. unsigned long flags;
  422. BUG_ON(!sta->sdata->bss);
  423. spin_lock_irqsave(&sta->local->sta_lock, flags);
  424. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  425. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  426. }
  427. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  428. struct sta_info *sta)
  429. {
  430. BUG_ON(!bss);
  431. __bss_tim_clear(bss, sta->sta.aid);
  432. if (sta->local->ops->set_tim) {
  433. sta->local->tim_in_locked_section = true;
  434. drv_set_tim(sta->local, &sta->sta, false);
  435. sta->local->tim_in_locked_section = false;
  436. }
  437. }
  438. void sta_info_clear_tim_bit(struct sta_info *sta)
  439. {
  440. unsigned long flags;
  441. BUG_ON(!sta->sdata->bss);
  442. spin_lock_irqsave(&sta->local->sta_lock, flags);
  443. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  444. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  445. }
  446. static void __sta_info_unlink(struct sta_info **sta)
  447. {
  448. struct ieee80211_local *local = (*sta)->local;
  449. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  450. /*
  451. * pull caller's reference if we're already gone.
  452. */
  453. if (sta_info_hash_del(local, *sta)) {
  454. *sta = NULL;
  455. return;
  456. }
  457. if ((*sta)->key) {
  458. ieee80211_key_free((*sta)->key);
  459. WARN_ON((*sta)->key);
  460. }
  461. list_del(&(*sta)->list);
  462. (*sta)->dead = true;
  463. if (test_and_clear_sta_flags(*sta,
  464. WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
  465. BUG_ON(!sdata->bss);
  466. atomic_dec(&sdata->bss->num_sta_ps);
  467. __sta_info_clear_tim_bit(sdata->bss, *sta);
  468. }
  469. local->num_sta--;
  470. local->sta_generation++;
  471. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  472. rcu_assign_pointer(sdata->u.vlan.sta, NULL);
  473. if (local->ops->sta_notify) {
  474. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  475. sdata = container_of(sdata->bss,
  476. struct ieee80211_sub_if_data,
  477. u.ap);
  478. drv_sta_notify(local, sdata, STA_NOTIFY_REMOVE,
  479. &(*sta)->sta);
  480. sdata = (*sta)->sdata;
  481. }
  482. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  483. mesh_accept_plinks_update(sdata);
  484. #ifdef CONFIG_MAC80211_MESH
  485. del_timer(&(*sta)->plink_timer);
  486. #endif
  487. }
  488. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  489. printk(KERN_DEBUG "%s: Removed STA %pM\n",
  490. wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
  491. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  492. /*
  493. * Finally, pull caller's reference if the STA is pinned by the
  494. * task that is adding the debugfs entries. In that case, we
  495. * leave the STA "to be freed".
  496. *
  497. * The rules are not trivial, but not too complex either:
  498. * (1) pin_status is only modified under the sta_lock
  499. * (2) STAs may only be pinned under the RTNL so that
  500. * sta_info_flush() is guaranteed to actually destroy
  501. * all STAs that are active for a given interface, this
  502. * is required for correctness because otherwise we
  503. * could notify a driver that an interface is going
  504. * away and only after that (!) notify it about a STA
  505. * on that interface going away.
  506. * (3) sta_info_debugfs_add_work() will set the status
  507. * to PINNED when it found an item that needs a new
  508. * debugfs directory created. In that case, that item
  509. * must not be freed although all *RCU* users are done
  510. * with it. Hence, we tell the caller of _unlink()
  511. * that the item is already gone (as can happen when
  512. * two tasks try to unlink/destroy at the same time)
  513. * (4) We set the pin_status to DESTROY here when we
  514. * find such an item.
  515. * (5) sta_info_debugfs_add_work() will reset the pin_status
  516. * from PINNED to NORMAL when it is done with the item,
  517. * but will check for DESTROY before resetting it in
  518. * which case it will free the item.
  519. */
  520. if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
  521. (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
  522. *sta = NULL;
  523. return;
  524. }
  525. }
  526. void sta_info_unlink(struct sta_info **sta)
  527. {
  528. struct ieee80211_local *local = (*sta)->local;
  529. unsigned long flags;
  530. spin_lock_irqsave(&local->sta_lock, flags);
  531. __sta_info_unlink(sta);
  532. spin_unlock_irqrestore(&local->sta_lock, flags);
  533. }
  534. static int sta_info_buffer_expired(struct sta_info *sta,
  535. struct sk_buff *skb)
  536. {
  537. struct ieee80211_tx_info *info;
  538. int timeout;
  539. if (!skb)
  540. return 0;
  541. info = IEEE80211_SKB_CB(skb);
  542. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  543. timeout = (sta->listen_interval *
  544. sta->sdata->vif.bss_conf.beacon_int *
  545. 32 / 15625) * HZ;
  546. if (timeout < STA_TX_BUFFER_EXPIRE)
  547. timeout = STA_TX_BUFFER_EXPIRE;
  548. return time_after(jiffies, info->control.jiffies + timeout);
  549. }
  550. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  551. struct sta_info *sta)
  552. {
  553. unsigned long flags;
  554. struct sk_buff *skb;
  555. struct ieee80211_sub_if_data *sdata;
  556. if (skb_queue_empty(&sta->ps_tx_buf))
  557. return;
  558. for (;;) {
  559. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  560. skb = skb_peek(&sta->ps_tx_buf);
  561. if (sta_info_buffer_expired(sta, skb))
  562. skb = __skb_dequeue(&sta->ps_tx_buf);
  563. else
  564. skb = NULL;
  565. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  566. if (!skb)
  567. break;
  568. sdata = sta->sdata;
  569. local->total_ps_buffered--;
  570. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  571. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  572. sta->sta.addr);
  573. #endif
  574. dev_kfree_skb(skb);
  575. if (skb_queue_empty(&sta->ps_tx_buf))
  576. sta_info_clear_tim_bit(sta);
  577. }
  578. }
  579. static void sta_info_cleanup(unsigned long data)
  580. {
  581. struct ieee80211_local *local = (struct ieee80211_local *) data;
  582. struct sta_info *sta;
  583. rcu_read_lock();
  584. list_for_each_entry_rcu(sta, &local->sta_list, list)
  585. sta_info_cleanup_expire_buffered(local, sta);
  586. rcu_read_unlock();
  587. if (local->quiescing)
  588. return;
  589. local->sta_cleanup.expires =
  590. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  591. add_timer(&local->sta_cleanup);
  592. }
  593. #ifdef CONFIG_MAC80211_DEBUGFS
  594. /*
  595. * See comment in __sta_info_unlink,
  596. * caller must hold local->sta_lock.
  597. */
  598. static void __sta_info_pin(struct sta_info *sta)
  599. {
  600. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
  601. sta->pin_status = STA_INFO_PIN_STAT_PINNED;
  602. }
  603. /*
  604. * See comment in __sta_info_unlink, returns sta if it
  605. * needs to be destroyed.
  606. */
  607. static struct sta_info *__sta_info_unpin(struct sta_info *sta)
  608. {
  609. struct sta_info *ret = NULL;
  610. unsigned long flags;
  611. spin_lock_irqsave(&sta->local->sta_lock, flags);
  612. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
  613. sta->pin_status != STA_INFO_PIN_STAT_PINNED);
  614. if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
  615. ret = sta;
  616. sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
  617. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  618. return ret;
  619. }
  620. static void sta_info_debugfs_add_work(struct work_struct *work)
  621. {
  622. struct ieee80211_local *local =
  623. container_of(work, struct ieee80211_local, sta_debugfs_add);
  624. struct sta_info *sta, *tmp;
  625. unsigned long flags;
  626. /* We need to keep the RTNL across the whole pinned status. */
  627. rtnl_lock();
  628. while (1) {
  629. sta = NULL;
  630. spin_lock_irqsave(&local->sta_lock, flags);
  631. list_for_each_entry(tmp, &local->sta_list, list) {
  632. /*
  633. * debugfs.add_has_run will be set by
  634. * ieee80211_sta_debugfs_add regardless
  635. * of what else it does.
  636. */
  637. if (!tmp->debugfs.add_has_run) {
  638. sta = tmp;
  639. __sta_info_pin(sta);
  640. break;
  641. }
  642. }
  643. spin_unlock_irqrestore(&local->sta_lock, flags);
  644. if (!sta)
  645. break;
  646. ieee80211_sta_debugfs_add(sta);
  647. rate_control_add_sta_debugfs(sta);
  648. sta = __sta_info_unpin(sta);
  649. sta_info_destroy(sta);
  650. }
  651. rtnl_unlock();
  652. }
  653. #endif
  654. void sta_info_init(struct ieee80211_local *local)
  655. {
  656. spin_lock_init(&local->sta_lock);
  657. INIT_LIST_HEAD(&local->sta_list);
  658. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  659. (unsigned long)local);
  660. local->sta_cleanup.expires =
  661. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  662. #ifdef CONFIG_MAC80211_DEBUGFS
  663. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  664. #endif
  665. }
  666. int sta_info_start(struct ieee80211_local *local)
  667. {
  668. add_timer(&local->sta_cleanup);
  669. return 0;
  670. }
  671. void sta_info_stop(struct ieee80211_local *local)
  672. {
  673. del_timer(&local->sta_cleanup);
  674. #ifdef CONFIG_MAC80211_DEBUGFS
  675. /*
  676. * Make sure the debugfs adding work isn't pending after this
  677. * because we're about to be destroyed. It doesn't matter
  678. * whether it ran or not since we're going to flush all STAs
  679. * anyway.
  680. */
  681. cancel_work_sync(&local->sta_debugfs_add);
  682. #endif
  683. sta_info_flush(local, NULL);
  684. }
  685. /**
  686. * sta_info_flush - flush matching STA entries from the STA table
  687. *
  688. * Returns the number of removed STA entries.
  689. *
  690. * @local: local interface data
  691. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  692. */
  693. int sta_info_flush(struct ieee80211_local *local,
  694. struct ieee80211_sub_if_data *sdata)
  695. {
  696. struct sta_info *sta, *tmp;
  697. LIST_HEAD(tmp_list);
  698. int ret = 0;
  699. unsigned long flags;
  700. might_sleep();
  701. spin_lock_irqsave(&local->sta_lock, flags);
  702. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  703. if (!sdata || sdata == sta->sdata) {
  704. __sta_info_unlink(&sta);
  705. if (sta) {
  706. list_add_tail(&sta->list, &tmp_list);
  707. ret++;
  708. }
  709. }
  710. }
  711. spin_unlock_irqrestore(&local->sta_lock, flags);
  712. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  713. sta_info_destroy(sta);
  714. return ret;
  715. }
  716. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  717. unsigned long exp_time)
  718. {
  719. struct ieee80211_local *local = sdata->local;
  720. struct sta_info *sta, *tmp;
  721. LIST_HEAD(tmp_list);
  722. unsigned long flags;
  723. spin_lock_irqsave(&local->sta_lock, flags);
  724. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  725. if (time_after(jiffies, sta->last_rx + exp_time)) {
  726. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  727. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  728. sdata->name, sta->sta.addr);
  729. #endif
  730. __sta_info_unlink(&sta);
  731. if (sta)
  732. list_add(&sta->list, &tmp_list);
  733. }
  734. spin_unlock_irqrestore(&local->sta_lock, flags);
  735. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  736. sta_info_destroy(sta);
  737. }
  738. struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
  739. const u8 *addr)
  740. {
  741. struct sta_info *sta, *nxt;
  742. /* Just return a random station ... first in list ... */
  743. for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
  744. return &sta->sta;
  745. return NULL;
  746. }
  747. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
  748. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  749. const u8 *addr)
  750. {
  751. struct ieee80211_sub_if_data *sdata;
  752. if (!vif)
  753. return NULL;
  754. sdata = vif_to_sdata(vif);
  755. return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
  756. }
  757. EXPORT_SYMBOL(ieee80211_find_sta);
  758. /* powersave support code */
  759. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  760. {
  761. struct ieee80211_sub_if_data *sdata = sta->sdata;
  762. struct ieee80211_local *local = sdata->local;
  763. int sent, buffered;
  764. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  765. if (!skb_queue_empty(&sta->ps_tx_buf))
  766. sta_info_clear_tim_bit(sta);
  767. /* Send all buffered frames to the station */
  768. sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
  769. buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
  770. sent += buffered;
  771. local->total_ps_buffered -= buffered;
  772. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  773. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  774. "since STA not sleeping anymore\n", sdata->name,
  775. sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
  776. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  777. }
  778. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  779. {
  780. struct ieee80211_sub_if_data *sdata = sta->sdata;
  781. struct ieee80211_local *local = sdata->local;
  782. struct sk_buff *skb;
  783. int no_pending_pkts;
  784. skb = skb_dequeue(&sta->tx_filtered);
  785. if (!skb) {
  786. skb = skb_dequeue(&sta->ps_tx_buf);
  787. if (skb)
  788. local->total_ps_buffered--;
  789. }
  790. no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
  791. skb_queue_empty(&sta->ps_tx_buf);
  792. if (skb) {
  793. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  794. struct ieee80211_hdr *hdr =
  795. (struct ieee80211_hdr *) skb->data;
  796. /*
  797. * Tell TX path to send this frame even though the STA may
  798. * still remain is PS mode after this frame exchange.
  799. */
  800. info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
  801. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  802. printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
  803. sta->sta.addr, sta->sta.aid,
  804. skb_queue_len(&sta->ps_tx_buf));
  805. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  806. /* Use MoreData flag to indicate whether there are more
  807. * buffered frames for this STA */
  808. if (no_pending_pkts)
  809. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  810. else
  811. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  812. ieee80211_add_pending_skb(local, skb);
  813. if (no_pending_pkts)
  814. sta_info_clear_tim_bit(sta);
  815. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  816. } else {
  817. /*
  818. * FIXME: This can be the result of a race condition between
  819. * us expiring a frame and the station polling for it.
  820. * Should we send it a null-func frame indicating we
  821. * have nothing buffered for it?
  822. */
  823. printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
  824. "though there are no buffered frames for it\n",
  825. sdata->name, sta->sta.addr);
  826. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  827. }
  828. }
  829. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  830. struct ieee80211_sta *pubsta, bool block)
  831. {
  832. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  833. if (block)
  834. set_sta_flags(sta, WLAN_STA_PS_DRIVER);
  835. else
  836. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  837. }
  838. EXPORT_SYMBOL(ieee80211_sta_block_awake);