sta_info.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/timer.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include "ieee80211_i.h"
  20. #include "driver-ops.h"
  21. #include "rate.h"
  22. #include "sta_info.h"
  23. #include "debugfs_sta.h"
  24. #include "mesh.h"
  25. /**
  26. * DOC: STA information lifetime rules
  27. *
  28. * STA info structures (&struct sta_info) are managed in a hash table
  29. * for faster lookup and a list for iteration. They are managed using
  30. * RCU, i.e. access to the list and hash table is protected by RCU.
  31. *
  32. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  33. * owns that structure. It must then insert it into the hash table using
  34. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  35. * case (which acquires an rcu read section but must not be called from
  36. * within one) will the pointer still be valid after the call. Note that
  37. * the caller may not do much with the STA info before inserting it, in
  38. * particular, it may not start any mesh peer link management or add
  39. * encryption keys.
  40. *
  41. * When the insertion fails (sta_info_insert()) returns non-zero), the
  42. * structure will have been freed by sta_info_insert()!
  43. *
  44. * Station entries are added by mac80211 when you establish a link with a
  45. * peer. This means different things for the different type of interfaces
  46. * we support. For a regular station this mean we add the AP sta when we
  47. * receive an association response from the AP. For IBSS this occurs when
  48. * get to know about a peer on the same IBSS. For WDS we add the sta for
  49. * the peer immediately upon device open. When using AP mode we add stations
  50. * for each respective station upon request from userspace through nl80211.
  51. *
  52. * In order to remove a STA info structure, various sta_info_destroy_*()
  53. * calls are available.
  54. *
  55. * There is no concept of ownership on a STA entry, each structure is
  56. * owned by the global hash table/list until it is removed. All users of
  57. * the structure need to be RCU protected so that the structure won't be
  58. * freed before they are done using it.
  59. */
  60. /* Caller must hold local->sta_lock */
  61. static int sta_info_hash_del(struct ieee80211_local *local,
  62. struct sta_info *sta)
  63. {
  64. struct sta_info *s;
  65. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  66. lockdep_is_held(&local->sta_lock));
  67. if (!s)
  68. return -ENOENT;
  69. if (s == sta) {
  70. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  71. s->hnext);
  72. return 0;
  73. }
  74. while (rcu_access_pointer(s->hnext) &&
  75. rcu_access_pointer(s->hnext) != sta)
  76. s = rcu_dereference_protected(s->hnext,
  77. lockdep_is_held(&local->sta_lock));
  78. if (rcu_access_pointer(s->hnext)) {
  79. rcu_assign_pointer(s->hnext, sta->hnext);
  80. return 0;
  81. }
  82. return -ENOENT;
  83. }
  84. /* protected by RCU */
  85. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  86. const u8 *addr)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct sta_info *sta;
  90. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  91. lockdep_is_held(&local->sta_lock) ||
  92. lockdep_is_held(&local->sta_mtx));
  93. while (sta) {
  94. if (sta->sdata == sdata &&
  95. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  96. break;
  97. sta = rcu_dereference_check(sta->hnext,
  98. lockdep_is_held(&local->sta_lock) ||
  99. lockdep_is_held(&local->sta_mtx));
  100. }
  101. return sta;
  102. }
  103. /*
  104. * Get sta info either from the specified interface
  105. * or from one of its vlans
  106. */
  107. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  108. const u8 *addr)
  109. {
  110. struct ieee80211_local *local = sdata->local;
  111. struct sta_info *sta;
  112. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  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. lockdep_is_held(&local->sta_lock) ||
  122. lockdep_is_held(&local->sta_mtx));
  123. }
  124. return sta;
  125. }
  126. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  127. int idx)
  128. {
  129. struct ieee80211_local *local = sdata->local;
  130. struct sta_info *sta;
  131. int i = 0;
  132. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  133. if (sdata != sta->sdata)
  134. continue;
  135. if (i < idx) {
  136. ++i;
  137. continue;
  138. }
  139. return sta;
  140. }
  141. return NULL;
  142. }
  143. /**
  144. * __sta_info_free - internal STA free helper
  145. *
  146. * @local: pointer to the global information
  147. * @sta: STA info to free
  148. *
  149. * This function must undo everything done by sta_info_alloc()
  150. * that may happen before sta_info_insert().
  151. */
  152. static void __sta_info_free(struct ieee80211_local *local,
  153. struct sta_info *sta)
  154. {
  155. if (sta->rate_ctrl) {
  156. rate_control_free_sta(sta);
  157. rate_control_put(sta->rate_ctrl);
  158. }
  159. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  160. wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
  161. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  162. kfree(sta);
  163. }
  164. /* Caller must hold local->sta_lock */
  165. static void sta_info_hash_add(struct ieee80211_local *local,
  166. struct sta_info *sta)
  167. {
  168. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  169. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  170. }
  171. static void sta_unblock(struct work_struct *wk)
  172. {
  173. struct sta_info *sta;
  174. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  175. if (sta->dead)
  176. return;
  177. if (!test_sta_flags(sta, WLAN_STA_PS_STA))
  178. ieee80211_sta_ps_deliver_wakeup(sta);
  179. else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
  180. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  181. ieee80211_sta_ps_deliver_poll_response(sta);
  182. } else
  183. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  184. }
  185. static int sta_prepare_rate_control(struct ieee80211_local *local,
  186. struct sta_info *sta, gfp_t gfp)
  187. {
  188. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  189. return 0;
  190. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  191. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  192. &sta->sta, gfp);
  193. if (!sta->rate_ctrl_priv) {
  194. rate_control_put(sta->rate_ctrl);
  195. return -ENOMEM;
  196. }
  197. return 0;
  198. }
  199. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  200. u8 *addr, gfp_t gfp)
  201. {
  202. struct ieee80211_local *local = sdata->local;
  203. struct sta_info *sta;
  204. struct timespec uptime;
  205. int i;
  206. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  207. if (!sta)
  208. return NULL;
  209. spin_lock_init(&sta->lock);
  210. spin_lock_init(&sta->flaglock);
  211. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  212. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  213. mutex_init(&sta->ampdu_mlme.mtx);
  214. memcpy(sta->sta.addr, addr, ETH_ALEN);
  215. sta->local = local;
  216. sta->sdata = sdata;
  217. sta->last_rx = jiffies;
  218. do_posix_clock_monotonic_gettime(&uptime);
  219. sta->last_connected = uptime.tv_sec;
  220. ewma_init(&sta->avg_signal, 1024, 8);
  221. if (sta_prepare_rate_control(local, sta, gfp)) {
  222. kfree(sta);
  223. return NULL;
  224. }
  225. for (i = 0; i < STA_TID_NUM; i++) {
  226. /*
  227. * timer_to_tid must be initialized with identity mapping
  228. * to enable session_timer's data differentiation. See
  229. * sta_rx_agg_session_timer_expired for usage.
  230. */
  231. sta->timer_to_tid[i] = i;
  232. }
  233. skb_queue_head_init(&sta->ps_tx_buf);
  234. skb_queue_head_init(&sta->tx_filtered);
  235. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  236. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  237. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  238. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  239. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  240. #ifdef CONFIG_MAC80211_MESH
  241. sta->plink_state = NL80211_PLINK_LISTEN;
  242. init_timer(&sta->plink_timer);
  243. #endif
  244. return sta;
  245. }
  246. static int sta_info_finish_insert(struct sta_info *sta, bool async)
  247. {
  248. struct ieee80211_local *local = sta->local;
  249. struct ieee80211_sub_if_data *sdata = sta->sdata;
  250. struct station_info sinfo;
  251. unsigned long flags;
  252. int err = 0;
  253. lockdep_assert_held(&local->sta_mtx);
  254. /* notify driver */
  255. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  256. sdata = container_of(sdata->bss,
  257. struct ieee80211_sub_if_data,
  258. u.ap);
  259. err = drv_sta_add(local, sdata, &sta->sta);
  260. if (err) {
  261. if (!async)
  262. return err;
  263. printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to driver (%d)"
  264. " - keeping it anyway.\n",
  265. sdata->name, sta->sta.addr, err);
  266. } else {
  267. sta->uploaded = true;
  268. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  269. if (async)
  270. wiphy_debug(local->hw.wiphy,
  271. "Finished adding IBSS STA %pM\n",
  272. sta->sta.addr);
  273. #endif
  274. }
  275. sdata = sta->sdata;
  276. if (!async) {
  277. local->num_sta++;
  278. local->sta_generation++;
  279. smp_mb();
  280. /* make the station visible */
  281. spin_lock_irqsave(&local->sta_lock, flags);
  282. sta_info_hash_add(local, sta);
  283. spin_unlock_irqrestore(&local->sta_lock, flags);
  284. }
  285. list_add(&sta->list, &local->sta_list);
  286. ieee80211_sta_debugfs_add(sta);
  287. rate_control_add_sta_debugfs(sta);
  288. memset(&sinfo, 0, sizeof(sinfo));
  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. mutex_lock(&local->key_mtx);
  547. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  548. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  549. if (sta->ptk)
  550. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  551. mutex_unlock(&local->key_mtx);
  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. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  584. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  585. cancel_work_sync(&sta->drv_unblock_wk);
  586. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  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. mod_timer(&local->sta_cleanup,
  640. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  641. }
  642. void sta_info_init(struct ieee80211_local *local)
  643. {
  644. spin_lock_init(&local->sta_lock);
  645. mutex_init(&local->sta_mtx);
  646. INIT_LIST_HEAD(&local->sta_list);
  647. INIT_LIST_HEAD(&local->sta_pending_list);
  648. INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
  649. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  650. (unsigned long)local);
  651. }
  652. void sta_info_stop(struct ieee80211_local *local)
  653. {
  654. del_timer(&local->sta_cleanup);
  655. sta_info_flush(local, NULL);
  656. }
  657. /**
  658. * sta_info_flush - flush matching STA entries from the STA table
  659. *
  660. * Returns the number of removed STA entries.
  661. *
  662. * @local: local interface data
  663. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  664. */
  665. int sta_info_flush(struct ieee80211_local *local,
  666. struct ieee80211_sub_if_data *sdata)
  667. {
  668. struct sta_info *sta, *tmp;
  669. int ret = 0;
  670. might_sleep();
  671. mutex_lock(&local->sta_mtx);
  672. sta_info_finish_pending(local);
  673. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  674. if (!sdata || sdata == sta->sdata)
  675. WARN_ON(__sta_info_destroy(sta));
  676. }
  677. mutex_unlock(&local->sta_mtx);
  678. return ret;
  679. }
  680. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  681. unsigned long exp_time)
  682. {
  683. struct ieee80211_local *local = sdata->local;
  684. struct sta_info *sta, *tmp;
  685. mutex_lock(&local->sta_mtx);
  686. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  687. if (time_after(jiffies, sta->last_rx + exp_time)) {
  688. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  689. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  690. sdata->name, sta->sta.addr);
  691. #endif
  692. WARN_ON(__sta_info_destroy(sta));
  693. }
  694. mutex_unlock(&local->sta_mtx);
  695. }
  696. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  697. const u8 *addr,
  698. const u8 *localaddr)
  699. {
  700. struct sta_info *sta, *nxt;
  701. /*
  702. * Just return a random station if localaddr is NULL
  703. * ... first in list.
  704. */
  705. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  706. if (localaddr &&
  707. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  708. continue;
  709. if (!sta->uploaded)
  710. return NULL;
  711. return &sta->sta;
  712. }
  713. return NULL;
  714. }
  715. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  716. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  717. const u8 *addr)
  718. {
  719. struct sta_info *sta;
  720. if (!vif)
  721. return NULL;
  722. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  723. if (!sta)
  724. return NULL;
  725. if (!sta->uploaded)
  726. return NULL;
  727. return &sta->sta;
  728. }
  729. EXPORT_SYMBOL(ieee80211_find_sta);
  730. static void clear_sta_ps_flags(void *_sta)
  731. {
  732. struct sta_info *sta = _sta;
  733. clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
  734. }
  735. /* powersave support code */
  736. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  737. {
  738. struct ieee80211_sub_if_data *sdata = sta->sdata;
  739. struct ieee80211_local *local = sdata->local;
  740. int sent, buffered;
  741. clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  742. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  743. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  744. if (!skb_queue_empty(&sta->ps_tx_buf))
  745. sta_info_clear_tim_bit(sta);
  746. /* Send all buffered frames to the station */
  747. sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
  748. buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
  749. clear_sta_ps_flags, sta);
  750. sent += buffered;
  751. local->total_ps_buffered -= buffered;
  752. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  753. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  754. "since STA not sleeping anymore\n", sdata->name,
  755. sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
  756. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  757. }
  758. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  759. {
  760. struct ieee80211_sub_if_data *sdata = sta->sdata;
  761. struct ieee80211_local *local = sdata->local;
  762. struct sk_buff *skb;
  763. int no_pending_pkts;
  764. skb = skb_dequeue(&sta->tx_filtered);
  765. if (!skb) {
  766. skb = skb_dequeue(&sta->ps_tx_buf);
  767. if (skb)
  768. local->total_ps_buffered--;
  769. }
  770. no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
  771. skb_queue_empty(&sta->ps_tx_buf);
  772. if (skb) {
  773. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  774. struct ieee80211_hdr *hdr =
  775. (struct ieee80211_hdr *) skb->data;
  776. /*
  777. * Tell TX path to send this frame even though the STA may
  778. * still remain is PS mode after this frame exchange.
  779. */
  780. info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
  781. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  782. printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
  783. sta->sta.addr, sta->sta.aid,
  784. skb_queue_len(&sta->ps_tx_buf));
  785. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  786. /* Use MoreData flag to indicate whether there are more
  787. * buffered frames for this STA */
  788. if (no_pending_pkts)
  789. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  790. else
  791. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  792. ieee80211_add_pending_skb(local, skb);
  793. if (no_pending_pkts)
  794. sta_info_clear_tim_bit(sta);
  795. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  796. } else {
  797. /*
  798. * FIXME: This can be the result of a race condition between
  799. * us expiring a frame and the station polling for it.
  800. * Should we send it a null-func frame indicating we
  801. * have nothing buffered for it?
  802. */
  803. printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
  804. "though there are no buffered frames for it\n",
  805. sdata->name, sta->sta.addr);
  806. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  807. }
  808. }
  809. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  810. struct ieee80211_sta *pubsta, bool block)
  811. {
  812. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  813. trace_api_sta_block_awake(sta->local, pubsta, block);
  814. if (block)
  815. set_sta_flags(sta, WLAN_STA_PS_DRIVER);
  816. else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
  817. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  818. }
  819. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  820. void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta)
  821. {
  822. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  823. set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  824. sta_info_set_tim_bit(sta);
  825. }
  826. EXPORT_SYMBOL(ieee80211_sta_set_tim);