sta_info.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. #include "wme.h"
  26. /**
  27. * DOC: STA information lifetime rules
  28. *
  29. * STA info structures (&struct sta_info) are managed in a hash table
  30. * for faster lookup and a list for iteration. They are managed using
  31. * RCU, i.e. access to the list and hash table is protected by RCU.
  32. *
  33. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  34. * owns that structure. It must then insert it into the hash table using
  35. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  36. * case (which acquires an rcu read section but must not be called from
  37. * within one) will the pointer still be valid after the call. Note that
  38. * the caller may not do much with the STA info before inserting it, in
  39. * particular, it may not start any mesh peer link management or add
  40. * encryption keys.
  41. *
  42. * When the insertion fails (sta_info_insert()) returns non-zero), the
  43. * structure will have been freed by sta_info_insert()!
  44. *
  45. * Station entries are added by mac80211 when you establish a link with a
  46. * peer. This means different things for the different type of interfaces
  47. * we support. For a regular station this mean we add the AP sta when we
  48. * receive an association response from the AP. For IBSS this occurs when
  49. * get to know about a peer on the same IBSS. For WDS we add the sta for
  50. * the peer immediately upon device open. When using AP mode we add stations
  51. * for each respective station upon request from userspace through nl80211.
  52. *
  53. * In order to remove a STA info structure, various sta_info_destroy_*()
  54. * calls are available.
  55. *
  56. * There is no concept of ownership on a STA entry, each structure is
  57. * owned by the global hash table/list until it is removed. All users of
  58. * the structure need to be RCU protected so that the structure won't be
  59. * freed before they are done using it.
  60. */
  61. /* Caller must hold local->sta_mtx */
  62. static int sta_info_hash_del(struct ieee80211_local *local,
  63. struct sta_info *sta)
  64. {
  65. struct sta_info *s;
  66. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  67. lockdep_is_held(&local->sta_mtx));
  68. if (!s)
  69. return -ENOENT;
  70. if (s == sta) {
  71. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  72. s->hnext);
  73. return 0;
  74. }
  75. while (rcu_access_pointer(s->hnext) &&
  76. rcu_access_pointer(s->hnext) != sta)
  77. s = rcu_dereference_protected(s->hnext,
  78. lockdep_is_held(&local->sta_mtx));
  79. if (rcu_access_pointer(s->hnext)) {
  80. rcu_assign_pointer(s->hnext, sta->hnext);
  81. return 0;
  82. }
  83. return -ENOENT;
  84. }
  85. /* protected by RCU */
  86. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  87. const u8 *addr)
  88. {
  89. struct ieee80211_local *local = sdata->local;
  90. struct sta_info *sta;
  91. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  92. lockdep_is_held(&local->sta_mtx));
  93. while (sta) {
  94. if (sta->sdata == sdata && !sta->dummy &&
  95. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  96. break;
  97. sta = rcu_dereference_check(sta->hnext,
  98. lockdep_is_held(&local->sta_mtx));
  99. }
  100. return sta;
  101. }
  102. /* get a station info entry even if it is a dummy station*/
  103. struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
  104. const u8 *addr)
  105. {
  106. struct ieee80211_local *local = sdata->local;
  107. struct sta_info *sta;
  108. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  109. lockdep_is_held(&local->sta_mtx));
  110. while (sta) {
  111. if (sta->sdata == sdata &&
  112. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  113. break;
  114. sta = rcu_dereference_check(sta->hnext,
  115. lockdep_is_held(&local->sta_mtx));
  116. }
  117. return sta;
  118. }
  119. /*
  120. * Get sta info either from the specified interface
  121. * or from one of its vlans
  122. */
  123. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  124. const u8 *addr)
  125. {
  126. struct ieee80211_local *local = sdata->local;
  127. struct sta_info *sta;
  128. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  129. lockdep_is_held(&local->sta_mtx));
  130. while (sta) {
  131. if ((sta->sdata == sdata ||
  132. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  133. !sta->dummy &&
  134. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  135. break;
  136. sta = rcu_dereference_check(sta->hnext,
  137. lockdep_is_held(&local->sta_mtx));
  138. }
  139. return sta;
  140. }
  141. /*
  142. * Get sta info either from the specified interface
  143. * or from one of its vlans (including dummy stations)
  144. */
  145. struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
  146. const u8 *addr)
  147. {
  148. struct ieee80211_local *local = sdata->local;
  149. struct sta_info *sta;
  150. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  151. lockdep_is_held(&local->sta_mtx));
  152. while (sta) {
  153. if ((sta->sdata == sdata ||
  154. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  155. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  156. break;
  157. sta = rcu_dereference_check(sta->hnext,
  158. lockdep_is_held(&local->sta_mtx));
  159. }
  160. return sta;
  161. }
  162. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  163. int idx)
  164. {
  165. struct ieee80211_local *local = sdata->local;
  166. struct sta_info *sta;
  167. int i = 0;
  168. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  169. if (sdata != sta->sdata)
  170. continue;
  171. if (i < idx) {
  172. ++i;
  173. continue;
  174. }
  175. return sta;
  176. }
  177. return NULL;
  178. }
  179. /**
  180. * sta_info_free - free STA
  181. *
  182. * @local: pointer to the global information
  183. * @sta: STA info to free
  184. *
  185. * This function must undo everything done by sta_info_alloc()
  186. * that may happen before sta_info_insert(). It may only be
  187. * called when sta_info_insert() has not been attempted (and
  188. * if that fails, the station is freed anyway.)
  189. */
  190. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  191. {
  192. if (sta->rate_ctrl)
  193. rate_control_free_sta(sta);
  194. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  195. wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
  196. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  197. kfree(sta);
  198. }
  199. /* Caller must hold local->sta_mtx */
  200. static void sta_info_hash_add(struct ieee80211_local *local,
  201. struct sta_info *sta)
  202. {
  203. lockdep_assert_held(&local->sta_mtx);
  204. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  205. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  206. }
  207. static void sta_unblock(struct work_struct *wk)
  208. {
  209. struct sta_info *sta;
  210. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  211. if (sta->dead)
  212. return;
  213. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  214. local_bh_disable();
  215. ieee80211_sta_ps_deliver_wakeup(sta);
  216. local_bh_enable();
  217. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  218. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  219. local_bh_disable();
  220. ieee80211_sta_ps_deliver_poll_response(sta);
  221. local_bh_enable();
  222. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  223. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  224. local_bh_disable();
  225. ieee80211_sta_ps_deliver_uapsd(sta);
  226. local_bh_enable();
  227. } else
  228. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  229. }
  230. static int sta_prepare_rate_control(struct ieee80211_local *local,
  231. struct sta_info *sta, gfp_t gfp)
  232. {
  233. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  234. return 0;
  235. sta->rate_ctrl = local->rate_ctrl;
  236. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  237. &sta->sta, gfp);
  238. if (!sta->rate_ctrl_priv)
  239. return -ENOMEM;
  240. return 0;
  241. }
  242. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  243. const u8 *addr, gfp_t gfp)
  244. {
  245. struct ieee80211_local *local = sdata->local;
  246. struct sta_info *sta;
  247. struct timespec uptime;
  248. int i;
  249. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  250. if (!sta)
  251. return NULL;
  252. spin_lock_init(&sta->lock);
  253. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  254. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  255. mutex_init(&sta->ampdu_mlme.mtx);
  256. memcpy(sta->sta.addr, addr, ETH_ALEN);
  257. sta->local = local;
  258. sta->sdata = sdata;
  259. sta->last_rx = jiffies;
  260. do_posix_clock_monotonic_gettime(&uptime);
  261. sta->last_connected = uptime.tv_sec;
  262. ewma_init(&sta->avg_signal, 1024, 8);
  263. if (sta_prepare_rate_control(local, sta, gfp)) {
  264. kfree(sta);
  265. return NULL;
  266. }
  267. for (i = 0; i < STA_TID_NUM; i++) {
  268. /*
  269. * timer_to_tid must be initialized with identity mapping
  270. * to enable session_timer's data differentiation. See
  271. * sta_rx_agg_session_timer_expired for usage.
  272. */
  273. sta->timer_to_tid[i] = i;
  274. }
  275. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  276. skb_queue_head_init(&sta->ps_tx_buf[i]);
  277. skb_queue_head_init(&sta->tx_filtered[i]);
  278. }
  279. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  280. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  281. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  282. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  283. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  284. #ifdef CONFIG_MAC80211_MESH
  285. sta->plink_state = NL80211_PLINK_LISTEN;
  286. init_timer(&sta->plink_timer);
  287. #endif
  288. return sta;
  289. }
  290. static int sta_info_insert_check(struct sta_info *sta)
  291. {
  292. struct ieee80211_sub_if_data *sdata = sta->sdata;
  293. /*
  294. * Can't be a WARN_ON because it can be triggered through a race:
  295. * something inserts a STA (on one CPU) without holding the RTNL
  296. * and another CPU turns off the net device.
  297. */
  298. if (unlikely(!ieee80211_sdata_running(sdata)))
  299. return -ENETDOWN;
  300. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  301. is_multicast_ether_addr(sta->sta.addr)))
  302. return -EINVAL;
  303. return 0;
  304. }
  305. /*
  306. * should be called with sta_mtx locked
  307. * this function replaces the mutex lock
  308. * with a RCU lock
  309. */
  310. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  311. {
  312. struct ieee80211_local *local = sta->local;
  313. struct ieee80211_sub_if_data *sdata = sta->sdata;
  314. struct sta_info *exist_sta;
  315. bool dummy_reinsert = false;
  316. int err = 0;
  317. lockdep_assert_held(&local->sta_mtx);
  318. /*
  319. * check if STA exists already.
  320. * only accept a scenario of a second call to sta_info_insert_finish
  321. * with a dummy station entry that was inserted earlier
  322. * in that case - assume that the dummy station flag should
  323. * be removed.
  324. */
  325. exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
  326. if (exist_sta) {
  327. if (exist_sta == sta && sta->dummy) {
  328. dummy_reinsert = true;
  329. } else {
  330. err = -EEXIST;
  331. goto out_err;
  332. }
  333. }
  334. if (!sta->dummy || dummy_reinsert) {
  335. /* notify driver */
  336. err = drv_sta_add(local, sdata, &sta->sta);
  337. if (err) {
  338. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  339. goto out_err;
  340. printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
  341. "driver (%d) - keeping it anyway.\n",
  342. sdata->name, sta->sta.addr, err);
  343. } else
  344. sta->uploaded = true;
  345. }
  346. if (!dummy_reinsert) {
  347. local->num_sta++;
  348. local->sta_generation++;
  349. smp_mb();
  350. /* make the station visible */
  351. sta_info_hash_add(local, sta);
  352. list_add(&sta->list, &local->sta_list);
  353. set_sta_flag(sta, WLAN_STA_INSERTED);
  354. } else {
  355. sta->dummy = false;
  356. }
  357. if (!sta->dummy) {
  358. struct station_info sinfo;
  359. ieee80211_sta_debugfs_add(sta);
  360. rate_control_add_sta_debugfs(sta);
  361. memset(&sinfo, 0, sizeof(sinfo));
  362. sinfo.filled = 0;
  363. sinfo.generation = local->sta_generation;
  364. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  365. }
  366. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  367. wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
  368. sta->dummy ? "dummy " : "", sta->sta.addr);
  369. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  370. /* move reference to rcu-protected */
  371. rcu_read_lock();
  372. mutex_unlock(&local->sta_mtx);
  373. if (ieee80211_vif_is_mesh(&sdata->vif))
  374. mesh_accept_plinks_update(sdata);
  375. return 0;
  376. out_err:
  377. mutex_unlock(&local->sta_mtx);
  378. rcu_read_lock();
  379. return err;
  380. }
  381. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  382. {
  383. struct ieee80211_local *local = sta->local;
  384. int err = 0;
  385. might_sleep();
  386. err = sta_info_insert_check(sta);
  387. if (err) {
  388. rcu_read_lock();
  389. goto out_free;
  390. }
  391. mutex_lock(&local->sta_mtx);
  392. err = sta_info_insert_finish(sta);
  393. if (err)
  394. goto out_free;
  395. return 0;
  396. out_free:
  397. BUG_ON(!err);
  398. sta_info_free(local, sta);
  399. return err;
  400. }
  401. int sta_info_insert(struct sta_info *sta)
  402. {
  403. int err = sta_info_insert_rcu(sta);
  404. rcu_read_unlock();
  405. return err;
  406. }
  407. /* Caller must hold sta->local->sta_mtx */
  408. int sta_info_reinsert(struct sta_info *sta)
  409. {
  410. struct ieee80211_local *local = sta->local;
  411. int err = 0;
  412. err = sta_info_insert_check(sta);
  413. if (err) {
  414. mutex_unlock(&local->sta_mtx);
  415. return err;
  416. }
  417. might_sleep();
  418. err = sta_info_insert_finish(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 unsigned long ieee80211_tids_for_ac(int ac)
  439. {
  440. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  441. switch (ac) {
  442. case IEEE80211_AC_VO:
  443. return BIT(6) | BIT(7);
  444. case IEEE80211_AC_VI:
  445. return BIT(4) | BIT(5);
  446. case IEEE80211_AC_BE:
  447. return BIT(0) | BIT(3);
  448. case IEEE80211_AC_BK:
  449. return BIT(1) | BIT(2);
  450. default:
  451. WARN_ON(1);
  452. return 0;
  453. }
  454. }
  455. void sta_info_recalc_tim(struct sta_info *sta)
  456. {
  457. struct ieee80211_local *local = sta->local;
  458. struct ieee80211_if_ap *bss = sta->sdata->bss;
  459. unsigned long flags;
  460. bool indicate_tim = false;
  461. u8 ignore_for_tim = sta->sta.uapsd_queues;
  462. int ac;
  463. if (WARN_ON_ONCE(!sta->sdata->bss))
  464. return;
  465. /* No need to do anything if the driver does all */
  466. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  467. return;
  468. if (sta->dead)
  469. goto done;
  470. /*
  471. * If all ACs are delivery-enabled then we should build
  472. * the TIM bit for all ACs anyway; if only some are then
  473. * we ignore those and build the TIM bit using only the
  474. * non-enabled ones.
  475. */
  476. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  477. ignore_for_tim = 0;
  478. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  479. unsigned long tids;
  480. if (ignore_for_tim & BIT(ac))
  481. continue;
  482. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  483. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  484. if (indicate_tim)
  485. break;
  486. tids = ieee80211_tids_for_ac(ac);
  487. indicate_tim |=
  488. sta->driver_buffered_tids & tids;
  489. }
  490. done:
  491. spin_lock_irqsave(&local->tim_lock, flags);
  492. if (indicate_tim)
  493. __bss_tim_set(bss, sta->sta.aid);
  494. else
  495. __bss_tim_clear(bss, sta->sta.aid);
  496. if (local->ops->set_tim) {
  497. local->tim_in_locked_section = true;
  498. drv_set_tim(local, &sta->sta, indicate_tim);
  499. local->tim_in_locked_section = false;
  500. }
  501. spin_unlock_irqrestore(&local->tim_lock, flags);
  502. }
  503. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  504. {
  505. struct ieee80211_tx_info *info;
  506. int timeout;
  507. if (!skb)
  508. return false;
  509. info = IEEE80211_SKB_CB(skb);
  510. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  511. timeout = (sta->listen_interval *
  512. sta->sdata->vif.bss_conf.beacon_int *
  513. 32 / 15625) * HZ;
  514. if (timeout < STA_TX_BUFFER_EXPIRE)
  515. timeout = STA_TX_BUFFER_EXPIRE;
  516. return time_after(jiffies, info->control.jiffies + timeout);
  517. }
  518. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  519. struct sta_info *sta, int ac)
  520. {
  521. unsigned long flags;
  522. struct sk_buff *skb;
  523. /*
  524. * First check for frames that should expire on the filtered
  525. * queue. Frames here were rejected by the driver and are on
  526. * a separate queue to avoid reordering with normal PS-buffered
  527. * frames. They also aren't accounted for right now in the
  528. * total_ps_buffered counter.
  529. */
  530. for (;;) {
  531. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  532. skb = skb_peek(&sta->tx_filtered[ac]);
  533. if (sta_info_buffer_expired(sta, skb))
  534. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  535. else
  536. skb = NULL;
  537. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  538. /*
  539. * Frames are queued in order, so if this one
  540. * hasn't expired yet we can stop testing. If
  541. * we actually reached the end of the queue we
  542. * also need to stop, of course.
  543. */
  544. if (!skb)
  545. break;
  546. dev_kfree_skb(skb);
  547. }
  548. /*
  549. * Now also check the normal PS-buffered queue, this will
  550. * only find something if the filtered queue was emptied
  551. * since the filtered frames are all before the normal PS
  552. * buffered frames.
  553. */
  554. for (;;) {
  555. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  556. skb = skb_peek(&sta->ps_tx_buf[ac]);
  557. if (sta_info_buffer_expired(sta, skb))
  558. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  559. else
  560. skb = NULL;
  561. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  562. /*
  563. * frames are queued in order, so if this one
  564. * hasn't expired yet (or we reached the end of
  565. * the queue) we can stop testing
  566. */
  567. if (!skb)
  568. break;
  569. local->total_ps_buffered--;
  570. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  571. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  572. sta->sta.addr);
  573. #endif
  574. dev_kfree_skb(skb);
  575. }
  576. /*
  577. * Finally, recalculate the TIM bit for this station -- it might
  578. * now be clear because the station was too slow to retrieve its
  579. * frames.
  580. */
  581. sta_info_recalc_tim(sta);
  582. /*
  583. * Return whether there are any frames still buffered, this is
  584. * used to check whether the cleanup timer still needs to run,
  585. * if there are no frames we don't need to rearm the timer.
  586. */
  587. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  588. skb_queue_empty(&sta->tx_filtered[ac]));
  589. }
  590. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  591. struct sta_info *sta)
  592. {
  593. bool have_buffered = false;
  594. int ac;
  595. /* This is only necessary for stations on BSS interfaces */
  596. if (!sta->sdata->bss)
  597. return false;
  598. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  599. have_buffered |=
  600. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  601. return have_buffered;
  602. }
  603. int __must_check __sta_info_destroy(struct sta_info *sta)
  604. {
  605. struct ieee80211_local *local;
  606. struct ieee80211_sub_if_data *sdata;
  607. int ret, i, ac;
  608. struct tid_ampdu_tx *tid_tx;
  609. might_sleep();
  610. if (!sta)
  611. return -ENOENT;
  612. local = sta->local;
  613. sdata = sta->sdata;
  614. lockdep_assert_held(&local->sta_mtx);
  615. /*
  616. * Before removing the station from the driver and
  617. * rate control, it might still start new aggregation
  618. * sessions -- block that to make sure the tear-down
  619. * will be sufficient.
  620. */
  621. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  622. ieee80211_sta_tear_down_BA_sessions(sta, true);
  623. ret = sta_info_hash_del(local, sta);
  624. if (ret)
  625. return ret;
  626. list_del(&sta->list);
  627. mutex_lock(&local->key_mtx);
  628. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  629. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  630. if (sta->ptk)
  631. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  632. mutex_unlock(&local->key_mtx);
  633. sta->dead = true;
  634. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  635. BUG_ON(!sdata->bss);
  636. clear_sta_flag(sta, WLAN_STA_PS_STA);
  637. atomic_dec(&sdata->bss->num_sta_ps);
  638. sta_info_recalc_tim(sta);
  639. }
  640. local->num_sta--;
  641. local->sta_generation++;
  642. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  643. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  644. while (sta->sta_state > IEEE80211_STA_NONE) {
  645. int err = sta_info_move_state(sta, sta->sta_state - 1);
  646. if (err) {
  647. WARN_ON_ONCE(1);
  648. break;
  649. }
  650. }
  651. if (sta->uploaded) {
  652. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  653. sdata = container_of(sdata->bss,
  654. struct ieee80211_sub_if_data,
  655. u.ap);
  656. drv_sta_remove(local, sdata, &sta->sta);
  657. sdata = sta->sdata;
  658. }
  659. /*
  660. * At this point, after we wait for an RCU grace period,
  661. * neither mac80211 nor the driver can reference this
  662. * sta struct any more except by still existing timers
  663. * associated with this station that we clean up below.
  664. */
  665. synchronize_rcu();
  666. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  667. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  668. __skb_queue_purge(&sta->ps_tx_buf[ac]);
  669. __skb_queue_purge(&sta->tx_filtered[ac]);
  670. }
  671. #ifdef CONFIG_MAC80211_MESH
  672. if (ieee80211_vif_is_mesh(&sdata->vif))
  673. mesh_accept_plinks_update(sdata);
  674. #endif
  675. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  676. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  677. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  678. cancel_work_sync(&sta->drv_unblock_wk);
  679. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  680. rate_control_remove_sta_debugfs(sta);
  681. ieee80211_sta_debugfs_remove(sta);
  682. #ifdef CONFIG_MAC80211_MESH
  683. if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  684. mesh_plink_deactivate(sta);
  685. del_timer_sync(&sta->plink_timer);
  686. }
  687. #endif
  688. /*
  689. * Destroy aggregation state here. It would be nice to wait for the
  690. * driver to finish aggregation stop and then clean up, but for now
  691. * drivers have to handle aggregation stop being requested, followed
  692. * directly by station destruction.
  693. */
  694. for (i = 0; i < STA_TID_NUM; i++) {
  695. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  696. if (!tid_tx)
  697. continue;
  698. __skb_queue_purge(&tid_tx->pending);
  699. kfree(tid_tx);
  700. }
  701. sta_info_free(local, sta);
  702. return 0;
  703. }
  704. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  705. {
  706. struct sta_info *sta;
  707. int ret;
  708. mutex_lock(&sdata->local->sta_mtx);
  709. sta = sta_info_get_rx(sdata, addr);
  710. ret = __sta_info_destroy(sta);
  711. mutex_unlock(&sdata->local->sta_mtx);
  712. return ret;
  713. }
  714. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  715. const u8 *addr)
  716. {
  717. struct sta_info *sta;
  718. int ret;
  719. mutex_lock(&sdata->local->sta_mtx);
  720. sta = sta_info_get_bss_rx(sdata, addr);
  721. ret = __sta_info_destroy(sta);
  722. mutex_unlock(&sdata->local->sta_mtx);
  723. return ret;
  724. }
  725. static void sta_info_cleanup(unsigned long data)
  726. {
  727. struct ieee80211_local *local = (struct ieee80211_local *) data;
  728. struct sta_info *sta;
  729. bool timer_needed = false;
  730. rcu_read_lock();
  731. list_for_each_entry_rcu(sta, &local->sta_list, list)
  732. if (sta_info_cleanup_expire_buffered(local, sta))
  733. timer_needed = true;
  734. rcu_read_unlock();
  735. if (local->quiescing)
  736. return;
  737. if (!timer_needed)
  738. return;
  739. mod_timer(&local->sta_cleanup,
  740. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  741. }
  742. void sta_info_init(struct ieee80211_local *local)
  743. {
  744. spin_lock_init(&local->tim_lock);
  745. mutex_init(&local->sta_mtx);
  746. INIT_LIST_HEAD(&local->sta_list);
  747. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  748. (unsigned long)local);
  749. }
  750. void sta_info_stop(struct ieee80211_local *local)
  751. {
  752. del_timer(&local->sta_cleanup);
  753. sta_info_flush(local, NULL);
  754. }
  755. /**
  756. * sta_info_flush - flush matching STA entries from the STA table
  757. *
  758. * Returns the number of removed STA entries.
  759. *
  760. * @local: local interface data
  761. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  762. */
  763. int sta_info_flush(struct ieee80211_local *local,
  764. struct ieee80211_sub_if_data *sdata)
  765. {
  766. struct sta_info *sta, *tmp;
  767. int ret = 0;
  768. might_sleep();
  769. mutex_lock(&local->sta_mtx);
  770. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  771. if (!sdata || sdata == sta->sdata)
  772. WARN_ON(__sta_info_destroy(sta));
  773. }
  774. mutex_unlock(&local->sta_mtx);
  775. return ret;
  776. }
  777. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  778. unsigned long exp_time)
  779. {
  780. struct ieee80211_local *local = sdata->local;
  781. struct sta_info *sta, *tmp;
  782. mutex_lock(&local->sta_mtx);
  783. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  784. if (sdata != sta->sdata)
  785. continue;
  786. if (time_after(jiffies, sta->last_rx + exp_time)) {
  787. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  788. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  789. sdata->name, sta->sta.addr);
  790. #endif
  791. WARN_ON(__sta_info_destroy(sta));
  792. }
  793. }
  794. mutex_unlock(&local->sta_mtx);
  795. }
  796. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  797. const u8 *addr,
  798. const u8 *localaddr)
  799. {
  800. struct sta_info *sta, *nxt;
  801. /*
  802. * Just return a random station if localaddr is NULL
  803. * ... first in list.
  804. */
  805. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  806. if (localaddr &&
  807. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  808. continue;
  809. if (!sta->uploaded)
  810. return NULL;
  811. return &sta->sta;
  812. }
  813. return NULL;
  814. }
  815. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  816. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  817. const u8 *addr)
  818. {
  819. struct sta_info *sta;
  820. if (!vif)
  821. return NULL;
  822. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  823. if (!sta)
  824. return NULL;
  825. if (!sta->uploaded)
  826. return NULL;
  827. return &sta->sta;
  828. }
  829. EXPORT_SYMBOL(ieee80211_find_sta);
  830. static void clear_sta_ps_flags(void *_sta)
  831. {
  832. struct sta_info *sta = _sta;
  833. struct ieee80211_sub_if_data *sdata = sta->sdata;
  834. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  835. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  836. atomic_dec(&sdata->bss->num_sta_ps);
  837. }
  838. /* powersave support code */
  839. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  840. {
  841. struct ieee80211_sub_if_data *sdata = sta->sdata;
  842. struct ieee80211_local *local = sdata->local;
  843. struct sk_buff_head pending;
  844. int filtered = 0, buffered = 0, ac;
  845. clear_sta_flag(sta, WLAN_STA_SP);
  846. BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
  847. sta->driver_buffered_tids = 0;
  848. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  849. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  850. skb_queue_head_init(&pending);
  851. /* Send all buffered frames to the station */
  852. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  853. int count = skb_queue_len(&pending), tmp;
  854. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  855. tmp = skb_queue_len(&pending);
  856. filtered += tmp - count;
  857. count = tmp;
  858. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  859. tmp = skb_queue_len(&pending);
  860. buffered += tmp - count;
  861. }
  862. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  863. local->total_ps_buffered -= buffered;
  864. sta_info_recalc_tim(sta);
  865. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  866. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  867. "since STA not sleeping anymore\n", sdata->name,
  868. sta->sta.addr, sta->sta.aid, filtered, buffered);
  869. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  870. }
  871. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  872. struct sta_info *sta, int tid,
  873. enum ieee80211_frame_release_type reason)
  874. {
  875. struct ieee80211_local *local = sdata->local;
  876. struct ieee80211_qos_hdr *nullfunc;
  877. struct sk_buff *skb;
  878. int size = sizeof(*nullfunc);
  879. __le16 fc;
  880. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  881. struct ieee80211_tx_info *info;
  882. if (qos) {
  883. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  884. IEEE80211_STYPE_QOS_NULLFUNC |
  885. IEEE80211_FCTL_FROMDS);
  886. } else {
  887. size -= 2;
  888. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  889. IEEE80211_STYPE_NULLFUNC |
  890. IEEE80211_FCTL_FROMDS);
  891. }
  892. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  893. if (!skb)
  894. return;
  895. skb_reserve(skb, local->hw.extra_tx_headroom);
  896. nullfunc = (void *) skb_put(skb, size);
  897. nullfunc->frame_control = fc;
  898. nullfunc->duration_id = 0;
  899. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  900. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  901. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  902. skb->priority = tid;
  903. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  904. if (qos) {
  905. nullfunc->qos_ctrl = cpu_to_le16(tid);
  906. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  907. nullfunc->qos_ctrl |=
  908. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  909. }
  910. info = IEEE80211_SKB_CB(skb);
  911. /*
  912. * Tell TX path to send this frame even though the
  913. * STA may still remain is PS mode after this frame
  914. * exchange. Also set EOSP to indicate this packet
  915. * ends the poll/service period.
  916. */
  917. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
  918. IEEE80211_TX_STATUS_EOSP |
  919. IEEE80211_TX_CTL_REQ_TX_STATUS;
  920. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  921. ieee80211_xmit(sdata, skb);
  922. }
  923. static void
  924. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  925. int n_frames, u8 ignored_acs,
  926. enum ieee80211_frame_release_type reason)
  927. {
  928. struct ieee80211_sub_if_data *sdata = sta->sdata;
  929. struct ieee80211_local *local = sdata->local;
  930. bool found = false;
  931. bool more_data = false;
  932. int ac;
  933. unsigned long driver_release_tids = 0;
  934. struct sk_buff_head frames;
  935. /* Service or PS-Poll period starts */
  936. set_sta_flag(sta, WLAN_STA_SP);
  937. __skb_queue_head_init(&frames);
  938. /*
  939. * Get response frame(s) and more data bit for it.
  940. */
  941. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  942. unsigned long tids;
  943. if (ignored_acs & BIT(ac))
  944. continue;
  945. tids = ieee80211_tids_for_ac(ac);
  946. if (!found) {
  947. driver_release_tids = sta->driver_buffered_tids & tids;
  948. if (driver_release_tids) {
  949. found = true;
  950. } else {
  951. struct sk_buff *skb;
  952. while (n_frames > 0) {
  953. skb = skb_dequeue(&sta->tx_filtered[ac]);
  954. if (!skb) {
  955. skb = skb_dequeue(
  956. &sta->ps_tx_buf[ac]);
  957. if (skb)
  958. local->total_ps_buffered--;
  959. }
  960. if (!skb)
  961. break;
  962. n_frames--;
  963. found = true;
  964. __skb_queue_tail(&frames, skb);
  965. }
  966. }
  967. /*
  968. * If the driver has data on more than one TID then
  969. * certainly there's more data if we release just a
  970. * single frame now (from a single TID).
  971. */
  972. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  973. hweight16(driver_release_tids) > 1) {
  974. more_data = true;
  975. driver_release_tids =
  976. BIT(ffs(driver_release_tids) - 1);
  977. break;
  978. }
  979. }
  980. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  981. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  982. more_data = true;
  983. break;
  984. }
  985. }
  986. if (!found) {
  987. int tid;
  988. /*
  989. * For PS-Poll, this can only happen due to a race condition
  990. * when we set the TIM bit and the station notices it, but
  991. * before it can poll for the frame we expire it.
  992. *
  993. * For uAPSD, this is said in the standard (11.2.1.5 h):
  994. * At each unscheduled SP for a non-AP STA, the AP shall
  995. * attempt to transmit at least one MSDU or MMPDU, but no
  996. * more than the value specified in the Max SP Length field
  997. * in the QoS Capability element from delivery-enabled ACs,
  998. * that are destined for the non-AP STA.
  999. *
  1000. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1001. */
  1002. /* This will evaluate to 1, 3, 5 or 7. */
  1003. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1004. ieee80211_send_null_response(sdata, sta, tid, reason);
  1005. return;
  1006. }
  1007. if (!driver_release_tids) {
  1008. struct sk_buff_head pending;
  1009. struct sk_buff *skb;
  1010. int num = 0;
  1011. u16 tids = 0;
  1012. skb_queue_head_init(&pending);
  1013. while ((skb = __skb_dequeue(&frames))) {
  1014. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1015. struct ieee80211_hdr *hdr = (void *) skb->data;
  1016. u8 *qoshdr = NULL;
  1017. num++;
  1018. /*
  1019. * Tell TX path to send this frame even though the
  1020. * STA may still remain is PS mode after this frame
  1021. * exchange.
  1022. */
  1023. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
  1024. /*
  1025. * Use MoreData flag to indicate whether there are
  1026. * more buffered frames for this STA
  1027. */
  1028. if (more_data || !skb_queue_empty(&frames))
  1029. hdr->frame_control |=
  1030. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1031. else
  1032. hdr->frame_control &=
  1033. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1034. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1035. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1036. qoshdr = ieee80211_get_qos_ctl(hdr);
  1037. /* set EOSP for the frame */
  1038. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1039. qoshdr && skb_queue_empty(&frames))
  1040. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1041. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1042. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1043. if (qoshdr)
  1044. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1045. else
  1046. tids |= BIT(0);
  1047. __skb_queue_tail(&pending, skb);
  1048. }
  1049. drv_allow_buffered_frames(local, sta, tids, num,
  1050. reason, more_data);
  1051. ieee80211_add_pending_skbs(local, &pending);
  1052. sta_info_recalc_tim(sta);
  1053. } else {
  1054. /*
  1055. * We need to release a frame that is buffered somewhere in the
  1056. * driver ... it'll have to handle that.
  1057. * Note that, as per the comment above, it'll also have to see
  1058. * if there is more than just one frame on the specific TID that
  1059. * we're releasing from, and it needs to set the more-data bit
  1060. * accordingly if we tell it that there's no more data. If we do
  1061. * tell it there's more data, then of course the more-data bit
  1062. * needs to be set anyway.
  1063. */
  1064. drv_release_buffered_frames(local, sta, driver_release_tids,
  1065. n_frames, reason, more_data);
  1066. /*
  1067. * Note that we don't recalculate the TIM bit here as it would
  1068. * most likely have no effect at all unless the driver told us
  1069. * that the TID became empty before returning here from the
  1070. * release function.
  1071. * Either way, however, when the driver tells us that the TID
  1072. * became empty we'll do the TIM recalculation.
  1073. */
  1074. }
  1075. }
  1076. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1077. {
  1078. u8 ignore_for_response = sta->sta.uapsd_queues;
  1079. /*
  1080. * If all ACs are delivery-enabled then we should reply
  1081. * from any of them, if only some are enabled we reply
  1082. * only from the non-enabled ones.
  1083. */
  1084. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1085. ignore_for_response = 0;
  1086. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1087. IEEE80211_FRAME_RELEASE_PSPOLL);
  1088. }
  1089. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1090. {
  1091. int n_frames = sta->sta.max_sp;
  1092. u8 delivery_enabled = sta->sta.uapsd_queues;
  1093. /*
  1094. * If we ever grow support for TSPEC this might happen if
  1095. * the TSPEC update from hostapd comes in between a trigger
  1096. * frame setting WLAN_STA_UAPSD in the RX path and this
  1097. * actually getting called.
  1098. */
  1099. if (!delivery_enabled)
  1100. return;
  1101. switch (sta->sta.max_sp) {
  1102. case 1:
  1103. n_frames = 2;
  1104. break;
  1105. case 2:
  1106. n_frames = 4;
  1107. break;
  1108. case 3:
  1109. n_frames = 6;
  1110. break;
  1111. case 0:
  1112. /* XXX: what is a good value? */
  1113. n_frames = 8;
  1114. break;
  1115. }
  1116. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1117. IEEE80211_FRAME_RELEASE_UAPSD);
  1118. }
  1119. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1120. struct ieee80211_sta *pubsta, bool block)
  1121. {
  1122. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1123. trace_api_sta_block_awake(sta->local, pubsta, block);
  1124. if (block)
  1125. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1126. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1127. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1128. }
  1129. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1130. void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
  1131. {
  1132. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1133. struct ieee80211_local *local = sta->local;
  1134. struct sk_buff *skb;
  1135. struct skb_eosp_msg_data *data;
  1136. trace_api_eosp(local, pubsta);
  1137. skb = alloc_skb(0, GFP_ATOMIC);
  1138. if (!skb) {
  1139. /* too bad ... but race is better than loss */
  1140. clear_sta_flag(sta, WLAN_STA_SP);
  1141. return;
  1142. }
  1143. data = (void *)skb->cb;
  1144. memcpy(data->sta, pubsta->addr, ETH_ALEN);
  1145. memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
  1146. skb->pkt_type = IEEE80211_EOSP_MSG;
  1147. skb_queue_tail(&local->skb_queue, skb);
  1148. tasklet_schedule(&local->tasklet);
  1149. }
  1150. EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
  1151. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1152. u8 tid, bool buffered)
  1153. {
  1154. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1155. if (WARN_ON(tid >= STA_TID_NUM))
  1156. return;
  1157. if (buffered)
  1158. set_bit(tid, &sta->driver_buffered_tids);
  1159. else
  1160. clear_bit(tid, &sta->driver_buffered_tids);
  1161. sta_info_recalc_tim(sta);
  1162. }
  1163. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1164. int sta_info_move_state(struct sta_info *sta,
  1165. enum ieee80211_sta_state new_state)
  1166. {
  1167. might_sleep();
  1168. if (sta->sta_state == new_state)
  1169. return 0;
  1170. switch (new_state) {
  1171. case IEEE80211_STA_NONE:
  1172. if (sta->sta_state == IEEE80211_STA_AUTH)
  1173. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1174. else
  1175. return -EINVAL;
  1176. break;
  1177. case IEEE80211_STA_AUTH:
  1178. if (sta->sta_state == IEEE80211_STA_NONE)
  1179. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1180. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1181. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1182. else
  1183. return -EINVAL;
  1184. break;
  1185. case IEEE80211_STA_ASSOC:
  1186. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1187. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1188. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1189. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1190. atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
  1191. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1192. } else
  1193. return -EINVAL;
  1194. break;
  1195. case IEEE80211_STA_AUTHORIZED:
  1196. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1197. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1198. atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
  1199. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1200. } else
  1201. return -EINVAL;
  1202. break;
  1203. default:
  1204. WARN(1, "invalid state %d", new_state);
  1205. return -EINVAL;
  1206. }
  1207. printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
  1208. sta->sdata->name, sta->sta.addr, new_state);
  1209. sta->sta_state = new_state;
  1210. return 0;
  1211. }