sta_info.c 25 KB

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