util.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  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/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. BUG_ON(!wiphy);
  39. local = wiphy_priv(wiphy);
  40. return &local->hw;
  41. }
  42. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  43. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  48. if (len < 16)
  49. return NULL;
  50. if (ieee80211_is_data(fc)) {
  51. if (len < 24) /* drop incorrect hdr len (data) */
  52. return NULL;
  53. if (ieee80211_has_a4(fc))
  54. return NULL;
  55. if (ieee80211_has_tods(fc))
  56. return hdr->addr1;
  57. if (ieee80211_has_fromds(fc))
  58. return hdr->addr2;
  59. return hdr->addr3;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if(ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  84. {
  85. struct sk_buff *skb;
  86. struct ieee80211_hdr *hdr;
  87. skb_queue_walk(&tx->skbs, skb) {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. }
  91. }
  92. int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
  93. int rate, int erp, int short_preamble)
  94. {
  95. int dur;
  96. /* calculate duration (in microseconds, rounded up to next higher
  97. * integer if it includes a fractional microsecond) to send frame of
  98. * len bytes (does not include FCS) at the given rate. Duration will
  99. * also include SIFS.
  100. *
  101. * rate is in 100 kbps, so divident is multiplied by 10 in the
  102. * DIV_ROUND_UP() operations.
  103. */
  104. if (band == IEEE80211_BAND_5GHZ || erp) {
  105. /*
  106. * OFDM:
  107. *
  108. * N_DBPS = DATARATE x 4
  109. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  110. * (16 = SIGNAL time, 6 = tail bits)
  111. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  112. *
  113. * T_SYM = 4 usec
  114. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  115. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  116. * signal ext = 6 usec
  117. */
  118. dur = 16; /* SIFS + signal ext */
  119. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  120. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  121. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  122. 4 * rate); /* T_SYM x N_SYM */
  123. } else {
  124. /*
  125. * 802.11b or 802.11g with 802.11b compatibility:
  126. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  127. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  128. *
  129. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  130. * aSIFSTime = 10 usec
  131. * aPreambleLength = 144 usec or 72 usec with short preamble
  132. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  133. */
  134. dur = 10; /* aSIFSTime = 10 usec */
  135. dur += short_preamble ? (72 + 24) : (144 + 48);
  136. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  137. }
  138. return dur;
  139. }
  140. /* Exported duration function for driver use */
  141. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  142. struct ieee80211_vif *vif,
  143. enum ieee80211_band band,
  144. size_t frame_len,
  145. struct ieee80211_rate *rate)
  146. {
  147. struct ieee80211_sub_if_data *sdata;
  148. u16 dur;
  149. int erp;
  150. bool short_preamble = false;
  151. erp = 0;
  152. if (vif) {
  153. sdata = vif_to_sdata(vif);
  154. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  155. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  156. erp = rate->flags & IEEE80211_RATE_ERP_G;
  157. }
  158. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  159. short_preamble);
  160. return cpu_to_le16(dur);
  161. }
  162. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  163. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  164. struct ieee80211_vif *vif, size_t frame_len,
  165. const struct ieee80211_tx_info *frame_txctl)
  166. {
  167. struct ieee80211_local *local = hw_to_local(hw);
  168. struct ieee80211_rate *rate;
  169. struct ieee80211_sub_if_data *sdata;
  170. bool short_preamble;
  171. int erp;
  172. u16 dur;
  173. struct ieee80211_supported_band *sband;
  174. sband = local->hw.wiphy->bands[frame_txctl->band];
  175. short_preamble = false;
  176. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  177. erp = 0;
  178. if (vif) {
  179. sdata = vif_to_sdata(vif);
  180. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  181. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  182. erp = rate->flags & IEEE80211_RATE_ERP_G;
  183. }
  184. /* CTS duration */
  185. dur = ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  186. erp, short_preamble);
  187. /* Data frame duration */
  188. dur += ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  189. erp, short_preamble);
  190. /* ACK duration */
  191. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  192. erp, short_preamble);
  193. return cpu_to_le16(dur);
  194. }
  195. EXPORT_SYMBOL(ieee80211_rts_duration);
  196. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  197. struct ieee80211_vif *vif,
  198. size_t frame_len,
  199. const struct ieee80211_tx_info *frame_txctl)
  200. {
  201. struct ieee80211_local *local = hw_to_local(hw);
  202. struct ieee80211_rate *rate;
  203. struct ieee80211_sub_if_data *sdata;
  204. bool short_preamble;
  205. int erp;
  206. u16 dur;
  207. struct ieee80211_supported_band *sband;
  208. sband = local->hw.wiphy->bands[frame_txctl->band];
  209. short_preamble = false;
  210. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  211. erp = 0;
  212. if (vif) {
  213. sdata = vif_to_sdata(vif);
  214. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  215. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  216. erp = rate->flags & IEEE80211_RATE_ERP_G;
  217. }
  218. /* Data frame duration */
  219. dur = ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  220. erp, short_preamble);
  221. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  222. /* ACK duration */
  223. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  224. erp, short_preamble);
  225. }
  226. return cpu_to_le16(dur);
  227. }
  228. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  229. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  230. {
  231. struct ieee80211_sub_if_data *sdata;
  232. int n_acs = IEEE80211_NUM_ACS;
  233. if (local->hw.queues < IEEE80211_NUM_ACS)
  234. n_acs = 1;
  235. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  236. int ac;
  237. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  238. continue;
  239. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  240. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  241. continue;
  242. for (ac = 0; ac < n_acs; ac++) {
  243. int ac_queue = sdata->vif.hw_queue[ac];
  244. if (ac_queue == queue ||
  245. (sdata->vif.cab_queue == queue &&
  246. local->queue_stop_reasons[ac_queue] == 0 &&
  247. skb_queue_empty(&local->pending[ac_queue])))
  248. netif_wake_subqueue(sdata->dev, ac);
  249. }
  250. }
  251. }
  252. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  253. enum queue_stop_reason reason)
  254. {
  255. struct ieee80211_local *local = hw_to_local(hw);
  256. trace_wake_queue(local, queue, reason);
  257. if (WARN_ON(queue >= hw->queues))
  258. return;
  259. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  260. return;
  261. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  262. if (local->queue_stop_reasons[queue] != 0)
  263. /* someone still has this queue stopped */
  264. return;
  265. if (skb_queue_empty(&local->pending[queue])) {
  266. rcu_read_lock();
  267. ieee80211_propagate_queue_wake(local, queue);
  268. rcu_read_unlock();
  269. } else
  270. tasklet_schedule(&local->tx_pending_tasklet);
  271. }
  272. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  273. enum queue_stop_reason reason)
  274. {
  275. struct ieee80211_local *local = hw_to_local(hw);
  276. unsigned long flags;
  277. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  278. __ieee80211_wake_queue(hw, queue, reason);
  279. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  280. }
  281. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  282. {
  283. ieee80211_wake_queue_by_reason(hw, queue,
  284. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  285. }
  286. EXPORT_SYMBOL(ieee80211_wake_queue);
  287. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  288. enum queue_stop_reason reason)
  289. {
  290. struct ieee80211_local *local = hw_to_local(hw);
  291. struct ieee80211_sub_if_data *sdata;
  292. int n_acs = IEEE80211_NUM_ACS;
  293. trace_stop_queue(local, queue, reason);
  294. if (WARN_ON(queue >= hw->queues))
  295. return;
  296. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  297. return;
  298. __set_bit(reason, &local->queue_stop_reasons[queue]);
  299. if (local->hw.queues < IEEE80211_NUM_ACS)
  300. n_acs = 1;
  301. rcu_read_lock();
  302. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  303. int ac;
  304. for (ac = 0; ac < n_acs; ac++) {
  305. if (sdata->vif.hw_queue[ac] == queue ||
  306. sdata->vif.cab_queue == queue)
  307. netif_stop_subqueue(sdata->dev, ac);
  308. }
  309. }
  310. rcu_read_unlock();
  311. }
  312. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  313. enum queue_stop_reason reason)
  314. {
  315. struct ieee80211_local *local = hw_to_local(hw);
  316. unsigned long flags;
  317. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  318. __ieee80211_stop_queue(hw, queue, reason);
  319. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  320. }
  321. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  322. {
  323. ieee80211_stop_queue_by_reason(hw, queue,
  324. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  325. }
  326. EXPORT_SYMBOL(ieee80211_stop_queue);
  327. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  328. struct sk_buff *skb)
  329. {
  330. struct ieee80211_hw *hw = &local->hw;
  331. unsigned long flags;
  332. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  333. int queue = info->hw_queue;
  334. if (WARN_ON(!info->control.vif)) {
  335. kfree_skb(skb);
  336. return;
  337. }
  338. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  339. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  340. __skb_queue_tail(&local->pending[queue], skb);
  341. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  342. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  343. }
  344. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  345. struct sk_buff_head *skbs,
  346. void (*fn)(void *data), void *data)
  347. {
  348. struct ieee80211_hw *hw = &local->hw;
  349. struct sk_buff *skb;
  350. unsigned long flags;
  351. int queue, i;
  352. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  353. while ((skb = skb_dequeue(skbs))) {
  354. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  355. if (WARN_ON(!info->control.vif)) {
  356. kfree_skb(skb);
  357. continue;
  358. }
  359. queue = info->hw_queue;
  360. __ieee80211_stop_queue(hw, queue,
  361. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  362. __skb_queue_tail(&local->pending[queue], skb);
  363. }
  364. if (fn)
  365. fn(data);
  366. for (i = 0; i < hw->queues; i++)
  367. __ieee80211_wake_queue(hw, i,
  368. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  369. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  370. }
  371. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  372. enum queue_stop_reason reason)
  373. {
  374. struct ieee80211_local *local = hw_to_local(hw);
  375. unsigned long flags;
  376. int i;
  377. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  378. for (i = 0; i < hw->queues; i++)
  379. __ieee80211_stop_queue(hw, i, reason);
  380. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  381. }
  382. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  383. {
  384. ieee80211_stop_queues_by_reason(hw,
  385. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  386. }
  387. EXPORT_SYMBOL(ieee80211_stop_queues);
  388. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  389. {
  390. struct ieee80211_local *local = hw_to_local(hw);
  391. unsigned long flags;
  392. int ret;
  393. if (WARN_ON(queue >= hw->queues))
  394. return true;
  395. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  396. ret = !!local->queue_stop_reasons[queue];
  397. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  398. return ret;
  399. }
  400. EXPORT_SYMBOL(ieee80211_queue_stopped);
  401. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  402. enum queue_stop_reason reason)
  403. {
  404. struct ieee80211_local *local = hw_to_local(hw);
  405. unsigned long flags;
  406. int i;
  407. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  408. for (i = 0; i < hw->queues; i++)
  409. __ieee80211_wake_queue(hw, i, reason);
  410. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  411. }
  412. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  413. {
  414. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  415. }
  416. EXPORT_SYMBOL(ieee80211_wake_queues);
  417. void ieee80211_iterate_active_interfaces(
  418. struct ieee80211_hw *hw,
  419. void (*iterator)(void *data, u8 *mac,
  420. struct ieee80211_vif *vif),
  421. void *data)
  422. {
  423. struct ieee80211_local *local = hw_to_local(hw);
  424. struct ieee80211_sub_if_data *sdata;
  425. mutex_lock(&local->iflist_mtx);
  426. list_for_each_entry(sdata, &local->interfaces, list) {
  427. switch (sdata->vif.type) {
  428. case NL80211_IFTYPE_MONITOR:
  429. case NL80211_IFTYPE_AP_VLAN:
  430. continue;
  431. default:
  432. break;
  433. }
  434. if (ieee80211_sdata_running(sdata))
  435. iterator(data, sdata->vif.addr,
  436. &sdata->vif);
  437. }
  438. sdata = rcu_dereference_protected(local->monitor_sdata,
  439. lockdep_is_held(&local->iflist_mtx));
  440. if (sdata)
  441. iterator(data, sdata->vif.addr, &sdata->vif);
  442. mutex_unlock(&local->iflist_mtx);
  443. }
  444. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  445. void ieee80211_iterate_active_interfaces_atomic(
  446. struct ieee80211_hw *hw,
  447. void (*iterator)(void *data, u8 *mac,
  448. struct ieee80211_vif *vif),
  449. void *data)
  450. {
  451. struct ieee80211_local *local = hw_to_local(hw);
  452. struct ieee80211_sub_if_data *sdata;
  453. rcu_read_lock();
  454. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  455. switch (sdata->vif.type) {
  456. case NL80211_IFTYPE_MONITOR:
  457. case NL80211_IFTYPE_AP_VLAN:
  458. continue;
  459. default:
  460. break;
  461. }
  462. if (ieee80211_sdata_running(sdata))
  463. iterator(data, sdata->vif.addr,
  464. &sdata->vif);
  465. }
  466. sdata = rcu_dereference(local->monitor_sdata);
  467. if (sdata)
  468. iterator(data, sdata->vif.addr, &sdata->vif);
  469. rcu_read_unlock();
  470. }
  471. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  472. /*
  473. * Nothing should have been stuffed into the workqueue during
  474. * the suspend->resume cycle. If this WARN is seen then there
  475. * is a bug with either the driver suspend or something in
  476. * mac80211 stuffing into the workqueue which we haven't yet
  477. * cleared during mac80211's suspend cycle.
  478. */
  479. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  480. {
  481. if (WARN(local->suspended && !local->resuming,
  482. "queueing ieee80211 work while going to suspend\n"))
  483. return false;
  484. return true;
  485. }
  486. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  487. {
  488. struct ieee80211_local *local = hw_to_local(hw);
  489. if (!ieee80211_can_queue_work(local))
  490. return;
  491. queue_work(local->workqueue, work);
  492. }
  493. EXPORT_SYMBOL(ieee80211_queue_work);
  494. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  495. struct delayed_work *dwork,
  496. unsigned long delay)
  497. {
  498. struct ieee80211_local *local = hw_to_local(hw);
  499. if (!ieee80211_can_queue_work(local))
  500. return;
  501. queue_delayed_work(local->workqueue, dwork, delay);
  502. }
  503. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  504. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  505. struct ieee802_11_elems *elems,
  506. u64 filter, u32 crc)
  507. {
  508. size_t left = len;
  509. u8 *pos = start;
  510. bool calc_crc = filter != 0;
  511. DECLARE_BITMAP(seen_elems, 256);
  512. bitmap_zero(seen_elems, 256);
  513. memset(elems, 0, sizeof(*elems));
  514. elems->ie_start = start;
  515. elems->total_len = len;
  516. while (left >= 2) {
  517. u8 id, elen;
  518. bool elem_parse_failed;
  519. id = *pos++;
  520. elen = *pos++;
  521. left -= 2;
  522. if (elen > left) {
  523. elems->parse_error = true;
  524. break;
  525. }
  526. if (id != WLAN_EID_VENDOR_SPECIFIC &&
  527. id != WLAN_EID_QUIET &&
  528. test_bit(id, seen_elems)) {
  529. elems->parse_error = true;
  530. left -= elen;
  531. pos += elen;
  532. continue;
  533. }
  534. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  535. crc = crc32_be(crc, pos - 2, elen + 2);
  536. elem_parse_failed = false;
  537. switch (id) {
  538. case WLAN_EID_SSID:
  539. elems->ssid = pos;
  540. elems->ssid_len = elen;
  541. break;
  542. case WLAN_EID_SUPP_RATES:
  543. elems->supp_rates = pos;
  544. elems->supp_rates_len = elen;
  545. break;
  546. case WLAN_EID_FH_PARAMS:
  547. elems->fh_params = pos;
  548. elems->fh_params_len = elen;
  549. break;
  550. case WLAN_EID_DS_PARAMS:
  551. elems->ds_params = pos;
  552. elems->ds_params_len = elen;
  553. break;
  554. case WLAN_EID_CF_PARAMS:
  555. elems->cf_params = pos;
  556. elems->cf_params_len = elen;
  557. break;
  558. case WLAN_EID_TIM:
  559. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  560. elems->tim = (void *)pos;
  561. elems->tim_len = elen;
  562. } else
  563. elem_parse_failed = true;
  564. break;
  565. case WLAN_EID_IBSS_PARAMS:
  566. elems->ibss_params = pos;
  567. elems->ibss_params_len = elen;
  568. break;
  569. case WLAN_EID_CHALLENGE:
  570. elems->challenge = pos;
  571. elems->challenge_len = elen;
  572. break;
  573. case WLAN_EID_VENDOR_SPECIFIC:
  574. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  575. pos[2] == 0xf2) {
  576. /* Microsoft OUI (00:50:F2) */
  577. if (calc_crc)
  578. crc = crc32_be(crc, pos - 2, elen + 2);
  579. if (pos[3] == 1) {
  580. /* OUI Type 1 - WPA IE */
  581. elems->wpa = pos;
  582. elems->wpa_len = elen;
  583. } else if (elen >= 5 && pos[3] == 2) {
  584. /* OUI Type 2 - WMM IE */
  585. if (pos[4] == 0) {
  586. elems->wmm_info = pos;
  587. elems->wmm_info_len = elen;
  588. } else if (pos[4] == 1) {
  589. elems->wmm_param = pos;
  590. elems->wmm_param_len = elen;
  591. }
  592. }
  593. }
  594. break;
  595. case WLAN_EID_RSN:
  596. elems->rsn = pos;
  597. elems->rsn_len = elen;
  598. break;
  599. case WLAN_EID_ERP_INFO:
  600. elems->erp_info = pos;
  601. elems->erp_info_len = elen;
  602. break;
  603. case WLAN_EID_EXT_SUPP_RATES:
  604. elems->ext_supp_rates = pos;
  605. elems->ext_supp_rates_len = elen;
  606. break;
  607. case WLAN_EID_HT_CAPABILITY:
  608. if (elen >= sizeof(struct ieee80211_ht_cap))
  609. elems->ht_cap_elem = (void *)pos;
  610. else
  611. elem_parse_failed = true;
  612. break;
  613. case WLAN_EID_HT_OPERATION:
  614. if (elen >= sizeof(struct ieee80211_ht_operation))
  615. elems->ht_operation = (void *)pos;
  616. else
  617. elem_parse_failed = true;
  618. break;
  619. case WLAN_EID_MESH_ID:
  620. elems->mesh_id = pos;
  621. elems->mesh_id_len = elen;
  622. break;
  623. case WLAN_EID_MESH_CONFIG:
  624. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  625. elems->mesh_config = (void *)pos;
  626. else
  627. elem_parse_failed = true;
  628. break;
  629. case WLAN_EID_PEER_MGMT:
  630. elems->peering = pos;
  631. elems->peering_len = elen;
  632. break;
  633. case WLAN_EID_PREQ:
  634. elems->preq = pos;
  635. elems->preq_len = elen;
  636. break;
  637. case WLAN_EID_PREP:
  638. elems->prep = pos;
  639. elems->prep_len = elen;
  640. break;
  641. case WLAN_EID_PERR:
  642. elems->perr = pos;
  643. elems->perr_len = elen;
  644. break;
  645. case WLAN_EID_RANN:
  646. if (elen >= sizeof(struct ieee80211_rann_ie))
  647. elems->rann = (void *)pos;
  648. else
  649. elem_parse_failed = true;
  650. break;
  651. case WLAN_EID_CHANNEL_SWITCH:
  652. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  653. elem_parse_failed = true;
  654. break;
  655. }
  656. elems->ch_switch_ie = (void *)pos;
  657. break;
  658. case WLAN_EID_QUIET:
  659. if (!elems->quiet_elem) {
  660. elems->quiet_elem = pos;
  661. elems->quiet_elem_len = elen;
  662. }
  663. elems->num_of_quiet_elem++;
  664. break;
  665. case WLAN_EID_COUNTRY:
  666. elems->country_elem = pos;
  667. elems->country_elem_len = elen;
  668. break;
  669. case WLAN_EID_PWR_CONSTRAINT:
  670. elems->pwr_constr_elem = pos;
  671. elems->pwr_constr_elem_len = elen;
  672. break;
  673. case WLAN_EID_TIMEOUT_INTERVAL:
  674. elems->timeout_int = pos;
  675. elems->timeout_int_len = elen;
  676. break;
  677. default:
  678. break;
  679. }
  680. if (elem_parse_failed)
  681. elems->parse_error = true;
  682. else
  683. set_bit(id, seen_elems);
  684. left -= elen;
  685. pos += elen;
  686. }
  687. if (left != 0)
  688. elems->parse_error = true;
  689. return crc;
  690. }
  691. void ieee802_11_parse_elems(u8 *start, size_t len,
  692. struct ieee802_11_elems *elems)
  693. {
  694. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  695. }
  696. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  697. bool bss_notify)
  698. {
  699. struct ieee80211_local *local = sdata->local;
  700. struct ieee80211_tx_queue_params qparam;
  701. int ac;
  702. bool use_11b, enable_qos;
  703. int aCWmin, aCWmax;
  704. if (!local->ops->conf_tx)
  705. return;
  706. if (local->hw.queues < IEEE80211_NUM_ACS)
  707. return;
  708. memset(&qparam, 0, sizeof(qparam));
  709. use_11b = (local->oper_channel->band == IEEE80211_BAND_2GHZ) &&
  710. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  711. /*
  712. * By default disable QoS in STA mode for old access points, which do
  713. * not support 802.11e. New APs will provide proper queue parameters,
  714. * that we will configure later.
  715. */
  716. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  717. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  718. /* Set defaults according to 802.11-2007 Table 7-37 */
  719. aCWmax = 1023;
  720. if (use_11b)
  721. aCWmin = 31;
  722. else
  723. aCWmin = 15;
  724. if (enable_qos) {
  725. switch (ac) {
  726. case IEEE80211_AC_BK:
  727. qparam.cw_max = aCWmax;
  728. qparam.cw_min = aCWmin;
  729. qparam.txop = 0;
  730. qparam.aifs = 7;
  731. break;
  732. /* never happens but let's not leave undefined */
  733. default:
  734. case IEEE80211_AC_BE:
  735. qparam.cw_max = aCWmax;
  736. qparam.cw_min = aCWmin;
  737. qparam.txop = 0;
  738. qparam.aifs = 3;
  739. break;
  740. case IEEE80211_AC_VI:
  741. qparam.cw_max = aCWmin;
  742. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  743. if (use_11b)
  744. qparam.txop = 6016/32;
  745. else
  746. qparam.txop = 3008/32;
  747. qparam.aifs = 2;
  748. break;
  749. case IEEE80211_AC_VO:
  750. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  751. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  752. if (use_11b)
  753. qparam.txop = 3264/32;
  754. else
  755. qparam.txop = 1504/32;
  756. qparam.aifs = 2;
  757. break;
  758. }
  759. } else {
  760. /* Confiure old 802.11b/g medium access rules. */
  761. qparam.cw_max = aCWmax;
  762. qparam.cw_min = aCWmin;
  763. qparam.txop = 0;
  764. qparam.aifs = 2;
  765. }
  766. qparam.uapsd = false;
  767. sdata->tx_conf[ac] = qparam;
  768. drv_conf_tx(local, sdata, ac, &qparam);
  769. }
  770. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  771. sdata->vif.bss_conf.qos = enable_qos;
  772. if (bss_notify)
  773. ieee80211_bss_info_change_notify(sdata,
  774. BSS_CHANGED_QOS);
  775. }
  776. }
  777. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  778. const size_t supp_rates_len,
  779. const u8 *supp_rates)
  780. {
  781. struct ieee80211_local *local = sdata->local;
  782. int i, have_higher_than_11mbit = 0;
  783. /* cf. IEEE 802.11 9.2.12 */
  784. for (i = 0; i < supp_rates_len; i++)
  785. if ((supp_rates[i] & 0x7f) * 5 > 110)
  786. have_higher_than_11mbit = 1;
  787. if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
  788. have_higher_than_11mbit)
  789. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  790. else
  791. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  792. ieee80211_set_wmm_default(sdata, true);
  793. }
  794. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  795. enum ieee80211_band band)
  796. {
  797. struct ieee80211_supported_band *sband;
  798. struct ieee80211_rate *bitrates;
  799. u32 mandatory_rates;
  800. enum ieee80211_rate_flags mandatory_flag;
  801. int i;
  802. sband = local->hw.wiphy->bands[band];
  803. if (WARN_ON(!sband))
  804. return 1;
  805. if (band == IEEE80211_BAND_2GHZ)
  806. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  807. else
  808. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  809. bitrates = sband->bitrates;
  810. mandatory_rates = 0;
  811. for (i = 0; i < sband->n_bitrates; i++)
  812. if (bitrates[i].flags & mandatory_flag)
  813. mandatory_rates |= BIT(i);
  814. return mandatory_rates;
  815. }
  816. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  817. u16 transaction, u16 auth_alg,
  818. u8 *extra, size_t extra_len, const u8 *da,
  819. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx)
  820. {
  821. struct ieee80211_local *local = sdata->local;
  822. struct sk_buff *skb;
  823. struct ieee80211_mgmt *mgmt;
  824. int err;
  825. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  826. sizeof(*mgmt) + 6 + extra_len);
  827. if (!skb)
  828. return;
  829. skb_reserve(skb, local->hw.extra_tx_headroom);
  830. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  831. memset(mgmt, 0, 24 + 6);
  832. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  833. IEEE80211_STYPE_AUTH);
  834. memcpy(mgmt->da, da, ETH_ALEN);
  835. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  836. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  837. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  838. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  839. mgmt->u.auth.status_code = cpu_to_le16(0);
  840. if (extra)
  841. memcpy(skb_put(skb, extra_len), extra, extra_len);
  842. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  843. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  844. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  845. WARN_ON(err);
  846. }
  847. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  848. ieee80211_tx_skb(sdata, skb);
  849. }
  850. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  851. const u8 *ie, size_t ie_len,
  852. enum ieee80211_band band, u32 rate_mask,
  853. u8 channel)
  854. {
  855. struct ieee80211_supported_band *sband;
  856. u8 *pos;
  857. size_t offset = 0, noffset;
  858. int supp_rates_len, i;
  859. u8 rates[32];
  860. int num_rates;
  861. int ext_rates_len;
  862. sband = local->hw.wiphy->bands[band];
  863. if (WARN_ON_ONCE(!sband))
  864. return 0;
  865. pos = buffer;
  866. num_rates = 0;
  867. for (i = 0; i < sband->n_bitrates; i++) {
  868. if ((BIT(i) & rate_mask) == 0)
  869. continue; /* skip rate */
  870. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  871. }
  872. supp_rates_len = min_t(int, num_rates, 8);
  873. *pos++ = WLAN_EID_SUPP_RATES;
  874. *pos++ = supp_rates_len;
  875. memcpy(pos, rates, supp_rates_len);
  876. pos += supp_rates_len;
  877. /* insert "request information" if in custom IEs */
  878. if (ie && ie_len) {
  879. static const u8 before_extrates[] = {
  880. WLAN_EID_SSID,
  881. WLAN_EID_SUPP_RATES,
  882. WLAN_EID_REQUEST,
  883. };
  884. noffset = ieee80211_ie_split(ie, ie_len,
  885. before_extrates,
  886. ARRAY_SIZE(before_extrates),
  887. offset);
  888. memcpy(pos, ie + offset, noffset - offset);
  889. pos += noffset - offset;
  890. offset = noffset;
  891. }
  892. ext_rates_len = num_rates - supp_rates_len;
  893. if (ext_rates_len > 0) {
  894. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  895. *pos++ = ext_rates_len;
  896. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  897. pos += ext_rates_len;
  898. }
  899. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  900. *pos++ = WLAN_EID_DS_PARAMS;
  901. *pos++ = 1;
  902. *pos++ = channel;
  903. }
  904. /* insert custom IEs that go before HT */
  905. if (ie && ie_len) {
  906. static const u8 before_ht[] = {
  907. WLAN_EID_SSID,
  908. WLAN_EID_SUPP_RATES,
  909. WLAN_EID_REQUEST,
  910. WLAN_EID_EXT_SUPP_RATES,
  911. WLAN_EID_DS_PARAMS,
  912. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  913. };
  914. noffset = ieee80211_ie_split(ie, ie_len,
  915. before_ht, ARRAY_SIZE(before_ht),
  916. offset);
  917. memcpy(pos, ie + offset, noffset - offset);
  918. pos += noffset - offset;
  919. offset = noffset;
  920. }
  921. if (sband->ht_cap.ht_supported)
  922. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  923. sband->ht_cap.cap);
  924. /*
  925. * If adding more here, adjust code in main.c
  926. * that calculates local->scan_ies_len.
  927. */
  928. /* add any remaining custom IEs */
  929. if (ie && ie_len) {
  930. noffset = ie_len;
  931. memcpy(pos, ie + offset, noffset - offset);
  932. pos += noffset - offset;
  933. }
  934. if (sband->vht_cap.vht_supported)
  935. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  936. sband->vht_cap.cap);
  937. return pos - buffer;
  938. }
  939. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  940. u8 *dst, u32 ratemask,
  941. struct ieee80211_channel *chan,
  942. const u8 *ssid, size_t ssid_len,
  943. const u8 *ie, size_t ie_len,
  944. bool directed)
  945. {
  946. struct ieee80211_local *local = sdata->local;
  947. struct sk_buff *skb;
  948. struct ieee80211_mgmt *mgmt;
  949. size_t buf_len;
  950. u8 *buf;
  951. u8 chan_no;
  952. /* FIXME: come up with a proper value */
  953. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  954. if (!buf)
  955. return NULL;
  956. /*
  957. * Do not send DS Channel parameter for directed probe requests
  958. * in order to maximize the chance that we get a response. Some
  959. * badly-behaved APs don't respond when this parameter is included.
  960. */
  961. if (directed)
  962. chan_no = 0;
  963. else
  964. chan_no = ieee80211_frequency_to_channel(chan->center_freq);
  965. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len, chan->band,
  966. ratemask, chan_no);
  967. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  968. ssid, ssid_len,
  969. buf, buf_len);
  970. if (!skb)
  971. goto out;
  972. if (dst) {
  973. mgmt = (struct ieee80211_mgmt *) skb->data;
  974. memcpy(mgmt->da, dst, ETH_ALEN);
  975. memcpy(mgmt->bssid, dst, ETH_ALEN);
  976. }
  977. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  978. out:
  979. kfree(buf);
  980. return skb;
  981. }
  982. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  983. const u8 *ssid, size_t ssid_len,
  984. const u8 *ie, size_t ie_len,
  985. u32 ratemask, bool directed, bool no_cck)
  986. {
  987. struct sk_buff *skb;
  988. skb = ieee80211_build_probe_req(sdata, dst, ratemask,
  989. sdata->local->hw.conf.channel,
  990. ssid, ssid_len,
  991. ie, ie_len, directed);
  992. if (skb) {
  993. if (no_cck)
  994. IEEE80211_SKB_CB(skb)->flags |=
  995. IEEE80211_TX_CTL_NO_CCK_RATE;
  996. ieee80211_tx_skb(sdata, skb);
  997. }
  998. }
  999. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  1000. struct ieee802_11_elems *elems,
  1001. enum ieee80211_band band, u32 *basic_rates)
  1002. {
  1003. struct ieee80211_supported_band *sband;
  1004. struct ieee80211_rate *bitrates;
  1005. size_t num_rates;
  1006. u32 supp_rates;
  1007. int i, j;
  1008. sband = local->hw.wiphy->bands[band];
  1009. if (WARN_ON(!sband))
  1010. return 1;
  1011. bitrates = sband->bitrates;
  1012. num_rates = sband->n_bitrates;
  1013. supp_rates = 0;
  1014. for (i = 0; i < elems->supp_rates_len +
  1015. elems->ext_supp_rates_len; i++) {
  1016. u8 rate = 0;
  1017. int own_rate;
  1018. bool is_basic;
  1019. if (i < elems->supp_rates_len)
  1020. rate = elems->supp_rates[i];
  1021. else if (elems->ext_supp_rates)
  1022. rate = elems->ext_supp_rates
  1023. [i - elems->supp_rates_len];
  1024. own_rate = 5 * (rate & 0x7f);
  1025. is_basic = !!(rate & 0x80);
  1026. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1027. continue;
  1028. for (j = 0; j < num_rates; j++) {
  1029. if (bitrates[j].bitrate == own_rate) {
  1030. supp_rates |= BIT(j);
  1031. if (basic_rates && is_basic)
  1032. *basic_rates |= BIT(j);
  1033. }
  1034. }
  1035. }
  1036. return supp_rates;
  1037. }
  1038. void ieee80211_stop_device(struct ieee80211_local *local)
  1039. {
  1040. ieee80211_led_radio(local, false);
  1041. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1042. cancel_work_sync(&local->reconfig_filter);
  1043. flush_workqueue(local->workqueue);
  1044. drv_stop(local);
  1045. }
  1046. int ieee80211_reconfig(struct ieee80211_local *local)
  1047. {
  1048. struct ieee80211_hw *hw = &local->hw;
  1049. struct ieee80211_sub_if_data *sdata;
  1050. struct sta_info *sta;
  1051. int res, i;
  1052. #ifdef CONFIG_PM
  1053. if (local->suspended)
  1054. local->resuming = true;
  1055. if (local->wowlan) {
  1056. local->wowlan = false;
  1057. res = drv_resume(local);
  1058. if (res < 0) {
  1059. local->resuming = false;
  1060. return res;
  1061. }
  1062. if (res == 0)
  1063. goto wake_up;
  1064. WARN_ON(res > 1);
  1065. /*
  1066. * res is 1, which means the driver requested
  1067. * to go through a regular reset on wakeup.
  1068. */
  1069. }
  1070. #endif
  1071. /* everything else happens only if HW was up & running */
  1072. if (!local->open_count)
  1073. goto wake_up;
  1074. /*
  1075. * Upon resume hardware can sometimes be goofy due to
  1076. * various platform / driver / bus issues, so restarting
  1077. * the device may at times not work immediately. Propagate
  1078. * the error.
  1079. */
  1080. res = drv_start(local);
  1081. if (res) {
  1082. WARN(local->suspended, "Hardware became unavailable "
  1083. "upon resume. This could be a software issue "
  1084. "prior to suspend or a hardware issue.\n");
  1085. return res;
  1086. }
  1087. /* setup fragmentation threshold */
  1088. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1089. /* setup RTS threshold */
  1090. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1091. /* reset coverage class */
  1092. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1093. ieee80211_led_radio(local, true);
  1094. ieee80211_mod_tpt_led_trig(local,
  1095. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1096. /* add interfaces */
  1097. sdata = rtnl_dereference(local->monitor_sdata);
  1098. if (sdata) {
  1099. res = drv_add_interface(local, sdata);
  1100. if (WARN_ON(res)) {
  1101. rcu_assign_pointer(local->monitor_sdata, NULL);
  1102. synchronize_net();
  1103. kfree(sdata);
  1104. }
  1105. }
  1106. list_for_each_entry(sdata, &local->interfaces, list) {
  1107. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1108. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1109. ieee80211_sdata_running(sdata))
  1110. res = drv_add_interface(local, sdata);
  1111. }
  1112. /* add STAs back */
  1113. mutex_lock(&local->sta_mtx);
  1114. list_for_each_entry(sta, &local->sta_list, list) {
  1115. enum ieee80211_sta_state state;
  1116. if (!sta->uploaded)
  1117. continue;
  1118. /* AP-mode stations will be added later */
  1119. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1120. continue;
  1121. for (state = IEEE80211_STA_NOTEXIST;
  1122. state < sta->sta_state; state++)
  1123. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1124. state + 1));
  1125. }
  1126. mutex_unlock(&local->sta_mtx);
  1127. /* reconfigure tx conf */
  1128. if (hw->queues >= IEEE80211_NUM_ACS) {
  1129. list_for_each_entry(sdata, &local->interfaces, list) {
  1130. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1131. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1132. !ieee80211_sdata_running(sdata))
  1133. continue;
  1134. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1135. drv_conf_tx(local, sdata, i,
  1136. &sdata->tx_conf[i]);
  1137. }
  1138. }
  1139. /* reconfigure hardware */
  1140. ieee80211_hw_config(local, ~0);
  1141. ieee80211_configure_filter(local);
  1142. /* Finally also reconfigure all the BSS information */
  1143. list_for_each_entry(sdata, &local->interfaces, list) {
  1144. u32 changed;
  1145. if (!ieee80211_sdata_running(sdata))
  1146. continue;
  1147. /* common change flags for all interface types */
  1148. changed = BSS_CHANGED_ERP_CTS_PROT |
  1149. BSS_CHANGED_ERP_PREAMBLE |
  1150. BSS_CHANGED_ERP_SLOT |
  1151. BSS_CHANGED_HT |
  1152. BSS_CHANGED_BASIC_RATES |
  1153. BSS_CHANGED_BEACON_INT |
  1154. BSS_CHANGED_BSSID |
  1155. BSS_CHANGED_CQM |
  1156. BSS_CHANGED_QOS |
  1157. BSS_CHANGED_IDLE;
  1158. switch (sdata->vif.type) {
  1159. case NL80211_IFTYPE_STATION:
  1160. changed |= BSS_CHANGED_ASSOC |
  1161. BSS_CHANGED_ARP_FILTER |
  1162. BSS_CHANGED_PS;
  1163. mutex_lock(&sdata->u.mgd.mtx);
  1164. ieee80211_bss_info_change_notify(sdata, changed);
  1165. mutex_unlock(&sdata->u.mgd.mtx);
  1166. break;
  1167. case NL80211_IFTYPE_ADHOC:
  1168. changed |= BSS_CHANGED_IBSS;
  1169. /* fall through */
  1170. case NL80211_IFTYPE_AP:
  1171. changed |= BSS_CHANGED_SSID;
  1172. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1173. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1174. /* fall through */
  1175. case NL80211_IFTYPE_MESH_POINT:
  1176. changed |= BSS_CHANGED_BEACON |
  1177. BSS_CHANGED_BEACON_ENABLED;
  1178. ieee80211_bss_info_change_notify(sdata, changed);
  1179. break;
  1180. case NL80211_IFTYPE_WDS:
  1181. break;
  1182. case NL80211_IFTYPE_AP_VLAN:
  1183. case NL80211_IFTYPE_MONITOR:
  1184. /* ignore virtual */
  1185. break;
  1186. case NL80211_IFTYPE_P2P_DEVICE:
  1187. /* not yet supported */
  1188. case NL80211_IFTYPE_UNSPECIFIED:
  1189. case NUM_NL80211_IFTYPES:
  1190. case NL80211_IFTYPE_P2P_CLIENT:
  1191. case NL80211_IFTYPE_P2P_GO:
  1192. WARN_ON(1);
  1193. break;
  1194. }
  1195. }
  1196. ieee80211_recalc_ps(local, -1);
  1197. /*
  1198. * The sta might be in psm against the ap (e.g. because
  1199. * this was the state before a hw restart), so we
  1200. * explicitly send a null packet in order to make sure
  1201. * it'll sync against the ap (and get out of psm).
  1202. */
  1203. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1204. list_for_each_entry(sdata, &local->interfaces, list) {
  1205. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1206. continue;
  1207. ieee80211_send_nullfunc(local, sdata, 0);
  1208. }
  1209. }
  1210. /* APs are now beaconing, add back stations */
  1211. mutex_lock(&local->sta_mtx);
  1212. list_for_each_entry(sta, &local->sta_list, list) {
  1213. enum ieee80211_sta_state state;
  1214. if (!sta->uploaded)
  1215. continue;
  1216. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1217. continue;
  1218. for (state = IEEE80211_STA_NOTEXIST;
  1219. state < sta->sta_state; state++)
  1220. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1221. state + 1));
  1222. }
  1223. mutex_unlock(&local->sta_mtx);
  1224. /* add back keys */
  1225. list_for_each_entry(sdata, &local->interfaces, list)
  1226. if (ieee80211_sdata_running(sdata))
  1227. ieee80211_enable_keys(sdata);
  1228. wake_up:
  1229. local->in_reconfig = false;
  1230. barrier();
  1231. /*
  1232. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1233. * sessions can be established after a resume.
  1234. *
  1235. * Also tear down aggregation sessions since reconfiguring
  1236. * them in a hardware restart scenario is not easily done
  1237. * right now, and the hardware will have lost information
  1238. * about the sessions, but we and the AP still think they
  1239. * are active. This is really a workaround though.
  1240. */
  1241. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1242. mutex_lock(&local->sta_mtx);
  1243. list_for_each_entry(sta, &local->sta_list, list) {
  1244. ieee80211_sta_tear_down_BA_sessions(sta, true);
  1245. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1246. }
  1247. mutex_unlock(&local->sta_mtx);
  1248. }
  1249. ieee80211_wake_queues_by_reason(hw,
  1250. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1251. /*
  1252. * If this is for hw restart things are still running.
  1253. * We may want to change that later, however.
  1254. */
  1255. if (!local->suspended)
  1256. return 0;
  1257. #ifdef CONFIG_PM
  1258. /* first set suspended false, then resuming */
  1259. local->suspended = false;
  1260. mb();
  1261. local->resuming = false;
  1262. list_for_each_entry(sdata, &local->interfaces, list) {
  1263. switch(sdata->vif.type) {
  1264. case NL80211_IFTYPE_STATION:
  1265. ieee80211_sta_restart(sdata);
  1266. break;
  1267. case NL80211_IFTYPE_ADHOC:
  1268. ieee80211_ibss_restart(sdata);
  1269. break;
  1270. case NL80211_IFTYPE_MESH_POINT:
  1271. ieee80211_mesh_restart(sdata);
  1272. break;
  1273. default:
  1274. break;
  1275. }
  1276. }
  1277. mod_timer(&local->sta_cleanup, jiffies + 1);
  1278. mutex_lock(&local->sta_mtx);
  1279. list_for_each_entry(sta, &local->sta_list, list)
  1280. mesh_plink_restart(sta);
  1281. mutex_unlock(&local->sta_mtx);
  1282. #else
  1283. WARN_ON(1);
  1284. #endif
  1285. return 0;
  1286. }
  1287. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1288. {
  1289. struct ieee80211_sub_if_data *sdata;
  1290. struct ieee80211_local *local;
  1291. struct ieee80211_key *key;
  1292. if (WARN_ON(!vif))
  1293. return;
  1294. sdata = vif_to_sdata(vif);
  1295. local = sdata->local;
  1296. if (WARN_ON(!local->resuming))
  1297. return;
  1298. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1299. return;
  1300. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1301. mutex_lock(&local->key_mtx);
  1302. list_for_each_entry(key, &sdata->key_list, list)
  1303. key->flags |= KEY_FLAG_TAINTED;
  1304. mutex_unlock(&local->key_mtx);
  1305. }
  1306. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1307. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1308. enum ieee80211_smps_mode *smps_mode)
  1309. {
  1310. if (ifmgd->associated) {
  1311. *smps_mode = ifmgd->ap_smps;
  1312. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1313. if (ifmgd->powersave)
  1314. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1315. else
  1316. *smps_mode = IEEE80211_SMPS_OFF;
  1317. }
  1318. return 1;
  1319. }
  1320. return 0;
  1321. }
  1322. /* must hold iflist_mtx */
  1323. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1324. {
  1325. struct ieee80211_sub_if_data *sdata;
  1326. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1327. int count = 0;
  1328. lockdep_assert_held(&local->iflist_mtx);
  1329. /*
  1330. * This function could be improved to handle multiple
  1331. * interfaces better, but right now it makes any
  1332. * non-station interfaces force SM PS to be turned
  1333. * off. If there are multiple station interfaces it
  1334. * could also use the best possible mode, e.g. if
  1335. * one is in static and the other in dynamic then
  1336. * dynamic is ok.
  1337. */
  1338. list_for_each_entry(sdata, &local->interfaces, list) {
  1339. if (!ieee80211_sdata_running(sdata))
  1340. continue;
  1341. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1342. goto set;
  1343. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1344. if (count > 1) {
  1345. smps_mode = IEEE80211_SMPS_OFF;
  1346. break;
  1347. }
  1348. }
  1349. if (smps_mode == local->smps_mode)
  1350. return;
  1351. set:
  1352. local->smps_mode = smps_mode;
  1353. /* changed flag is auto-detected for this */
  1354. ieee80211_hw_config(local, 0);
  1355. }
  1356. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1357. {
  1358. int i;
  1359. for (i = 0; i < n_ids; i++)
  1360. if (ids[i] == id)
  1361. return true;
  1362. return false;
  1363. }
  1364. /**
  1365. * ieee80211_ie_split - split an IE buffer according to ordering
  1366. *
  1367. * @ies: the IE buffer
  1368. * @ielen: the length of the IE buffer
  1369. * @ids: an array with element IDs that are allowed before
  1370. * the split
  1371. * @n_ids: the size of the element ID array
  1372. * @offset: offset where to start splitting in the buffer
  1373. *
  1374. * This function splits an IE buffer by updating the @offset
  1375. * variable to point to the location where the buffer should be
  1376. * split.
  1377. *
  1378. * It assumes that the given IE buffer is well-formed, this
  1379. * has to be guaranteed by the caller!
  1380. *
  1381. * It also assumes that the IEs in the buffer are ordered
  1382. * correctly, if not the result of using this function will not
  1383. * be ordered correctly either, i.e. it does no reordering.
  1384. *
  1385. * The function returns the offset where the next part of the
  1386. * buffer starts, which may be @ielen if the entire (remainder)
  1387. * of the buffer should be used.
  1388. */
  1389. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1390. const u8 *ids, int n_ids, size_t offset)
  1391. {
  1392. size_t pos = offset;
  1393. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1394. pos += 2 + ies[pos + 1];
  1395. return pos;
  1396. }
  1397. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1398. {
  1399. size_t pos = offset;
  1400. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1401. pos += 2 + ies[pos + 1];
  1402. return pos;
  1403. }
  1404. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1405. int rssi_min_thold,
  1406. int rssi_max_thold)
  1407. {
  1408. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1409. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1410. return;
  1411. /*
  1412. * Scale up threshold values before storing it, as the RSSI averaging
  1413. * algorithm uses a scaled up value as well. Change this scaling
  1414. * factor if the RSSI averaging algorithm changes.
  1415. */
  1416. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1417. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1418. }
  1419. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1420. int rssi_min_thold,
  1421. int rssi_max_thold)
  1422. {
  1423. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1424. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1425. rssi_min_thold > rssi_max_thold);
  1426. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1427. rssi_max_thold);
  1428. }
  1429. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1430. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1431. {
  1432. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1433. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1434. }
  1435. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1436. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1437. u16 cap)
  1438. {
  1439. __le16 tmp;
  1440. *pos++ = WLAN_EID_HT_CAPABILITY;
  1441. *pos++ = sizeof(struct ieee80211_ht_cap);
  1442. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1443. /* capability flags */
  1444. tmp = cpu_to_le16(cap);
  1445. memcpy(pos, &tmp, sizeof(u16));
  1446. pos += sizeof(u16);
  1447. /* AMPDU parameters */
  1448. *pos++ = ht_cap->ampdu_factor |
  1449. (ht_cap->ampdu_density <<
  1450. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1451. /* MCS set */
  1452. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1453. pos += sizeof(ht_cap->mcs);
  1454. /* extended capabilities */
  1455. pos += sizeof(__le16);
  1456. /* BF capabilities */
  1457. pos += sizeof(__le32);
  1458. /* antenna selection */
  1459. pos += sizeof(u8);
  1460. return pos;
  1461. }
  1462. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1463. u32 cap)
  1464. {
  1465. __le32 tmp;
  1466. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1467. *pos++ = sizeof(struct ieee80211_vht_capabilities);
  1468. memset(pos, 0, sizeof(struct ieee80211_vht_capabilities));
  1469. /* capability flags */
  1470. tmp = cpu_to_le32(cap);
  1471. memcpy(pos, &tmp, sizeof(u32));
  1472. pos += sizeof(u32);
  1473. /* VHT MCS set */
  1474. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1475. pos += sizeof(vht_cap->vht_mcs);
  1476. return pos;
  1477. }
  1478. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1479. struct ieee80211_channel *channel,
  1480. enum nl80211_channel_type channel_type,
  1481. u16 prot_mode)
  1482. {
  1483. struct ieee80211_ht_operation *ht_oper;
  1484. /* Build HT Information */
  1485. *pos++ = WLAN_EID_HT_OPERATION;
  1486. *pos++ = sizeof(struct ieee80211_ht_operation);
  1487. ht_oper = (struct ieee80211_ht_operation *)pos;
  1488. ht_oper->primary_chan =
  1489. ieee80211_frequency_to_channel(channel->center_freq);
  1490. switch (channel_type) {
  1491. case NL80211_CHAN_HT40MINUS:
  1492. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1493. break;
  1494. case NL80211_CHAN_HT40PLUS:
  1495. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1496. break;
  1497. case NL80211_CHAN_HT20:
  1498. default:
  1499. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1500. break;
  1501. }
  1502. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1503. channel_type != NL80211_CHAN_NO_HT &&
  1504. channel_type != NL80211_CHAN_HT20)
  1505. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1506. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1507. ht_oper->stbc_param = 0x0000;
  1508. /* It seems that Basic MCS set and Supported MCS set
  1509. are identical for the first 10 bytes */
  1510. memset(&ht_oper->basic_set, 0, 16);
  1511. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1512. return pos + sizeof(struct ieee80211_ht_operation);
  1513. }
  1514. enum nl80211_channel_type
  1515. ieee80211_ht_oper_to_channel_type(struct ieee80211_ht_operation *ht_oper)
  1516. {
  1517. enum nl80211_channel_type channel_type;
  1518. if (!ht_oper)
  1519. return NL80211_CHAN_NO_HT;
  1520. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1521. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1522. channel_type = NL80211_CHAN_HT20;
  1523. break;
  1524. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1525. channel_type = NL80211_CHAN_HT40PLUS;
  1526. break;
  1527. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1528. channel_type = NL80211_CHAN_HT40MINUS;
  1529. break;
  1530. default:
  1531. channel_type = NL80211_CHAN_NO_HT;
  1532. }
  1533. return channel_type;
  1534. }
  1535. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1536. struct sk_buff *skb, bool need_basic,
  1537. enum ieee80211_band band)
  1538. {
  1539. struct ieee80211_local *local = sdata->local;
  1540. struct ieee80211_supported_band *sband;
  1541. int rate;
  1542. u8 i, rates, *pos;
  1543. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1544. sband = local->hw.wiphy->bands[band];
  1545. rates = sband->n_bitrates;
  1546. if (rates > 8)
  1547. rates = 8;
  1548. if (skb_tailroom(skb) < rates + 2)
  1549. return -ENOMEM;
  1550. pos = skb_put(skb, rates + 2);
  1551. *pos++ = WLAN_EID_SUPP_RATES;
  1552. *pos++ = rates;
  1553. for (i = 0; i < rates; i++) {
  1554. u8 basic = 0;
  1555. if (need_basic && basic_rates & BIT(i))
  1556. basic = 0x80;
  1557. rate = sband->bitrates[i].bitrate;
  1558. *pos++ = basic | (u8) (rate / 5);
  1559. }
  1560. return 0;
  1561. }
  1562. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1563. struct sk_buff *skb, bool need_basic,
  1564. enum ieee80211_band band)
  1565. {
  1566. struct ieee80211_local *local = sdata->local;
  1567. struct ieee80211_supported_band *sband;
  1568. int rate;
  1569. u8 i, exrates, *pos;
  1570. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1571. sband = local->hw.wiphy->bands[band];
  1572. exrates = sband->n_bitrates;
  1573. if (exrates > 8)
  1574. exrates -= 8;
  1575. else
  1576. exrates = 0;
  1577. if (skb_tailroom(skb) < exrates + 2)
  1578. return -ENOMEM;
  1579. if (exrates) {
  1580. pos = skb_put(skb, exrates + 2);
  1581. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1582. *pos++ = exrates;
  1583. for (i = 8; i < sband->n_bitrates; i++) {
  1584. u8 basic = 0;
  1585. if (need_basic && basic_rates & BIT(i))
  1586. basic = 0x80;
  1587. rate = sband->bitrates[i].bitrate;
  1588. *pos++ = basic | (u8) (rate / 5);
  1589. }
  1590. }
  1591. return 0;
  1592. }
  1593. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1594. {
  1595. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1596. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1597. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1598. /* non-managed type inferfaces */
  1599. return 0;
  1600. }
  1601. return ifmgd->ave_beacon_signal;
  1602. }
  1603. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);