util.c 46 KB

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