sta_info.c 26 KB

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