sta_info.c 26 KB

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