sta_info.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/timer.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include "ieee80211_i.h"
  20. #include "driver-ops.h"
  21. #include "rate.h"
  22. #include "sta_info.h"
  23. #include "debugfs_sta.h"
  24. #include "mesh.h"
  25. /**
  26. * DOC: STA information lifetime rules
  27. *
  28. * STA info structures (&struct sta_info) are managed in a hash table
  29. * for faster lookup and a list for iteration. They are managed using
  30. * RCU, i.e. access to the list and hash table is protected by RCU.
  31. *
  32. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  33. * owns that structure. It must then insert it into the hash table using
  34. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  35. * case (which acquires an rcu read section but must not be called from
  36. * within one) will the pointer still be valid after the call. Note that
  37. * the caller may not do much with the STA info before inserting it, in
  38. * particular, it may not start any mesh peer link management or add
  39. * encryption keys.
  40. *
  41. * When the insertion fails (sta_info_insert()) returns non-zero), the
  42. * structure will have been freed by sta_info_insert()!
  43. *
  44. * Station entries are added by mac80211 when you establish a link with a
  45. * peer. This means different things for the different type of interfaces
  46. * we support. For a regular station this mean we add the AP sta when we
  47. * receive an association response from the AP. For IBSS this occurs when
  48. * get to know about a peer on the same IBSS. For WDS we add the sta for
  49. * the peer immediately upon device open. When using AP mode we add stations
  50. * for each respective station upon request from userspace through nl80211.
  51. *
  52. * In order to remove a STA info structure, various sta_info_destroy_*()
  53. * calls are available.
  54. *
  55. * There is no concept of ownership on a STA entry, each structure is
  56. * owned by the global hash table/list until it is removed. All users of
  57. * the structure need to be RCU protected so that the structure won't be
  58. * freed before they are done using it.
  59. */
  60. /* Caller must hold local->sta_lock */
  61. static int sta_info_hash_del(struct ieee80211_local *local,
  62. struct sta_info *sta)
  63. {
  64. struct sta_info *s;
  65. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  66. lockdep_is_held(&local->sta_lock));
  67. if (!s)
  68. return -ENOENT;
  69. if (s == sta) {
  70. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  71. s->hnext);
  72. return 0;
  73. }
  74. while (rcu_access_pointer(s->hnext) &&
  75. rcu_access_pointer(s->hnext) != sta)
  76. s = rcu_dereference_protected(s->hnext,
  77. lockdep_is_held(&local->sta_lock));
  78. if (rcu_access_pointer(s->hnext)) {
  79. rcu_assign_pointer(s->hnext, sta->hnext);
  80. return 0;
  81. }
  82. return -ENOENT;
  83. }
  84. /* protected by RCU */
  85. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  86. const u8 *addr)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct sta_info *sta;
  90. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  91. lockdep_is_held(&local->sta_lock) ||
  92. lockdep_is_held(&local->sta_mtx));
  93. while (sta) {
  94. if (sta->sdata == sdata && !sta->dummy &&
  95. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  96. break;
  97. sta = rcu_dereference_check(sta->hnext,
  98. lockdep_is_held(&local->sta_lock) ||
  99. lockdep_is_held(&local->sta_mtx));
  100. }
  101. return sta;
  102. }
  103. /* get a station info entry even if it is a dummy station*/
  104. struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
  105. const u8 *addr)
  106. {
  107. struct ieee80211_local *local = sdata->local;
  108. struct sta_info *sta;
  109. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  110. lockdep_is_held(&local->sta_lock) ||
  111. lockdep_is_held(&local->sta_mtx));
  112. while (sta) {
  113. if (sta->sdata == sdata &&
  114. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  115. break;
  116. sta = rcu_dereference_check(sta->hnext,
  117. lockdep_is_held(&local->sta_lock) ||
  118. lockdep_is_held(&local->sta_mtx));
  119. }
  120. return sta;
  121. }
  122. /*
  123. * Get sta info either from the specified interface
  124. * or from one of its vlans
  125. */
  126. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  127. const u8 *addr)
  128. {
  129. struct ieee80211_local *local = sdata->local;
  130. struct sta_info *sta;
  131. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  132. lockdep_is_held(&local->sta_lock) ||
  133. lockdep_is_held(&local->sta_mtx));
  134. while (sta) {
  135. if ((sta->sdata == sdata ||
  136. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  137. !sta->dummy &&
  138. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  139. break;
  140. sta = rcu_dereference_check(sta->hnext,
  141. lockdep_is_held(&local->sta_lock) ||
  142. lockdep_is_held(&local->sta_mtx));
  143. }
  144. return sta;
  145. }
  146. /*
  147. * Get sta info either from the specified interface
  148. * or from one of its vlans (including dummy stations)
  149. */
  150. struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
  151. const u8 *addr)
  152. {
  153. struct ieee80211_local *local = sdata->local;
  154. struct sta_info *sta;
  155. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  156. lockdep_is_held(&local->sta_lock) ||
  157. lockdep_is_held(&local->sta_mtx));
  158. while (sta) {
  159. if ((sta->sdata == sdata ||
  160. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  161. memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
  162. break;
  163. sta = rcu_dereference_check(sta->hnext,
  164. lockdep_is_held(&local->sta_lock) ||
  165. lockdep_is_held(&local->sta_mtx));
  166. }
  167. return sta;
  168. }
  169. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  170. int idx)
  171. {
  172. struct ieee80211_local *local = sdata->local;
  173. struct sta_info *sta;
  174. int i = 0;
  175. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  176. if (sdata != sta->sdata)
  177. continue;
  178. if (i < idx) {
  179. ++i;
  180. continue;
  181. }
  182. return sta;
  183. }
  184. return NULL;
  185. }
  186. /**
  187. * __sta_info_free - internal STA free helper
  188. *
  189. * @local: pointer to the global information
  190. * @sta: STA info to free
  191. *
  192. * This function must undo everything done by sta_info_alloc()
  193. * that may happen before sta_info_insert().
  194. */
  195. static void __sta_info_free(struct ieee80211_local *local,
  196. struct sta_info *sta)
  197. {
  198. if (sta->rate_ctrl) {
  199. rate_control_free_sta(sta);
  200. rate_control_put(sta->rate_ctrl);
  201. }
  202. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  203. wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
  204. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  205. kfree(sta);
  206. }
  207. /* Caller must hold local->sta_lock */
  208. static void sta_info_hash_add(struct ieee80211_local *local,
  209. struct sta_info *sta)
  210. {
  211. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  212. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  213. }
  214. static void sta_unblock(struct work_struct *wk)
  215. {
  216. struct sta_info *sta;
  217. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  218. if (sta->dead)
  219. return;
  220. if (!test_sta_flags(sta, WLAN_STA_PS_STA))
  221. ieee80211_sta_ps_deliver_wakeup(sta);
  222. else if (test_and_clear_sta_flags(sta, WLAN_STA_PSPOLL)) {
  223. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  224. ieee80211_sta_ps_deliver_poll_response(sta);
  225. } else
  226. clear_sta_flags(sta, WLAN_STA_PS_DRIVER);
  227. }
  228. static int sta_prepare_rate_control(struct ieee80211_local *local,
  229. struct sta_info *sta, gfp_t gfp)
  230. {
  231. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  232. return 0;
  233. sta->rate_ctrl = rate_control_get(local->rate_ctrl);
  234. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  235. &sta->sta, gfp);
  236. if (!sta->rate_ctrl_priv) {
  237. rate_control_put(sta->rate_ctrl);
  238. return -ENOMEM;
  239. }
  240. return 0;
  241. }
  242. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  243. u8 *addr, gfp_t gfp)
  244. {
  245. struct ieee80211_local *local = sdata->local;
  246. struct sta_info *sta;
  247. struct timespec uptime;
  248. int i;
  249. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  250. if (!sta)
  251. return NULL;
  252. spin_lock_init(&sta->lock);
  253. spin_lock_init(&sta->flaglock);
  254. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  255. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  256. mutex_init(&sta->ampdu_mlme.mtx);
  257. memcpy(sta->sta.addr, addr, ETH_ALEN);
  258. sta->local = local;
  259. sta->sdata = sdata;
  260. sta->last_rx = jiffies;
  261. do_posix_clock_monotonic_gettime(&uptime);
  262. sta->last_connected = uptime.tv_sec;
  263. ewma_init(&sta->avg_signal, 1024, 8);
  264. if (sta_prepare_rate_control(local, sta, gfp)) {
  265. kfree(sta);
  266. return NULL;
  267. }
  268. for (i = 0; i < STA_TID_NUM; i++) {
  269. /*
  270. * timer_to_tid must be initialized with identity mapping
  271. * to enable session_timer's data differentiation. See
  272. * sta_rx_agg_session_timer_expired for usage.
  273. */
  274. sta->timer_to_tid[i] = i;
  275. }
  276. skb_queue_head_init(&sta->ps_tx_buf);
  277. skb_queue_head_init(&sta->tx_filtered);
  278. for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
  279. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  280. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  281. wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
  282. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  283. #ifdef CONFIG_MAC80211_MESH
  284. sta->plink_state = NL80211_PLINK_LISTEN;
  285. init_timer(&sta->plink_timer);
  286. #endif
  287. return sta;
  288. }
  289. static int sta_info_finish_insert(struct sta_info *sta,
  290. bool async, bool dummy_reinsert)
  291. {
  292. struct ieee80211_local *local = sta->local;
  293. struct ieee80211_sub_if_data *sdata = sta->sdata;
  294. struct station_info sinfo;
  295. unsigned long flags;
  296. int err = 0;
  297. lockdep_assert_held(&local->sta_mtx);
  298. if (!sta->dummy || dummy_reinsert) {
  299. /* notify driver */
  300. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  301. sdata = container_of(sdata->bss,
  302. struct ieee80211_sub_if_data,
  303. u.ap);
  304. err = drv_sta_add(local, sdata, &sta->sta);
  305. if (err) {
  306. if (!async)
  307. return err;
  308. printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
  309. "driver (%d) - keeping it anyway.\n",
  310. sdata->name, sta->sta.addr, err);
  311. } else {
  312. sta->uploaded = true;
  313. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  314. if (async)
  315. wiphy_debug(local->hw.wiphy,
  316. "Finished adding IBSS STA %pM\n",
  317. sta->sta.addr);
  318. #endif
  319. }
  320. sdata = sta->sdata;
  321. }
  322. if (!dummy_reinsert) {
  323. if (!async) {
  324. local->num_sta++;
  325. local->sta_generation++;
  326. smp_mb();
  327. /* make the station visible */
  328. spin_lock_irqsave(&local->sta_lock, flags);
  329. sta_info_hash_add(local, sta);
  330. spin_unlock_irqrestore(&local->sta_lock, flags);
  331. }
  332. list_add(&sta->list, &local->sta_list);
  333. } else {
  334. sta->dummy = false;
  335. }
  336. if (!sta->dummy) {
  337. ieee80211_sta_debugfs_add(sta);
  338. rate_control_add_sta_debugfs(sta);
  339. memset(&sinfo, 0, sizeof(sinfo));
  340. sinfo.filled = 0;
  341. sinfo.generation = local->sta_generation;
  342. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  343. }
  344. return 0;
  345. }
  346. static void sta_info_finish_pending(struct ieee80211_local *local)
  347. {
  348. struct sta_info *sta;
  349. unsigned long flags;
  350. spin_lock_irqsave(&local->sta_lock, flags);
  351. while (!list_empty(&local->sta_pending_list)) {
  352. sta = list_first_entry(&local->sta_pending_list,
  353. struct sta_info, list);
  354. list_del(&sta->list);
  355. spin_unlock_irqrestore(&local->sta_lock, flags);
  356. sta_info_finish_insert(sta, true, false);
  357. spin_lock_irqsave(&local->sta_lock, flags);
  358. }
  359. spin_unlock_irqrestore(&local->sta_lock, flags);
  360. }
  361. static void sta_info_finish_work(struct work_struct *work)
  362. {
  363. struct ieee80211_local *local =
  364. container_of(work, struct ieee80211_local, sta_finish_work);
  365. mutex_lock(&local->sta_mtx);
  366. sta_info_finish_pending(local);
  367. mutex_unlock(&local->sta_mtx);
  368. }
  369. static int sta_info_insert_check(struct sta_info *sta)
  370. {
  371. struct ieee80211_sub_if_data *sdata = sta->sdata;
  372. /*
  373. * Can't be a WARN_ON because it can be triggered through a race:
  374. * something inserts a STA (on one CPU) without holding the RTNL
  375. * and another CPU turns off the net device.
  376. */
  377. if (unlikely(!ieee80211_sdata_running(sdata)))
  378. return -ENETDOWN;
  379. if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
  380. is_multicast_ether_addr(sta->sta.addr)))
  381. return -EINVAL;
  382. return 0;
  383. }
  384. static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
  385. {
  386. struct ieee80211_local *local = sta->local;
  387. struct ieee80211_sub_if_data *sdata = sta->sdata;
  388. unsigned long flags;
  389. spin_lock_irqsave(&local->sta_lock, flags);
  390. /* check if STA exists already */
  391. if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
  392. spin_unlock_irqrestore(&local->sta_lock, flags);
  393. rcu_read_lock();
  394. return -EEXIST;
  395. }
  396. local->num_sta++;
  397. local->sta_generation++;
  398. smp_mb();
  399. sta_info_hash_add(local, sta);
  400. list_add_tail(&sta->list, &local->sta_pending_list);
  401. rcu_read_lock();
  402. spin_unlock_irqrestore(&local->sta_lock, flags);
  403. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  404. wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
  405. sta->sta.addr);
  406. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  407. ieee80211_queue_work(&local->hw, &local->sta_finish_work);
  408. return 0;
  409. }
  410. /*
  411. * should be called with sta_mtx locked
  412. * this function replaces the mutex lock
  413. * with a RCU lock
  414. */
  415. static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
  416. {
  417. struct ieee80211_local *local = sta->local;
  418. struct ieee80211_sub_if_data *sdata = sta->sdata;
  419. unsigned long flags;
  420. struct sta_info *exist_sta;
  421. bool dummy_reinsert = false;
  422. int err = 0;
  423. lockdep_assert_held(&local->sta_mtx);
  424. /*
  425. * On first glance, this will look racy, because the code
  426. * in this function, which inserts a station with sleeping,
  427. * unlocks the sta_lock between checking existence in the
  428. * hash table and inserting into it.
  429. *
  430. * However, it is not racy against itself because it keeps
  431. * the mutex locked.
  432. */
  433. spin_lock_irqsave(&local->sta_lock, flags);
  434. /*
  435. * check if STA exists already.
  436. * only accept a scenario of a second call to sta_info_insert_non_ibss
  437. * with a dummy station entry that was inserted earlier
  438. * in that case - assume that the dummy station flag should
  439. * be removed.
  440. */
  441. exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
  442. if (exist_sta) {
  443. if (exist_sta == sta && sta->dummy) {
  444. dummy_reinsert = true;
  445. } else {
  446. spin_unlock_irqrestore(&local->sta_lock, flags);
  447. mutex_unlock(&local->sta_mtx);
  448. rcu_read_lock();
  449. return -EEXIST;
  450. }
  451. }
  452. spin_unlock_irqrestore(&local->sta_lock, flags);
  453. err = sta_info_finish_insert(sta, false, dummy_reinsert);
  454. if (err) {
  455. mutex_unlock(&local->sta_mtx);
  456. rcu_read_lock();
  457. return err;
  458. }
  459. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  460. wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
  461. sta->dummy ? "dummy " : "", sta->sta.addr);
  462. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  463. /* move reference to rcu-protected */
  464. rcu_read_lock();
  465. mutex_unlock(&local->sta_mtx);
  466. if (ieee80211_vif_is_mesh(&sdata->vif))
  467. mesh_accept_plinks_update(sdata);
  468. return 0;
  469. }
  470. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  471. {
  472. struct ieee80211_local *local = sta->local;
  473. struct ieee80211_sub_if_data *sdata = sta->sdata;
  474. int err = 0;
  475. err = sta_info_insert_check(sta);
  476. if (err) {
  477. rcu_read_lock();
  478. goto out_free;
  479. }
  480. /*
  481. * In ad-hoc mode, we sometimes need to insert stations
  482. * from tasklet context from the RX path. To avoid races,
  483. * always do so in that case -- see the comment below.
  484. */
  485. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  486. err = sta_info_insert_ibss(sta);
  487. if (err)
  488. goto out_free;
  489. return 0;
  490. }
  491. /*
  492. * It might seem that the function called below is in race against
  493. * the function call above that atomically inserts the station... That,
  494. * however, is not true because the above code can only
  495. * be invoked for IBSS interfaces, and the below code will
  496. * not be -- and the two do not race against each other as
  497. * the hash table also keys off the interface.
  498. */
  499. might_sleep();
  500. mutex_lock(&local->sta_mtx);
  501. err = sta_info_insert_non_ibss(sta);
  502. if (err)
  503. goto out_free;
  504. return 0;
  505. out_free:
  506. BUG_ON(!err);
  507. __sta_info_free(local, sta);
  508. return err;
  509. }
  510. int sta_info_insert(struct sta_info *sta)
  511. {
  512. int err = sta_info_insert_rcu(sta);
  513. rcu_read_unlock();
  514. return err;
  515. }
  516. /* Caller must hold sta->local->sta_mtx */
  517. int sta_info_reinsert(struct sta_info *sta)
  518. {
  519. struct ieee80211_local *local = sta->local;
  520. int err = 0;
  521. err = sta_info_insert_check(sta);
  522. if (err) {
  523. mutex_unlock(&local->sta_mtx);
  524. return err;
  525. }
  526. might_sleep();
  527. err = sta_info_insert_non_ibss(sta);
  528. rcu_read_unlock();
  529. return err;
  530. }
  531. static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
  532. {
  533. /*
  534. * This format has been mandated by the IEEE specifications,
  535. * so this line may not be changed to use the __set_bit() format.
  536. */
  537. bss->tim[aid / 8] |= (1 << (aid % 8));
  538. }
  539. static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
  540. {
  541. /*
  542. * This format has been mandated by the IEEE specifications,
  543. * so this line may not be changed to use the __clear_bit() format.
  544. */
  545. bss->tim[aid / 8] &= ~(1 << (aid % 8));
  546. }
  547. static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
  548. struct sta_info *sta)
  549. {
  550. BUG_ON(!bss);
  551. __bss_tim_set(bss, sta->sta.aid);
  552. if (sta->local->ops->set_tim) {
  553. sta->local->tim_in_locked_section = true;
  554. drv_set_tim(sta->local, &sta->sta, true);
  555. sta->local->tim_in_locked_section = false;
  556. }
  557. }
  558. void sta_info_set_tim_bit(struct sta_info *sta)
  559. {
  560. unsigned long flags;
  561. BUG_ON(!sta->sdata->bss);
  562. spin_lock_irqsave(&sta->local->sta_lock, flags);
  563. __sta_info_set_tim_bit(sta->sdata->bss, sta);
  564. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  565. }
  566. static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
  567. struct sta_info *sta)
  568. {
  569. BUG_ON(!bss);
  570. __bss_tim_clear(bss, sta->sta.aid);
  571. if (sta->local->ops->set_tim) {
  572. sta->local->tim_in_locked_section = true;
  573. drv_set_tim(sta->local, &sta->sta, false);
  574. sta->local->tim_in_locked_section = false;
  575. }
  576. }
  577. void sta_info_clear_tim_bit(struct sta_info *sta)
  578. {
  579. unsigned long flags;
  580. BUG_ON(!sta->sdata->bss);
  581. spin_lock_irqsave(&sta->local->sta_lock, flags);
  582. __sta_info_clear_tim_bit(sta->sdata->bss, sta);
  583. spin_unlock_irqrestore(&sta->local->sta_lock, flags);
  584. }
  585. static int sta_info_buffer_expired(struct sta_info *sta,
  586. struct sk_buff *skb)
  587. {
  588. struct ieee80211_tx_info *info;
  589. int timeout;
  590. if (!skb)
  591. return 0;
  592. info = IEEE80211_SKB_CB(skb);
  593. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  594. timeout = (sta->listen_interval *
  595. sta->sdata->vif.bss_conf.beacon_int *
  596. 32 / 15625) * HZ;
  597. if (timeout < STA_TX_BUFFER_EXPIRE)
  598. timeout = STA_TX_BUFFER_EXPIRE;
  599. return time_after(jiffies, info->control.jiffies + timeout);
  600. }
  601. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  602. struct sta_info *sta)
  603. {
  604. unsigned long flags;
  605. struct sk_buff *skb;
  606. if (skb_queue_empty(&sta->ps_tx_buf))
  607. return false;
  608. for (;;) {
  609. spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
  610. skb = skb_peek(&sta->ps_tx_buf);
  611. if (sta_info_buffer_expired(sta, skb))
  612. skb = __skb_dequeue(&sta->ps_tx_buf);
  613. else
  614. skb = NULL;
  615. spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
  616. if (!skb)
  617. break;
  618. local->total_ps_buffered--;
  619. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  620. printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
  621. sta->sta.addr);
  622. #endif
  623. dev_kfree_skb(skb);
  624. if (skb_queue_empty(&sta->ps_tx_buf) &&
  625. !test_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF))
  626. sta_info_clear_tim_bit(sta);
  627. }
  628. return true;
  629. }
  630. static int __must_check __sta_info_destroy(struct sta_info *sta)
  631. {
  632. struct ieee80211_local *local;
  633. struct ieee80211_sub_if_data *sdata;
  634. struct sk_buff *skb;
  635. unsigned long flags;
  636. int ret, i;
  637. might_sleep();
  638. if (!sta)
  639. return -ENOENT;
  640. local = sta->local;
  641. sdata = sta->sdata;
  642. /*
  643. * Before removing the station from the driver and
  644. * rate control, it might still start new aggregation
  645. * sessions -- block that to make sure the tear-down
  646. * will be sufficient.
  647. */
  648. set_sta_flags(sta, WLAN_STA_BLOCK_BA);
  649. ieee80211_sta_tear_down_BA_sessions(sta, true);
  650. spin_lock_irqsave(&local->sta_lock, flags);
  651. ret = sta_info_hash_del(local, sta);
  652. /* this might still be the pending list ... which is fine */
  653. if (!ret)
  654. list_del(&sta->list);
  655. spin_unlock_irqrestore(&local->sta_lock, flags);
  656. if (ret)
  657. return ret;
  658. mutex_lock(&local->key_mtx);
  659. for (i = 0; i < NUM_DEFAULT_KEYS; i++)
  660. __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
  661. if (sta->ptk)
  662. __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
  663. mutex_unlock(&local->key_mtx);
  664. sta->dead = true;
  665. if (test_and_clear_sta_flags(sta,
  666. WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) {
  667. BUG_ON(!sdata->bss);
  668. atomic_dec(&sdata->bss->num_sta_ps);
  669. __sta_info_clear_tim_bit(sdata->bss, sta);
  670. }
  671. local->num_sta--;
  672. local->sta_generation++;
  673. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  674. rcu_assign_pointer(sdata->u.vlan.sta, NULL);
  675. if (sta->uploaded) {
  676. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  677. sdata = container_of(sdata->bss,
  678. struct ieee80211_sub_if_data,
  679. u.ap);
  680. drv_sta_remove(local, sdata, &sta->sta);
  681. sdata = sta->sdata;
  682. }
  683. /*
  684. * At this point, after we wait for an RCU grace period,
  685. * neither mac80211 nor the driver can reference this
  686. * sta struct any more except by still existing timers
  687. * associated with this station that we clean up below.
  688. */
  689. synchronize_rcu();
  690. #ifdef CONFIG_MAC80211_MESH
  691. if (ieee80211_vif_is_mesh(&sdata->vif))
  692. mesh_accept_plinks_update(sdata);
  693. #endif
  694. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  695. wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
  696. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  697. cancel_work_sync(&sta->drv_unblock_wk);
  698. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  699. rate_control_remove_sta_debugfs(sta);
  700. ieee80211_sta_debugfs_remove(sta);
  701. #ifdef CONFIG_MAC80211_MESH
  702. if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  703. mesh_plink_deactivate(sta);
  704. del_timer_sync(&sta->plink_timer);
  705. }
  706. #endif
  707. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  708. local->total_ps_buffered--;
  709. dev_kfree_skb_any(skb);
  710. }
  711. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL)
  712. dev_kfree_skb_any(skb);
  713. __sta_info_free(local, sta);
  714. return 0;
  715. }
  716. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  717. {
  718. struct sta_info *sta;
  719. int ret;
  720. mutex_lock(&sdata->local->sta_mtx);
  721. sta = sta_info_get_rx(sdata, addr);
  722. ret = __sta_info_destroy(sta);
  723. mutex_unlock(&sdata->local->sta_mtx);
  724. return ret;
  725. }
  726. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  727. const u8 *addr)
  728. {
  729. struct sta_info *sta;
  730. int ret;
  731. mutex_lock(&sdata->local->sta_mtx);
  732. sta = sta_info_get_bss_rx(sdata, addr);
  733. ret = __sta_info_destroy(sta);
  734. mutex_unlock(&sdata->local->sta_mtx);
  735. return ret;
  736. }
  737. static void sta_info_cleanup(unsigned long data)
  738. {
  739. struct ieee80211_local *local = (struct ieee80211_local *) data;
  740. struct sta_info *sta;
  741. bool timer_needed = false;
  742. rcu_read_lock();
  743. list_for_each_entry_rcu(sta, &local->sta_list, list)
  744. if (sta_info_cleanup_expire_buffered(local, sta))
  745. timer_needed = true;
  746. rcu_read_unlock();
  747. if (local->quiescing)
  748. return;
  749. if (!timer_needed)
  750. return;
  751. mod_timer(&local->sta_cleanup,
  752. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  753. }
  754. void sta_info_init(struct ieee80211_local *local)
  755. {
  756. spin_lock_init(&local->sta_lock);
  757. mutex_init(&local->sta_mtx);
  758. INIT_LIST_HEAD(&local->sta_list);
  759. INIT_LIST_HEAD(&local->sta_pending_list);
  760. INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
  761. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  762. (unsigned long)local);
  763. }
  764. void sta_info_stop(struct ieee80211_local *local)
  765. {
  766. del_timer(&local->sta_cleanup);
  767. sta_info_flush(local, NULL);
  768. }
  769. /**
  770. * sta_info_flush - flush matching STA entries from the STA table
  771. *
  772. * Returns the number of removed STA entries.
  773. *
  774. * @local: local interface data
  775. * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
  776. */
  777. int sta_info_flush(struct ieee80211_local *local,
  778. struct ieee80211_sub_if_data *sdata)
  779. {
  780. struct sta_info *sta, *tmp;
  781. int ret = 0;
  782. might_sleep();
  783. mutex_lock(&local->sta_mtx);
  784. sta_info_finish_pending(local);
  785. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  786. if (!sdata || sdata == sta->sdata)
  787. WARN_ON(__sta_info_destroy(sta));
  788. }
  789. mutex_unlock(&local->sta_mtx);
  790. return ret;
  791. }
  792. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  793. unsigned long exp_time)
  794. {
  795. struct ieee80211_local *local = sdata->local;
  796. struct sta_info *sta, *tmp;
  797. mutex_lock(&local->sta_mtx);
  798. list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
  799. if (time_after(jiffies, sta->last_rx + exp_time)) {
  800. #ifdef CONFIG_MAC80211_IBSS_DEBUG
  801. printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
  802. sdata->name, sta->sta.addr);
  803. #endif
  804. WARN_ON(__sta_info_destroy(sta));
  805. }
  806. mutex_unlock(&local->sta_mtx);
  807. }
  808. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  809. const u8 *addr,
  810. const u8 *localaddr)
  811. {
  812. struct sta_info *sta, *nxt;
  813. /*
  814. * Just return a random station if localaddr is NULL
  815. * ... first in list.
  816. */
  817. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  818. if (localaddr &&
  819. compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
  820. continue;
  821. if (!sta->uploaded)
  822. return NULL;
  823. return &sta->sta;
  824. }
  825. return NULL;
  826. }
  827. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  828. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  829. const u8 *addr)
  830. {
  831. struct sta_info *sta;
  832. if (!vif)
  833. return NULL;
  834. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  835. if (!sta)
  836. return NULL;
  837. if (!sta->uploaded)
  838. return NULL;
  839. return &sta->sta;
  840. }
  841. EXPORT_SYMBOL(ieee80211_find_sta);
  842. static void clear_sta_ps_flags(void *_sta)
  843. {
  844. struct sta_info *sta = _sta;
  845. clear_sta_flags(sta, WLAN_STA_PS_DRIVER | WLAN_STA_PS_STA);
  846. }
  847. /* powersave support code */
  848. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  849. {
  850. struct ieee80211_sub_if_data *sdata = sta->sdata;
  851. struct ieee80211_local *local = sdata->local;
  852. int sent, buffered;
  853. clear_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  854. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  855. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  856. if (!skb_queue_empty(&sta->ps_tx_buf))
  857. sta_info_clear_tim_bit(sta);
  858. /* Send all buffered frames to the station */
  859. sent = ieee80211_add_pending_skbs(local, &sta->tx_filtered);
  860. buffered = ieee80211_add_pending_skbs_fn(local, &sta->ps_tx_buf,
  861. clear_sta_ps_flags, sta);
  862. sent += buffered;
  863. local->total_ps_buffered -= buffered;
  864. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  865. printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
  866. "since STA not sleeping anymore\n", sdata->name,
  867. sta->sta.addr, sta->sta.aid, sent - buffered, buffered);
  868. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  869. }
  870. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  871. {
  872. struct ieee80211_sub_if_data *sdata = sta->sdata;
  873. struct ieee80211_local *local = sdata->local;
  874. struct sk_buff *skb;
  875. int no_pending_pkts;
  876. skb = skb_dequeue(&sta->tx_filtered);
  877. if (!skb) {
  878. skb = skb_dequeue(&sta->ps_tx_buf);
  879. if (skb)
  880. local->total_ps_buffered--;
  881. }
  882. no_pending_pkts = skb_queue_empty(&sta->tx_filtered) &&
  883. skb_queue_empty(&sta->ps_tx_buf);
  884. if (skb) {
  885. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  886. struct ieee80211_hdr *hdr =
  887. (struct ieee80211_hdr *) skb->data;
  888. /*
  889. * Tell TX path to send this frame even though the STA may
  890. * still remain is PS mode after this frame exchange.
  891. */
  892. info->flags |= IEEE80211_TX_CTL_PSPOLL_RESPONSE;
  893. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  894. printk(KERN_DEBUG "STA %pM aid %d: PS Poll (entries after %d)\n",
  895. sta->sta.addr, sta->sta.aid,
  896. skb_queue_len(&sta->ps_tx_buf));
  897. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  898. /* Use MoreData flag to indicate whether there are more
  899. * buffered frames for this STA */
  900. if (no_pending_pkts)
  901. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  902. else
  903. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  904. ieee80211_add_pending_skb(local, skb);
  905. if (no_pending_pkts)
  906. sta_info_clear_tim_bit(sta);
  907. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  908. } else {
  909. /*
  910. * FIXME: This can be the result of a race condition between
  911. * us expiring a frame and the station polling for it.
  912. * Should we send it a null-func frame indicating we
  913. * have nothing buffered for it?
  914. */
  915. printk(KERN_DEBUG "%s: STA %pM sent PS Poll even "
  916. "though there are no buffered frames for it\n",
  917. sdata->name, sta->sta.addr);
  918. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  919. }
  920. }
  921. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  922. struct ieee80211_sta *pubsta, bool block)
  923. {
  924. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  925. trace_api_sta_block_awake(sta->local, pubsta, block);
  926. if (block)
  927. set_sta_flags(sta, WLAN_STA_PS_DRIVER);
  928. else if (test_sta_flags(sta, WLAN_STA_PS_DRIVER))
  929. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  930. }
  931. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  932. void ieee80211_sta_set_tim(struct ieee80211_sta *pubsta)
  933. {
  934. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  935. set_sta_flags(sta, WLAN_STA_PS_DRIVER_BUF);
  936. sta_info_set_tim_bit(sta);
  937. }
  938. EXPORT_SYMBOL(ieee80211_sta_set_tim);