util.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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;
  84. struct ieee80211_hdr *hdr;
  85. skb_queue_walk(&tx->skbs, skb) {
  86. hdr = (struct ieee80211_hdr *) skb->data;
  87. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  88. }
  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. void 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, 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. queue = skb_get_queue_mapping(skb);
  330. __skb_queue_tail(&local->pending[queue], skb);
  331. }
  332. if (fn)
  333. fn(data);
  334. for (i = 0; i < hw->queues; i++)
  335. __ieee80211_wake_queue(hw, i,
  336. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  337. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  338. }
  339. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  340. struct sk_buff_head *skbs)
  341. {
  342. ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
  343. }
  344. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  345. enum queue_stop_reason reason)
  346. {
  347. struct ieee80211_local *local = hw_to_local(hw);
  348. unsigned long flags;
  349. int i;
  350. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  351. for (i = 0; i < hw->queues; i++)
  352. __ieee80211_stop_queue(hw, i, reason);
  353. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  354. }
  355. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  356. {
  357. ieee80211_stop_queues_by_reason(hw,
  358. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  359. }
  360. EXPORT_SYMBOL(ieee80211_stop_queues);
  361. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  362. {
  363. struct ieee80211_local *local = hw_to_local(hw);
  364. unsigned long flags;
  365. int ret;
  366. if (WARN_ON(queue >= hw->queues))
  367. return true;
  368. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  369. ret = !!local->queue_stop_reasons[queue];
  370. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL(ieee80211_queue_stopped);
  374. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  375. enum queue_stop_reason reason)
  376. {
  377. struct ieee80211_local *local = hw_to_local(hw);
  378. unsigned long flags;
  379. int i;
  380. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  381. for (i = 0; i < hw->queues; i++)
  382. __ieee80211_wake_queue(hw, i, reason);
  383. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  384. }
  385. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  386. {
  387. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  388. }
  389. EXPORT_SYMBOL(ieee80211_wake_queues);
  390. void ieee80211_iterate_active_interfaces(
  391. struct ieee80211_hw *hw,
  392. void (*iterator)(void *data, u8 *mac,
  393. struct ieee80211_vif *vif),
  394. void *data)
  395. {
  396. struct ieee80211_local *local = hw_to_local(hw);
  397. struct ieee80211_sub_if_data *sdata;
  398. mutex_lock(&local->iflist_mtx);
  399. list_for_each_entry(sdata, &local->interfaces, list) {
  400. switch (sdata->vif.type) {
  401. case NL80211_IFTYPE_MONITOR:
  402. case NL80211_IFTYPE_AP_VLAN:
  403. continue;
  404. default:
  405. break;
  406. }
  407. if (ieee80211_sdata_running(sdata))
  408. iterator(data, sdata->vif.addr,
  409. &sdata->vif);
  410. }
  411. mutex_unlock(&local->iflist_mtx);
  412. }
  413. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  414. void ieee80211_iterate_active_interfaces_atomic(
  415. struct ieee80211_hw *hw,
  416. void (*iterator)(void *data, u8 *mac,
  417. struct ieee80211_vif *vif),
  418. void *data)
  419. {
  420. struct ieee80211_local *local = hw_to_local(hw);
  421. struct ieee80211_sub_if_data *sdata;
  422. rcu_read_lock();
  423. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  424. switch (sdata->vif.type) {
  425. case NL80211_IFTYPE_MONITOR:
  426. case NL80211_IFTYPE_AP_VLAN:
  427. continue;
  428. default:
  429. break;
  430. }
  431. if (ieee80211_sdata_running(sdata))
  432. iterator(data, sdata->vif.addr,
  433. &sdata->vif);
  434. }
  435. rcu_read_unlock();
  436. }
  437. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  438. /*
  439. * Nothing should have been stuffed into the workqueue during
  440. * the suspend->resume cycle. If this WARN is seen then there
  441. * is a bug with either the driver suspend or something in
  442. * mac80211 stuffing into the workqueue which we haven't yet
  443. * cleared during mac80211's suspend cycle.
  444. */
  445. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  446. {
  447. if (WARN(local->suspended && !local->resuming,
  448. "queueing ieee80211 work while going to suspend\n"))
  449. return false;
  450. return true;
  451. }
  452. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  453. {
  454. struct ieee80211_local *local = hw_to_local(hw);
  455. if (!ieee80211_can_queue_work(local))
  456. return;
  457. queue_work(local->workqueue, work);
  458. }
  459. EXPORT_SYMBOL(ieee80211_queue_work);
  460. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  461. struct delayed_work *dwork,
  462. unsigned long delay)
  463. {
  464. struct ieee80211_local *local = hw_to_local(hw);
  465. if (!ieee80211_can_queue_work(local))
  466. return;
  467. queue_delayed_work(local->workqueue, dwork, delay);
  468. }
  469. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  470. void ieee802_11_parse_elems(u8 *start, size_t len,
  471. struct ieee802_11_elems *elems)
  472. {
  473. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  474. }
  475. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  476. {
  477. struct ieee80211_local *local = sdata->local;
  478. struct ieee80211_tx_queue_params qparam;
  479. int queue;
  480. bool use_11b;
  481. int aCWmin, aCWmax;
  482. if (!local->ops->conf_tx)
  483. return;
  484. memset(&qparam, 0, sizeof(qparam));
  485. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  486. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  487. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  488. /* Set defaults according to 802.11-2007 Table 7-37 */
  489. aCWmax = 1023;
  490. if (use_11b)
  491. aCWmin = 31;
  492. else
  493. aCWmin = 15;
  494. switch (queue) {
  495. case 3: /* AC_BK */
  496. qparam.cw_max = aCWmax;
  497. qparam.cw_min = aCWmin;
  498. qparam.txop = 0;
  499. qparam.aifs = 7;
  500. break;
  501. default: /* never happens but let's not leave undefined */
  502. case 2: /* AC_BE */
  503. qparam.cw_max = aCWmax;
  504. qparam.cw_min = aCWmin;
  505. qparam.txop = 0;
  506. qparam.aifs = 3;
  507. break;
  508. case 1: /* AC_VI */
  509. qparam.cw_max = aCWmin;
  510. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  511. if (use_11b)
  512. qparam.txop = 6016/32;
  513. else
  514. qparam.txop = 3008/32;
  515. qparam.aifs = 2;
  516. break;
  517. case 0: /* AC_VO */
  518. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  519. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  520. if (use_11b)
  521. qparam.txop = 3264/32;
  522. else
  523. qparam.txop = 1504/32;
  524. qparam.aifs = 2;
  525. break;
  526. }
  527. qparam.uapsd = false;
  528. sdata->tx_conf[queue] = qparam;
  529. drv_conf_tx(local, sdata, queue, &qparam);
  530. }
  531. /* after reinitialize QoS TX queues setting to default,
  532. * disable QoS at all */
  533. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  534. sdata->vif.bss_conf.qos =
  535. sdata->vif.type != NL80211_IFTYPE_STATION;
  536. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  537. }
  538. }
  539. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  540. const size_t supp_rates_len,
  541. const u8 *supp_rates)
  542. {
  543. struct ieee80211_local *local = sdata->local;
  544. int i, have_higher_than_11mbit = 0;
  545. /* cf. IEEE 802.11 9.2.12 */
  546. for (i = 0; i < supp_rates_len; i++)
  547. if ((supp_rates[i] & 0x7f) * 5 > 110)
  548. have_higher_than_11mbit = 1;
  549. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  550. have_higher_than_11mbit)
  551. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  552. else
  553. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  554. ieee80211_set_wmm_default(sdata);
  555. }
  556. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  557. enum ieee80211_band band)
  558. {
  559. struct ieee80211_supported_band *sband;
  560. struct ieee80211_rate *bitrates;
  561. u32 mandatory_rates;
  562. enum ieee80211_rate_flags mandatory_flag;
  563. int i;
  564. sband = local->hw.wiphy->bands[band];
  565. if (!sband) {
  566. WARN_ON(1);
  567. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  568. }
  569. if (band == IEEE80211_BAND_2GHZ)
  570. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  571. else
  572. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  573. bitrates = sband->bitrates;
  574. mandatory_rates = 0;
  575. for (i = 0; i < sband->n_bitrates; i++)
  576. if (bitrates[i].flags & mandatory_flag)
  577. mandatory_rates |= BIT(i);
  578. return mandatory_rates;
  579. }
  580. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  581. u16 transaction, u16 auth_alg,
  582. u8 *extra, size_t extra_len, const u8 *bssid,
  583. const u8 *key, u8 key_len, u8 key_idx)
  584. {
  585. struct ieee80211_local *local = sdata->local;
  586. struct sk_buff *skb;
  587. struct ieee80211_mgmt *mgmt;
  588. int err;
  589. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  590. sizeof(*mgmt) + 6 + extra_len);
  591. if (!skb)
  592. return;
  593. skb_reserve(skb, local->hw.extra_tx_headroom);
  594. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  595. memset(mgmt, 0, 24 + 6);
  596. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  597. IEEE80211_STYPE_AUTH);
  598. memcpy(mgmt->da, bssid, ETH_ALEN);
  599. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  600. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  601. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  602. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  603. mgmt->u.auth.status_code = cpu_to_le16(0);
  604. if (extra)
  605. memcpy(skb_put(skb, extra_len), extra, extra_len);
  606. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  607. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  608. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  609. WARN_ON(err);
  610. }
  611. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  612. ieee80211_tx_skb(sdata, skb);
  613. }
  614. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  615. const u8 *ie, size_t ie_len,
  616. enum ieee80211_band band, u32 rate_mask,
  617. u8 channel)
  618. {
  619. struct ieee80211_supported_band *sband;
  620. u8 *pos;
  621. size_t offset = 0, noffset;
  622. int supp_rates_len, i;
  623. u8 rates[32];
  624. int num_rates;
  625. int ext_rates_len;
  626. sband = local->hw.wiphy->bands[band];
  627. pos = buffer;
  628. num_rates = 0;
  629. for (i = 0; i < sband->n_bitrates; i++) {
  630. if ((BIT(i) & rate_mask) == 0)
  631. continue; /* skip rate */
  632. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  633. }
  634. supp_rates_len = min_t(int, num_rates, 8);
  635. *pos++ = WLAN_EID_SUPP_RATES;
  636. *pos++ = supp_rates_len;
  637. memcpy(pos, rates, supp_rates_len);
  638. pos += supp_rates_len;
  639. /* insert "request information" if in custom IEs */
  640. if (ie && ie_len) {
  641. static const u8 before_extrates[] = {
  642. WLAN_EID_SSID,
  643. WLAN_EID_SUPP_RATES,
  644. WLAN_EID_REQUEST,
  645. };
  646. noffset = ieee80211_ie_split(ie, ie_len,
  647. before_extrates,
  648. ARRAY_SIZE(before_extrates),
  649. offset);
  650. memcpy(pos, ie + offset, noffset - offset);
  651. pos += noffset - offset;
  652. offset = noffset;
  653. }
  654. ext_rates_len = num_rates - supp_rates_len;
  655. if (ext_rates_len > 0) {
  656. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  657. *pos++ = ext_rates_len;
  658. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  659. pos += ext_rates_len;
  660. }
  661. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  662. *pos++ = WLAN_EID_DS_PARAMS;
  663. *pos++ = 1;
  664. *pos++ = channel;
  665. }
  666. /* insert custom IEs that go before HT */
  667. if (ie && ie_len) {
  668. static const u8 before_ht[] = {
  669. WLAN_EID_SSID,
  670. WLAN_EID_SUPP_RATES,
  671. WLAN_EID_REQUEST,
  672. WLAN_EID_EXT_SUPP_RATES,
  673. WLAN_EID_DS_PARAMS,
  674. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  675. };
  676. noffset = ieee80211_ie_split(ie, ie_len,
  677. before_ht, ARRAY_SIZE(before_ht),
  678. offset);
  679. memcpy(pos, ie + offset, noffset - offset);
  680. pos += noffset - offset;
  681. offset = noffset;
  682. }
  683. if (sband->ht_cap.ht_supported)
  684. pos = ieee80211_ie_build_ht_cap(pos, sband, sband->ht_cap.cap);
  685. /*
  686. * If adding more here, adjust code in main.c
  687. * that calculates local->scan_ies_len.
  688. */
  689. /* add any remaining custom IEs */
  690. if (ie && ie_len) {
  691. noffset = ie_len;
  692. memcpy(pos, ie + offset, noffset - offset);
  693. pos += noffset - offset;
  694. }
  695. return pos - buffer;
  696. }
  697. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  698. u8 *dst, u32 ratemask,
  699. const u8 *ssid, size_t ssid_len,
  700. const u8 *ie, size_t ie_len,
  701. bool directed)
  702. {
  703. struct ieee80211_local *local = sdata->local;
  704. struct sk_buff *skb;
  705. struct ieee80211_mgmt *mgmt;
  706. size_t buf_len;
  707. u8 *buf;
  708. u8 chan;
  709. /* FIXME: come up with a proper value */
  710. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  711. if (!buf)
  712. return NULL;
  713. /*
  714. * Do not send DS Channel parameter for directed probe requests
  715. * in order to maximize the chance that we get a response. Some
  716. * badly-behaved APs don't respond when this parameter is included.
  717. */
  718. if (directed)
  719. chan = 0;
  720. else
  721. chan = ieee80211_frequency_to_channel(
  722. local->hw.conf.channel->center_freq);
  723. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  724. local->hw.conf.channel->band,
  725. ratemask, chan);
  726. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  727. ssid, ssid_len,
  728. buf, buf_len);
  729. if (dst) {
  730. mgmt = (struct ieee80211_mgmt *) skb->data;
  731. memcpy(mgmt->da, dst, ETH_ALEN);
  732. memcpy(mgmt->bssid, dst, ETH_ALEN);
  733. }
  734. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  735. kfree(buf);
  736. return skb;
  737. }
  738. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  739. const u8 *ssid, size_t ssid_len,
  740. const u8 *ie, size_t ie_len,
  741. u32 ratemask, bool directed, bool no_cck)
  742. {
  743. struct sk_buff *skb;
  744. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  745. ie, ie_len, directed);
  746. if (skb) {
  747. if (no_cck)
  748. IEEE80211_SKB_CB(skb)->flags |=
  749. IEEE80211_TX_CTL_NO_CCK_RATE;
  750. ieee80211_tx_skb(sdata, skb);
  751. }
  752. }
  753. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  754. struct ieee802_11_elems *elems,
  755. enum ieee80211_band band)
  756. {
  757. struct ieee80211_supported_band *sband;
  758. struct ieee80211_rate *bitrates;
  759. size_t num_rates;
  760. u32 supp_rates;
  761. int i, j;
  762. sband = local->hw.wiphy->bands[band];
  763. if (!sband) {
  764. WARN_ON(1);
  765. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  766. }
  767. bitrates = sband->bitrates;
  768. num_rates = sband->n_bitrates;
  769. supp_rates = 0;
  770. for (i = 0; i < elems->supp_rates_len +
  771. elems->ext_supp_rates_len; i++) {
  772. u8 rate = 0;
  773. int own_rate;
  774. if (i < elems->supp_rates_len)
  775. rate = elems->supp_rates[i];
  776. else if (elems->ext_supp_rates)
  777. rate = elems->ext_supp_rates
  778. [i - elems->supp_rates_len];
  779. own_rate = 5 * (rate & 0x7f);
  780. for (j = 0; j < num_rates; j++)
  781. if (bitrates[j].bitrate == own_rate)
  782. supp_rates |= BIT(j);
  783. }
  784. return supp_rates;
  785. }
  786. void ieee80211_stop_device(struct ieee80211_local *local)
  787. {
  788. ieee80211_led_radio(local, false);
  789. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  790. cancel_work_sync(&local->reconfig_filter);
  791. flush_workqueue(local->workqueue);
  792. drv_stop(local);
  793. }
  794. int ieee80211_reconfig(struct ieee80211_local *local)
  795. {
  796. struct ieee80211_hw *hw = &local->hw;
  797. struct ieee80211_sub_if_data *sdata;
  798. struct sta_info *sta;
  799. int res, i;
  800. #ifdef CONFIG_PM
  801. if (local->suspended)
  802. local->resuming = true;
  803. if (local->wowlan) {
  804. local->wowlan = false;
  805. res = drv_resume(local);
  806. if (res < 0) {
  807. local->resuming = false;
  808. return res;
  809. }
  810. if (res == 0)
  811. goto wake_up;
  812. WARN_ON(res > 1);
  813. /*
  814. * res is 1, which means the driver requested
  815. * to go through a regular reset on wakeup.
  816. */
  817. }
  818. #endif
  819. /* setup fragmentation threshold */
  820. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  821. /* setup RTS threshold */
  822. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  823. /* reset coverage class */
  824. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  825. /* everything else happens only if HW was up & running */
  826. if (!local->open_count)
  827. goto wake_up;
  828. /*
  829. * Upon resume hardware can sometimes be goofy due to
  830. * various platform / driver / bus issues, so restarting
  831. * the device may at times not work immediately. Propagate
  832. * the error.
  833. */
  834. res = drv_start(local);
  835. if (res) {
  836. WARN(local->suspended, "Hardware became unavailable "
  837. "upon resume. This could be a software issue "
  838. "prior to suspend or a hardware issue.\n");
  839. return res;
  840. }
  841. ieee80211_led_radio(local, true);
  842. ieee80211_mod_tpt_led_trig(local,
  843. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  844. /* add interfaces */
  845. list_for_each_entry(sdata, &local->interfaces, list) {
  846. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  847. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  848. ieee80211_sdata_running(sdata))
  849. res = drv_add_interface(local, sdata);
  850. }
  851. /* add STAs back */
  852. mutex_lock(&local->sta_mtx);
  853. list_for_each_entry(sta, &local->sta_list, list) {
  854. if (sta->uploaded) {
  855. sdata = sta->sdata;
  856. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  857. sdata = container_of(sdata->bss,
  858. struct ieee80211_sub_if_data,
  859. u.ap);
  860. memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
  861. WARN_ON(drv_sta_add(local, sdata, &sta->sta));
  862. }
  863. }
  864. mutex_unlock(&local->sta_mtx);
  865. /* reconfigure tx conf */
  866. list_for_each_entry(sdata, &local->interfaces, list) {
  867. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  868. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  869. !ieee80211_sdata_running(sdata))
  870. continue;
  871. for (i = 0; i < hw->queues; i++)
  872. drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
  873. }
  874. /* reconfigure hardware */
  875. ieee80211_hw_config(local, ~0);
  876. ieee80211_configure_filter(local);
  877. /* Finally also reconfigure all the BSS information */
  878. list_for_each_entry(sdata, &local->interfaces, list) {
  879. u32 changed;
  880. if (!ieee80211_sdata_running(sdata))
  881. continue;
  882. /* common change flags for all interface types */
  883. changed = BSS_CHANGED_ERP_CTS_PROT |
  884. BSS_CHANGED_ERP_PREAMBLE |
  885. BSS_CHANGED_ERP_SLOT |
  886. BSS_CHANGED_HT |
  887. BSS_CHANGED_BASIC_RATES |
  888. BSS_CHANGED_BEACON_INT |
  889. BSS_CHANGED_BSSID |
  890. BSS_CHANGED_CQM |
  891. BSS_CHANGED_QOS |
  892. BSS_CHANGED_IDLE;
  893. switch (sdata->vif.type) {
  894. case NL80211_IFTYPE_STATION:
  895. changed |= BSS_CHANGED_ASSOC;
  896. mutex_lock(&sdata->u.mgd.mtx);
  897. ieee80211_bss_info_change_notify(sdata, changed);
  898. mutex_unlock(&sdata->u.mgd.mtx);
  899. break;
  900. case NL80211_IFTYPE_ADHOC:
  901. changed |= BSS_CHANGED_IBSS;
  902. /* fall through */
  903. case NL80211_IFTYPE_AP:
  904. changed |= BSS_CHANGED_SSID |
  905. BSS_CHANGED_AP_PROBE_RESP;
  906. /* fall through */
  907. case NL80211_IFTYPE_MESH_POINT:
  908. changed |= BSS_CHANGED_BEACON |
  909. BSS_CHANGED_BEACON_ENABLED;
  910. ieee80211_bss_info_change_notify(sdata, changed);
  911. break;
  912. case NL80211_IFTYPE_WDS:
  913. break;
  914. case NL80211_IFTYPE_AP_VLAN:
  915. case NL80211_IFTYPE_MONITOR:
  916. /* ignore virtual */
  917. break;
  918. case NL80211_IFTYPE_UNSPECIFIED:
  919. case NUM_NL80211_IFTYPES:
  920. case NL80211_IFTYPE_P2P_CLIENT:
  921. case NL80211_IFTYPE_P2P_GO:
  922. WARN_ON(1);
  923. break;
  924. }
  925. }
  926. ieee80211_recalc_ps(local, -1);
  927. /*
  928. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  929. * sessions can be established after a resume.
  930. *
  931. * Also tear down aggregation sessions since reconfiguring
  932. * them in a hardware restart scenario is not easily done
  933. * right now, and the hardware will have lost information
  934. * about the sessions, but we and the AP still think they
  935. * are active. This is really a workaround though.
  936. */
  937. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  938. mutex_lock(&local->sta_mtx);
  939. list_for_each_entry(sta, &local->sta_list, list) {
  940. ieee80211_sta_tear_down_BA_sessions(sta, true);
  941. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  942. }
  943. mutex_unlock(&local->sta_mtx);
  944. }
  945. /* add back keys */
  946. list_for_each_entry(sdata, &local->interfaces, list)
  947. if (ieee80211_sdata_running(sdata))
  948. ieee80211_enable_keys(sdata);
  949. wake_up:
  950. ieee80211_wake_queues_by_reason(hw,
  951. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  952. /*
  953. * If this is for hw restart things are still running.
  954. * We may want to change that later, however.
  955. */
  956. if (!local->suspended)
  957. return 0;
  958. #ifdef CONFIG_PM
  959. /* first set suspended false, then resuming */
  960. local->suspended = false;
  961. mb();
  962. local->resuming = false;
  963. list_for_each_entry(sdata, &local->interfaces, list) {
  964. switch(sdata->vif.type) {
  965. case NL80211_IFTYPE_STATION:
  966. ieee80211_sta_restart(sdata);
  967. break;
  968. case NL80211_IFTYPE_ADHOC:
  969. ieee80211_ibss_restart(sdata);
  970. break;
  971. case NL80211_IFTYPE_MESH_POINT:
  972. ieee80211_mesh_restart(sdata);
  973. break;
  974. default:
  975. break;
  976. }
  977. }
  978. mod_timer(&local->sta_cleanup, jiffies + 1);
  979. mutex_lock(&local->sta_mtx);
  980. list_for_each_entry(sta, &local->sta_list, list)
  981. mesh_plink_restart(sta);
  982. mutex_unlock(&local->sta_mtx);
  983. #else
  984. WARN_ON(1);
  985. #endif
  986. return 0;
  987. }
  988. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  989. {
  990. struct ieee80211_sub_if_data *sdata;
  991. struct ieee80211_local *local;
  992. struct ieee80211_key *key;
  993. if (WARN_ON(!vif))
  994. return;
  995. sdata = vif_to_sdata(vif);
  996. local = sdata->local;
  997. if (WARN_ON(!local->resuming))
  998. return;
  999. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1000. return;
  1001. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1002. mutex_lock(&local->key_mtx);
  1003. list_for_each_entry(key, &sdata->key_list, list)
  1004. key->flags |= KEY_FLAG_TAINTED;
  1005. mutex_unlock(&local->key_mtx);
  1006. }
  1007. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1008. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1009. enum ieee80211_smps_mode *smps_mode)
  1010. {
  1011. if (ifmgd->associated) {
  1012. *smps_mode = ifmgd->ap_smps;
  1013. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1014. if (ifmgd->powersave)
  1015. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1016. else
  1017. *smps_mode = IEEE80211_SMPS_OFF;
  1018. }
  1019. return 1;
  1020. }
  1021. return 0;
  1022. }
  1023. /* must hold iflist_mtx */
  1024. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1025. {
  1026. struct ieee80211_sub_if_data *sdata;
  1027. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1028. int count = 0;
  1029. lockdep_assert_held(&local->iflist_mtx);
  1030. /*
  1031. * This function could be improved to handle multiple
  1032. * interfaces better, but right now it makes any
  1033. * non-station interfaces force SM PS to be turned
  1034. * off. If there are multiple station interfaces it
  1035. * could also use the best possible mode, e.g. if
  1036. * one is in static and the other in dynamic then
  1037. * dynamic is ok.
  1038. */
  1039. list_for_each_entry(sdata, &local->interfaces, list) {
  1040. if (!ieee80211_sdata_running(sdata))
  1041. continue;
  1042. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1043. goto set;
  1044. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1045. if (count > 1) {
  1046. smps_mode = IEEE80211_SMPS_OFF;
  1047. break;
  1048. }
  1049. }
  1050. if (smps_mode == local->smps_mode)
  1051. return;
  1052. set:
  1053. local->smps_mode = smps_mode;
  1054. /* changed flag is auto-detected for this */
  1055. ieee80211_hw_config(local, 0);
  1056. }
  1057. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1058. {
  1059. int i;
  1060. for (i = 0; i < n_ids; i++)
  1061. if (ids[i] == id)
  1062. return true;
  1063. return false;
  1064. }
  1065. /**
  1066. * ieee80211_ie_split - split an IE buffer according to ordering
  1067. *
  1068. * @ies: the IE buffer
  1069. * @ielen: the length of the IE buffer
  1070. * @ids: an array with element IDs that are allowed before
  1071. * the split
  1072. * @n_ids: the size of the element ID array
  1073. * @offset: offset where to start splitting in the buffer
  1074. *
  1075. * This function splits an IE buffer by updating the @offset
  1076. * variable to point to the location where the buffer should be
  1077. * split.
  1078. *
  1079. * It assumes that the given IE buffer is well-formed, this
  1080. * has to be guaranteed by the caller!
  1081. *
  1082. * It also assumes that the IEs in the buffer are ordered
  1083. * correctly, if not the result of using this function will not
  1084. * be ordered correctly either, i.e. it does no reordering.
  1085. *
  1086. * The function returns the offset where the next part of the
  1087. * buffer starts, which may be @ielen if the entire (remainder)
  1088. * of the buffer should be used.
  1089. */
  1090. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1091. const u8 *ids, int n_ids, size_t offset)
  1092. {
  1093. size_t pos = offset;
  1094. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1095. pos += 2 + ies[pos + 1];
  1096. return pos;
  1097. }
  1098. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1099. {
  1100. size_t pos = offset;
  1101. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1102. pos += 2 + ies[pos + 1];
  1103. return pos;
  1104. }
  1105. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1106. int rssi_min_thold,
  1107. int rssi_max_thold)
  1108. {
  1109. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1110. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1111. return;
  1112. /*
  1113. * Scale up threshold values before storing it, as the RSSI averaging
  1114. * algorithm uses a scaled up value as well. Change this scaling
  1115. * factor if the RSSI averaging algorithm changes.
  1116. */
  1117. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1118. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1119. }
  1120. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1121. int rssi_min_thold,
  1122. int rssi_max_thold)
  1123. {
  1124. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1125. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1126. rssi_min_thold > rssi_max_thold);
  1127. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1128. rssi_max_thold);
  1129. }
  1130. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1131. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1132. {
  1133. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1134. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1135. }
  1136. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1137. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_supported_band *sband,
  1138. u16 cap)
  1139. {
  1140. __le16 tmp;
  1141. *pos++ = WLAN_EID_HT_CAPABILITY;
  1142. *pos++ = sizeof(struct ieee80211_ht_cap);
  1143. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1144. /* capability flags */
  1145. tmp = cpu_to_le16(cap);
  1146. memcpy(pos, &tmp, sizeof(u16));
  1147. pos += sizeof(u16);
  1148. /* AMPDU parameters */
  1149. *pos++ = sband->ht_cap.ampdu_factor |
  1150. (sband->ht_cap.ampdu_density <<
  1151. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1152. /* MCS set */
  1153. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  1154. pos += sizeof(sband->ht_cap.mcs);
  1155. /* extended capabilities */
  1156. pos += sizeof(__le16);
  1157. /* BF capabilities */
  1158. pos += sizeof(__le32);
  1159. /* antenna selection */
  1160. pos += sizeof(u8);
  1161. return pos;
  1162. }
  1163. u8 *ieee80211_ie_build_ht_info(u8 *pos,
  1164. struct ieee80211_sta_ht_cap *ht_cap,
  1165. struct ieee80211_channel *channel,
  1166. enum nl80211_channel_type channel_type)
  1167. {
  1168. struct ieee80211_ht_info *ht_info;
  1169. /* Build HT Information */
  1170. *pos++ = WLAN_EID_HT_INFORMATION;
  1171. *pos++ = sizeof(struct ieee80211_ht_info);
  1172. ht_info = (struct ieee80211_ht_info *)pos;
  1173. ht_info->control_chan =
  1174. ieee80211_frequency_to_channel(channel->center_freq);
  1175. switch (channel_type) {
  1176. case NL80211_CHAN_HT40MINUS:
  1177. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1178. break;
  1179. case NL80211_CHAN_HT40PLUS:
  1180. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1181. break;
  1182. case NL80211_CHAN_HT20:
  1183. default:
  1184. ht_info->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1185. break;
  1186. }
  1187. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  1188. ht_info->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1189. ht_info->operation_mode = 0x0000;
  1190. ht_info->stbc_param = 0x0000;
  1191. /* It seems that Basic MCS set and Supported MCS set
  1192. are identical for the first 10 bytes */
  1193. memset(&ht_info->basic_set, 0, 16);
  1194. memcpy(&ht_info->basic_set, &ht_cap->mcs, 10);
  1195. return pos + sizeof(struct ieee80211_ht_info);
  1196. }
  1197. enum nl80211_channel_type
  1198. ieee80211_ht_info_to_channel_type(struct ieee80211_ht_info *ht_info)
  1199. {
  1200. enum nl80211_channel_type channel_type;
  1201. if (!ht_info)
  1202. return NL80211_CHAN_NO_HT;
  1203. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1204. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1205. channel_type = NL80211_CHAN_HT20;
  1206. break;
  1207. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1208. channel_type = NL80211_CHAN_HT40PLUS;
  1209. break;
  1210. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1211. channel_type = NL80211_CHAN_HT40MINUS;
  1212. break;
  1213. default:
  1214. channel_type = NL80211_CHAN_NO_HT;
  1215. }
  1216. return channel_type;
  1217. }
  1218. int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1219. {
  1220. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1221. struct ieee80211_local *local = sdata->local;
  1222. struct ieee80211_supported_band *sband;
  1223. int rate;
  1224. u8 i, rates, *pos;
  1225. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1226. rates = sband->n_bitrates;
  1227. if (rates > 8)
  1228. rates = 8;
  1229. if (skb_tailroom(skb) < rates + 2)
  1230. return -ENOMEM;
  1231. pos = skb_put(skb, rates + 2);
  1232. *pos++ = WLAN_EID_SUPP_RATES;
  1233. *pos++ = rates;
  1234. for (i = 0; i < rates; i++) {
  1235. rate = sband->bitrates[i].bitrate;
  1236. *pos++ = (u8) (rate / 5);
  1237. }
  1238. return 0;
  1239. }
  1240. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1241. {
  1242. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1243. struct ieee80211_local *local = sdata->local;
  1244. struct ieee80211_supported_band *sband;
  1245. int rate;
  1246. u8 i, exrates, *pos;
  1247. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1248. exrates = sband->n_bitrates;
  1249. if (exrates > 8)
  1250. exrates -= 8;
  1251. else
  1252. exrates = 0;
  1253. if (skb_tailroom(skb) < exrates + 2)
  1254. return -ENOMEM;
  1255. if (exrates) {
  1256. pos = skb_put(skb, exrates + 2);
  1257. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1258. *pos++ = exrates;
  1259. for (i = 8; i < sband->n_bitrates; i++) {
  1260. rate = sband->bitrates[i].bitrate;
  1261. *pos++ = (u8) (rate / 5);
  1262. }
  1263. }
  1264. return 0;
  1265. }