sta_info.c 38 KB

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