sta_info.c 26 KB

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