util.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/bitmap.h>
  21. #include <net/net_namespace.h>
  22. #include <net/cfg80211.h>
  23. #include <net/rtnetlink.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wme.h"
  29. #include "led.h"
  30. #include "wep.h"
  31. /* privid for wiphys to determine whether they belong to us or not */
  32. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  33. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  34. {
  35. struct ieee80211_local *local;
  36. BUG_ON(!wiphy);
  37. local = wiphy_priv(wiphy);
  38. return &local->hw;
  39. }
  40. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  41. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  42. enum nl80211_iftype type)
  43. {
  44. __le16 fc = hdr->frame_control;
  45. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  46. if (len < 16)
  47. return NULL;
  48. if (ieee80211_is_data(fc)) {
  49. if (len < 24) /* drop incorrect hdr len (data) */
  50. return NULL;
  51. if (ieee80211_has_a4(fc))
  52. return NULL;
  53. if (ieee80211_has_tods(fc))
  54. return hdr->addr1;
  55. if (ieee80211_has_fromds(fc))
  56. return hdr->addr2;
  57. return hdr->addr3;
  58. }
  59. if (ieee80211_is_mgmt(fc)) {
  60. if (len < 24) /* drop incorrect hdr len (mgmt) */
  61. return NULL;
  62. return hdr->addr3;
  63. }
  64. if (ieee80211_is_ctl(fc)) {
  65. if(ieee80211_is_pspoll(fc))
  66. return hdr->addr1;
  67. if (ieee80211_is_back_req(fc)) {
  68. switch (type) {
  69. case NL80211_IFTYPE_STATION:
  70. return hdr->addr2;
  71. case NL80211_IFTYPE_AP:
  72. case NL80211_IFTYPE_AP_VLAN:
  73. return hdr->addr1;
  74. default:
  75. break; /* fall through to the return */
  76. }
  77. }
  78. }
  79. return NULL;
  80. }
  81. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  82. {
  83. struct sk_buff *skb = tx->skb;
  84. struct ieee80211_hdr *hdr;
  85. do {
  86. hdr = (struct ieee80211_hdr *) skb->data;
  87. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  88. } while ((skb = skb->next));
  89. }
  90. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  91. int rate, int erp, int short_preamble)
  92. {
  93. int dur;
  94. /* calculate duration (in microseconds, rounded up to next higher
  95. * integer if it includes a fractional microsecond) to send frame of
  96. * len bytes (does not include FCS) at the given rate. Duration will
  97. * also include SIFS.
  98. *
  99. * rate is in 100 kbps, so divident is multiplied by 10 in the
  100. * DIV_ROUND_UP() operations.
  101. */
  102. if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
  103. /*
  104. * OFDM:
  105. *
  106. * N_DBPS = DATARATE x 4
  107. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  108. * (16 = SIGNAL time, 6 = tail bits)
  109. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  110. *
  111. * T_SYM = 4 usec
  112. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  113. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  114. * signal ext = 6 usec
  115. */
  116. dur = 16; /* SIFS + signal ext */
  117. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  118. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  119. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  120. 4 * rate); /* T_SYM x N_SYM */
  121. } else {
  122. /*
  123. * 802.11b or 802.11g with 802.11b compatibility:
  124. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  125. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  126. *
  127. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  128. * aSIFSTime = 10 usec
  129. * aPreambleLength = 144 usec or 72 usec with short preamble
  130. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  131. */
  132. dur = 10; /* aSIFSTime = 10 usec */
  133. dur += short_preamble ? (72 + 24) : (144 + 48);
  134. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  135. }
  136. return dur;
  137. }
  138. /* Exported duration function for driver use */
  139. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  140. struct ieee80211_vif *vif,
  141. size_t frame_len,
  142. struct ieee80211_rate *rate)
  143. {
  144. struct ieee80211_local *local = hw_to_local(hw);
  145. struct ieee80211_sub_if_data *sdata;
  146. u16 dur;
  147. int erp;
  148. bool short_preamble = false;
  149. erp = 0;
  150. if (vif) {
  151. sdata = vif_to_sdata(vif);
  152. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  153. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  154. erp = rate->flags & IEEE80211_RATE_ERP_G;
  155. }
  156. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
  157. short_preamble);
  158. return cpu_to_le16(dur);
  159. }
  160. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  161. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  162. struct ieee80211_vif *vif, size_t frame_len,
  163. const struct ieee80211_tx_info *frame_txctl)
  164. {
  165. struct ieee80211_local *local = hw_to_local(hw);
  166. struct ieee80211_rate *rate;
  167. struct ieee80211_sub_if_data *sdata;
  168. bool short_preamble;
  169. int erp;
  170. u16 dur;
  171. struct ieee80211_supported_band *sband;
  172. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  173. short_preamble = false;
  174. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  175. erp = 0;
  176. if (vif) {
  177. sdata = vif_to_sdata(vif);
  178. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  179. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  180. erp = rate->flags & IEEE80211_RATE_ERP_G;
  181. }
  182. /* CTS duration */
  183. dur = ieee80211_frame_duration(local, 10, rate->bitrate,
  184. erp, short_preamble);
  185. /* Data frame duration */
  186. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  187. erp, short_preamble);
  188. /* ACK duration */
  189. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  190. erp, short_preamble);
  191. return cpu_to_le16(dur);
  192. }
  193. EXPORT_SYMBOL(ieee80211_rts_duration);
  194. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  195. struct ieee80211_vif *vif,
  196. size_t frame_len,
  197. const struct ieee80211_tx_info *frame_txctl)
  198. {
  199. struct ieee80211_local *local = hw_to_local(hw);
  200. struct ieee80211_rate *rate;
  201. struct ieee80211_sub_if_data *sdata;
  202. bool short_preamble;
  203. int erp;
  204. u16 dur;
  205. struct ieee80211_supported_band *sband;
  206. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  207. short_preamble = false;
  208. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  209. erp = 0;
  210. if (vif) {
  211. sdata = vif_to_sdata(vif);
  212. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  213. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  214. erp = rate->flags & IEEE80211_RATE_ERP_G;
  215. }
  216. /* Data frame duration */
  217. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
  218. erp, short_preamble);
  219. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  220. /* ACK duration */
  221. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  222. erp, short_preamble);
  223. }
  224. return cpu_to_le16(dur);
  225. }
  226. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  227. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  228. enum queue_stop_reason reason)
  229. {
  230. struct ieee80211_local *local = hw_to_local(hw);
  231. struct ieee80211_sub_if_data *sdata;
  232. trace_wake_queue(local, queue, reason);
  233. if (WARN_ON(queue >= hw->queues))
  234. return;
  235. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  236. if (local->queue_stop_reasons[queue] != 0)
  237. /* someone still has this queue stopped */
  238. return;
  239. if (skb_queue_empty(&local->pending[queue])) {
  240. rcu_read_lock();
  241. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  242. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  243. continue;
  244. netif_wake_subqueue(sdata->dev, queue);
  245. }
  246. rcu_read_unlock();
  247. } else
  248. tasklet_schedule(&local->tx_pending_tasklet);
  249. }
  250. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  251. enum queue_stop_reason reason)
  252. {
  253. struct ieee80211_local *local = hw_to_local(hw);
  254. unsigned long flags;
  255. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  256. __ieee80211_wake_queue(hw, queue, reason);
  257. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  258. }
  259. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  260. {
  261. ieee80211_wake_queue_by_reason(hw, queue,
  262. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  263. }
  264. EXPORT_SYMBOL(ieee80211_wake_queue);
  265. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  266. enum queue_stop_reason reason)
  267. {
  268. struct ieee80211_local *local = hw_to_local(hw);
  269. struct ieee80211_sub_if_data *sdata;
  270. trace_stop_queue(local, queue, reason);
  271. if (WARN_ON(queue >= hw->queues))
  272. return;
  273. __set_bit(reason, &local->queue_stop_reasons[queue]);
  274. rcu_read_lock();
  275. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  276. netif_stop_subqueue(sdata->dev, queue);
  277. rcu_read_unlock();
  278. }
  279. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  280. enum queue_stop_reason reason)
  281. {
  282. struct ieee80211_local *local = hw_to_local(hw);
  283. unsigned long flags;
  284. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  285. __ieee80211_stop_queue(hw, queue, reason);
  286. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  287. }
  288. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  289. {
  290. ieee80211_stop_queue_by_reason(hw, queue,
  291. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  292. }
  293. EXPORT_SYMBOL(ieee80211_stop_queue);
  294. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  295. struct sk_buff *skb)
  296. {
  297. struct ieee80211_hw *hw = &local->hw;
  298. unsigned long flags;
  299. int queue = skb_get_queue_mapping(skb);
  300. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  301. if (WARN_ON(!info->control.vif)) {
  302. kfree_skb(skb);
  303. return;
  304. }
  305. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  306. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  307. __skb_queue_tail(&local->pending[queue], skb);
  308. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  309. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  310. }
  311. int ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  312. struct sk_buff_head *skbs,
  313. void (*fn)(void *data), void *data)
  314. {
  315. struct ieee80211_hw *hw = &local->hw;
  316. struct sk_buff *skb;
  317. unsigned long flags;
  318. int queue, ret = 0, i;
  319. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  320. for (i = 0; i < hw->queues; i++)
  321. __ieee80211_stop_queue(hw, i,
  322. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  323. while ((skb = skb_dequeue(skbs))) {
  324. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  325. if (WARN_ON(!info->control.vif)) {
  326. kfree_skb(skb);
  327. continue;
  328. }
  329. ret++;
  330. queue = skb_get_queue_mapping(skb);
  331. __skb_queue_tail(&local->pending[queue], skb);
  332. }
  333. if (fn)
  334. fn(data);
  335. for (i = 0; i < hw->queues; i++)
  336. __ieee80211_wake_queue(hw, i,
  337. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  338. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  339. return ret;
  340. }
  341. int ieee80211_add_pending_skbs(struct ieee80211_local *local,
  342. struct sk_buff_head *skbs)
  343. {
  344. return ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
  345. }
  346. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  347. enum queue_stop_reason reason)
  348. {
  349. struct ieee80211_local *local = hw_to_local(hw);
  350. unsigned long flags;
  351. int i;
  352. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  353. for (i = 0; i < hw->queues; i++)
  354. __ieee80211_stop_queue(hw, i, reason);
  355. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  356. }
  357. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  358. {
  359. ieee80211_stop_queues_by_reason(hw,
  360. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  361. }
  362. EXPORT_SYMBOL(ieee80211_stop_queues);
  363. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  364. {
  365. struct ieee80211_local *local = hw_to_local(hw);
  366. unsigned long flags;
  367. int ret;
  368. if (WARN_ON(queue >= hw->queues))
  369. return true;
  370. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  371. ret = !!local->queue_stop_reasons[queue];
  372. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL(ieee80211_queue_stopped);
  376. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  377. enum queue_stop_reason reason)
  378. {
  379. struct ieee80211_local *local = hw_to_local(hw);
  380. unsigned long flags;
  381. int i;
  382. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  383. for (i = 0; i < hw->queues; i++)
  384. __ieee80211_wake_queue(hw, i, reason);
  385. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  386. }
  387. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  388. {
  389. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  390. }
  391. EXPORT_SYMBOL(ieee80211_wake_queues);
  392. void ieee80211_iterate_active_interfaces(
  393. struct ieee80211_hw *hw,
  394. void (*iterator)(void *data, u8 *mac,
  395. struct ieee80211_vif *vif),
  396. void *data)
  397. {
  398. struct ieee80211_local *local = hw_to_local(hw);
  399. struct ieee80211_sub_if_data *sdata;
  400. mutex_lock(&local->iflist_mtx);
  401. list_for_each_entry(sdata, &local->interfaces, list) {
  402. switch (sdata->vif.type) {
  403. case NL80211_IFTYPE_MONITOR:
  404. case NL80211_IFTYPE_AP_VLAN:
  405. continue;
  406. default:
  407. break;
  408. }
  409. if (ieee80211_sdata_running(sdata))
  410. iterator(data, sdata->vif.addr,
  411. &sdata->vif);
  412. }
  413. mutex_unlock(&local->iflist_mtx);
  414. }
  415. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  416. void ieee80211_iterate_active_interfaces_atomic(
  417. struct ieee80211_hw *hw,
  418. void (*iterator)(void *data, u8 *mac,
  419. struct ieee80211_vif *vif),
  420. void *data)
  421. {
  422. struct ieee80211_local *local = hw_to_local(hw);
  423. struct ieee80211_sub_if_data *sdata;
  424. rcu_read_lock();
  425. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  426. switch (sdata->vif.type) {
  427. case NL80211_IFTYPE_MONITOR:
  428. case NL80211_IFTYPE_AP_VLAN:
  429. continue;
  430. default:
  431. break;
  432. }
  433. if (ieee80211_sdata_running(sdata))
  434. iterator(data, sdata->vif.addr,
  435. &sdata->vif);
  436. }
  437. rcu_read_unlock();
  438. }
  439. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  440. /*
  441. * Nothing should have been stuffed into the workqueue during
  442. * the suspend->resume cycle. If this WARN is seen then there
  443. * is a bug with either the driver suspend or something in
  444. * mac80211 stuffing into the workqueue which we haven't yet
  445. * cleared during mac80211's suspend cycle.
  446. */
  447. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  448. {
  449. if (WARN(local->suspended && !local->resuming,
  450. "queueing ieee80211 work while going to suspend\n"))
  451. return false;
  452. return true;
  453. }
  454. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  455. {
  456. struct ieee80211_local *local = hw_to_local(hw);
  457. if (!ieee80211_can_queue_work(local))
  458. return;
  459. queue_work(local->workqueue, work);
  460. }
  461. EXPORT_SYMBOL(ieee80211_queue_work);
  462. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  463. struct delayed_work *dwork,
  464. unsigned long delay)
  465. {
  466. struct ieee80211_local *local = hw_to_local(hw);
  467. if (!ieee80211_can_queue_work(local))
  468. return;
  469. queue_delayed_work(local->workqueue, dwork, delay);
  470. }
  471. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  472. void ieee802_11_parse_elems(u8 *start, size_t len,
  473. struct ieee802_11_elems *elems)
  474. {
  475. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  476. }
  477. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  478. {
  479. struct ieee80211_local *local = sdata->local;
  480. struct ieee80211_tx_queue_params qparam;
  481. int queue;
  482. bool use_11b;
  483. int aCWmin, aCWmax;
  484. if (!local->ops->conf_tx)
  485. return;
  486. memset(&qparam, 0, sizeof(qparam));
  487. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  488. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  489. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  490. /* Set defaults according to 802.11-2007 Table 7-37 */
  491. aCWmax = 1023;
  492. if (use_11b)
  493. aCWmin = 31;
  494. else
  495. aCWmin = 15;
  496. switch (queue) {
  497. case 3: /* AC_BK */
  498. qparam.cw_max = aCWmax;
  499. qparam.cw_min = aCWmin;
  500. qparam.txop = 0;
  501. qparam.aifs = 7;
  502. break;
  503. default: /* never happens but let's not leave undefined */
  504. case 2: /* AC_BE */
  505. qparam.cw_max = aCWmax;
  506. qparam.cw_min = aCWmin;
  507. qparam.txop = 0;
  508. qparam.aifs = 3;
  509. break;
  510. case 1: /* AC_VI */
  511. qparam.cw_max = aCWmin;
  512. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  513. if (use_11b)
  514. qparam.txop = 6016/32;
  515. else
  516. qparam.txop = 3008/32;
  517. qparam.aifs = 2;
  518. break;
  519. case 0: /* AC_VO */
  520. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  521. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  522. if (use_11b)
  523. qparam.txop = 3264/32;
  524. else
  525. qparam.txop = 1504/32;
  526. qparam.aifs = 2;
  527. break;
  528. }
  529. qparam.uapsd = false;
  530. local->tx_conf[queue] = qparam;
  531. drv_conf_tx(local, queue, &qparam);
  532. }
  533. /* after reinitialize QoS TX queues setting to default,
  534. * disable QoS at all */
  535. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  536. sdata->vif.bss_conf.qos =
  537. sdata->vif.type != NL80211_IFTYPE_STATION;
  538. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  539. }
  540. }
  541. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  542. const size_t supp_rates_len,
  543. const u8 *supp_rates)
  544. {
  545. struct ieee80211_local *local = sdata->local;
  546. int i, have_higher_than_11mbit = 0;
  547. /* cf. IEEE 802.11 9.2.12 */
  548. for (i = 0; i < supp_rates_len; i++)
  549. if ((supp_rates[i] & 0x7f) * 5 > 110)
  550. have_higher_than_11mbit = 1;
  551. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  552. have_higher_than_11mbit)
  553. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  554. else
  555. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  556. ieee80211_set_wmm_default(sdata);
  557. }
  558. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  559. enum ieee80211_band band)
  560. {
  561. struct ieee80211_supported_band *sband;
  562. struct ieee80211_rate *bitrates;
  563. u32 mandatory_rates;
  564. enum ieee80211_rate_flags mandatory_flag;
  565. int i;
  566. sband = local->hw.wiphy->bands[band];
  567. if (!sband) {
  568. WARN_ON(1);
  569. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  570. }
  571. if (band == IEEE80211_BAND_2GHZ)
  572. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  573. else
  574. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  575. bitrates = sband->bitrates;
  576. mandatory_rates = 0;
  577. for (i = 0; i < sband->n_bitrates; i++)
  578. if (bitrates[i].flags & mandatory_flag)
  579. mandatory_rates |= BIT(i);
  580. return mandatory_rates;
  581. }
  582. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  583. u16 transaction, u16 auth_alg,
  584. u8 *extra, size_t extra_len, const u8 *bssid,
  585. const u8 *key, u8 key_len, u8 key_idx)
  586. {
  587. struct ieee80211_local *local = sdata->local;
  588. struct sk_buff *skb;
  589. struct ieee80211_mgmt *mgmt;
  590. int err;
  591. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  592. sizeof(*mgmt) + 6 + extra_len);
  593. if (!skb)
  594. return;
  595. skb_reserve(skb, local->hw.extra_tx_headroom);
  596. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  597. memset(mgmt, 0, 24 + 6);
  598. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  599. IEEE80211_STYPE_AUTH);
  600. memcpy(mgmt->da, bssid, ETH_ALEN);
  601. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  602. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  603. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  604. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  605. mgmt->u.auth.status_code = cpu_to_le16(0);
  606. if (extra)
  607. memcpy(skb_put(skb, extra_len), extra, extra_len);
  608. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  609. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  610. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  611. WARN_ON(err);
  612. }
  613. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  614. ieee80211_tx_skb(sdata, skb);
  615. }
  616. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  617. const u8 *ie, size_t ie_len,
  618. enum ieee80211_band band, u32 rate_mask,
  619. u8 channel)
  620. {
  621. struct ieee80211_supported_band *sband;
  622. u8 *pos;
  623. size_t offset = 0, noffset;
  624. int supp_rates_len, i;
  625. u8 rates[32];
  626. int num_rates;
  627. int ext_rates_len;
  628. sband = local->hw.wiphy->bands[band];
  629. pos = buffer;
  630. num_rates = 0;
  631. for (i = 0; i < sband->n_bitrates; i++) {
  632. if ((BIT(i) & rate_mask) == 0)
  633. continue; /* skip rate */
  634. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  635. }
  636. supp_rates_len = min_t(int, num_rates, 8);
  637. *pos++ = WLAN_EID_SUPP_RATES;
  638. *pos++ = supp_rates_len;
  639. memcpy(pos, rates, supp_rates_len);
  640. pos += supp_rates_len;
  641. /* insert "request information" if in custom IEs */
  642. if (ie && ie_len) {
  643. static const u8 before_extrates[] = {
  644. WLAN_EID_SSID,
  645. WLAN_EID_SUPP_RATES,
  646. WLAN_EID_REQUEST,
  647. };
  648. noffset = ieee80211_ie_split(ie, ie_len,
  649. before_extrates,
  650. ARRAY_SIZE(before_extrates),
  651. offset);
  652. memcpy(pos, ie + offset, noffset - offset);
  653. pos += noffset - offset;
  654. offset = noffset;
  655. }
  656. ext_rates_len = num_rates - supp_rates_len;
  657. if (ext_rates_len > 0) {
  658. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  659. *pos++ = ext_rates_len;
  660. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  661. pos += ext_rates_len;
  662. }
  663. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  664. *pos++ = WLAN_EID_DS_PARAMS;
  665. *pos++ = 1;
  666. *pos++ = channel;
  667. }
  668. /* insert custom IEs that go before HT */
  669. if (ie && ie_len) {
  670. static const u8 before_ht[] = {
  671. WLAN_EID_SSID,
  672. WLAN_EID_SUPP_RATES,
  673. WLAN_EID_REQUEST,
  674. WLAN_EID_EXT_SUPP_RATES,
  675. WLAN_EID_DS_PARAMS,
  676. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  677. };
  678. noffset = ieee80211_ie_split(ie, ie_len,
  679. before_ht, ARRAY_SIZE(before_ht),
  680. offset);
  681. memcpy(pos, ie + offset, noffset - offset);
  682. pos += noffset - offset;
  683. offset = noffset;
  684. }
  685. if (sband->ht_cap.ht_supported) {
  686. u16 cap = sband->ht_cap.cap;
  687. __le16 tmp;
  688. *pos++ = WLAN_EID_HT_CAPABILITY;
  689. *pos++ = sizeof(struct ieee80211_ht_cap);
  690. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  691. tmp = cpu_to_le16(cap);
  692. memcpy(pos, &tmp, sizeof(u16));
  693. pos += sizeof(u16);
  694. *pos++ = sband->ht_cap.ampdu_factor |
  695. (sband->ht_cap.ampdu_density <<
  696. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  697. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  698. pos += sizeof(sband->ht_cap.mcs);
  699. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  700. }
  701. /*
  702. * If adding more here, adjust code in main.c
  703. * that calculates local->scan_ies_len.
  704. */
  705. /* add any remaining custom IEs */
  706. if (ie && ie_len) {
  707. noffset = ie_len;
  708. memcpy(pos, ie + offset, noffset - offset);
  709. pos += noffset - offset;
  710. }
  711. return pos - buffer;
  712. }
  713. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  714. u8 *dst, u32 ratemask,
  715. const u8 *ssid, size_t ssid_len,
  716. const u8 *ie, size_t ie_len,
  717. bool directed)
  718. {
  719. struct ieee80211_local *local = sdata->local;
  720. struct sk_buff *skb;
  721. struct ieee80211_mgmt *mgmt;
  722. size_t buf_len;
  723. u8 *buf;
  724. u8 chan;
  725. /* FIXME: come up with a proper value */
  726. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  727. if (!buf)
  728. return NULL;
  729. /*
  730. * Do not send DS Channel parameter for directed probe requests
  731. * in order to maximize the chance that we get a response. Some
  732. * badly-behaved APs don't respond when this parameter is included.
  733. */
  734. if (directed)
  735. chan = 0;
  736. else
  737. chan = ieee80211_frequency_to_channel(
  738. local->hw.conf.channel->center_freq);
  739. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  740. local->hw.conf.channel->band,
  741. ratemask, chan);
  742. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  743. ssid, ssid_len,
  744. buf, buf_len);
  745. if (dst) {
  746. mgmt = (struct ieee80211_mgmt *) skb->data;
  747. memcpy(mgmt->da, dst, ETH_ALEN);
  748. memcpy(mgmt->bssid, dst, ETH_ALEN);
  749. }
  750. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  751. kfree(buf);
  752. return skb;
  753. }
  754. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  755. const u8 *ssid, size_t ssid_len,
  756. const u8 *ie, size_t ie_len,
  757. u32 ratemask, bool directed, bool no_cck)
  758. {
  759. struct sk_buff *skb;
  760. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  761. ie, ie_len, directed);
  762. if (skb) {
  763. if (no_cck)
  764. IEEE80211_SKB_CB(skb)->flags |=
  765. IEEE80211_TX_CTL_NO_CCK_RATE;
  766. ieee80211_tx_skb(sdata, skb);
  767. }
  768. }
  769. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  770. struct ieee802_11_elems *elems,
  771. enum ieee80211_band band)
  772. {
  773. struct ieee80211_supported_band *sband;
  774. struct ieee80211_rate *bitrates;
  775. size_t num_rates;
  776. u32 supp_rates;
  777. int i, j;
  778. sband = local->hw.wiphy->bands[band];
  779. if (!sband) {
  780. WARN_ON(1);
  781. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  782. }
  783. bitrates = sband->bitrates;
  784. num_rates = sband->n_bitrates;
  785. supp_rates = 0;
  786. for (i = 0; i < elems->supp_rates_len +
  787. elems->ext_supp_rates_len; i++) {
  788. u8 rate = 0;
  789. int own_rate;
  790. if (i < elems->supp_rates_len)
  791. rate = elems->supp_rates[i];
  792. else if (elems->ext_supp_rates)
  793. rate = elems->ext_supp_rates
  794. [i - elems->supp_rates_len];
  795. own_rate = 5 * (rate & 0x7f);
  796. for (j = 0; j < num_rates; j++)
  797. if (bitrates[j].bitrate == own_rate)
  798. supp_rates |= BIT(j);
  799. }
  800. return supp_rates;
  801. }
  802. void ieee80211_stop_device(struct ieee80211_local *local)
  803. {
  804. ieee80211_led_radio(local, false);
  805. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  806. cancel_work_sync(&local->reconfig_filter);
  807. flush_workqueue(local->workqueue);
  808. drv_stop(local);
  809. }
  810. int ieee80211_reconfig(struct ieee80211_local *local)
  811. {
  812. struct ieee80211_hw *hw = &local->hw;
  813. struct ieee80211_sub_if_data *sdata;
  814. struct sta_info *sta;
  815. int res, i;
  816. #ifdef CONFIG_PM
  817. if (local->suspended)
  818. local->resuming = true;
  819. if (local->wowlan) {
  820. local->wowlan = false;
  821. res = drv_resume(local);
  822. if (res < 0) {
  823. local->resuming = false;
  824. return res;
  825. }
  826. if (res == 0)
  827. goto wake_up;
  828. WARN_ON(res > 1);
  829. /*
  830. * res is 1, which means the driver requested
  831. * to go through a regular reset on wakeup.
  832. */
  833. }
  834. #endif
  835. /* setup fragmentation threshold */
  836. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  837. /* setup RTS threshold */
  838. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  839. /* reset coverage class */
  840. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  841. /* everything else happens only if HW was up & running */
  842. if (!local->open_count)
  843. goto wake_up;
  844. /*
  845. * Upon resume hardware can sometimes be goofy due to
  846. * various platform / driver / bus issues, so restarting
  847. * the device may at times not work immediately. Propagate
  848. * the error.
  849. */
  850. res = drv_start(local);
  851. if (res) {
  852. WARN(local->suspended, "Hardware became unavailable "
  853. "upon resume. This could be a software issue "
  854. "prior to suspend or a hardware issue.\n");
  855. return res;
  856. }
  857. ieee80211_led_radio(local, true);
  858. ieee80211_mod_tpt_led_trig(local,
  859. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  860. /* add interfaces */
  861. list_for_each_entry(sdata, &local->interfaces, list) {
  862. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  863. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  864. ieee80211_sdata_running(sdata))
  865. res = drv_add_interface(local, &sdata->vif);
  866. }
  867. /* add STAs back */
  868. mutex_lock(&local->sta_mtx);
  869. list_for_each_entry(sta, &local->sta_list, list) {
  870. if (sta->uploaded) {
  871. sdata = sta->sdata;
  872. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  873. sdata = container_of(sdata->bss,
  874. struct ieee80211_sub_if_data,
  875. u.ap);
  876. memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
  877. WARN_ON(drv_sta_add(local, sdata, &sta->sta));
  878. }
  879. }
  880. mutex_unlock(&local->sta_mtx);
  881. /* reconfigure tx conf */
  882. for (i = 0; i < hw->queues; i++)
  883. drv_conf_tx(local, i, &local->tx_conf[i]);
  884. /* reconfigure hardware */
  885. ieee80211_hw_config(local, ~0);
  886. ieee80211_configure_filter(local);
  887. /* Finally also reconfigure all the BSS information */
  888. list_for_each_entry(sdata, &local->interfaces, list) {
  889. u32 changed;
  890. if (!ieee80211_sdata_running(sdata))
  891. continue;
  892. /* common change flags for all interface types */
  893. changed = BSS_CHANGED_ERP_CTS_PROT |
  894. BSS_CHANGED_ERP_PREAMBLE |
  895. BSS_CHANGED_ERP_SLOT |
  896. BSS_CHANGED_HT |
  897. BSS_CHANGED_BASIC_RATES |
  898. BSS_CHANGED_BEACON_INT |
  899. BSS_CHANGED_BSSID |
  900. BSS_CHANGED_CQM |
  901. BSS_CHANGED_QOS;
  902. switch (sdata->vif.type) {
  903. case NL80211_IFTYPE_STATION:
  904. changed |= BSS_CHANGED_ASSOC;
  905. mutex_lock(&sdata->u.mgd.mtx);
  906. ieee80211_bss_info_change_notify(sdata, changed);
  907. mutex_unlock(&sdata->u.mgd.mtx);
  908. break;
  909. case NL80211_IFTYPE_ADHOC:
  910. changed |= BSS_CHANGED_IBSS;
  911. /* fall through */
  912. case NL80211_IFTYPE_AP:
  913. changed |= BSS_CHANGED_SSID;
  914. /* fall through */
  915. case NL80211_IFTYPE_MESH_POINT:
  916. changed |= BSS_CHANGED_BEACON |
  917. BSS_CHANGED_BEACON_ENABLED;
  918. ieee80211_bss_info_change_notify(sdata, changed);
  919. break;
  920. case NL80211_IFTYPE_WDS:
  921. break;
  922. case NL80211_IFTYPE_AP_VLAN:
  923. case NL80211_IFTYPE_MONITOR:
  924. /* ignore virtual */
  925. break;
  926. case NL80211_IFTYPE_UNSPECIFIED:
  927. case NUM_NL80211_IFTYPES:
  928. case NL80211_IFTYPE_P2P_CLIENT:
  929. case NL80211_IFTYPE_P2P_GO:
  930. WARN_ON(1);
  931. break;
  932. }
  933. }
  934. /*
  935. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  936. * sessions can be established after a resume.
  937. *
  938. * Also tear down aggregation sessions since reconfiguring
  939. * them in a hardware restart scenario is not easily done
  940. * right now, and the hardware will have lost information
  941. * about the sessions, but we and the AP still think they
  942. * are active. This is really a workaround though.
  943. */
  944. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  945. mutex_lock(&local->sta_mtx);
  946. list_for_each_entry(sta, &local->sta_list, list) {
  947. ieee80211_sta_tear_down_BA_sessions(sta, true);
  948. clear_sta_flags(sta, WLAN_STA_BLOCK_BA);
  949. }
  950. mutex_unlock(&local->sta_mtx);
  951. }
  952. /* add back keys */
  953. list_for_each_entry(sdata, &local->interfaces, list)
  954. if (ieee80211_sdata_running(sdata))
  955. ieee80211_enable_keys(sdata);
  956. wake_up:
  957. ieee80211_wake_queues_by_reason(hw,
  958. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  959. /*
  960. * If this is for hw restart things are still running.
  961. * We may want to change that later, however.
  962. */
  963. if (!local->suspended)
  964. return 0;
  965. #ifdef CONFIG_PM
  966. /* first set suspended false, then resuming */
  967. local->suspended = false;
  968. mb();
  969. local->resuming = false;
  970. list_for_each_entry(sdata, &local->interfaces, list) {
  971. switch(sdata->vif.type) {
  972. case NL80211_IFTYPE_STATION:
  973. ieee80211_sta_restart(sdata);
  974. break;
  975. case NL80211_IFTYPE_ADHOC:
  976. ieee80211_ibss_restart(sdata);
  977. break;
  978. case NL80211_IFTYPE_MESH_POINT:
  979. ieee80211_mesh_restart(sdata);
  980. break;
  981. default:
  982. break;
  983. }
  984. }
  985. mod_timer(&local->sta_cleanup, jiffies + 1);
  986. mutex_lock(&local->sta_mtx);
  987. list_for_each_entry(sta, &local->sta_list, list)
  988. mesh_plink_restart(sta);
  989. mutex_unlock(&local->sta_mtx);
  990. #else
  991. WARN_ON(1);
  992. #endif
  993. return 0;
  994. }
  995. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  996. {
  997. struct ieee80211_sub_if_data *sdata;
  998. struct ieee80211_local *local;
  999. struct ieee80211_key *key;
  1000. if (WARN_ON(!vif))
  1001. return;
  1002. sdata = vif_to_sdata(vif);
  1003. local = sdata->local;
  1004. if (WARN_ON(!local->resuming))
  1005. return;
  1006. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1007. return;
  1008. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1009. mutex_lock(&local->key_mtx);
  1010. list_for_each_entry(key, &sdata->key_list, list)
  1011. key->flags |= KEY_FLAG_TAINTED;
  1012. mutex_unlock(&local->key_mtx);
  1013. }
  1014. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1015. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1016. enum ieee80211_smps_mode *smps_mode)
  1017. {
  1018. if (ifmgd->associated) {
  1019. *smps_mode = ifmgd->ap_smps;
  1020. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1021. if (ifmgd->powersave)
  1022. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1023. else
  1024. *smps_mode = IEEE80211_SMPS_OFF;
  1025. }
  1026. return 1;
  1027. }
  1028. return 0;
  1029. }
  1030. /* must hold iflist_mtx */
  1031. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1032. {
  1033. struct ieee80211_sub_if_data *sdata;
  1034. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1035. int count = 0;
  1036. lockdep_assert_held(&local->iflist_mtx);
  1037. /*
  1038. * This function could be improved to handle multiple
  1039. * interfaces better, but right now it makes any
  1040. * non-station interfaces force SM PS to be turned
  1041. * off. If there are multiple station interfaces it
  1042. * could also use the best possible mode, e.g. if
  1043. * one is in static and the other in dynamic then
  1044. * dynamic is ok.
  1045. */
  1046. list_for_each_entry(sdata, &local->interfaces, list) {
  1047. if (!ieee80211_sdata_running(sdata))
  1048. continue;
  1049. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1050. goto set;
  1051. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1052. if (count > 1) {
  1053. smps_mode = IEEE80211_SMPS_OFF;
  1054. break;
  1055. }
  1056. }
  1057. if (smps_mode == local->smps_mode)
  1058. return;
  1059. set:
  1060. local->smps_mode = smps_mode;
  1061. /* changed flag is auto-detected for this */
  1062. ieee80211_hw_config(local, 0);
  1063. }
  1064. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1065. {
  1066. int i;
  1067. for (i = 0; i < n_ids; i++)
  1068. if (ids[i] == id)
  1069. return true;
  1070. return false;
  1071. }
  1072. /**
  1073. * ieee80211_ie_split - split an IE buffer according to ordering
  1074. *
  1075. * @ies: the IE buffer
  1076. * @ielen: the length of the IE buffer
  1077. * @ids: an array with element IDs that are allowed before
  1078. * the split
  1079. * @n_ids: the size of the element ID array
  1080. * @offset: offset where to start splitting in the buffer
  1081. *
  1082. * This function splits an IE buffer by updating the @offset
  1083. * variable to point to the location where the buffer should be
  1084. * split.
  1085. *
  1086. * It assumes that the given IE buffer is well-formed, this
  1087. * has to be guaranteed by the caller!
  1088. *
  1089. * It also assumes that the IEs in the buffer are ordered
  1090. * correctly, if not the result of using this function will not
  1091. * be ordered correctly either, i.e. it does no reordering.
  1092. *
  1093. * The function returns the offset where the next part of the
  1094. * buffer starts, which may be @ielen if the entire (remainder)
  1095. * of the buffer should be used.
  1096. */
  1097. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1098. const u8 *ids, int n_ids, size_t offset)
  1099. {
  1100. size_t pos = offset;
  1101. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1102. pos += 2 + ies[pos + 1];
  1103. return pos;
  1104. }
  1105. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1106. {
  1107. size_t pos = offset;
  1108. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1109. pos += 2 + ies[pos + 1];
  1110. return pos;
  1111. }
  1112. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1113. int rssi_min_thold,
  1114. int rssi_max_thold)
  1115. {
  1116. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1117. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1118. return;
  1119. /*
  1120. * Scale up threshold values before storing it, as the RSSI averaging
  1121. * algorithm uses a scaled up value as well. Change this scaling
  1122. * factor if the RSSI averaging algorithm changes.
  1123. */
  1124. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1125. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1126. }
  1127. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1128. int rssi_min_thold,
  1129. int rssi_max_thold)
  1130. {
  1131. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1132. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1133. rssi_min_thold > rssi_max_thold);
  1134. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1135. rssi_max_thold);
  1136. }
  1137. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1138. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1139. {
  1140. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1141. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1142. }
  1143. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);