sta_info.c 26 KB

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