sta_info.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. unsigned long flags;
  311. int err = 0;
  312. /*
  313. * Can't be a WARN_ON because it can be triggered through a race:
  314. * something inserts a STA (on one CPU) without holding the RTNL
  315. * and another CPU turns off the net device.
  316. */
  317. if (unlikely(!netif_running(sdata->dev))) {
  318. err = -ENETDOWN;
  319. goto out_free;
  320. }
  321. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  322. is_multicast_ether_addr(sta->sta.addr))) {
  323. err = -EINVAL;
  324. goto out_free;
  325. }
  326. spin_lock_irqsave(&local->sta_lock, flags);
  327. /* check if STA exists already */
  328. if (sta_info_get(sdata, sta->sta.addr)) {
  329. spin_unlock_irqrestore(&local->sta_lock, flags);
  330. err = -EEXIST;
  331. goto out_free;
  332. }
  333. list_add(&sta->list, &local->sta_list);
  334. local->sta_generation++;
  335. local->num_sta++;
  336. sta_info_hash_add(local, sta);
  337. /* notify driver */
  338. if (local->ops->sta_notify) {
  339. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  340. sdata = container_of(sdata->bss,
  341. struct ieee80211_sub_if_data,
  342. u.ap);
  343. drv_sta_notify(local, sdata, STA_NOTIFY_ADD, &sta->sta);
  344. sdata = sta->sdata;
  345. }
  346. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  347. printk(KERN_DEBUG "%s: Inserted STA %pM\n",
  348. wiphy_name(local->hw.wiphy), sta->sta.addr);
  349. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  350. spin_unlock_irqrestore(&local->sta_lock, flags);
  351. #ifdef CONFIG_MAC80211_DEBUGFS
  352. /*
  353. * Debugfs entry adding might sleep, so schedule process
  354. * context task for adding entry for STAs that do not yet
  355. * have one.
  356. * NOTE: due to auto-freeing semantics this may only be done
  357. * if the insertion is successful!
  358. */
  359. schedule_work(&local->sta_debugfs_add);
  360. #endif
  361. if (ieee80211_vif_is_mesh(&sdata->vif))
  362. mesh_accept_plinks_update(sdata);
  363. return 0;
  364. out_free:
  365. BUG_ON(!err);
  366. __sta_info_free(local, sta);
  367. return err;
  368. }
  369. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  370. {
  371. /*
  372. * This format has been mandated by the IEEE specifications,
  373. * so this line may not be changed to use the __set_bit() format.
  374. */
  375. bss->tim[aid / 8] |= (1 << (aid % 8));
  376. }
  377. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  378. {
  379. /*
  380. * This format has been mandated by the IEEE specifications,
  381. * so this line may not be changed to use the __clear_bit() format.
  382. */
  383. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  384. }
  385. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  386. struct sta_info *sta)
  387. {
  388. BUG_ON(!bss);
  389. __bss_tim_set(bss, sta->sta.aid);
  390. if (sta->local->ops->set_tim) {
  391. sta->local->tim_in_locked_section = true;
  392. drv_set_tim(sta->local, &sta->sta, true);
  393. sta->local->tim_in_locked_section = false;
  394. }
  395. }
  396. void sta_info_set_tim_bit(struct sta_info *sta)
  397. {
  398. unsigned long flags;
  399. BUG_ON(!sta->sdata->bss);
  400. spin_lock_irqsave(&sta->local->sta_lock, flags);
  401. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  402. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  403. }
  404. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  405. struct sta_info *sta)
  406. {
  407. BUG_ON(!bss);
  408. __bss_tim_clear(bss, sta->sta.aid);
  409. if (sta->local->ops->set_tim) {
  410. sta->local->tim_in_locked_section = true;
  411. drv_set_tim(sta->local, &sta->sta, false);
  412. sta->local->tim_in_locked_section = false;
  413. }
  414. }
  415. void sta_info_clear_tim_bit(struct sta_info *sta)
  416. {
  417. unsigned long flags;
  418. BUG_ON(!sta->sdata->bss);
  419. spin_lock_irqsave(&sta->local->sta_lock, flags);
  420. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  421. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  422. }
  423. static void __sta_info_unlink(struct sta_info **sta)
  424. {
  425. struct ieee80211_local *local = (*sta)->local;
  426. struct ieee80211_sub_if_data *sdata = (*sta)->sdata;
  427. /*
  428. * pull caller's reference if we're already gone.
  429. */
  430. if (sta_info_hash_del(local, *sta)) {
  431. *sta = NULL;
  432. return;
  433. }
  434. if ((*sta)->key) {
  435. ieee80211_key_free((*sta)->key);
  436. WARN_ON((*sta)->key);
  437. }
  438. list_del(&(*sta)->list);
  439. (*sta)->dead = true;
  440. if (test_and_clear_sta_flags(*sta,
  441. WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
  442. BUG_ON(!sdata->bss);
  443. atomic_dec(&sdata->bss->num_sta_ps);
  444. __sta_info_clear_tim_bit(sdata->bss, *sta);
  445. }
  446. local->num_sta--;
  447. local->sta_generation++;
  448. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  449. rcu_assign_pointer(sdata->u.vlan.sta, NULL);
  450. if (local->ops->sta_notify) {
  451. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  452. sdata = container_of(sdata->bss,
  453. struct ieee80211_sub_if_data,
  454. u.ap);
  455. drv_sta_notify(local, sdata, STA_NOTIFY_REMOVE,
  456. &(*sta)->sta);
  457. sdata = (*sta)->sdata;
  458. }
  459. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  460. mesh_accept_plinks_update(sdata);
  461. #ifdef CONFIG_MAC80211_MESH
  462. del_timer(&(*sta)->plink_timer);
  463. #endif
  464. }
  465. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  466. printk(KERN_DEBUG "%s: Removed STA %pM\n",
  467. wiphy_name(local->hw.wiphy), (*sta)->sta.addr);
  468. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  469. /*
  470. * Finally, pull caller's reference if the STA is pinned by the
  471. * task that is adding the debugfs entries. In that case, we
  472. * leave the STA "to be freed".
  473. *
  474. * The rules are not trivial, but not too complex either:
  475. * (1) pin_status is only modified under the sta_lock
  476. * (2) STAs may only be pinned under the RTNL so that
  477. * sta_info_flush() is guaranteed to actually destroy
  478. * all STAs that are active for a given interface, this
  479. * is required for correctness because otherwise we
  480. * could notify a driver that an interface is going
  481. * away and only after that (!) notify it about a STA
  482. * on that interface going away.
  483. * (3) sta_info_debugfs_add_work() will set the status
  484. * to PINNED when it found an item that needs a new
  485. * debugfs directory created. In that case, that item
  486. * must not be freed although all *RCU* users are done
  487. * with it. Hence, we tell the caller of _unlink()
  488. * that the item is already gone (as can happen when
  489. * two tasks try to unlink/destroy at the same time)
  490. * (4) We set the pin_status to DESTROY here when we
  491. * find such an item.
  492. * (5) sta_info_debugfs_add_work() will reset the pin_status
  493. * from PINNED to NORMAL when it is done with the item,
  494. * but will check for DESTROY before resetting it in
  495. * which case it will free the item.
  496. */
  497. if ((*sta)->pin_status == STA_INFO_PIN_STAT_PINNED) {
  498. (*sta)->pin_status = STA_INFO_PIN_STAT_DESTROY;
  499. *sta = NULL;
  500. return;
  501. }
  502. }
  503. void sta_info_unlink(struct sta_info **sta)
  504. {
  505. struct ieee80211_local *local = (*sta)->local;
  506. unsigned long flags;
  507. spin_lock_irqsave(&local->sta_lock, flags);
  508. __sta_info_unlink(sta);
  509. spin_unlock_irqrestore(&local->sta_lock, flags);
  510. }
  511. static int sta_info_buffer_expired(struct sta_info *sta,
  512. struct sk_buff *skb)
  513. {
  514. struct ieee80211_tx_info *info;
  515. int timeout;
  516. if (!skb)
  517. return 0;
  518. info = IEEE80211_SKB_CB(skb);
  519. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  520. timeout = (sta->listen_interval *
  521. sta->sdata->vif.bss_conf.beacon_int *
  522. 32 / 15625) * HZ;
  523. if (timeout < STA_TX_BUFFER_EXPIRE)
  524. timeout = STA_TX_BUFFER_EXPIRE;
  525. return time_after(jiffies, info->control.jiffies + timeout);
  526. }
  527. static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  528. struct sta_info *sta)
  529. {
  530. unsigned long flags;
  531. struct sk_buff *skb;
  532. struct ieee80211_sub_if_data *sdata;
  533. if (skb_queue_empty(&sta->ps_tx_buf))
  534. return;
  535. for (;;) {
  536. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  537. skb = skb_peek(&sta->ps_tx_buf);
  538. if (sta_info_buffer_expired(sta, skb))
  539. skb = __skb_dequeue(&sta->ps_tx_buf);
  540. else
  541. skb = NULL;
  542. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  543. if (!skb)
  544. break;
  545. sdata = sta->sdata;
  546. local->total_ps_buffered--;
  547. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  548. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  549. sta->sta.addr);
  550. #endif
  551. dev_kfree_skb(skb);
  552. if (skb_queue_empty(&sta->ps_tx_buf))
  553. sta_info_clear_tim_bit(sta);
  554. }
  555. }
  556. static void sta_info_cleanup(unsigned long data)
  557. {
  558. struct ieee80211_local *local = (struct ieee80211_local *) data;
  559. struct sta_info *sta;
  560. rcu_read_lock();
  561. list_for_each_entry_rcu(sta, &local->sta_list, list)
  562. sta_info_cleanup_expire_buffered(local, sta);
  563. rcu_read_unlock();
  564. if (local->quiescing)
  565. return;
  566. local->sta_cleanup.expires =
  567. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  568. add_timer(&local->sta_cleanup);
  569. }
  570. #ifdef CONFIG_MAC80211_DEBUGFS
  571. /*
  572. * See comment in __sta_info_unlink,
  573. * caller must hold local->sta_lock.
  574. */
  575. static void __sta_info_pin(struct sta_info *sta)
  576. {
  577. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_NORMAL);
  578. sta->pin_status = STA_INFO_PIN_STAT_PINNED;
  579. }
  580. /*
  581. * See comment in __sta_info_unlink, returns sta if it
  582. * needs to be destroyed.
  583. */
  584. static struct sta_info *__sta_info_unpin(struct sta_info *sta)
  585. {
  586. struct sta_info *ret = NULL;
  587. unsigned long flags;
  588. spin_lock_irqsave(&sta->local->sta_lock, flags);
  589. WARN_ON(sta->pin_status != STA_INFO_PIN_STAT_DESTROY &&
  590. sta->pin_status != STA_INFO_PIN_STAT_PINNED);
  591. if (sta->pin_status == STA_INFO_PIN_STAT_DESTROY)
  592. ret = sta;
  593. sta->pin_status = STA_INFO_PIN_STAT_NORMAL;
  594. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  595. return ret;
  596. }
  597. static void sta_info_debugfs_add_work(struct work_struct *work)
  598. {
  599. struct ieee80211_local *local =
  600. container_of(work, struct ieee80211_local, sta_debugfs_add);
  601. struct sta_info *sta, *tmp;
  602. unsigned long flags;
  603. /* We need to keep the RTNL across the whole pinned status. */
  604. rtnl_lock();
  605. while (1) {
  606. sta = NULL;
  607. spin_lock_irqsave(&local->sta_lock, flags);
  608. list_for_each_entry(tmp, &local->sta_list, list) {
  609. /*
  610. * debugfs.add_has_run will be set by
  611. * ieee80211_sta_debugfs_add regardless
  612. * of what else it does.
  613. */
  614. if (!tmp->debugfs.add_has_run) {
  615. sta = tmp;
  616. __sta_info_pin(sta);
  617. break;
  618. }
  619. }
  620. spin_unlock_irqrestore(&local->sta_lock, flags);
  621. if (!sta)
  622. break;
  623. ieee80211_sta_debugfs_add(sta);
  624. rate_control_add_sta_debugfs(sta);
  625. sta = __sta_info_unpin(sta);
  626. sta_info_destroy(sta);
  627. }
  628. rtnl_unlock();
  629. }
  630. #endif
  631. void sta_info_init(struct ieee80211_local *local)
  632. {
  633. spin_lock_init(&local->sta_lock);
  634. INIT_LIST_HEAD(&local->sta_list);
  635. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  636. (unsigned long)local);
  637. local->sta_cleanup.expires =
  638. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
  639. #ifdef CONFIG_MAC80211_DEBUGFS
  640. INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_work);
  641. #endif
  642. }
  643. int sta_info_start(struct ieee80211_local *local)
  644. {
  645. add_timer(&local->sta_cleanup);
  646. return 0;
  647. }
  648. void sta_info_stop(struct ieee80211_local *local)
  649. {
  650. del_timer(&local->sta_cleanup);
  651. #ifdef CONFIG_MAC80211_DEBUGFS
  652. /*
  653. * Make sure the debugfs adding work isn't pending after this
  654. * because we're about to be destroyed. It doesn't matter
  655. * whether it ran or not since we're going to flush all STAs
  656. * anyway.
  657. */
  658. cancel_work_sync(&local->sta_debugfs_add);
  659. #endif
  660. sta_info_flush(local, NULL);
  661. }
  662. /**
  663. * sta_info_flush - flush matching STA entries from the STA table
  664. *
  665. * Returns the number of removed STA entries.
  666. *
  667. * @local: local interface data
  668. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  669. */
  670. int sta_info_flush(struct ieee80211_local *local,
  671. struct ieee80211_sub_if_data *sdata)
  672. {
  673. struct sta_info *sta, *tmp;
  674. LIST_HEAD(tmp_list);
  675. int ret = 0;
  676. unsigned long flags;
  677. might_sleep();
  678. spin_lock_irqsave(&local->sta_lock, flags);
  679. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  680. if (!sdata || sdata == sta->sdata) {
  681. __sta_info_unlink(&sta);
  682. if (sta) {
  683. list_add_tail(&sta->list, &tmp_list);
  684. ret++;
  685. }
  686. }
  687. }
  688. spin_unlock_irqrestore(&local->sta_lock, flags);
  689. list_for_each_entry_safe(sta, tmp, &tmp_list, list)
  690. sta_info_destroy(sta);
  691. return ret;
  692. }
  693. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  694. unsigned long exp_time)
  695. {
  696. struct ieee80211_local *local = sdata->local;
  697. struct sta_info *sta, *tmp;
  698. LIST_HEAD(tmp_list);
  699. unsigned long flags;
  700. spin_lock_irqsave(&local->sta_lock, flags);
  701. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  702. if (time_after(jiffies, sta->last_rx + exp_time)) {
  703. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  704. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  705. sdata->name, sta->sta.addr);
  706. #endif
  707. __sta_info_unlink(&sta);
  708. if (sta)
  709. list_add(&sta->list, &tmp_list);
  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. }
  715. struct ieee80211_sta *ieee80211_find_sta_by_hw(struct ieee80211_hw *hw,
  716. const u8 *addr)
  717. {
  718. struct sta_info *sta, *nxt;
  719. /* Just return a random station ... first in list ... */
  720. for_each_sta_info(hw_to_local(hw), addr, sta, nxt)
  721. return &sta->sta;
  722. return NULL;
  723. }
  724. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_hw);
  725. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  726. const u8 *addr)
  727. {
  728. struct ieee80211_sub_if_data *sdata;
  729. if (!vif)
  730. return NULL;
  731. sdata = vif_to_sdata(vif);
  732. return ieee80211_find_sta_by_hw(&sdata->local->hw, addr);
  733. }
  734. EXPORT_SYMBOL(ieee80211_find_sta);
  735. /* powersave support code */
  736. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  737. {
  738. struct ieee80211_sub_if_data *sdata = sta->sdata;
  739. struct ieee80211_local *local = sdata->local;
  740. int sent, buffered;
  741. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  742. if (!skb_queue_empty(&sta->ps_tx_buf))
  743. sta_info_clear_tim_bit(sta);
  744. /* Send all buffered frames to the station */
  745. sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
  746. buffered = ieee80211_add_pending_skbs(local, &sta->ps_tx_buf);
  747. sent += buffered;
  748. local->total_ps_buffered -= buffered;
  749. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  750. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  751. "since STA not sleeping anymore\n", sdata->name,
  752. sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
  753. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  754. }
  755. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  756. {
  757. struct ieee80211_sub_if_data *sdata = sta->sdata;
  758. struct ieee80211_local *local = sdata->local;
  759. struct sk_buff *skb;
  760. int no_pending_pkts;
  761. skb = skb_dequeue(&sta->tx_filtered);
  762. if (!skb) {
  763. skb = skb_dequeue(&sta->ps_tx_buf);
  764. if (skb)
  765. local->total_ps_buffered--;
  766. }
  767. no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
  768. skb_queue_empty(&sta->ps_tx_buf);
  769. if (skb) {
  770. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  771. struct ieee80211_hdr *hdr =
  772. (struct ieee80211_hdr *) skb->data;
  773. /*
  774. * Tell TX path to send this frame even though the STA may
  775. * still remain is PS mode after this frame exchange.
  776. */
  777. info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
  778. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  779. printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
  780. sta->sta.addr, sta->sta.aid,
  781. skb_queue_len(&sta->ps_tx_buf));
  782. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  783. /* Use MoreData flag to indicate whether there are more
  784. * buffered frames for this STA */
  785. if (no_pending_pkts)
  786. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  787. else
  788. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  789. ieee80211_add_pending_skb(local, skb);
  790. if (no_pending_pkts)
  791. sta_info_clear_tim_bit(sta);
  792. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  793. } else {
  794. /*
  795. * FIXME: This can be the result of a race condition between
  796. * us expiring a frame and the station polling for it.
  797. * Should we send it a null-func frame indicating we
  798. * have nothing buffered for it?
  799. */
  800. printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
  801. "though there are no buffered frames for it\n",
  802. sdata->name, sta->sta.addr);
  803. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  804. }
  805. }
  806. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  807. struct ieee80211_sta *pubsta, bool block)
  808. {
  809. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  810. if (block)
  811. set_sta_flags(sta, WLAN_STA_PS_DRIVER);
  812. else
  813. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  814. }
  815. EXPORT_SYMBOL(ieee80211_sta_block_awake);