sta_info.c 36 KB

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