sta_info.c 25 KB

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