sta_info.c 26 KB

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