sta_info.c 26 KB

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