sta_info.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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. sta->sta_state = IEEE80211_STA_NONE;
  261. do_posix_clock_monotonic_gettime(&uptime);
  262. sta->last_connected = uptime.tv_sec;
  263. ewma_init(&sta->avg_signal, 1024, 8);
  264. if (sta_prepare_rate_control(local, sta, gfp)) {
  265. kfree(sta);
  266. return NULL;
  267. }
  268. for (i = 0; i < STA_TID_NUM; i++) {
  269. /*
  270. * timer_to_tid must be initialized with identity mapping
  271. * to enable session_timer's data differentiation. See
  272. * sta_rx_agg_session_timer_expired for usage.
  273. */
  274. sta->timer_to_tid[i] = i;
  275. }
  276. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  277. skb_queue_head_init(&sta->ps_tx_buf[i]);
  278. skb_queue_head_init(&sta->tx_filtered[i]);
  279. }
  280. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  281. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  282. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  283. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  284. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  285. #ifdef CONFIG_MAC80211_MESH
  286. sta->plink_state = NL80211_PLINK_LISTEN;
  287. init_timer(&sta->plink_timer);
  288. #endif
  289. return sta;
  290. }
  291. static int sta_info_insert_check(struct sta_info *sta)
  292. {
  293. struct ieee80211_sub_if_data *sdata = sta->sdata;
  294. /*
  295. * Can't be a WARN_ON because it can be triggered through a race:
  296. * something inserts a STA (on one CPU) without holding the RTNL
  297. * and another CPU turns off the net device.
  298. */
  299. if (unlikely(!ieee80211_sdata_running(sdata)))
  300. return -ENETDOWN;
  301. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  302. is_multicast_ether_addr(sta->sta.addr)))
  303. return -EINVAL;
  304. return 0;
  305. }
  306. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  307. struct ieee80211_sub_if_data *sdata,
  308. struct sta_info *sta)
  309. {
  310. enum ieee80211_sta_state state;
  311. int err = 0;
  312. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  313. err = drv_sta_state(local, sdata, sta, state, state + 1);
  314. if (err)
  315. break;
  316. }
  317. if (!err) {
  318. /*
  319. * Drivers using legacy sta_add/sta_remove callbacks only
  320. * get uploaded set to true after sta_add is called.
  321. */
  322. if (!local->ops->sta_add)
  323. sta->uploaded = true;
  324. return 0;
  325. }
  326. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  327. printk(KERN_DEBUG
  328. "%s: failed to move IBSS STA %pM to state %d (%d) - keeping it anyway.\n",
  329. sdata->name, sta->sta.addr, state + 1, err);
  330. err = 0;
  331. }
  332. /* unwind on error */
  333. for (; state > IEEE80211_STA_NOTEXIST; state--)
  334. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  335. return err;
  336. }
  337. /*
  338. * should be called with sta_mtx locked
  339. * this function replaces the mutex lock
  340. * with a RCU lock
  341. */
  342. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  343. {
  344. struct ieee80211_local *local = sta->local;
  345. struct ieee80211_sub_if_data *sdata = sta->sdata;
  346. struct sta_info *exist_sta;
  347. bool dummy_reinsert = false;
  348. int err = 0;
  349. lockdep_assert_held(&local->sta_mtx);
  350. /*
  351. * check if STA exists already.
  352. * only accept a scenario of a second call to sta_info_insert_finish
  353. * with a dummy station entry that was inserted earlier
  354. * in that case - assume that the dummy station flag should
  355. * be removed.
  356. */
  357. exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
  358. if (exist_sta) {
  359. if (exist_sta == sta && sta->dummy) {
  360. dummy_reinsert = true;
  361. } else {
  362. err = -EEXIST;
  363. goto out_err;
  364. }
  365. }
  366. if (!sta->dummy || dummy_reinsert) {
  367. /* notify driver */
  368. err = sta_info_insert_drv_state(local, sdata, sta);
  369. if (err)
  370. goto out_err;
  371. }
  372. if (!dummy_reinsert) {
  373. local->num_sta++;
  374. local->sta_generation++;
  375. smp_mb();
  376. /* make the station visible */
  377. sta_info_hash_add(local, sta);
  378. list_add(&sta->list, &local->sta_list);
  379. set_sta_flag(sta, WLAN_STA_INSERTED);
  380. } else {
  381. sta->dummy = false;
  382. }
  383. if (!sta->dummy) {
  384. struct station_info sinfo;
  385. ieee80211_sta_debugfs_add(sta);
  386. rate_control_add_sta_debugfs(sta);
  387. memset(&sinfo, 0, sizeof(sinfo));
  388. sinfo.filled = 0;
  389. sinfo.generation = local->sta_generation;
  390. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  391. }
  392. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  393. wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
  394. sta->dummy ? "dummy " : "", sta->sta.addr);
  395. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  396. /* move reference to rcu-protected */
  397. rcu_read_lock();
  398. mutex_unlock(&local->sta_mtx);
  399. if (ieee80211_vif_is_mesh(&sdata->vif))
  400. mesh_accept_plinks_update(sdata);
  401. return 0;
  402. out_err:
  403. mutex_unlock(&local->sta_mtx);
  404. rcu_read_lock();
  405. return err;
  406. }
  407. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  408. {
  409. struct ieee80211_local *local = sta->local;
  410. int err = 0;
  411. might_sleep();
  412. err = sta_info_insert_check(sta);
  413. if (err) {
  414. rcu_read_lock();
  415. goto out_free;
  416. }
  417. mutex_lock(&local->sta_mtx);
  418. err = sta_info_insert_finish(sta);
  419. if (err)
  420. goto out_free;
  421. return 0;
  422. out_free:
  423. BUG_ON(!err);
  424. sta_info_free(local, sta);
  425. return err;
  426. }
  427. int sta_info_insert(struct sta_info *sta)
  428. {
  429. int err = sta_info_insert_rcu(sta);
  430. rcu_read_unlock();
  431. return err;
  432. }
  433. /* Caller must hold sta->local->sta_mtx */
  434. int sta_info_reinsert(struct sta_info *sta)
  435. {
  436. struct ieee80211_local *local = sta->local;
  437. int err = 0;
  438. err = sta_info_insert_check(sta);
  439. if (err) {
  440. mutex_unlock(&local->sta_mtx);
  441. return err;
  442. }
  443. might_sleep();
  444. err = sta_info_insert_finish(sta);
  445. rcu_read_unlock();
  446. return err;
  447. }
  448. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  449. {
  450. /*
  451. * This format has been mandated by the IEEE specifications,
  452. * so this line may not be changed to use the __set_bit() format.
  453. */
  454. bss->tim[aid / 8] |= (1 << (aid % 8));
  455. }
  456. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  457. {
  458. /*
  459. * This format has been mandated by the IEEE specifications,
  460. * so this line may not be changed to use the __clear_bit() format.
  461. */
  462. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  463. }
  464. static unsigned long ieee80211_tids_for_ac(int ac)
  465. {
  466. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  467. switch (ac) {
  468. case IEEE80211_AC_VO:
  469. return BIT(6) | BIT(7);
  470. case IEEE80211_AC_VI:
  471. return BIT(4) | BIT(5);
  472. case IEEE80211_AC_BE:
  473. return BIT(0) | BIT(3);
  474. case IEEE80211_AC_BK:
  475. return BIT(1) | BIT(2);
  476. default:
  477. WARN_ON(1);
  478. return 0;
  479. }
  480. }
  481. void sta_info_recalc_tim(struct sta_info *sta)
  482. {
  483. struct ieee80211_local *local = sta->local;
  484. struct ieee80211_if_ap *bss = sta->sdata->bss;
  485. unsigned long flags;
  486. bool indicate_tim = false;
  487. u8 ignore_for_tim = sta->sta.uapsd_queues;
  488. int ac;
  489. if (WARN_ON_ONCE(!sta->sdata->bss))
  490. return;
  491. /* No need to do anything if the driver does all */
  492. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  493. return;
  494. if (sta->dead)
  495. goto done;
  496. /*
  497. * If all ACs are delivery-enabled then we should build
  498. * the TIM bit for all ACs anyway; if only some are then
  499. * we ignore those and build the TIM bit using only the
  500. * non-enabled ones.
  501. */
  502. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  503. ignore_for_tim = 0;
  504. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  505. unsigned long tids;
  506. if (ignore_for_tim & BIT(ac))
  507. continue;
  508. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  509. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  510. if (indicate_tim)
  511. break;
  512. tids = ieee80211_tids_for_ac(ac);
  513. indicate_tim |=
  514. sta->driver_buffered_tids & tids;
  515. }
  516. done:
  517. spin_lock_irqsave(&local->tim_lock, flags);
  518. if (indicate_tim)
  519. __bss_tim_set(bss, sta->sta.aid);
  520. else
  521. __bss_tim_clear(bss, sta->sta.aid);
  522. if (local->ops->set_tim) {
  523. local->tim_in_locked_section = true;
  524. drv_set_tim(local, &sta->sta, indicate_tim);
  525. local->tim_in_locked_section = false;
  526. }
  527. spin_unlock_irqrestore(&local->tim_lock, flags);
  528. }
  529. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  530. {
  531. struct ieee80211_tx_info *info;
  532. int timeout;
  533. if (!skb)
  534. return false;
  535. info = IEEE80211_SKB_CB(skb);
  536. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  537. timeout = (sta->listen_interval *
  538. sta->sdata->vif.bss_conf.beacon_int *
  539. 32 / 15625) * HZ;
  540. if (timeout < STA_TX_BUFFER_EXPIRE)
  541. timeout = STA_TX_BUFFER_EXPIRE;
  542. return time_after(jiffies, info->control.jiffies + timeout);
  543. }
  544. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  545. struct sta_info *sta, int ac)
  546. {
  547. unsigned long flags;
  548. struct sk_buff *skb;
  549. /*
  550. * First check for frames that should expire on the filtered
  551. * queue. Frames here were rejected by the driver and are on
  552. * a separate queue to avoid reordering with normal PS-buffered
  553. * frames. They also aren't accounted for right now in the
  554. * total_ps_buffered counter.
  555. */
  556. for (;;) {
  557. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  558. skb = skb_peek(&sta->tx_filtered[ac]);
  559. if (sta_info_buffer_expired(sta, skb))
  560. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  561. else
  562. skb = NULL;
  563. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  564. /*
  565. * Frames are queued in order, so if this one
  566. * hasn't expired yet we can stop testing. If
  567. * we actually reached the end of the queue we
  568. * also need to stop, of course.
  569. */
  570. if (!skb)
  571. break;
  572. dev_kfree_skb(skb);
  573. }
  574. /*
  575. * Now also check the normal PS-buffered queue, this will
  576. * only find something if the filtered queue was emptied
  577. * since the filtered frames are all before the normal PS
  578. * buffered frames.
  579. */
  580. for (;;) {
  581. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  582. skb = skb_peek(&sta->ps_tx_buf[ac]);
  583. if (sta_info_buffer_expired(sta, skb))
  584. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  585. else
  586. skb = NULL;
  587. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  588. /*
  589. * frames are queued in order, so if this one
  590. * hasn't expired yet (or we reached the end of
  591. * the queue) we can stop testing
  592. */
  593. if (!skb)
  594. break;
  595. local->total_ps_buffered--;
  596. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  597. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  598. sta->sta.addr);
  599. #endif
  600. dev_kfree_skb(skb);
  601. }
  602. /*
  603. * Finally, recalculate the TIM bit for this station -- it might
  604. * now be clear because the station was too slow to retrieve its
  605. * frames.
  606. */
  607. sta_info_recalc_tim(sta);
  608. /*
  609. * Return whether there are any frames still buffered, this is
  610. * used to check whether the cleanup timer still needs to run,
  611. * if there are no frames we don't need to rearm the timer.
  612. */
  613. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  614. skb_queue_empty(&sta->tx_filtered[ac]));
  615. }
  616. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  617. struct sta_info *sta)
  618. {
  619. bool have_buffered = false;
  620. int ac;
  621. /* This is only necessary for stations on BSS interfaces */
  622. if (!sta->sdata->bss)
  623. return false;
  624. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  625. have_buffered |=
  626. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  627. return have_buffered;
  628. }
  629. int __must_check __sta_info_destroy(struct sta_info *sta)
  630. {
  631. struct ieee80211_local *local;
  632. struct ieee80211_sub_if_data *sdata;
  633. int ret, i, ac;
  634. struct tid_ampdu_tx *tid_tx;
  635. might_sleep();
  636. if (!sta)
  637. return -ENOENT;
  638. local = sta->local;
  639. sdata = sta->sdata;
  640. lockdep_assert_held(&local->sta_mtx);
  641. /*
  642. * Before removing the station from the driver and
  643. * rate control, it might still start new aggregation
  644. * sessions -- block that to make sure the tear-down
  645. * will be sufficient.
  646. */
  647. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  648. ieee80211_sta_tear_down_BA_sessions(sta, true);
  649. ret = sta_info_hash_del(local, sta);
  650. if (ret)
  651. return ret;
  652. list_del(&sta->list);
  653. mutex_lock(&local->key_mtx);
  654. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  655. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  656. if (sta->ptk)
  657. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  658. mutex_unlock(&local->key_mtx);
  659. sta->dead = true;
  660. local->num_sta--;
  661. local->sta_generation++;
  662. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  663. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  664. while (sta->sta_state > IEEE80211_STA_NONE) {
  665. ret = sta_info_move_state(sta, sta->sta_state - 1);
  666. if (ret) {
  667. WARN_ON_ONCE(1);
  668. break;
  669. }
  670. }
  671. if (sta->uploaded) {
  672. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  673. IEEE80211_STA_NOTEXIST);
  674. WARN_ON_ONCE(ret != 0);
  675. }
  676. /*
  677. * At this point, after we wait for an RCU grace period,
  678. * neither mac80211 nor the driver can reference this
  679. * sta struct any more except by still existing timers
  680. * associated with this station that we clean up below.
  681. */
  682. synchronize_rcu();
  683. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  684. BUG_ON(!sdata->bss);
  685. clear_sta_flag(sta, WLAN_STA_PS_STA);
  686. atomic_dec(&sdata->bss->num_sta_ps);
  687. sta_info_recalc_tim(sta);
  688. }
  689. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  690. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  691. __skb_queue_purge(&sta->ps_tx_buf[ac]);
  692. __skb_queue_purge(&sta->tx_filtered[ac]);
  693. }
  694. #ifdef CONFIG_MAC80211_MESH
  695. if (ieee80211_vif_is_mesh(&sdata->vif))
  696. mesh_accept_plinks_update(sdata);
  697. #endif
  698. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  699. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  700. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  701. cancel_work_sync(&sta->drv_unblock_wk);
  702. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  703. rate_control_remove_sta_debugfs(sta);
  704. ieee80211_sta_debugfs_remove(sta);
  705. #ifdef CONFIG_MAC80211_MESH
  706. if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  707. mesh_plink_deactivate(sta);
  708. del_timer_sync(&sta->plink_timer);
  709. }
  710. #endif
  711. /*
  712. * Destroy aggregation state here. It would be nice to wait for the
  713. * driver to finish aggregation stop and then clean up, but for now
  714. * drivers have to handle aggregation stop being requested, followed
  715. * directly by station destruction.
  716. */
  717. for (i = 0; i < STA_TID_NUM; i++) {
  718. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  719. if (!tid_tx)
  720. continue;
  721. __skb_queue_purge(&tid_tx->pending);
  722. kfree(tid_tx);
  723. }
  724. sta_info_free(local, sta);
  725. return 0;
  726. }
  727. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  728. {
  729. struct sta_info *sta;
  730. int ret;
  731. mutex_lock(&sdata->local->sta_mtx);
  732. sta = sta_info_get_rx(sdata, addr);
  733. ret = __sta_info_destroy(sta);
  734. mutex_unlock(&sdata->local->sta_mtx);
  735. return ret;
  736. }
  737. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  738. const u8 *addr)
  739. {
  740. struct sta_info *sta;
  741. int ret;
  742. mutex_lock(&sdata->local->sta_mtx);
  743. sta = sta_info_get_bss_rx(sdata, addr);
  744. ret = __sta_info_destroy(sta);
  745. mutex_unlock(&sdata->local->sta_mtx);
  746. return ret;
  747. }
  748. static void sta_info_cleanup(unsigned long data)
  749. {
  750. struct ieee80211_local *local = (struct ieee80211_local *) data;
  751. struct sta_info *sta;
  752. bool timer_needed = false;
  753. rcu_read_lock();
  754. list_for_each_entry_rcu(sta, &local->sta_list, list)
  755. if (sta_info_cleanup_expire_buffered(local, sta))
  756. timer_needed = true;
  757. rcu_read_unlock();
  758. if (local->quiescing)
  759. return;
  760. if (!timer_needed)
  761. return;
  762. mod_timer(&local->sta_cleanup,
  763. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  764. }
  765. void sta_info_init(struct ieee80211_local *local)
  766. {
  767. spin_lock_init(&local->tim_lock);
  768. mutex_init(&local->sta_mtx);
  769. INIT_LIST_HEAD(&local->sta_list);
  770. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  771. (unsigned long)local);
  772. }
  773. void sta_info_stop(struct ieee80211_local *local)
  774. {
  775. del_timer(&local->sta_cleanup);
  776. sta_info_flush(local, NULL);
  777. }
  778. /**
  779. * sta_info_flush - flush matching STA entries from the STA table
  780. *
  781. * Returns the number of removed STA entries.
  782. *
  783. * @local: local interface data
  784. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  785. */
  786. int sta_info_flush(struct ieee80211_local *local,
  787. struct ieee80211_sub_if_data *sdata)
  788. {
  789. struct sta_info *sta, *tmp;
  790. int ret = 0;
  791. might_sleep();
  792. mutex_lock(&local->sta_mtx);
  793. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  794. if (!sdata || sdata == sta->sdata)
  795. WARN_ON(__sta_info_destroy(sta));
  796. }
  797. mutex_unlock(&local->sta_mtx);
  798. return ret;
  799. }
  800. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  801. unsigned long exp_time)
  802. {
  803. struct ieee80211_local *local = sdata->local;
  804. struct sta_info *sta, *tmp;
  805. mutex_lock(&local->sta_mtx);
  806. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  807. if (sdata != sta->sdata)
  808. continue;
  809. if (time_after(jiffies, sta->last_rx + exp_time)) {
  810. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  811. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  812. sdata->name, sta->sta.addr);
  813. #endif
  814. WARN_ON(__sta_info_destroy(sta));
  815. }
  816. }
  817. mutex_unlock(&local->sta_mtx);
  818. }
  819. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  820. const u8 *addr,
  821. const u8 *localaddr)
  822. {
  823. struct sta_info *sta, *nxt;
  824. /*
  825. * Just return a random station if localaddr is NULL
  826. * ... first in list.
  827. */
  828. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  829. if (localaddr &&
  830. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  831. continue;
  832. if (!sta->uploaded)
  833. return NULL;
  834. return &sta->sta;
  835. }
  836. return NULL;
  837. }
  838. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  839. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  840. const u8 *addr)
  841. {
  842. struct sta_info *sta;
  843. if (!vif)
  844. return NULL;
  845. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  846. if (!sta)
  847. return NULL;
  848. if (!sta->uploaded)
  849. return NULL;
  850. return &sta->sta;
  851. }
  852. EXPORT_SYMBOL(ieee80211_find_sta);
  853. static void clear_sta_ps_flags(void *_sta)
  854. {
  855. struct sta_info *sta = _sta;
  856. struct ieee80211_sub_if_data *sdata = sta->sdata;
  857. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  858. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  859. atomic_dec(&sdata->bss->num_sta_ps);
  860. }
  861. /* powersave support code */
  862. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  863. {
  864. struct ieee80211_sub_if_data *sdata = sta->sdata;
  865. struct ieee80211_local *local = sdata->local;
  866. struct sk_buff_head pending;
  867. int filtered = 0, buffered = 0, ac;
  868. clear_sta_flag(sta, WLAN_STA_SP);
  869. BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
  870. sta->driver_buffered_tids = 0;
  871. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  872. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  873. skb_queue_head_init(&pending);
  874. /* Send all buffered frames to the station */
  875. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  876. int count = skb_queue_len(&pending), tmp;
  877. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  878. tmp = skb_queue_len(&pending);
  879. filtered += tmp - count;
  880. count = tmp;
  881. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  882. tmp = skb_queue_len(&pending);
  883. buffered += tmp - count;
  884. }
  885. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  886. local->total_ps_buffered -= buffered;
  887. sta_info_recalc_tim(sta);
  888. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  889. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  890. "since STA not sleeping anymore\n", sdata->name,
  891. sta->sta.addr, sta->sta.aid, filtered, buffered);
  892. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  893. }
  894. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  895. struct sta_info *sta, int tid,
  896. enum ieee80211_frame_release_type reason)
  897. {
  898. struct ieee80211_local *local = sdata->local;
  899. struct ieee80211_qos_hdr *nullfunc;
  900. struct sk_buff *skb;
  901. int size = sizeof(*nullfunc);
  902. __le16 fc;
  903. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  904. struct ieee80211_tx_info *info;
  905. if (qos) {
  906. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  907. IEEE80211_STYPE_QOS_NULLFUNC |
  908. IEEE80211_FCTL_FROMDS);
  909. } else {
  910. size -= 2;
  911. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  912. IEEE80211_STYPE_NULLFUNC |
  913. IEEE80211_FCTL_FROMDS);
  914. }
  915. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  916. if (!skb)
  917. return;
  918. skb_reserve(skb, local->hw.extra_tx_headroom);
  919. nullfunc = (void *) skb_put(skb, size);
  920. nullfunc->frame_control = fc;
  921. nullfunc->duration_id = 0;
  922. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  923. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  924. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  925. skb->priority = tid;
  926. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  927. if (qos) {
  928. nullfunc->qos_ctrl = cpu_to_le16(tid);
  929. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  930. nullfunc->qos_ctrl |=
  931. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  932. }
  933. info = IEEE80211_SKB_CB(skb);
  934. /*
  935. * Tell TX path to send this frame even though the
  936. * STA may still remain is PS mode after this frame
  937. * exchange. Also set EOSP to indicate this packet
  938. * ends the poll/service period.
  939. */
  940. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
  941. IEEE80211_TX_STATUS_EOSP |
  942. IEEE80211_TX_CTL_REQ_TX_STATUS;
  943. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  944. ieee80211_xmit(sdata, skb);
  945. }
  946. static void
  947. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  948. int n_frames, u8 ignored_acs,
  949. enum ieee80211_frame_release_type reason)
  950. {
  951. struct ieee80211_sub_if_data *sdata = sta->sdata;
  952. struct ieee80211_local *local = sdata->local;
  953. bool found = false;
  954. bool more_data = false;
  955. int ac;
  956. unsigned long driver_release_tids = 0;
  957. struct sk_buff_head frames;
  958. /* Service or PS-Poll period starts */
  959. set_sta_flag(sta, WLAN_STA_SP);
  960. __skb_queue_head_init(&frames);
  961. /*
  962. * Get response frame(s) and more data bit for it.
  963. */
  964. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  965. unsigned long tids;
  966. if (ignored_acs & BIT(ac))
  967. continue;
  968. tids = ieee80211_tids_for_ac(ac);
  969. if (!found) {
  970. driver_release_tids = sta->driver_buffered_tids & tids;
  971. if (driver_release_tids) {
  972. found = true;
  973. } else {
  974. struct sk_buff *skb;
  975. while (n_frames > 0) {
  976. skb = skb_dequeue(&sta->tx_filtered[ac]);
  977. if (!skb) {
  978. skb = skb_dequeue(
  979. &sta->ps_tx_buf[ac]);
  980. if (skb)
  981. local->total_ps_buffered--;
  982. }
  983. if (!skb)
  984. break;
  985. n_frames--;
  986. found = true;
  987. __skb_queue_tail(&frames, skb);
  988. }
  989. }
  990. /*
  991. * If the driver has data on more than one TID then
  992. * certainly there's more data if we release just a
  993. * single frame now (from a single TID).
  994. */
  995. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  996. hweight16(driver_release_tids) > 1) {
  997. more_data = true;
  998. driver_release_tids =
  999. BIT(ffs(driver_release_tids) - 1);
  1000. break;
  1001. }
  1002. }
  1003. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1004. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  1005. more_data = true;
  1006. break;
  1007. }
  1008. }
  1009. if (!found) {
  1010. int tid;
  1011. /*
  1012. * For PS-Poll, this can only happen due to a race condition
  1013. * when we set the TIM bit and the station notices it, but
  1014. * before it can poll for the frame we expire it.
  1015. *
  1016. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1017. * At each unscheduled SP for a non-AP STA, the AP shall
  1018. * attempt to transmit at least one MSDU or MMPDU, but no
  1019. * more than the value specified in the Max SP Length field
  1020. * in the QoS Capability element from delivery-enabled ACs,
  1021. * that are destined for the non-AP STA.
  1022. *
  1023. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1024. */
  1025. /* This will evaluate to 1, 3, 5 or 7. */
  1026. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1027. ieee80211_send_null_response(sdata, sta, tid, reason);
  1028. return;
  1029. }
  1030. if (!driver_release_tids) {
  1031. struct sk_buff_head pending;
  1032. struct sk_buff *skb;
  1033. int num = 0;
  1034. u16 tids = 0;
  1035. skb_queue_head_init(&pending);
  1036. while ((skb = __skb_dequeue(&frames))) {
  1037. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1038. struct ieee80211_hdr *hdr = (void *) skb->data;
  1039. u8 *qoshdr = NULL;
  1040. num++;
  1041. /*
  1042. * Tell TX path to send this frame even though the
  1043. * STA may still remain is PS mode after this frame
  1044. * exchange.
  1045. */
  1046. info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
  1047. /*
  1048. * Use MoreData flag to indicate whether there are
  1049. * more buffered frames for this STA
  1050. */
  1051. if (more_data || !skb_queue_empty(&frames))
  1052. hdr->frame_control |=
  1053. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1054. else
  1055. hdr->frame_control &=
  1056. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1057. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1058. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1059. qoshdr = ieee80211_get_qos_ctl(hdr);
  1060. /* set EOSP for the frame */
  1061. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1062. qoshdr && skb_queue_empty(&frames))
  1063. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1064. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1065. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1066. if (qoshdr)
  1067. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1068. else
  1069. tids |= BIT(0);
  1070. __skb_queue_tail(&pending, skb);
  1071. }
  1072. drv_allow_buffered_frames(local, sta, tids, num,
  1073. reason, more_data);
  1074. ieee80211_add_pending_skbs(local, &pending);
  1075. sta_info_recalc_tim(sta);
  1076. } else {
  1077. /*
  1078. * We need to release a frame that is buffered somewhere in the
  1079. * driver ... it'll have to handle that.
  1080. * Note that, as per the comment above, it'll also have to see
  1081. * if there is more than just one frame on the specific TID that
  1082. * we're releasing from, and it needs to set the more-data bit
  1083. * accordingly if we tell it that there's no more data. If we do
  1084. * tell it there's more data, then of course the more-data bit
  1085. * needs to be set anyway.
  1086. */
  1087. drv_release_buffered_frames(local, sta, driver_release_tids,
  1088. n_frames, reason, more_data);
  1089. /*
  1090. * Note that we don't recalculate the TIM bit here as it would
  1091. * most likely have no effect at all unless the driver told us
  1092. * that the TID became empty before returning here from the
  1093. * release function.
  1094. * Either way, however, when the driver tells us that the TID
  1095. * became empty we'll do the TIM recalculation.
  1096. */
  1097. }
  1098. }
  1099. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1100. {
  1101. u8 ignore_for_response = sta->sta.uapsd_queues;
  1102. /*
  1103. * If all ACs are delivery-enabled then we should reply
  1104. * from any of them, if only some are enabled we reply
  1105. * only from the non-enabled ones.
  1106. */
  1107. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1108. ignore_for_response = 0;
  1109. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1110. IEEE80211_FRAME_RELEASE_PSPOLL);
  1111. }
  1112. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1113. {
  1114. int n_frames = sta->sta.max_sp;
  1115. u8 delivery_enabled = sta->sta.uapsd_queues;
  1116. /*
  1117. * If we ever grow support for TSPEC this might happen if
  1118. * the TSPEC update from hostapd comes in between a trigger
  1119. * frame setting WLAN_STA_UAPSD in the RX path and this
  1120. * actually getting called.
  1121. */
  1122. if (!delivery_enabled)
  1123. return;
  1124. switch (sta->sta.max_sp) {
  1125. case 1:
  1126. n_frames = 2;
  1127. break;
  1128. case 2:
  1129. n_frames = 4;
  1130. break;
  1131. case 3:
  1132. n_frames = 6;
  1133. break;
  1134. case 0:
  1135. /* XXX: what is a good value? */
  1136. n_frames = 8;
  1137. break;
  1138. }
  1139. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1140. IEEE80211_FRAME_RELEASE_UAPSD);
  1141. }
  1142. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1143. struct ieee80211_sta *pubsta, bool block)
  1144. {
  1145. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1146. trace_api_sta_block_awake(sta->local, pubsta, block);
  1147. if (block)
  1148. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1149. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1150. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1151. }
  1152. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1153. void ieee80211_sta_eosp_irqsafe(struct ieee80211_sta *pubsta)
  1154. {
  1155. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1156. struct ieee80211_local *local = sta->local;
  1157. struct sk_buff *skb;
  1158. struct skb_eosp_msg_data *data;
  1159. trace_api_eosp(local, pubsta);
  1160. skb = alloc_skb(0, GFP_ATOMIC);
  1161. if (!skb) {
  1162. /* too bad ... but race is better than loss */
  1163. clear_sta_flag(sta, WLAN_STA_SP);
  1164. return;
  1165. }
  1166. data = (void *)skb->cb;
  1167. memcpy(data->sta, pubsta->addr, ETH_ALEN);
  1168. memcpy(data->iface, sta->sdata->vif.addr, ETH_ALEN);
  1169. skb->pkt_type = IEEE80211_EOSP_MSG;
  1170. skb_queue_tail(&local->skb_queue, skb);
  1171. tasklet_schedule(&local->tasklet);
  1172. }
  1173. EXPORT_SYMBOL(ieee80211_sta_eosp_irqsafe);
  1174. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1175. u8 tid, bool buffered)
  1176. {
  1177. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1178. if (WARN_ON(tid >= STA_TID_NUM))
  1179. return;
  1180. if (buffered)
  1181. set_bit(tid, &sta->driver_buffered_tids);
  1182. else
  1183. clear_bit(tid, &sta->driver_buffered_tids);
  1184. sta_info_recalc_tim(sta);
  1185. }
  1186. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1187. int sta_info_move_state(struct sta_info *sta,
  1188. enum ieee80211_sta_state new_state)
  1189. {
  1190. might_sleep();
  1191. if (sta->sta_state == new_state)
  1192. return 0;
  1193. /* check allowed transitions first */
  1194. switch (new_state) {
  1195. case IEEE80211_STA_NONE:
  1196. if (sta->sta_state != IEEE80211_STA_AUTH)
  1197. return -EINVAL;
  1198. break;
  1199. case IEEE80211_STA_AUTH:
  1200. if (sta->sta_state != IEEE80211_STA_NONE &&
  1201. sta->sta_state != IEEE80211_STA_ASSOC)
  1202. return -EINVAL;
  1203. break;
  1204. case IEEE80211_STA_ASSOC:
  1205. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1206. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1207. return -EINVAL;
  1208. break;
  1209. case IEEE80211_STA_AUTHORIZED:
  1210. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1211. return -EINVAL;
  1212. break;
  1213. default:
  1214. WARN(1, "invalid state %d", new_state);
  1215. return -EINVAL;
  1216. }
  1217. printk(KERN_DEBUG "%s: moving STA %pM to state %d\n",
  1218. sta->sdata->name, sta->sta.addr, new_state);
  1219. /*
  1220. * notify the driver before the actual changes so it can
  1221. * fail the transition
  1222. */
  1223. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1224. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1225. sta->sta_state, new_state);
  1226. if (err)
  1227. return err;
  1228. }
  1229. /* reflect the change in all state variables */
  1230. switch (new_state) {
  1231. case IEEE80211_STA_NONE:
  1232. if (sta->sta_state == IEEE80211_STA_AUTH)
  1233. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1234. break;
  1235. case IEEE80211_STA_AUTH:
  1236. if (sta->sta_state == IEEE80211_STA_NONE)
  1237. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1238. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1239. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1240. break;
  1241. case IEEE80211_STA_ASSOC:
  1242. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1243. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1244. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1245. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1246. atomic_dec(&sta->sdata->u.ap.num_sta_authorized);
  1247. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1248. }
  1249. break;
  1250. case IEEE80211_STA_AUTHORIZED:
  1251. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1252. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1253. atomic_inc(&sta->sdata->u.ap.num_sta_authorized);
  1254. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1255. }
  1256. break;
  1257. default:
  1258. break;
  1259. }
  1260. sta->sta_state = new_state;
  1261. return 0;
  1262. }