sta_info.c 27 KB

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