util.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright (C) 2015-2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018-2019 Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * utilities for mac80211
  15. */
  16. #include <net/mac80211.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/export.h>
  19. #include <linux/types.h>
  20. #include <linux/slab.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/crc32.h>
  26. #include <net/net_namespace.h>
  27. #include <net/cfg80211.h>
  28. #include <net/rtnetlink.h>
  29. #include "ieee80211_i.h"
  30. #include "driver-ops.h"
  31. #include "rate.h"
  32. #include "mesh.h"
  33. #include "wme.h"
  34. #include "led.h"
  35. #include "wep.h"
  36. /* privid for wiphys to determine whether they belong to us or not */
  37. const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
  38. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  39. {
  40. struct ieee80211_local *local;
  41. BUG_ON(!wiphy);
  42. local = wiphy_priv(wiphy);
  43. return &local->hw;
  44. }
  45. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  46. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  47. {
  48. struct sk_buff *skb;
  49. struct ieee80211_hdr *hdr;
  50. skb_queue_walk(&tx->skbs, skb) {
  51. hdr = (struct ieee80211_hdr *) skb->data;
  52. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  53. }
  54. }
  55. int ieee80211_frame_duration(enum nl80211_band band, size_t len,
  56. int rate, int erp, int short_preamble,
  57. int shift)
  58. {
  59. int dur;
  60. /* calculate duration (in microseconds, rounded up to next higher
  61. * integer if it includes a fractional microsecond) to send frame of
  62. * len bytes (does not include FCS) at the given rate. Duration will
  63. * also include SIFS.
  64. *
  65. * rate is in 100 kbps, so divident is multiplied by 10 in the
  66. * DIV_ROUND_UP() operations.
  67. *
  68. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  69. * is assumed to be 0 otherwise.
  70. */
  71. if (band == NL80211_BAND_5GHZ || erp) {
  72. /*
  73. * OFDM:
  74. *
  75. * N_DBPS = DATARATE x 4
  76. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  77. * (16 = SIGNAL time, 6 = tail bits)
  78. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  79. *
  80. * T_SYM = 4 usec
  81. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  82. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  83. * signal ext = 6 usec
  84. */
  85. dur = 16; /* SIFS + signal ext */
  86. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  87. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  88. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  89. * * times 4 for 5 MHz
  90. * * times 2 for 10 MHz
  91. */
  92. dur *= 1 << shift;
  93. /* rates should already consider the channel bandwidth,
  94. * don't apply divisor again.
  95. */
  96. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  97. 4 * rate); /* T_SYM x N_SYM */
  98. } else {
  99. /*
  100. * 802.11b or 802.11g with 802.11b compatibility:
  101. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  102. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  103. *
  104. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  105. * aSIFSTime = 10 usec
  106. * aPreambleLength = 144 usec or 72 usec with short preamble
  107. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  108. */
  109. dur = 10; /* aSIFSTime = 10 usec */
  110. dur += short_preamble ? (72 + 24) : (144 + 48);
  111. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  112. }
  113. return dur;
  114. }
  115. /* Exported duration function for driver use */
  116. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  117. struct ieee80211_vif *vif,
  118. enum nl80211_band band,
  119. size_t frame_len,
  120. struct ieee80211_rate *rate)
  121. {
  122. struct ieee80211_sub_if_data *sdata;
  123. u16 dur;
  124. int erp, shift = 0;
  125. bool short_preamble = false;
  126. erp = 0;
  127. if (vif) {
  128. sdata = vif_to_sdata(vif);
  129. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  130. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  131. erp = rate->flags & IEEE80211_RATE_ERP_G;
  132. shift = ieee80211_vif_get_shift(vif);
  133. }
  134. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  135. short_preamble, shift);
  136. return cpu_to_le16(dur);
  137. }
  138. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  139. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  140. struct ieee80211_vif *vif, size_t frame_len,
  141. const struct ieee80211_tx_info *frame_txctl)
  142. {
  143. struct ieee80211_local *local = hw_to_local(hw);
  144. struct ieee80211_rate *rate;
  145. struct ieee80211_sub_if_data *sdata;
  146. bool short_preamble;
  147. int erp, shift = 0, bitrate;
  148. u16 dur;
  149. struct ieee80211_supported_band *sband;
  150. sband = local->hw.wiphy->bands[frame_txctl->band];
  151. short_preamble = false;
  152. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  153. erp = 0;
  154. if (vif) {
  155. sdata = vif_to_sdata(vif);
  156. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  157. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  158. erp = rate->flags & IEEE80211_RATE_ERP_G;
  159. shift = ieee80211_vif_get_shift(vif);
  160. }
  161. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  162. /* CTS duration */
  163. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  164. erp, short_preamble, shift);
  165. /* Data frame duration */
  166. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  167. erp, short_preamble, shift);
  168. /* ACK duration */
  169. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  170. erp, short_preamble, shift);
  171. return cpu_to_le16(dur);
  172. }
  173. EXPORT_SYMBOL(ieee80211_rts_duration);
  174. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  175. struct ieee80211_vif *vif,
  176. size_t frame_len,
  177. const struct ieee80211_tx_info *frame_txctl)
  178. {
  179. struct ieee80211_local *local = hw_to_local(hw);
  180. struct ieee80211_rate *rate;
  181. struct ieee80211_sub_if_data *sdata;
  182. bool short_preamble;
  183. int erp, shift = 0, bitrate;
  184. u16 dur;
  185. struct ieee80211_supported_band *sband;
  186. sband = local->hw.wiphy->bands[frame_txctl->band];
  187. short_preamble = false;
  188. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  189. erp = 0;
  190. if (vif) {
  191. sdata = vif_to_sdata(vif);
  192. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  193. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  194. erp = rate->flags & IEEE80211_RATE_ERP_G;
  195. shift = ieee80211_vif_get_shift(vif);
  196. }
  197. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  198. /* Data frame duration */
  199. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  200. erp, short_preamble, shift);
  201. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  202. /* ACK duration */
  203. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  204. erp, short_preamble, shift);
  205. }
  206. return cpu_to_le16(dur);
  207. }
  208. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  209. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  210. {
  211. struct ieee80211_sub_if_data *sdata;
  212. int n_acs = IEEE80211_NUM_ACS;
  213. if (local->ops->wake_tx_queue)
  214. return;
  215. if (local->hw.queues < IEEE80211_NUM_ACS)
  216. n_acs = 1;
  217. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  218. int ac;
  219. if (!sdata->dev)
  220. continue;
  221. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  222. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  223. continue;
  224. for (ac = 0; ac < n_acs; ac++) {
  225. int ac_queue = sdata->vif.hw_queue[ac];
  226. if (ac_queue == queue ||
  227. (sdata->vif.cab_queue == queue &&
  228. local->queue_stop_reasons[ac_queue] == 0 &&
  229. skb_queue_empty(&local->pending[ac_queue])))
  230. netif_wake_subqueue(sdata->dev, ac);
  231. }
  232. }
  233. }
  234. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  235. enum queue_stop_reason reason,
  236. bool refcounted)
  237. {
  238. struct ieee80211_local *local = hw_to_local(hw);
  239. trace_wake_queue(local, queue, reason);
  240. if (WARN_ON(queue >= hw->queues))
  241. return;
  242. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  243. return;
  244. if (!refcounted) {
  245. local->q_stop_reasons[queue][reason] = 0;
  246. } else {
  247. local->q_stop_reasons[queue][reason]--;
  248. if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
  249. local->q_stop_reasons[queue][reason] = 0;
  250. }
  251. if (local->q_stop_reasons[queue][reason] == 0)
  252. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  253. if (local->queue_stop_reasons[queue] != 0)
  254. /* someone still has this queue stopped */
  255. return;
  256. if (skb_queue_empty(&local->pending[queue])) {
  257. rcu_read_lock();
  258. ieee80211_propagate_queue_wake(local, queue);
  259. rcu_read_unlock();
  260. } else
  261. tasklet_schedule(&local->tx_pending_tasklet);
  262. }
  263. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  264. enum queue_stop_reason reason,
  265. bool refcounted)
  266. {
  267. struct ieee80211_local *local = hw_to_local(hw);
  268. unsigned long flags;
  269. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  270. __ieee80211_wake_queue(hw, queue, reason, refcounted);
  271. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  272. }
  273. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  274. {
  275. ieee80211_wake_queue_by_reason(hw, queue,
  276. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  277. false);
  278. }
  279. EXPORT_SYMBOL(ieee80211_wake_queue);
  280. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  281. enum queue_stop_reason reason,
  282. bool refcounted)
  283. {
  284. struct ieee80211_local *local = hw_to_local(hw);
  285. struct ieee80211_sub_if_data *sdata;
  286. int n_acs = IEEE80211_NUM_ACS;
  287. trace_stop_queue(local, queue, reason);
  288. if (WARN_ON(queue >= hw->queues))
  289. return;
  290. if (!refcounted)
  291. local->q_stop_reasons[queue][reason] = 1;
  292. else
  293. local->q_stop_reasons[queue][reason]++;
  294. if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
  295. return;
  296. if (local->ops->wake_tx_queue)
  297. return;
  298. if (local->hw.queues < IEEE80211_NUM_ACS)
  299. n_acs = 1;
  300. rcu_read_lock();
  301. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  302. int ac;
  303. if (!sdata->dev)
  304. continue;
  305. for (ac = 0; ac < n_acs; ac++) {
  306. if (sdata->vif.hw_queue[ac] == queue ||
  307. sdata->vif.cab_queue == queue)
  308. netif_stop_subqueue(sdata->dev, ac);
  309. }
  310. }
  311. rcu_read_unlock();
  312. }
  313. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  314. enum queue_stop_reason reason,
  315. bool refcounted)
  316. {
  317. struct ieee80211_local *local = hw_to_local(hw);
  318. unsigned long flags;
  319. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  320. __ieee80211_stop_queue(hw, queue, reason, refcounted);
  321. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  322. }
  323. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  324. {
  325. ieee80211_stop_queue_by_reason(hw, queue,
  326. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  327. false);
  328. }
  329. EXPORT_SYMBOL(ieee80211_stop_queue);
  330. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  331. struct sk_buff *skb)
  332. {
  333. struct ieee80211_hw *hw = &local->hw;
  334. unsigned long flags;
  335. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  336. int queue = info->hw_queue;
  337. if (WARN_ON(!info->control.vif)) {
  338. ieee80211_free_txskb(&local->hw, skb);
  339. return;
  340. }
  341. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  342. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  343. false);
  344. __skb_queue_tail(&local->pending[queue], skb);
  345. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  346. false);
  347. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  348. }
  349. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  350. struct sk_buff_head *skbs)
  351. {
  352. struct ieee80211_hw *hw = &local->hw;
  353. struct sk_buff *skb;
  354. unsigned long flags;
  355. int queue, i;
  356. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  357. while ((skb = skb_dequeue(skbs))) {
  358. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  359. if (WARN_ON(!info->control.vif)) {
  360. ieee80211_free_txskb(&local->hw, skb);
  361. continue;
  362. }
  363. queue = info->hw_queue;
  364. __ieee80211_stop_queue(hw, queue,
  365. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  366. false);
  367. __skb_queue_tail(&local->pending[queue], skb);
  368. }
  369. for (i = 0; i < hw->queues; i++)
  370. __ieee80211_wake_queue(hw, i,
  371. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  372. false);
  373. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  374. }
  375. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  376. unsigned long queues,
  377. enum queue_stop_reason reason,
  378. bool refcounted)
  379. {
  380. struct ieee80211_local *local = hw_to_local(hw);
  381. unsigned long flags;
  382. int i;
  383. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  384. for_each_set_bit(i, &queues, hw->queues)
  385. __ieee80211_stop_queue(hw, i, reason, refcounted);
  386. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  387. }
  388. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  389. {
  390. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  391. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  392. false);
  393. }
  394. EXPORT_SYMBOL(ieee80211_stop_queues);
  395. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  396. {
  397. struct ieee80211_local *local = hw_to_local(hw);
  398. unsigned long flags;
  399. int ret;
  400. if (WARN_ON(queue >= hw->queues))
  401. return true;
  402. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  403. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  404. &local->queue_stop_reasons[queue]);
  405. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL(ieee80211_queue_stopped);
  409. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  410. unsigned long queues,
  411. enum queue_stop_reason reason,
  412. bool refcounted)
  413. {
  414. struct ieee80211_local *local = hw_to_local(hw);
  415. unsigned long flags;
  416. int i;
  417. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  418. for_each_set_bit(i, &queues, hw->queues)
  419. __ieee80211_wake_queue(hw, i, reason, refcounted);
  420. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  421. }
  422. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  423. {
  424. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  425. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  426. false);
  427. }
  428. EXPORT_SYMBOL(ieee80211_wake_queues);
  429. static unsigned int
  430. ieee80211_get_vif_queues(struct ieee80211_local *local,
  431. struct ieee80211_sub_if_data *sdata)
  432. {
  433. unsigned int queues;
  434. if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  435. int ac;
  436. queues = 0;
  437. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  438. queues |= BIT(sdata->vif.hw_queue[ac]);
  439. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  440. queues |= BIT(sdata->vif.cab_queue);
  441. } else {
  442. /* all queues */
  443. queues = BIT(local->hw.queues) - 1;
  444. }
  445. return queues;
  446. }
  447. void __ieee80211_flush_queues(struct ieee80211_local *local,
  448. struct ieee80211_sub_if_data *sdata,
  449. unsigned int queues, bool drop)
  450. {
  451. if (!local->ops->flush)
  452. return;
  453. /*
  454. * If no queue was set, or if the HW doesn't support
  455. * IEEE80211_HW_QUEUE_CONTROL - flush all queues
  456. */
  457. if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  458. queues = ieee80211_get_vif_queues(local, sdata);
  459. ieee80211_stop_queues_by_reason(&local->hw, queues,
  460. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  461. false);
  462. drv_flush(local, sdata, queues, drop);
  463. ieee80211_wake_queues_by_reason(&local->hw, queues,
  464. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  465. false);
  466. }
  467. void ieee80211_flush_queues(struct ieee80211_local *local,
  468. struct ieee80211_sub_if_data *sdata, bool drop)
  469. {
  470. __ieee80211_flush_queues(local, sdata, 0, drop);
  471. }
  472. void ieee80211_stop_vif_queues(struct ieee80211_local *local,
  473. struct ieee80211_sub_if_data *sdata,
  474. enum queue_stop_reason reason)
  475. {
  476. ieee80211_stop_queues_by_reason(&local->hw,
  477. ieee80211_get_vif_queues(local, sdata),
  478. reason, true);
  479. }
  480. void ieee80211_wake_vif_queues(struct ieee80211_local *local,
  481. struct ieee80211_sub_if_data *sdata,
  482. enum queue_stop_reason reason)
  483. {
  484. ieee80211_wake_queues_by_reason(&local->hw,
  485. ieee80211_get_vif_queues(local, sdata),
  486. reason, true);
  487. }
  488. static void __iterate_interfaces(struct ieee80211_local *local,
  489. u32 iter_flags,
  490. void (*iterator)(void *data, u8 *mac,
  491. struct ieee80211_vif *vif),
  492. void *data)
  493. {
  494. struct ieee80211_sub_if_data *sdata;
  495. bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
  496. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  497. switch (sdata->vif.type) {
  498. case NL80211_IFTYPE_MONITOR:
  499. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  500. continue;
  501. break;
  502. case NL80211_IFTYPE_AP_VLAN:
  503. continue;
  504. default:
  505. break;
  506. }
  507. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  508. active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  509. continue;
  510. if (ieee80211_sdata_running(sdata) || !active_only)
  511. iterator(data, sdata->vif.addr,
  512. &sdata->vif);
  513. }
  514. sdata = rcu_dereference_check(local->monitor_sdata,
  515. lockdep_is_held(&local->iflist_mtx) ||
  516. lockdep_rtnl_is_held());
  517. if (sdata &&
  518. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
  519. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  520. iterator(data, sdata->vif.addr, &sdata->vif);
  521. }
  522. void ieee80211_iterate_interfaces(
  523. struct ieee80211_hw *hw, u32 iter_flags,
  524. void (*iterator)(void *data, u8 *mac,
  525. struct ieee80211_vif *vif),
  526. void *data)
  527. {
  528. struct ieee80211_local *local = hw_to_local(hw);
  529. mutex_lock(&local->iflist_mtx);
  530. __iterate_interfaces(local, iter_flags, iterator, data);
  531. mutex_unlock(&local->iflist_mtx);
  532. }
  533. EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
  534. void ieee80211_iterate_active_interfaces_atomic(
  535. struct ieee80211_hw *hw, u32 iter_flags,
  536. void (*iterator)(void *data, u8 *mac,
  537. struct ieee80211_vif *vif),
  538. void *data)
  539. {
  540. struct ieee80211_local *local = hw_to_local(hw);
  541. rcu_read_lock();
  542. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  543. iterator, data);
  544. rcu_read_unlock();
  545. }
  546. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  547. void ieee80211_iterate_active_interfaces_rtnl(
  548. struct ieee80211_hw *hw, u32 iter_flags,
  549. void (*iterator)(void *data, u8 *mac,
  550. struct ieee80211_vif *vif),
  551. void *data)
  552. {
  553. struct ieee80211_local *local = hw_to_local(hw);
  554. ASSERT_RTNL();
  555. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  556. iterator, data);
  557. }
  558. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
  559. static void __iterate_stations(struct ieee80211_local *local,
  560. void (*iterator)(void *data,
  561. struct ieee80211_sta *sta),
  562. void *data)
  563. {
  564. struct sta_info *sta;
  565. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  566. if (!sta->uploaded)
  567. continue;
  568. iterator(data, &sta->sta);
  569. }
  570. }
  571. void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
  572. void (*iterator)(void *data,
  573. struct ieee80211_sta *sta),
  574. void *data)
  575. {
  576. struct ieee80211_local *local = hw_to_local(hw);
  577. rcu_read_lock();
  578. __iterate_stations(local, iterator, data);
  579. rcu_read_unlock();
  580. }
  581. EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
  582. struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
  583. {
  584. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  585. if (!ieee80211_sdata_running(sdata) ||
  586. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  587. return NULL;
  588. return &sdata->vif;
  589. }
  590. EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
  591. struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
  592. {
  593. struct ieee80211_sub_if_data *sdata;
  594. if (!vif)
  595. return NULL;
  596. sdata = vif_to_sdata(vif);
  597. if (!ieee80211_sdata_running(sdata) ||
  598. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  599. return NULL;
  600. return &sdata->wdev;
  601. }
  602. EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
  603. /*
  604. * Nothing should have been stuffed into the workqueue during
  605. * the suspend->resume cycle. Since we can't check each caller
  606. * of this function if we are already quiescing / suspended,
  607. * check here and don't WARN since this can actually happen when
  608. * the rx path (for example) is racing against __ieee80211_suspend
  609. * and suspending / quiescing was set after the rx path checked
  610. * them.
  611. */
  612. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  613. {
  614. if (local->quiescing || (local->suspended && !local->resuming)) {
  615. pr_warn("queueing ieee80211 work while going to suspend\n");
  616. return false;
  617. }
  618. return true;
  619. }
  620. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  621. {
  622. struct ieee80211_local *local = hw_to_local(hw);
  623. if (!ieee80211_can_queue_work(local))
  624. return;
  625. queue_work(local->workqueue, work);
  626. }
  627. EXPORT_SYMBOL(ieee80211_queue_work);
  628. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  629. struct delayed_work *dwork,
  630. unsigned long delay)
  631. {
  632. struct ieee80211_local *local = hw_to_local(hw);
  633. if (!ieee80211_can_queue_work(local))
  634. return;
  635. queue_delayed_work(local->workqueue, dwork, delay);
  636. }
  637. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  638. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  639. struct ieee802_11_elems *elems,
  640. u64 filter, u32 crc)
  641. {
  642. size_t left = len;
  643. const u8 *pos = start;
  644. bool calc_crc = filter != 0;
  645. DECLARE_BITMAP(seen_elems, 256);
  646. const u8 *ie;
  647. bitmap_zero(seen_elems, 256);
  648. memset(elems, 0, sizeof(*elems));
  649. elems->ie_start = start;
  650. elems->total_len = len;
  651. while (left >= 2) {
  652. u8 id, elen;
  653. bool elem_parse_failed;
  654. id = *pos++;
  655. elen = *pos++;
  656. left -= 2;
  657. if (elen > left) {
  658. elems->parse_error = true;
  659. break;
  660. }
  661. switch (id) {
  662. case WLAN_EID_SSID:
  663. case WLAN_EID_SUPP_RATES:
  664. case WLAN_EID_FH_PARAMS:
  665. case WLAN_EID_DS_PARAMS:
  666. case WLAN_EID_CF_PARAMS:
  667. case WLAN_EID_TIM:
  668. case WLAN_EID_IBSS_PARAMS:
  669. case WLAN_EID_CHALLENGE:
  670. case WLAN_EID_RSN:
  671. case WLAN_EID_ERP_INFO:
  672. case WLAN_EID_EXT_SUPP_RATES:
  673. case WLAN_EID_HT_CAPABILITY:
  674. case WLAN_EID_HT_OPERATION:
  675. case WLAN_EID_VHT_CAPABILITY:
  676. case WLAN_EID_VHT_OPERATION:
  677. case WLAN_EID_MESH_ID:
  678. case WLAN_EID_MESH_CONFIG:
  679. case WLAN_EID_PEER_MGMT:
  680. case WLAN_EID_PREQ:
  681. case WLAN_EID_PREP:
  682. case WLAN_EID_PERR:
  683. case WLAN_EID_RANN:
  684. case WLAN_EID_CHANNEL_SWITCH:
  685. case WLAN_EID_EXT_CHANSWITCH_ANN:
  686. case WLAN_EID_COUNTRY:
  687. case WLAN_EID_PWR_CONSTRAINT:
  688. case WLAN_EID_TIMEOUT_INTERVAL:
  689. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  690. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  691. case WLAN_EID_CHAN_SWITCH_PARAM:
  692. case WLAN_EID_EXT_CAPABILITY:
  693. case WLAN_EID_CHAN_SWITCH_TIMING:
  694. case WLAN_EID_LINK_ID:
  695. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  696. /*
  697. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  698. * that if the content gets bigger it might be needed more than once
  699. */
  700. if (test_bit(id, seen_elems)) {
  701. elems->parse_error = true;
  702. left -= elen;
  703. pos += elen;
  704. continue;
  705. }
  706. break;
  707. }
  708. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  709. crc = crc32_be(crc, pos - 2, elen + 2);
  710. elem_parse_failed = false;
  711. switch (id) {
  712. case WLAN_EID_LINK_ID:
  713. if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
  714. elem_parse_failed = true;
  715. break;
  716. }
  717. elems->lnk_id = (void *)(pos - 2);
  718. break;
  719. case WLAN_EID_CHAN_SWITCH_TIMING:
  720. if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
  721. elem_parse_failed = true;
  722. break;
  723. }
  724. elems->ch_sw_timing = (void *)pos;
  725. break;
  726. case WLAN_EID_EXT_CAPABILITY:
  727. elems->ext_capab = pos;
  728. elems->ext_capab_len = elen;
  729. break;
  730. case WLAN_EID_SSID:
  731. elems->ssid = pos;
  732. elems->ssid_len = elen;
  733. break;
  734. case WLAN_EID_SUPP_RATES:
  735. elems->supp_rates = pos;
  736. elems->supp_rates_len = elen;
  737. break;
  738. case WLAN_EID_DS_PARAMS:
  739. if (elen >= 1)
  740. elems->ds_params = pos;
  741. else
  742. elem_parse_failed = true;
  743. break;
  744. case WLAN_EID_TIM:
  745. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  746. elems->tim = (void *)pos;
  747. elems->tim_len = elen;
  748. } else
  749. elem_parse_failed = true;
  750. break;
  751. case WLAN_EID_CHALLENGE:
  752. elems->challenge = pos;
  753. elems->challenge_len = elen;
  754. break;
  755. case WLAN_EID_VENDOR_SPECIFIC:
  756. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  757. pos[2] == 0xf2) {
  758. /* Microsoft OUI (00:50:F2) */
  759. if (calc_crc)
  760. crc = crc32_be(crc, pos - 2, elen + 2);
  761. if (elen >= 5 && pos[3] == 2) {
  762. /* OUI Type 2 - WMM IE */
  763. if (pos[4] == 0) {
  764. elems->wmm_info = pos;
  765. elems->wmm_info_len = elen;
  766. } else if (pos[4] == 1) {
  767. elems->wmm_param = pos;
  768. elems->wmm_param_len = elen;
  769. }
  770. }
  771. }
  772. break;
  773. case WLAN_EID_RSN:
  774. elems->rsn = pos;
  775. elems->rsn_len = elen;
  776. break;
  777. case WLAN_EID_ERP_INFO:
  778. if (elen >= 1)
  779. elems->erp_info = pos;
  780. else
  781. elem_parse_failed = true;
  782. break;
  783. case WLAN_EID_EXT_SUPP_RATES:
  784. elems->ext_supp_rates = pos;
  785. elems->ext_supp_rates_len = elen;
  786. break;
  787. case WLAN_EID_HT_CAPABILITY:
  788. if (elen >= sizeof(struct ieee80211_ht_cap))
  789. elems->ht_cap_elem = (void *)pos;
  790. else
  791. elem_parse_failed = true;
  792. break;
  793. case WLAN_EID_HT_OPERATION:
  794. if (elen >= sizeof(struct ieee80211_ht_operation))
  795. elems->ht_operation = (void *)pos;
  796. else
  797. elem_parse_failed = true;
  798. break;
  799. case WLAN_EID_VHT_CAPABILITY:
  800. if (elen >= sizeof(struct ieee80211_vht_cap))
  801. elems->vht_cap_elem = (void *)pos;
  802. else
  803. elem_parse_failed = true;
  804. break;
  805. case WLAN_EID_VHT_OPERATION:
  806. if (elen >= sizeof(struct ieee80211_vht_operation))
  807. elems->vht_operation = (void *)pos;
  808. else
  809. elem_parse_failed = true;
  810. break;
  811. case WLAN_EID_OPMODE_NOTIF:
  812. if (elen > 0)
  813. elems->opmode_notif = pos;
  814. else
  815. elem_parse_failed = true;
  816. break;
  817. case WLAN_EID_MESH_ID:
  818. elems->mesh_id = pos;
  819. elems->mesh_id_len = elen;
  820. break;
  821. case WLAN_EID_MESH_CONFIG:
  822. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  823. elems->mesh_config = (void *)pos;
  824. else
  825. elem_parse_failed = true;
  826. break;
  827. case WLAN_EID_PEER_MGMT:
  828. elems->peering = pos;
  829. elems->peering_len = elen;
  830. break;
  831. case WLAN_EID_MESH_AWAKE_WINDOW:
  832. if (elen >= 2)
  833. elems->awake_window = (void *)pos;
  834. break;
  835. case WLAN_EID_PREQ:
  836. elems->preq = pos;
  837. elems->preq_len = elen;
  838. break;
  839. case WLAN_EID_PREP:
  840. elems->prep = pos;
  841. elems->prep_len = elen;
  842. break;
  843. case WLAN_EID_PERR:
  844. elems->perr = pos;
  845. elems->perr_len = elen;
  846. break;
  847. case WLAN_EID_RANN:
  848. if (elen >= sizeof(struct ieee80211_rann_ie))
  849. elems->rann = (void *)pos;
  850. else
  851. elem_parse_failed = true;
  852. break;
  853. case WLAN_EID_CHANNEL_SWITCH:
  854. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  855. elem_parse_failed = true;
  856. break;
  857. }
  858. elems->ch_switch_ie = (void *)pos;
  859. break;
  860. case WLAN_EID_EXT_CHANSWITCH_ANN:
  861. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  862. elem_parse_failed = true;
  863. break;
  864. }
  865. elems->ext_chansw_ie = (void *)pos;
  866. break;
  867. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  868. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  869. elem_parse_failed = true;
  870. break;
  871. }
  872. elems->sec_chan_offs = (void *)pos;
  873. break;
  874. case WLAN_EID_CHAN_SWITCH_PARAM:
  875. if (elen !=
  876. sizeof(*elems->mesh_chansw_params_ie)) {
  877. elem_parse_failed = true;
  878. break;
  879. }
  880. elems->mesh_chansw_params_ie = (void *)pos;
  881. break;
  882. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  883. if (!action ||
  884. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  885. elem_parse_failed = true;
  886. break;
  887. }
  888. elems->wide_bw_chansw_ie = (void *)pos;
  889. break;
  890. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  891. if (action) {
  892. elem_parse_failed = true;
  893. break;
  894. }
  895. /*
  896. * This is a bit tricky, but as we only care about
  897. * the wide bandwidth channel switch element, so
  898. * just parse it out manually.
  899. */
  900. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  901. pos, elen);
  902. if (ie) {
  903. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  904. elems->wide_bw_chansw_ie =
  905. (void *)(ie + 2);
  906. else
  907. elem_parse_failed = true;
  908. }
  909. break;
  910. case WLAN_EID_COUNTRY:
  911. elems->country_elem = pos;
  912. elems->country_elem_len = elen;
  913. break;
  914. case WLAN_EID_PWR_CONSTRAINT:
  915. if (elen != 1) {
  916. elem_parse_failed = true;
  917. break;
  918. }
  919. elems->pwr_constr_elem = pos;
  920. break;
  921. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  922. /* Lots of different options exist, but we only care
  923. * about the Dynamic Transmit Power Control element.
  924. * First check for the Cisco OUI, then for the DTPC
  925. * tag (0x00).
  926. */
  927. if (elen < 4) {
  928. elem_parse_failed = true;
  929. break;
  930. }
  931. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  932. pos[2] != 0x96 || pos[3] != 0x00)
  933. break;
  934. if (elen != 6) {
  935. elem_parse_failed = true;
  936. break;
  937. }
  938. if (calc_crc)
  939. crc = crc32_be(crc, pos - 2, elen + 2);
  940. elems->cisco_dtpc_elem = pos;
  941. break;
  942. case WLAN_EID_TIMEOUT_INTERVAL:
  943. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  944. elems->timeout_int = (void *)pos;
  945. else
  946. elem_parse_failed = true;
  947. break;
  948. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  949. if (elen >= sizeof(*elems->max_idle_period_ie))
  950. elems->max_idle_period_ie = (void *)pos;
  951. break;
  952. case WLAN_EID_EXTENSION:
  953. if (pos[0] == WLAN_EID_EXT_HE_MU_EDCA &&
  954. elen >= (sizeof(*elems->mu_edca_param_set) + 1)) {
  955. elems->mu_edca_param_set = (void *)&pos[1];
  956. } else if (pos[0] == WLAN_EID_EXT_HE_CAPABILITY) {
  957. elems->he_cap = (void *)&pos[1];
  958. elems->he_cap_len = elen - 1;
  959. } else if (pos[0] == WLAN_EID_EXT_HE_OPERATION &&
  960. elen >= sizeof(*elems->he_operation) &&
  961. elen >= ieee80211_he_oper_size(&pos[1])) {
  962. elems->he_operation = (void *)&pos[1];
  963. } else if (pos[0] == WLAN_EID_EXT_UORA && elen >= 1) {
  964. elems->uora_element = (void *)&pos[1];
  965. }
  966. break;
  967. default:
  968. break;
  969. }
  970. if (elem_parse_failed)
  971. elems->parse_error = true;
  972. else
  973. __set_bit(id, seen_elems);
  974. left -= elen;
  975. pos += elen;
  976. }
  977. if (left != 0)
  978. elems->parse_error = true;
  979. return crc;
  980. }
  981. void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
  982. struct ieee80211_tx_queue_params
  983. *qparam, int ac)
  984. {
  985. struct ieee80211_chanctx_conf *chanctx_conf;
  986. const struct ieee80211_reg_rule *rrule;
  987. const struct ieee80211_wmm_ac *wmm_ac;
  988. u16 center_freq = 0;
  989. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  990. sdata->vif.type != NL80211_IFTYPE_STATION)
  991. return;
  992. rcu_read_lock();
  993. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  994. if (chanctx_conf)
  995. center_freq = chanctx_conf->def.chan->center_freq;
  996. if (!center_freq) {
  997. rcu_read_unlock();
  998. return;
  999. }
  1000. rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
  1001. if (IS_ERR_OR_NULL(rrule) || !rrule->has_wmm) {
  1002. rcu_read_unlock();
  1003. return;
  1004. }
  1005. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1006. wmm_ac = &rrule->wmm_rule.ap[ac];
  1007. else
  1008. wmm_ac = &rrule->wmm_rule.client[ac];
  1009. qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
  1010. qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
  1011. qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
  1012. qparam->txop = min_t(u16, qparam->txop, wmm_ac->cot / 32);
  1013. rcu_read_unlock();
  1014. }
  1015. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  1016. bool bss_notify, bool enable_qos)
  1017. {
  1018. struct ieee80211_local *local = sdata->local;
  1019. struct ieee80211_tx_queue_params qparam;
  1020. struct ieee80211_chanctx_conf *chanctx_conf;
  1021. int ac;
  1022. bool use_11b;
  1023. bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
  1024. int aCWmin, aCWmax;
  1025. if (!local->ops->conf_tx)
  1026. return;
  1027. if (local->hw.queues < IEEE80211_NUM_ACS)
  1028. return;
  1029. memset(&qparam, 0, sizeof(qparam));
  1030. rcu_read_lock();
  1031. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1032. use_11b = (chanctx_conf &&
  1033. chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
  1034. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  1035. rcu_read_unlock();
  1036. is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
  1037. /* Set defaults according to 802.11-2007 Table 7-37 */
  1038. aCWmax = 1023;
  1039. if (use_11b)
  1040. aCWmin = 31;
  1041. else
  1042. aCWmin = 15;
  1043. /* Confiure old 802.11b/g medium access rules. */
  1044. qparam.cw_max = aCWmax;
  1045. qparam.cw_min = aCWmin;
  1046. qparam.txop = 0;
  1047. qparam.aifs = 2;
  1048. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1049. /* Update if QoS is enabled. */
  1050. if (enable_qos) {
  1051. switch (ac) {
  1052. case IEEE80211_AC_BK:
  1053. qparam.cw_max = aCWmax;
  1054. qparam.cw_min = aCWmin;
  1055. qparam.txop = 0;
  1056. if (is_ocb)
  1057. qparam.aifs = 9;
  1058. else
  1059. qparam.aifs = 7;
  1060. break;
  1061. /* never happens but let's not leave undefined */
  1062. default:
  1063. case IEEE80211_AC_BE:
  1064. qparam.cw_max = aCWmax;
  1065. qparam.cw_min = aCWmin;
  1066. qparam.txop = 0;
  1067. if (is_ocb)
  1068. qparam.aifs = 6;
  1069. else
  1070. qparam.aifs = 3;
  1071. break;
  1072. case IEEE80211_AC_VI:
  1073. qparam.cw_max = aCWmin;
  1074. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  1075. if (is_ocb)
  1076. qparam.txop = 0;
  1077. else if (use_11b)
  1078. qparam.txop = 6016/32;
  1079. else
  1080. qparam.txop = 3008/32;
  1081. if (is_ocb)
  1082. qparam.aifs = 3;
  1083. else
  1084. qparam.aifs = 2;
  1085. break;
  1086. case IEEE80211_AC_VO:
  1087. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  1088. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  1089. if (is_ocb)
  1090. qparam.txop = 0;
  1091. else if (use_11b)
  1092. qparam.txop = 3264/32;
  1093. else
  1094. qparam.txop = 1504/32;
  1095. qparam.aifs = 2;
  1096. break;
  1097. }
  1098. }
  1099. ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
  1100. qparam.uapsd = false;
  1101. sdata->tx_conf[ac] = qparam;
  1102. drv_conf_tx(local, sdata, ac, &qparam);
  1103. }
  1104. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1105. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  1106. sdata->vif.type != NL80211_IFTYPE_NAN) {
  1107. sdata->vif.bss_conf.qos = enable_qos;
  1108. if (bss_notify)
  1109. ieee80211_bss_info_change_notify(sdata,
  1110. BSS_CHANGED_QOS);
  1111. }
  1112. }
  1113. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  1114. u16 transaction, u16 auth_alg, u16 status,
  1115. const u8 *extra, size_t extra_len, const u8 *da,
  1116. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  1117. u32 tx_flags)
  1118. {
  1119. struct ieee80211_local *local = sdata->local;
  1120. struct sk_buff *skb;
  1121. struct ieee80211_mgmt *mgmt;
  1122. int err;
  1123. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  1124. skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
  1125. 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
  1126. if (!skb)
  1127. return;
  1128. skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
  1129. mgmt = skb_put_zero(skb, 24 + 6);
  1130. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1131. IEEE80211_STYPE_AUTH);
  1132. memcpy(mgmt->da, da, ETH_ALEN);
  1133. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1134. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1135. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  1136. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  1137. mgmt->u.auth.status_code = cpu_to_le16(status);
  1138. if (extra)
  1139. skb_put_data(skb, extra, extra_len);
  1140. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  1141. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1142. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  1143. WARN_ON(err);
  1144. }
  1145. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1146. tx_flags;
  1147. ieee80211_tx_skb(sdata, skb);
  1148. }
  1149. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  1150. const u8 *bssid, u16 stype, u16 reason,
  1151. bool send_frame, u8 *frame_buf)
  1152. {
  1153. struct ieee80211_local *local = sdata->local;
  1154. struct sk_buff *skb;
  1155. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  1156. /* build frame */
  1157. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  1158. mgmt->duration = 0; /* initialize only */
  1159. mgmt->seq_ctrl = 0; /* initialize only */
  1160. memcpy(mgmt->da, bssid, ETH_ALEN);
  1161. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1162. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1163. /* u.deauth.reason_code == u.disassoc.reason_code */
  1164. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  1165. if (send_frame) {
  1166. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1167. IEEE80211_DEAUTH_FRAME_LEN);
  1168. if (!skb)
  1169. return;
  1170. skb_reserve(skb, local->hw.extra_tx_headroom);
  1171. /* copy in frame */
  1172. skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1173. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1174. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1175. IEEE80211_SKB_CB(skb)->flags |=
  1176. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1177. ieee80211_tx_skb(sdata, skb);
  1178. }
  1179. }
  1180. static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
  1181. u8 *buffer, size_t buffer_len,
  1182. const u8 *ie, size_t ie_len,
  1183. enum nl80211_band band,
  1184. u32 rate_mask,
  1185. struct cfg80211_chan_def *chandef,
  1186. size_t *offset, u32 flags)
  1187. {
  1188. struct ieee80211_supported_band *sband;
  1189. const struct ieee80211_sta_he_cap *he_cap;
  1190. u8 *pos = buffer, *end = buffer + buffer_len;
  1191. size_t noffset;
  1192. int supp_rates_len, i;
  1193. u8 rates[32];
  1194. int num_rates;
  1195. int ext_rates_len;
  1196. int shift;
  1197. u32 rate_flags;
  1198. bool have_80mhz = false;
  1199. *offset = 0;
  1200. sband = local->hw.wiphy->bands[band];
  1201. if (WARN_ON_ONCE(!sband))
  1202. return 0;
  1203. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1204. shift = ieee80211_chandef_get_shift(chandef);
  1205. num_rates = 0;
  1206. for (i = 0; i < sband->n_bitrates; i++) {
  1207. if ((BIT(i) & rate_mask) == 0)
  1208. continue; /* skip rate */
  1209. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1210. continue;
  1211. rates[num_rates++] =
  1212. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1213. (1 << shift) * 5);
  1214. }
  1215. supp_rates_len = min_t(int, num_rates, 8);
  1216. if (end - pos < 2 + supp_rates_len)
  1217. goto out_err;
  1218. *pos++ = WLAN_EID_SUPP_RATES;
  1219. *pos++ = supp_rates_len;
  1220. memcpy(pos, rates, supp_rates_len);
  1221. pos += supp_rates_len;
  1222. /* insert "request information" if in custom IEs */
  1223. if (ie && ie_len) {
  1224. static const u8 before_extrates[] = {
  1225. WLAN_EID_SSID,
  1226. WLAN_EID_SUPP_RATES,
  1227. WLAN_EID_REQUEST,
  1228. };
  1229. noffset = ieee80211_ie_split(ie, ie_len,
  1230. before_extrates,
  1231. ARRAY_SIZE(before_extrates),
  1232. *offset);
  1233. if (end - pos < noffset - *offset)
  1234. goto out_err;
  1235. memcpy(pos, ie + *offset, noffset - *offset);
  1236. pos += noffset - *offset;
  1237. *offset = noffset;
  1238. }
  1239. ext_rates_len = num_rates - supp_rates_len;
  1240. if (ext_rates_len > 0) {
  1241. if (end - pos < 2 + ext_rates_len)
  1242. goto out_err;
  1243. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1244. *pos++ = ext_rates_len;
  1245. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1246. pos += ext_rates_len;
  1247. }
  1248. if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
  1249. if (end - pos < 3)
  1250. goto out_err;
  1251. *pos++ = WLAN_EID_DS_PARAMS;
  1252. *pos++ = 1;
  1253. *pos++ = ieee80211_frequency_to_channel(
  1254. chandef->chan->center_freq);
  1255. }
  1256. if (flags & IEEE80211_PROBE_FLAG_MIN_CONTENT)
  1257. goto done;
  1258. /* insert custom IEs that go before HT */
  1259. if (ie && ie_len) {
  1260. static const u8 before_ht[] = {
  1261. /*
  1262. * no need to list the ones split off already
  1263. * (or generated here)
  1264. */
  1265. WLAN_EID_DS_PARAMS,
  1266. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1267. };
  1268. noffset = ieee80211_ie_split(ie, ie_len,
  1269. before_ht, ARRAY_SIZE(before_ht),
  1270. *offset);
  1271. if (end - pos < noffset - *offset)
  1272. goto out_err;
  1273. memcpy(pos, ie + *offset, noffset - *offset);
  1274. pos += noffset - *offset;
  1275. *offset = noffset;
  1276. }
  1277. if (sband->ht_cap.ht_supported) {
  1278. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1279. goto out_err;
  1280. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1281. sband->ht_cap.cap);
  1282. }
  1283. /* insert custom IEs that go before VHT */
  1284. if (ie && ie_len) {
  1285. static const u8 before_vht[] = {
  1286. /*
  1287. * no need to list the ones split off already
  1288. * (or generated here)
  1289. */
  1290. WLAN_EID_BSS_COEX_2040,
  1291. WLAN_EID_EXT_CAPABILITY,
  1292. WLAN_EID_SSID_LIST,
  1293. WLAN_EID_CHANNEL_USAGE,
  1294. WLAN_EID_INTERWORKING,
  1295. WLAN_EID_MESH_ID,
  1296. /* 60 GHz (Multi-band, DMG, MMS) can't happen */
  1297. };
  1298. noffset = ieee80211_ie_split(ie, ie_len,
  1299. before_vht, ARRAY_SIZE(before_vht),
  1300. *offset);
  1301. if (end - pos < noffset - *offset)
  1302. goto out_err;
  1303. memcpy(pos, ie + *offset, noffset - *offset);
  1304. pos += noffset - *offset;
  1305. *offset = noffset;
  1306. }
  1307. /* Check if any channel in this sband supports at least 80 MHz */
  1308. for (i = 0; i < sband->n_channels; i++) {
  1309. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  1310. IEEE80211_CHAN_NO_80MHZ))
  1311. continue;
  1312. have_80mhz = true;
  1313. break;
  1314. }
  1315. if (sband->vht_cap.vht_supported && have_80mhz) {
  1316. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1317. goto out_err;
  1318. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1319. sband->vht_cap.cap);
  1320. }
  1321. /* insert custom IEs that go before HE */
  1322. if (ie && ie_len) {
  1323. static const u8 before_he[] = {
  1324. /*
  1325. * no need to list the ones split off before VHT
  1326. * or generated here
  1327. */
  1328. WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_REQ_PARAMS,
  1329. WLAN_EID_AP_CSN,
  1330. /* TODO: add 11ah/11aj/11ak elements */
  1331. };
  1332. noffset = ieee80211_ie_split(ie, ie_len,
  1333. before_he, ARRAY_SIZE(before_he),
  1334. *offset);
  1335. if (end - pos < noffset - *offset)
  1336. goto out_err;
  1337. memcpy(pos, ie + *offset, noffset - *offset);
  1338. pos += noffset - *offset;
  1339. *offset = noffset;
  1340. }
  1341. he_cap = ieee80211_get_he_sta_cap(sband);
  1342. if (he_cap) {
  1343. pos = ieee80211_ie_build_he_cap(pos, he_cap, end);
  1344. if (!pos)
  1345. goto out_err;
  1346. }
  1347. /*
  1348. * If adding more here, adjust code in main.c
  1349. * that calculates local->scan_ies_len.
  1350. */
  1351. return pos - buffer;
  1352. out_err:
  1353. WARN_ONCE(1, "not enough space for preq IEs\n");
  1354. done:
  1355. return pos - buffer;
  1356. }
  1357. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1358. size_t buffer_len,
  1359. struct ieee80211_scan_ies *ie_desc,
  1360. const u8 *ie, size_t ie_len,
  1361. u8 bands_used, u32 *rate_masks,
  1362. struct cfg80211_chan_def *chandef,
  1363. u32 flags)
  1364. {
  1365. size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
  1366. int i;
  1367. memset(ie_desc, 0, sizeof(*ie_desc));
  1368. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1369. if (bands_used & BIT(i)) {
  1370. pos += ieee80211_build_preq_ies_band(local,
  1371. buffer + pos,
  1372. buffer_len - pos,
  1373. ie, ie_len, i,
  1374. rate_masks[i],
  1375. chandef,
  1376. &custom_ie_offset,
  1377. flags);
  1378. ie_desc->ies[i] = buffer + old_pos;
  1379. ie_desc->len[i] = pos - old_pos;
  1380. old_pos = pos;
  1381. }
  1382. }
  1383. /* add any remaining custom IEs */
  1384. if (ie && ie_len) {
  1385. if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
  1386. "not enough space for preq custom IEs\n"))
  1387. return pos;
  1388. memcpy(buffer + pos, ie + custom_ie_offset,
  1389. ie_len - custom_ie_offset);
  1390. ie_desc->common_ies = buffer + pos;
  1391. ie_desc->common_ie_len = ie_len - custom_ie_offset;
  1392. pos += ie_len - custom_ie_offset;
  1393. }
  1394. return pos;
  1395. };
  1396. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1397. const u8 *src, const u8 *dst,
  1398. u32 ratemask,
  1399. struct ieee80211_channel *chan,
  1400. const u8 *ssid, size_t ssid_len,
  1401. const u8 *ie, size_t ie_len,
  1402. u32 flags)
  1403. {
  1404. struct ieee80211_local *local = sdata->local;
  1405. struct cfg80211_chan_def chandef;
  1406. struct sk_buff *skb;
  1407. struct ieee80211_mgmt *mgmt;
  1408. int ies_len;
  1409. u32 rate_masks[NUM_NL80211_BANDS] = {};
  1410. struct ieee80211_scan_ies dummy_ie_desc;
  1411. /*
  1412. * Do not send DS Channel parameter for directed probe requests
  1413. * in order to maximize the chance that we get a response. Some
  1414. * badly-behaved APs don't respond when this parameter is included.
  1415. */
  1416. chandef.width = sdata->vif.bss_conf.chandef.width;
  1417. if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
  1418. chandef.chan = NULL;
  1419. else
  1420. chandef.chan = chan;
  1421. skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
  1422. 100 + ie_len);
  1423. if (!skb)
  1424. return NULL;
  1425. rate_masks[chan->band] = ratemask;
  1426. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1427. skb_tailroom(skb), &dummy_ie_desc,
  1428. ie, ie_len, BIT(chan->band),
  1429. rate_masks, &chandef, flags);
  1430. skb_put(skb, ies_len);
  1431. if (dst) {
  1432. mgmt = (struct ieee80211_mgmt *) skb->data;
  1433. memcpy(mgmt->da, dst, ETH_ALEN);
  1434. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1435. }
  1436. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1437. return skb;
  1438. }
  1439. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1440. struct ieee802_11_elems *elems,
  1441. enum nl80211_band band, u32 *basic_rates)
  1442. {
  1443. struct ieee80211_supported_band *sband;
  1444. size_t num_rates;
  1445. u32 supp_rates, rate_flags;
  1446. int i, j, shift;
  1447. sband = sdata->local->hw.wiphy->bands[band];
  1448. if (WARN_ON(!sband))
  1449. return 1;
  1450. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1451. shift = ieee80211_vif_get_shift(&sdata->vif);
  1452. num_rates = sband->n_bitrates;
  1453. supp_rates = 0;
  1454. for (i = 0; i < elems->supp_rates_len +
  1455. elems->ext_supp_rates_len; i++) {
  1456. u8 rate = 0;
  1457. int own_rate;
  1458. bool is_basic;
  1459. if (i < elems->supp_rates_len)
  1460. rate = elems->supp_rates[i];
  1461. else if (elems->ext_supp_rates)
  1462. rate = elems->ext_supp_rates
  1463. [i - elems->supp_rates_len];
  1464. own_rate = 5 * (rate & 0x7f);
  1465. is_basic = !!(rate & 0x80);
  1466. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1467. continue;
  1468. for (j = 0; j < num_rates; j++) {
  1469. int brate;
  1470. if ((rate_flags & sband->bitrates[j].flags)
  1471. != rate_flags)
  1472. continue;
  1473. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1474. 1 << shift);
  1475. if (brate == own_rate) {
  1476. supp_rates |= BIT(j);
  1477. if (basic_rates && is_basic)
  1478. *basic_rates |= BIT(j);
  1479. }
  1480. }
  1481. }
  1482. return supp_rates;
  1483. }
  1484. void ieee80211_stop_device(struct ieee80211_local *local)
  1485. {
  1486. ieee80211_led_radio(local, false);
  1487. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1488. cancel_work_sync(&local->reconfig_filter);
  1489. flush_workqueue(local->workqueue);
  1490. drv_stop(local);
  1491. }
  1492. static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
  1493. bool aborted)
  1494. {
  1495. /* It's possible that we don't handle the scan completion in
  1496. * time during suspend, so if it's still marked as completed
  1497. * here, queue the work and flush it to clean things up.
  1498. * Instead of calling the worker function directly here, we
  1499. * really queue it to avoid potential races with other flows
  1500. * scheduling the same work.
  1501. */
  1502. if (test_bit(SCAN_COMPLETED, &local->scanning)) {
  1503. /* If coming from reconfiguration failure, abort the scan so
  1504. * we don't attempt to continue a partial HW scan - which is
  1505. * possible otherwise if (e.g.) the 2.4 GHz portion was the
  1506. * completed scan, and a 5 GHz portion is still pending.
  1507. */
  1508. if (aborted)
  1509. set_bit(SCAN_ABORTED, &local->scanning);
  1510. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  1511. flush_delayed_work(&local->scan_work);
  1512. }
  1513. }
  1514. static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
  1515. {
  1516. struct ieee80211_sub_if_data *sdata;
  1517. struct ieee80211_chanctx *ctx;
  1518. /*
  1519. * We get here if during resume the device can't be restarted properly.
  1520. * We might also get here if this happens during HW reset, which is a
  1521. * slightly different situation and we need to drop all connections in
  1522. * the latter case.
  1523. *
  1524. * Ask cfg80211 to turn off all interfaces, this will result in more
  1525. * warnings but at least we'll then get into a clean stopped state.
  1526. */
  1527. local->resuming = false;
  1528. local->suspended = false;
  1529. local->in_reconfig = false;
  1530. ieee80211_flush_completed_scan(local, true);
  1531. /* scheduled scan clearly can't be running any more, but tell
  1532. * cfg80211 and clear local state
  1533. */
  1534. ieee80211_sched_scan_end(local);
  1535. list_for_each_entry(sdata, &local->interfaces, list)
  1536. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  1537. /* Mark channel contexts as not being in the driver any more to avoid
  1538. * removing them from the driver during the shutdown process...
  1539. */
  1540. mutex_lock(&local->chanctx_mtx);
  1541. list_for_each_entry(ctx, &local->chanctx_list, list)
  1542. ctx->driver_present = false;
  1543. mutex_unlock(&local->chanctx_mtx);
  1544. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1545. }
  1546. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1547. struct ieee80211_sub_if_data *sdata)
  1548. {
  1549. struct ieee80211_chanctx_conf *conf;
  1550. struct ieee80211_chanctx *ctx;
  1551. if (!local->use_chanctx)
  1552. return;
  1553. mutex_lock(&local->chanctx_mtx);
  1554. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1555. lockdep_is_held(&local->chanctx_mtx));
  1556. if (conf) {
  1557. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1558. drv_assign_vif_chanctx(local, sdata, ctx);
  1559. }
  1560. mutex_unlock(&local->chanctx_mtx);
  1561. }
  1562. static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
  1563. {
  1564. struct ieee80211_local *local = sdata->local;
  1565. struct sta_info *sta;
  1566. /* add STAs back */
  1567. mutex_lock(&local->sta_mtx);
  1568. list_for_each_entry(sta, &local->sta_list, list) {
  1569. enum ieee80211_sta_state state;
  1570. if (!sta->uploaded || sta->sdata != sdata)
  1571. continue;
  1572. for (state = IEEE80211_STA_NOTEXIST;
  1573. state < sta->sta_state; state++)
  1574. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1575. state + 1));
  1576. }
  1577. mutex_unlock(&local->sta_mtx);
  1578. }
  1579. static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
  1580. {
  1581. struct cfg80211_nan_func *func, **funcs;
  1582. int res, id, i = 0;
  1583. res = drv_start_nan(sdata->local, sdata,
  1584. &sdata->u.nan.conf);
  1585. if (WARN_ON(res))
  1586. return res;
  1587. funcs = kcalloc(sdata->local->hw.max_nan_de_entries + 1,
  1588. sizeof(*funcs),
  1589. GFP_KERNEL);
  1590. if (!funcs)
  1591. return -ENOMEM;
  1592. /* Add all the functions:
  1593. * This is a little bit ugly. We need to call a potentially sleeping
  1594. * callback for each NAN function, so we can't hold the spinlock.
  1595. */
  1596. spin_lock_bh(&sdata->u.nan.func_lock);
  1597. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
  1598. funcs[i++] = func;
  1599. spin_unlock_bh(&sdata->u.nan.func_lock);
  1600. for (i = 0; funcs[i]; i++) {
  1601. res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
  1602. if (WARN_ON(res))
  1603. ieee80211_nan_func_terminated(&sdata->vif,
  1604. funcs[i]->instance_id,
  1605. NL80211_NAN_FUNC_TERM_REASON_ERROR,
  1606. GFP_KERNEL);
  1607. }
  1608. kfree(funcs);
  1609. return 0;
  1610. }
  1611. int ieee80211_reconfig(struct ieee80211_local *local)
  1612. {
  1613. struct ieee80211_hw *hw = &local->hw;
  1614. struct ieee80211_sub_if_data *sdata;
  1615. struct ieee80211_chanctx *ctx;
  1616. struct sta_info *sta;
  1617. int res, i;
  1618. bool reconfig_due_to_wowlan = false;
  1619. struct ieee80211_sub_if_data *sched_scan_sdata;
  1620. struct cfg80211_sched_scan_request *sched_scan_req;
  1621. bool sched_scan_stopped = false;
  1622. bool suspended = local->suspended;
  1623. /* nothing to do if HW shouldn't run */
  1624. if (!local->open_count)
  1625. goto wake_up;
  1626. #ifdef CONFIG_PM
  1627. if (suspended)
  1628. local->resuming = true;
  1629. if (local->wowlan) {
  1630. /*
  1631. * In the wowlan case, both mac80211 and the device
  1632. * are functional when the resume op is called, so
  1633. * clear local->suspended so the device could operate
  1634. * normally (e.g. pass rx frames).
  1635. */
  1636. local->suspended = false;
  1637. res = drv_resume(local);
  1638. local->wowlan = false;
  1639. if (res < 0) {
  1640. local->resuming = false;
  1641. return res;
  1642. }
  1643. if (res == 0)
  1644. goto wake_up;
  1645. WARN_ON(res > 1);
  1646. /*
  1647. * res is 1, which means the driver requested
  1648. * to go through a regular reset on wakeup.
  1649. * restore local->suspended in this case.
  1650. */
  1651. reconfig_due_to_wowlan = true;
  1652. local->suspended = true;
  1653. }
  1654. #endif
  1655. /*
  1656. * In case of hw_restart during suspend (without wowlan),
  1657. * cancel restart work, as we are reconfiguring the device
  1658. * anyway.
  1659. * Note that restart_work is scheduled on a frozen workqueue,
  1660. * so we can't deadlock in this case.
  1661. */
  1662. if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
  1663. cancel_work_sync(&local->restart_work);
  1664. local->started = false;
  1665. /*
  1666. * Upon resume hardware can sometimes be goofy due to
  1667. * various platform / driver / bus issues, so restarting
  1668. * the device may at times not work immediately. Propagate
  1669. * the error.
  1670. */
  1671. res = drv_start(local);
  1672. if (res) {
  1673. if (suspended)
  1674. WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
  1675. else
  1676. WARN(1, "Hardware became unavailable during restart.\n");
  1677. ieee80211_handle_reconfig_failure(local);
  1678. return res;
  1679. }
  1680. /* setup fragmentation threshold */
  1681. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1682. /* setup RTS threshold */
  1683. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1684. /* reset coverage class */
  1685. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1686. ieee80211_led_radio(local, true);
  1687. ieee80211_mod_tpt_led_trig(local,
  1688. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1689. /* add interfaces */
  1690. sdata = rtnl_dereference(local->monitor_sdata);
  1691. if (sdata) {
  1692. /* in HW restart it exists already */
  1693. WARN_ON(local->resuming);
  1694. res = drv_add_interface(local, sdata);
  1695. if (WARN_ON(res)) {
  1696. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  1697. synchronize_net();
  1698. kfree(sdata);
  1699. }
  1700. }
  1701. list_for_each_entry(sdata, &local->interfaces, list) {
  1702. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1703. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1704. ieee80211_sdata_running(sdata)) {
  1705. res = drv_add_interface(local, sdata);
  1706. if (WARN_ON(res))
  1707. break;
  1708. }
  1709. }
  1710. /* If adding any of the interfaces failed above, roll back and
  1711. * report failure.
  1712. */
  1713. if (res) {
  1714. list_for_each_entry_continue_reverse(sdata, &local->interfaces,
  1715. list)
  1716. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1717. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1718. ieee80211_sdata_running(sdata))
  1719. drv_remove_interface(local, sdata);
  1720. ieee80211_handle_reconfig_failure(local);
  1721. return res;
  1722. }
  1723. /* add channel contexts */
  1724. if (local->use_chanctx) {
  1725. mutex_lock(&local->chanctx_mtx);
  1726. list_for_each_entry(ctx, &local->chanctx_list, list)
  1727. if (ctx->replace_state !=
  1728. IEEE80211_CHANCTX_REPLACES_OTHER)
  1729. WARN_ON(drv_add_chanctx(local, ctx));
  1730. mutex_unlock(&local->chanctx_mtx);
  1731. sdata = rtnl_dereference(local->monitor_sdata);
  1732. if (sdata && ieee80211_sdata_running(sdata))
  1733. ieee80211_assign_chanctx(local, sdata);
  1734. }
  1735. /* reconfigure hardware */
  1736. ieee80211_hw_config(local, ~0);
  1737. ieee80211_configure_filter(local);
  1738. /* Finally also reconfigure all the BSS information */
  1739. list_for_each_entry(sdata, &local->interfaces, list) {
  1740. u32 changed;
  1741. if (!ieee80211_sdata_running(sdata))
  1742. continue;
  1743. ieee80211_assign_chanctx(local, sdata);
  1744. switch (sdata->vif.type) {
  1745. case NL80211_IFTYPE_AP_VLAN:
  1746. case NL80211_IFTYPE_MONITOR:
  1747. break;
  1748. case NL80211_IFTYPE_ADHOC:
  1749. if (sdata->vif.bss_conf.ibss_joined)
  1750. WARN_ON(drv_join_ibss(local, sdata));
  1751. /* fall through */
  1752. default:
  1753. ieee80211_reconfig_stations(sdata);
  1754. /* fall through */
  1755. case NL80211_IFTYPE_AP: /* AP stations are handled later */
  1756. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1757. drv_conf_tx(local, sdata, i,
  1758. &sdata->tx_conf[i]);
  1759. break;
  1760. }
  1761. /* common change flags for all interface types */
  1762. changed = BSS_CHANGED_ERP_CTS_PROT |
  1763. BSS_CHANGED_ERP_PREAMBLE |
  1764. BSS_CHANGED_ERP_SLOT |
  1765. BSS_CHANGED_HT |
  1766. BSS_CHANGED_BASIC_RATES |
  1767. BSS_CHANGED_BEACON_INT |
  1768. BSS_CHANGED_BSSID |
  1769. BSS_CHANGED_CQM |
  1770. BSS_CHANGED_QOS |
  1771. BSS_CHANGED_IDLE |
  1772. BSS_CHANGED_TXPOWER |
  1773. BSS_CHANGED_MCAST_RATE;
  1774. if (sdata->vif.mu_mimo_owner)
  1775. changed |= BSS_CHANGED_MU_GROUPS;
  1776. switch (sdata->vif.type) {
  1777. case NL80211_IFTYPE_STATION:
  1778. changed |= BSS_CHANGED_ASSOC |
  1779. BSS_CHANGED_ARP_FILTER |
  1780. BSS_CHANGED_PS;
  1781. /* Re-send beacon info report to the driver */
  1782. if (sdata->u.mgd.have_beacon)
  1783. changed |= BSS_CHANGED_BEACON_INFO;
  1784. if (sdata->vif.bss_conf.max_idle_period ||
  1785. sdata->vif.bss_conf.protected_keep_alive)
  1786. changed |= BSS_CHANGED_KEEP_ALIVE;
  1787. sdata_lock(sdata);
  1788. ieee80211_bss_info_change_notify(sdata, changed);
  1789. sdata_unlock(sdata);
  1790. break;
  1791. case NL80211_IFTYPE_OCB:
  1792. changed |= BSS_CHANGED_OCB;
  1793. ieee80211_bss_info_change_notify(sdata, changed);
  1794. break;
  1795. case NL80211_IFTYPE_ADHOC:
  1796. changed |= BSS_CHANGED_IBSS;
  1797. /* fall through */
  1798. case NL80211_IFTYPE_AP:
  1799. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1800. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1801. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1802. if (rcu_access_pointer(sdata->u.ap.beacon))
  1803. drv_start_ap(local, sdata);
  1804. }
  1805. /* fall through */
  1806. case NL80211_IFTYPE_MESH_POINT:
  1807. if (sdata->vif.bss_conf.enable_beacon) {
  1808. changed |= BSS_CHANGED_BEACON |
  1809. BSS_CHANGED_BEACON_ENABLED;
  1810. ieee80211_bss_info_change_notify(sdata, changed);
  1811. }
  1812. break;
  1813. case NL80211_IFTYPE_NAN:
  1814. res = ieee80211_reconfig_nan(sdata);
  1815. if (res < 0) {
  1816. ieee80211_handle_reconfig_failure(local);
  1817. return res;
  1818. }
  1819. break;
  1820. case NL80211_IFTYPE_WDS:
  1821. case NL80211_IFTYPE_AP_VLAN:
  1822. case NL80211_IFTYPE_MONITOR:
  1823. case NL80211_IFTYPE_P2P_DEVICE:
  1824. /* nothing to do */
  1825. break;
  1826. case NL80211_IFTYPE_UNSPECIFIED:
  1827. case NUM_NL80211_IFTYPES:
  1828. case NL80211_IFTYPE_P2P_CLIENT:
  1829. case NL80211_IFTYPE_P2P_GO:
  1830. WARN_ON(1);
  1831. break;
  1832. }
  1833. }
  1834. ieee80211_recalc_ps(local);
  1835. /*
  1836. * The sta might be in psm against the ap (e.g. because
  1837. * this was the state before a hw restart), so we
  1838. * explicitly send a null packet in order to make sure
  1839. * it'll sync against the ap (and get out of psm).
  1840. */
  1841. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1842. list_for_each_entry(sdata, &local->interfaces, list) {
  1843. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1844. continue;
  1845. if (!sdata->u.mgd.associated)
  1846. continue;
  1847. ieee80211_send_nullfunc(local, sdata, false);
  1848. }
  1849. }
  1850. /* APs are now beaconing, add back stations */
  1851. mutex_lock(&local->sta_mtx);
  1852. list_for_each_entry(sta, &local->sta_list, list) {
  1853. enum ieee80211_sta_state state;
  1854. if (!sta->uploaded)
  1855. continue;
  1856. if (sta->sdata->vif.type != NL80211_IFTYPE_AP &&
  1857. sta->sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1858. continue;
  1859. for (state = IEEE80211_STA_NOTEXIST;
  1860. state < sta->sta_state; state++)
  1861. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1862. state + 1));
  1863. }
  1864. mutex_unlock(&local->sta_mtx);
  1865. /* add back keys */
  1866. list_for_each_entry(sdata, &local->interfaces, list)
  1867. ieee80211_reset_crypto_tx_tailroom(sdata);
  1868. list_for_each_entry(sdata, &local->interfaces, list)
  1869. if (ieee80211_sdata_running(sdata))
  1870. ieee80211_enable_keys(sdata);
  1871. /* Reconfigure sched scan if it was interrupted by FW restart */
  1872. mutex_lock(&local->mtx);
  1873. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1874. lockdep_is_held(&local->mtx));
  1875. sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
  1876. lockdep_is_held(&local->mtx));
  1877. if (sched_scan_sdata && sched_scan_req)
  1878. /*
  1879. * Sched scan stopped, but we don't want to report it. Instead,
  1880. * we're trying to reschedule. However, if more than one scan
  1881. * plan was set, we cannot reschedule since we don't know which
  1882. * scan plan was currently running (and some scan plans may have
  1883. * already finished).
  1884. */
  1885. if (sched_scan_req->n_scan_plans > 1 ||
  1886. __ieee80211_request_sched_scan_start(sched_scan_sdata,
  1887. sched_scan_req)) {
  1888. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1889. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1890. sched_scan_stopped = true;
  1891. }
  1892. mutex_unlock(&local->mtx);
  1893. if (sched_scan_stopped)
  1894. cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
  1895. wake_up:
  1896. if (local->monitors == local->open_count && local->monitors > 0)
  1897. ieee80211_add_virtual_monitor(local);
  1898. /*
  1899. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1900. * sessions can be established after a resume.
  1901. *
  1902. * Also tear down aggregation sessions since reconfiguring
  1903. * them in a hardware restart scenario is not easily done
  1904. * right now, and the hardware will have lost information
  1905. * about the sessions, but we and the AP still think they
  1906. * are active. This is really a workaround though.
  1907. */
  1908. if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
  1909. mutex_lock(&local->sta_mtx);
  1910. list_for_each_entry(sta, &local->sta_list, list) {
  1911. if (!local->resuming)
  1912. ieee80211_sta_tear_down_BA_sessions(
  1913. sta, AGG_STOP_LOCAL_REQUEST);
  1914. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1915. }
  1916. mutex_unlock(&local->sta_mtx);
  1917. }
  1918. if (local->in_reconfig) {
  1919. local->in_reconfig = false;
  1920. barrier();
  1921. /* Restart deferred ROCs */
  1922. mutex_lock(&local->mtx);
  1923. ieee80211_start_next_roc(local);
  1924. mutex_unlock(&local->mtx);
  1925. /* Requeue all works */
  1926. list_for_each_entry(sdata, &local->interfaces, list)
  1927. ieee80211_queue_work(&local->hw, &sdata->work);
  1928. }
  1929. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1930. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  1931. false);
  1932. /*
  1933. * If this is for hw restart things are still running.
  1934. * We may want to change that later, however.
  1935. */
  1936. if (local->open_count && (!suspended || reconfig_due_to_wowlan))
  1937. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
  1938. if (!suspended)
  1939. return 0;
  1940. #ifdef CONFIG_PM
  1941. /* first set suspended false, then resuming */
  1942. local->suspended = false;
  1943. mb();
  1944. local->resuming = false;
  1945. ieee80211_flush_completed_scan(local, false);
  1946. if (local->open_count && !reconfig_due_to_wowlan)
  1947. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
  1948. list_for_each_entry(sdata, &local->interfaces, list) {
  1949. if (!ieee80211_sdata_running(sdata))
  1950. continue;
  1951. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1952. ieee80211_sta_restart(sdata);
  1953. }
  1954. mod_timer(&local->sta_cleanup, jiffies + 1);
  1955. #else
  1956. WARN_ON(1);
  1957. #endif
  1958. return 0;
  1959. }
  1960. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1961. {
  1962. struct ieee80211_sub_if_data *sdata;
  1963. struct ieee80211_local *local;
  1964. struct ieee80211_key *key;
  1965. if (WARN_ON(!vif))
  1966. return;
  1967. sdata = vif_to_sdata(vif);
  1968. local = sdata->local;
  1969. if (WARN_ON(!local->resuming))
  1970. return;
  1971. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1972. return;
  1973. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1974. mutex_lock(&local->key_mtx);
  1975. list_for_each_entry(key, &sdata->key_list, list)
  1976. key->flags |= KEY_FLAG_TAINTED;
  1977. mutex_unlock(&local->key_mtx);
  1978. }
  1979. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1980. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1981. {
  1982. struct ieee80211_local *local = sdata->local;
  1983. struct ieee80211_chanctx_conf *chanctx_conf;
  1984. struct ieee80211_chanctx *chanctx;
  1985. mutex_lock(&local->chanctx_mtx);
  1986. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1987. lockdep_is_held(&local->chanctx_mtx));
  1988. /*
  1989. * This function can be called from a work, thus it may be possible
  1990. * that the chanctx_conf is removed (due to a disconnection, for
  1991. * example).
  1992. * So nothing should be done in such case.
  1993. */
  1994. if (!chanctx_conf)
  1995. goto unlock;
  1996. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1997. ieee80211_recalc_smps_chanctx(local, chanctx);
  1998. unlock:
  1999. mutex_unlock(&local->chanctx_mtx);
  2000. }
  2001. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
  2002. {
  2003. struct ieee80211_local *local = sdata->local;
  2004. struct ieee80211_chanctx_conf *chanctx_conf;
  2005. struct ieee80211_chanctx *chanctx;
  2006. mutex_lock(&local->chanctx_mtx);
  2007. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  2008. lockdep_is_held(&local->chanctx_mtx));
  2009. if (WARN_ON_ONCE(!chanctx_conf))
  2010. goto unlock;
  2011. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  2012. ieee80211_recalc_chanctx_min_def(local, chanctx);
  2013. unlock:
  2014. mutex_unlock(&local->chanctx_mtx);
  2015. }
  2016. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  2017. {
  2018. size_t pos = offset;
  2019. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  2020. pos += 2 + ies[pos + 1];
  2021. return pos;
  2022. }
  2023. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  2024. int rssi_min_thold,
  2025. int rssi_max_thold)
  2026. {
  2027. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  2028. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  2029. return;
  2030. /*
  2031. * Scale up threshold values before storing it, as the RSSI averaging
  2032. * algorithm uses a scaled up value as well. Change this scaling
  2033. * factor if the RSSI averaging algorithm changes.
  2034. */
  2035. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  2036. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  2037. }
  2038. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  2039. int rssi_min_thold,
  2040. int rssi_max_thold)
  2041. {
  2042. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2043. WARN_ON(rssi_min_thold == rssi_max_thold ||
  2044. rssi_min_thold > rssi_max_thold);
  2045. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  2046. rssi_max_thold);
  2047. }
  2048. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  2049. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  2050. {
  2051. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2052. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  2053. }
  2054. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  2055. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2056. u16 cap)
  2057. {
  2058. __le16 tmp;
  2059. *pos++ = WLAN_EID_HT_CAPABILITY;
  2060. *pos++ = sizeof(struct ieee80211_ht_cap);
  2061. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  2062. /* capability flags */
  2063. tmp = cpu_to_le16(cap);
  2064. memcpy(pos, &tmp, sizeof(u16));
  2065. pos += sizeof(u16);
  2066. /* AMPDU parameters */
  2067. *pos++ = ht_cap->ampdu_factor |
  2068. (ht_cap->ampdu_density <<
  2069. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  2070. /* MCS set */
  2071. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  2072. pos += sizeof(ht_cap->mcs);
  2073. /* extended capabilities */
  2074. pos += sizeof(__le16);
  2075. /* BF capabilities */
  2076. pos += sizeof(__le32);
  2077. /* antenna selection */
  2078. pos += sizeof(u8);
  2079. return pos;
  2080. }
  2081. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2082. u32 cap)
  2083. {
  2084. __le32 tmp;
  2085. *pos++ = WLAN_EID_VHT_CAPABILITY;
  2086. *pos++ = sizeof(struct ieee80211_vht_cap);
  2087. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  2088. /* capability flags */
  2089. tmp = cpu_to_le32(cap);
  2090. memcpy(pos, &tmp, sizeof(u32));
  2091. pos += sizeof(u32);
  2092. /* VHT MCS set */
  2093. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  2094. pos += sizeof(vht_cap->vht_mcs);
  2095. return pos;
  2096. }
  2097. u8 *ieee80211_ie_build_he_cap(u8 *pos,
  2098. const struct ieee80211_sta_he_cap *he_cap,
  2099. u8 *end)
  2100. {
  2101. u8 n;
  2102. u8 ie_len;
  2103. u8 *orig_pos = pos;
  2104. /* Make sure we have place for the IE */
  2105. /*
  2106. * TODO: the 1 added is because this temporarily is under the EXTENSION
  2107. * IE. Get rid of it when it moves.
  2108. */
  2109. if (!he_cap)
  2110. return orig_pos;
  2111. n = ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem);
  2112. ie_len = 2 + 1 +
  2113. sizeof(he_cap->he_cap_elem) + n +
  2114. ieee80211_he_ppe_size(he_cap->ppe_thres[0],
  2115. he_cap->he_cap_elem.phy_cap_info);
  2116. if ((end - pos) < ie_len)
  2117. return orig_pos;
  2118. *pos++ = WLAN_EID_EXTENSION;
  2119. pos++; /* We'll set the size later below */
  2120. *pos++ = WLAN_EID_EXT_HE_CAPABILITY;
  2121. /* Fixed data */
  2122. memcpy(pos, &he_cap->he_cap_elem, sizeof(he_cap->he_cap_elem));
  2123. pos += sizeof(he_cap->he_cap_elem);
  2124. memcpy(pos, &he_cap->he_mcs_nss_supp, n);
  2125. pos += n;
  2126. /* Check if PPE Threshold should be present */
  2127. if ((he_cap->he_cap_elem.phy_cap_info[6] &
  2128. IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) == 0)
  2129. goto end;
  2130. /*
  2131. * Calculate how many PPET16/PPET8 pairs are to come. Algorithm:
  2132. * (NSS_M1 + 1) x (num of 1 bits in RU_INDEX_BITMASK)
  2133. */
  2134. n = hweight8(he_cap->ppe_thres[0] &
  2135. IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
  2136. n *= (1 + ((he_cap->ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) >>
  2137. IEEE80211_PPE_THRES_NSS_POS));
  2138. /*
  2139. * Each pair is 6 bits, and we need to add the 7 "header" bits to the
  2140. * total size.
  2141. */
  2142. n = (n * IEEE80211_PPE_THRES_INFO_PPET_SIZE * 2) + 7;
  2143. n = DIV_ROUND_UP(n, 8);
  2144. /* Copy PPE Thresholds */
  2145. memcpy(pos, &he_cap->ppe_thres, n);
  2146. pos += n;
  2147. end:
  2148. orig_pos[1] = (pos - orig_pos) - 2;
  2149. return pos;
  2150. }
  2151. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2152. const struct cfg80211_chan_def *chandef,
  2153. u16 prot_mode, bool rifs_mode)
  2154. {
  2155. struct ieee80211_ht_operation *ht_oper;
  2156. /* Build HT Information */
  2157. *pos++ = WLAN_EID_HT_OPERATION;
  2158. *pos++ = sizeof(struct ieee80211_ht_operation);
  2159. ht_oper = (struct ieee80211_ht_operation *)pos;
  2160. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  2161. chandef->chan->center_freq);
  2162. switch (chandef->width) {
  2163. case NL80211_CHAN_WIDTH_160:
  2164. case NL80211_CHAN_WIDTH_80P80:
  2165. case NL80211_CHAN_WIDTH_80:
  2166. case NL80211_CHAN_WIDTH_40:
  2167. if (chandef->center_freq1 > chandef->chan->center_freq)
  2168. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2169. else
  2170. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2171. break;
  2172. default:
  2173. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2174. break;
  2175. }
  2176. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  2177. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  2178. chandef->width != NL80211_CHAN_WIDTH_20)
  2179. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  2180. if (rifs_mode)
  2181. ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
  2182. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  2183. ht_oper->stbc_param = 0x0000;
  2184. /* It seems that Basic MCS set and Supported MCS set
  2185. are identical for the first 10 bytes */
  2186. memset(&ht_oper->basic_set, 0, 16);
  2187. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  2188. return pos + sizeof(struct ieee80211_ht_operation);
  2189. }
  2190. void ieee80211_ie_build_wide_bw_cs(u8 *pos,
  2191. const struct cfg80211_chan_def *chandef)
  2192. {
  2193. *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
  2194. *pos++ = 3; /* IE length */
  2195. /* New channel width */
  2196. switch (chandef->width) {
  2197. case NL80211_CHAN_WIDTH_80:
  2198. *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2199. break;
  2200. case NL80211_CHAN_WIDTH_160:
  2201. *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
  2202. break;
  2203. case NL80211_CHAN_WIDTH_80P80:
  2204. *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
  2205. break;
  2206. default:
  2207. *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2208. }
  2209. /* new center frequency segment 0 */
  2210. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
  2211. /* new center frequency segment 1 */
  2212. if (chandef->center_freq2)
  2213. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
  2214. else
  2215. *pos++ = 0;
  2216. }
  2217. u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2218. const struct cfg80211_chan_def *chandef)
  2219. {
  2220. struct ieee80211_vht_operation *vht_oper;
  2221. *pos++ = WLAN_EID_VHT_OPERATION;
  2222. *pos++ = sizeof(struct ieee80211_vht_operation);
  2223. vht_oper = (struct ieee80211_vht_operation *)pos;
  2224. vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
  2225. chandef->center_freq1);
  2226. if (chandef->center_freq2)
  2227. vht_oper->center_freq_seg1_idx =
  2228. ieee80211_frequency_to_channel(chandef->center_freq2);
  2229. else
  2230. vht_oper->center_freq_seg1_idx = 0x00;
  2231. switch (chandef->width) {
  2232. case NL80211_CHAN_WIDTH_160:
  2233. /*
  2234. * Convert 160 MHz channel width to new style as interop
  2235. * workaround.
  2236. */
  2237. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2238. vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
  2239. if (chandef->chan->center_freq < chandef->center_freq1)
  2240. vht_oper->center_freq_seg0_idx -= 8;
  2241. else
  2242. vht_oper->center_freq_seg0_idx += 8;
  2243. break;
  2244. case NL80211_CHAN_WIDTH_80P80:
  2245. /*
  2246. * Convert 80+80 MHz channel width to new style as interop
  2247. * workaround.
  2248. */
  2249. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2250. break;
  2251. case NL80211_CHAN_WIDTH_80:
  2252. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2253. break;
  2254. default:
  2255. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2256. break;
  2257. }
  2258. /* don't require special VHT peer rates */
  2259. vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
  2260. return pos + sizeof(struct ieee80211_vht_operation);
  2261. }
  2262. bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
  2263. struct cfg80211_chan_def *chandef)
  2264. {
  2265. enum nl80211_channel_type channel_type;
  2266. if (!ht_oper)
  2267. return false;
  2268. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2269. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  2270. channel_type = NL80211_CHAN_HT20;
  2271. break;
  2272. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2273. channel_type = NL80211_CHAN_HT40PLUS;
  2274. break;
  2275. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2276. channel_type = NL80211_CHAN_HT40MINUS;
  2277. break;
  2278. default:
  2279. channel_type = NL80211_CHAN_NO_HT;
  2280. return false;
  2281. }
  2282. cfg80211_chandef_create(chandef, chandef->chan, channel_type);
  2283. return true;
  2284. }
  2285. bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
  2286. struct cfg80211_chan_def *chandef)
  2287. {
  2288. struct cfg80211_chan_def new = *chandef;
  2289. int cf1, cf2;
  2290. if (!oper)
  2291. return false;
  2292. cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
  2293. chandef->chan->band);
  2294. cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
  2295. chandef->chan->band);
  2296. switch (oper->chan_width) {
  2297. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2298. break;
  2299. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2300. new.width = NL80211_CHAN_WIDTH_80;
  2301. new.center_freq1 = cf1;
  2302. /* If needed, adjust based on the newer interop workaround. */
  2303. if (oper->center_freq_seg1_idx) {
  2304. unsigned int diff;
  2305. diff = abs(oper->center_freq_seg1_idx -
  2306. oper->center_freq_seg0_idx);
  2307. if (diff == 8) {
  2308. new.width = NL80211_CHAN_WIDTH_160;
  2309. new.center_freq1 = cf2;
  2310. } else if (diff > 8) {
  2311. new.width = NL80211_CHAN_WIDTH_80P80;
  2312. new.center_freq2 = cf2;
  2313. }
  2314. }
  2315. break;
  2316. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2317. new.width = NL80211_CHAN_WIDTH_160;
  2318. new.center_freq1 = cf1;
  2319. break;
  2320. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2321. new.width = NL80211_CHAN_WIDTH_80P80;
  2322. new.center_freq1 = cf1;
  2323. new.center_freq2 = cf2;
  2324. break;
  2325. default:
  2326. return false;
  2327. }
  2328. if (!cfg80211_chandef_valid(&new))
  2329. return false;
  2330. *chandef = new;
  2331. return true;
  2332. }
  2333. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  2334. const struct ieee80211_supported_band *sband,
  2335. const u8 *srates, int srates_len, u32 *rates)
  2336. {
  2337. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  2338. int shift = ieee80211_chandef_get_shift(chandef);
  2339. struct ieee80211_rate *br;
  2340. int brate, rate, i, j, count = 0;
  2341. *rates = 0;
  2342. for (i = 0; i < srates_len; i++) {
  2343. rate = srates[i] & 0x7f;
  2344. for (j = 0; j < sband->n_bitrates; j++) {
  2345. br = &sband->bitrates[j];
  2346. if ((rate_flags & br->flags) != rate_flags)
  2347. continue;
  2348. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2349. if (brate == rate) {
  2350. *rates |= BIT(j);
  2351. count++;
  2352. break;
  2353. }
  2354. }
  2355. }
  2356. return count;
  2357. }
  2358. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  2359. struct sk_buff *skb, bool need_basic,
  2360. enum nl80211_band band)
  2361. {
  2362. struct ieee80211_local *local = sdata->local;
  2363. struct ieee80211_supported_band *sband;
  2364. int rate, shift;
  2365. u8 i, rates, *pos;
  2366. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2367. u32 rate_flags;
  2368. shift = ieee80211_vif_get_shift(&sdata->vif);
  2369. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2370. sband = local->hw.wiphy->bands[band];
  2371. rates = 0;
  2372. for (i = 0; i < sband->n_bitrates; i++) {
  2373. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2374. continue;
  2375. rates++;
  2376. }
  2377. if (rates > 8)
  2378. rates = 8;
  2379. if (skb_tailroom(skb) < rates + 2)
  2380. return -ENOMEM;
  2381. pos = skb_put(skb, rates + 2);
  2382. *pos++ = WLAN_EID_SUPP_RATES;
  2383. *pos++ = rates;
  2384. for (i = 0; i < rates; i++) {
  2385. u8 basic = 0;
  2386. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2387. continue;
  2388. if (need_basic && basic_rates & BIT(i))
  2389. basic = 0x80;
  2390. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2391. 5 * (1 << shift));
  2392. *pos++ = basic | (u8) rate;
  2393. }
  2394. return 0;
  2395. }
  2396. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  2397. struct sk_buff *skb, bool need_basic,
  2398. enum nl80211_band band)
  2399. {
  2400. struct ieee80211_local *local = sdata->local;
  2401. struct ieee80211_supported_band *sband;
  2402. int rate, shift;
  2403. u8 i, exrates, *pos;
  2404. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2405. u32 rate_flags;
  2406. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2407. shift = ieee80211_vif_get_shift(&sdata->vif);
  2408. sband = local->hw.wiphy->bands[band];
  2409. exrates = 0;
  2410. for (i = 0; i < sband->n_bitrates; i++) {
  2411. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2412. continue;
  2413. exrates++;
  2414. }
  2415. if (exrates > 8)
  2416. exrates -= 8;
  2417. else
  2418. exrates = 0;
  2419. if (skb_tailroom(skb) < exrates + 2)
  2420. return -ENOMEM;
  2421. if (exrates) {
  2422. pos = skb_put(skb, exrates + 2);
  2423. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  2424. *pos++ = exrates;
  2425. for (i = 8; i < sband->n_bitrates; i++) {
  2426. u8 basic = 0;
  2427. if ((rate_flags & sband->bitrates[i].flags)
  2428. != rate_flags)
  2429. continue;
  2430. if (need_basic && basic_rates & BIT(i))
  2431. basic = 0x80;
  2432. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2433. 5 * (1 << shift));
  2434. *pos++ = basic | (u8) rate;
  2435. }
  2436. }
  2437. return 0;
  2438. }
  2439. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  2440. {
  2441. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2442. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2443. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  2444. /* non-managed type inferfaces */
  2445. return 0;
  2446. }
  2447. return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2448. }
  2449. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  2450. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  2451. {
  2452. if (!mcs)
  2453. return 1;
  2454. /* TODO: consider rx_highest */
  2455. if (mcs->rx_mask[3])
  2456. return 4;
  2457. if (mcs->rx_mask[2])
  2458. return 3;
  2459. if (mcs->rx_mask[1])
  2460. return 2;
  2461. return 1;
  2462. }
  2463. /**
  2464. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  2465. * @local: mac80211 hw info struct
  2466. * @status: RX status
  2467. * @mpdu_len: total MPDU length (including FCS)
  2468. * @mpdu_offset: offset into MPDU to calculate timestamp at
  2469. *
  2470. * This function calculates the RX timestamp at the given MPDU offset, taking
  2471. * into account what the RX timestamp was. An offset of 0 will just normalize
  2472. * the timestamp to TSF at beginning of MPDU reception.
  2473. */
  2474. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  2475. struct ieee80211_rx_status *status,
  2476. unsigned int mpdu_len,
  2477. unsigned int mpdu_offset)
  2478. {
  2479. u64 ts = status->mactime;
  2480. struct rate_info ri;
  2481. u16 rate;
  2482. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  2483. return 0;
  2484. memset(&ri, 0, sizeof(ri));
  2485. ri.bw = status->bw;
  2486. /* Fill cfg80211 rate info */
  2487. switch (status->encoding) {
  2488. case RX_ENC_HT:
  2489. ri.mcs = status->rate_idx;
  2490. ri.flags |= RATE_INFO_FLAGS_MCS;
  2491. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2492. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2493. break;
  2494. case RX_ENC_VHT:
  2495. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  2496. ri.mcs = status->rate_idx;
  2497. ri.nss = status->nss;
  2498. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2499. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2500. break;
  2501. default:
  2502. WARN_ON(1);
  2503. /* fall through */
  2504. case RX_ENC_LEGACY: {
  2505. struct ieee80211_supported_band *sband;
  2506. int shift = 0;
  2507. int bitrate;
  2508. switch (status->bw) {
  2509. case RATE_INFO_BW_10:
  2510. shift = 1;
  2511. break;
  2512. case RATE_INFO_BW_5:
  2513. shift = 2;
  2514. break;
  2515. }
  2516. sband = local->hw.wiphy->bands[status->band];
  2517. bitrate = sband->bitrates[status->rate_idx].bitrate;
  2518. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  2519. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  2520. /* TODO: handle HT/VHT preambles */
  2521. if (status->band == NL80211_BAND_5GHZ) {
  2522. ts += 20 << shift;
  2523. mpdu_offset += 2;
  2524. } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
  2525. ts += 96;
  2526. } else {
  2527. ts += 192;
  2528. }
  2529. }
  2530. break;
  2531. }
  2532. }
  2533. rate = cfg80211_calculate_bitrate(&ri);
  2534. if (WARN_ONCE(!rate,
  2535. "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
  2536. (unsigned long long)status->flag, status->rate_idx,
  2537. status->nss))
  2538. return 0;
  2539. /* rewind from end of MPDU */
  2540. if (status->flag & RX_FLAG_MACTIME_END)
  2541. ts -= mpdu_len * 8 * 10 / rate;
  2542. ts += mpdu_offset * 8 * 10 / rate;
  2543. return ts;
  2544. }
  2545. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  2546. {
  2547. struct ieee80211_sub_if_data *sdata;
  2548. struct cfg80211_chan_def chandef;
  2549. /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
  2550. ASSERT_RTNL();
  2551. mutex_lock(&local->mtx);
  2552. list_for_each_entry(sdata, &local->interfaces, list) {
  2553. /* it might be waiting for the local->mtx, but then
  2554. * by the time it gets it, sdata->wdev.cac_started
  2555. * will no longer be true
  2556. */
  2557. cancel_delayed_work(&sdata->dfs_cac_timer_work);
  2558. if (sdata->wdev.cac_started) {
  2559. chandef = sdata->vif.bss_conf.chandef;
  2560. ieee80211_vif_release_channel(sdata);
  2561. cfg80211_cac_event(sdata->dev,
  2562. &chandef,
  2563. NL80211_RADAR_CAC_ABORTED,
  2564. GFP_KERNEL);
  2565. }
  2566. }
  2567. mutex_unlock(&local->mtx);
  2568. }
  2569. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  2570. {
  2571. struct ieee80211_local *local =
  2572. container_of(work, struct ieee80211_local, radar_detected_work);
  2573. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  2574. struct ieee80211_chanctx *ctx;
  2575. int num_chanctx = 0;
  2576. mutex_lock(&local->chanctx_mtx);
  2577. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2578. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
  2579. continue;
  2580. num_chanctx++;
  2581. chandef = ctx->conf.def;
  2582. }
  2583. mutex_unlock(&local->chanctx_mtx);
  2584. rtnl_lock();
  2585. ieee80211_dfs_cac_cancel(local);
  2586. rtnl_unlock();
  2587. if (num_chanctx > 1)
  2588. /* XXX: multi-channel is not supported yet */
  2589. WARN_ON(1);
  2590. else
  2591. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  2592. }
  2593. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  2594. {
  2595. struct ieee80211_local *local = hw_to_local(hw);
  2596. trace_api_radar_detected(local);
  2597. schedule_work(&local->radar_detected_work);
  2598. }
  2599. EXPORT_SYMBOL(ieee80211_radar_detected);
  2600. u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  2601. {
  2602. u32 ret;
  2603. int tmp;
  2604. switch (c->width) {
  2605. case NL80211_CHAN_WIDTH_20:
  2606. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2607. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2608. break;
  2609. case NL80211_CHAN_WIDTH_40:
  2610. c->width = NL80211_CHAN_WIDTH_20;
  2611. c->center_freq1 = c->chan->center_freq;
  2612. ret = IEEE80211_STA_DISABLE_40MHZ |
  2613. IEEE80211_STA_DISABLE_VHT;
  2614. break;
  2615. case NL80211_CHAN_WIDTH_80:
  2616. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2617. /* n_P40 */
  2618. tmp /= 2;
  2619. /* freq_P40 */
  2620. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2621. c->width = NL80211_CHAN_WIDTH_40;
  2622. ret = IEEE80211_STA_DISABLE_VHT;
  2623. break;
  2624. case NL80211_CHAN_WIDTH_80P80:
  2625. c->center_freq2 = 0;
  2626. c->width = NL80211_CHAN_WIDTH_80;
  2627. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2628. IEEE80211_STA_DISABLE_160MHZ;
  2629. break;
  2630. case NL80211_CHAN_WIDTH_160:
  2631. /* n_P20 */
  2632. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2633. /* n_P80 */
  2634. tmp /= 4;
  2635. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2636. c->width = NL80211_CHAN_WIDTH_80;
  2637. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2638. IEEE80211_STA_DISABLE_160MHZ;
  2639. break;
  2640. default:
  2641. case NL80211_CHAN_WIDTH_20_NOHT:
  2642. WARN_ON_ONCE(1);
  2643. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2644. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2645. break;
  2646. case NL80211_CHAN_WIDTH_5:
  2647. case NL80211_CHAN_WIDTH_10:
  2648. WARN_ON_ONCE(1);
  2649. /* keep c->width */
  2650. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2651. break;
  2652. }
  2653. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2654. return ret;
  2655. }
  2656. /*
  2657. * Returns true if smps_mode_new is strictly more restrictive than
  2658. * smps_mode_old.
  2659. */
  2660. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  2661. enum ieee80211_smps_mode smps_mode_new)
  2662. {
  2663. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  2664. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  2665. return false;
  2666. switch (smps_mode_old) {
  2667. case IEEE80211_SMPS_STATIC:
  2668. return false;
  2669. case IEEE80211_SMPS_DYNAMIC:
  2670. return smps_mode_new == IEEE80211_SMPS_STATIC;
  2671. case IEEE80211_SMPS_OFF:
  2672. return smps_mode_new != IEEE80211_SMPS_OFF;
  2673. default:
  2674. WARN_ON(1);
  2675. }
  2676. return false;
  2677. }
  2678. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  2679. struct cfg80211_csa_settings *csa_settings)
  2680. {
  2681. struct sk_buff *skb;
  2682. struct ieee80211_mgmt *mgmt;
  2683. struct ieee80211_local *local = sdata->local;
  2684. int freq;
  2685. int hdr_len = offsetofend(struct ieee80211_mgmt,
  2686. u.action.u.chan_switch);
  2687. u8 *pos;
  2688. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2689. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2690. return -EOPNOTSUPP;
  2691. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  2692. 5 + /* channel switch announcement element */
  2693. 3 + /* secondary channel offset element */
  2694. 5 + /* wide bandwidth channel switch announcement */
  2695. 8); /* mesh channel switch parameters element */
  2696. if (!skb)
  2697. return -ENOMEM;
  2698. skb_reserve(skb, local->tx_headroom);
  2699. mgmt = skb_put_zero(skb, hdr_len);
  2700. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2701. IEEE80211_STYPE_ACTION);
  2702. eth_broadcast_addr(mgmt->da);
  2703. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2704. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2705. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2706. } else {
  2707. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2708. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  2709. }
  2710. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  2711. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  2712. pos = skb_put(skb, 5);
  2713. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  2714. *pos++ = 3; /* IE length */
  2715. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  2716. freq = csa_settings->chandef.chan->center_freq;
  2717. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  2718. *pos++ = csa_settings->count; /* count */
  2719. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  2720. enum nl80211_channel_type ch_type;
  2721. skb_put(skb, 3);
  2722. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  2723. *pos++ = 1; /* IE length */
  2724. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  2725. if (ch_type == NL80211_CHAN_HT40PLUS)
  2726. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2727. else
  2728. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2729. }
  2730. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2731. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2732. skb_put(skb, 8);
  2733. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  2734. *pos++ = 6; /* IE length */
  2735. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  2736. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  2737. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  2738. *pos++ |= csa_settings->block_tx ?
  2739. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  2740. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  2741. pos += 2;
  2742. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  2743. pos += 2;
  2744. }
  2745. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
  2746. csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
  2747. csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
  2748. skb_put(skb, 5);
  2749. ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
  2750. }
  2751. ieee80211_tx_skb(sdata, skb);
  2752. return 0;
  2753. }
  2754. bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
  2755. {
  2756. return !(cs == NULL || cs->cipher == 0 ||
  2757. cs->hdr_len < cs->pn_len + cs->pn_off ||
  2758. cs->hdr_len <= cs->key_idx_off ||
  2759. cs->key_idx_shift > 7 ||
  2760. cs->key_idx_mask == 0);
  2761. }
  2762. bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
  2763. {
  2764. int i;
  2765. /* Ensure we have enough iftype bitmap space for all iftype values */
  2766. WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
  2767. for (i = 0; i < n; i++)
  2768. if (!ieee80211_cs_valid(&cs[i]))
  2769. return false;
  2770. return true;
  2771. }
  2772. const struct ieee80211_cipher_scheme *
  2773. ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
  2774. enum nl80211_iftype iftype)
  2775. {
  2776. const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
  2777. int n = local->hw.n_cipher_schemes;
  2778. int i;
  2779. const struct ieee80211_cipher_scheme *cs = NULL;
  2780. for (i = 0; i < n; i++) {
  2781. if (l[i].cipher == cipher) {
  2782. cs = &l[i];
  2783. break;
  2784. }
  2785. }
  2786. if (!cs || !(cs->iftype & BIT(iftype)))
  2787. return NULL;
  2788. return cs;
  2789. }
  2790. int ieee80211_cs_headroom(struct ieee80211_local *local,
  2791. struct cfg80211_crypto_settings *crypto,
  2792. enum nl80211_iftype iftype)
  2793. {
  2794. const struct ieee80211_cipher_scheme *cs;
  2795. int headroom = IEEE80211_ENCRYPT_HEADROOM;
  2796. int i;
  2797. for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
  2798. cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
  2799. iftype);
  2800. if (cs && headroom < cs->hdr_len)
  2801. headroom = cs->hdr_len;
  2802. }
  2803. cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
  2804. if (cs && headroom < cs->hdr_len)
  2805. headroom = cs->hdr_len;
  2806. return headroom;
  2807. }
  2808. static bool
  2809. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  2810. {
  2811. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  2812. int skip;
  2813. if (end > 0)
  2814. return false;
  2815. /* One shot NOA */
  2816. if (data->count[i] == 1)
  2817. return false;
  2818. if (data->desc[i].interval == 0)
  2819. return false;
  2820. /* End time is in the past, check for repetitions */
  2821. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  2822. if (data->count[i] < 255) {
  2823. if (data->count[i] <= skip) {
  2824. data->count[i] = 0;
  2825. return false;
  2826. }
  2827. data->count[i] -= skip;
  2828. }
  2829. data->desc[i].start += skip * data->desc[i].interval;
  2830. return true;
  2831. }
  2832. static bool
  2833. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  2834. s32 *offset)
  2835. {
  2836. bool ret = false;
  2837. int i;
  2838. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2839. s32 cur;
  2840. if (!data->count[i])
  2841. continue;
  2842. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  2843. ret = true;
  2844. cur = data->desc[i].start - tsf;
  2845. if (cur > *offset)
  2846. continue;
  2847. cur = data->desc[i].start + data->desc[i].duration - tsf;
  2848. if (cur > *offset)
  2849. *offset = cur;
  2850. }
  2851. return ret;
  2852. }
  2853. static u32
  2854. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  2855. {
  2856. s32 offset = 0;
  2857. int tries = 0;
  2858. /*
  2859. * arbitrary limit, used to avoid infinite loops when combined NoA
  2860. * descriptors cover the full time period.
  2861. */
  2862. int max_tries = 5;
  2863. ieee80211_extend_absent_time(data, tsf, &offset);
  2864. do {
  2865. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  2866. break;
  2867. tries++;
  2868. } while (tries < max_tries);
  2869. return offset;
  2870. }
  2871. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  2872. {
  2873. u32 next_offset = BIT(31) - 1;
  2874. int i;
  2875. data->absent = 0;
  2876. data->has_next_tsf = false;
  2877. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2878. s32 start;
  2879. if (!data->count[i])
  2880. continue;
  2881. ieee80211_extend_noa_desc(data, tsf, i);
  2882. start = data->desc[i].start - tsf;
  2883. if (start <= 0)
  2884. data->absent |= BIT(i);
  2885. if (next_offset > start)
  2886. next_offset = start;
  2887. data->has_next_tsf = true;
  2888. }
  2889. if (data->absent)
  2890. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  2891. data->next_tsf = tsf + next_offset;
  2892. }
  2893. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  2894. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  2895. struct ieee80211_noa_data *data, u32 tsf)
  2896. {
  2897. int ret = 0;
  2898. int i;
  2899. memset(data, 0, sizeof(*data));
  2900. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2901. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  2902. if (!desc->count || !desc->duration)
  2903. continue;
  2904. data->count[i] = desc->count;
  2905. data->desc[i].start = le32_to_cpu(desc->start_time);
  2906. data->desc[i].duration = le32_to_cpu(desc->duration);
  2907. data->desc[i].interval = le32_to_cpu(desc->interval);
  2908. if (data->count[i] > 1 &&
  2909. data->desc[i].interval < data->desc[i].duration)
  2910. continue;
  2911. ieee80211_extend_noa_desc(data, tsf, i);
  2912. ret++;
  2913. }
  2914. if (ret)
  2915. ieee80211_update_p2p_noa(data, tsf);
  2916. return ret;
  2917. }
  2918. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  2919. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  2920. struct ieee80211_sub_if_data *sdata)
  2921. {
  2922. u64 tsf = drv_get_tsf(local, sdata);
  2923. u64 dtim_count = 0;
  2924. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  2925. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  2926. struct ps_data *ps;
  2927. u8 bcns_from_dtim;
  2928. if (tsf == -1ULL || !beacon_int || !dtim_period)
  2929. return;
  2930. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2931. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  2932. if (!sdata->bss)
  2933. return;
  2934. ps = &sdata->bss->ps;
  2935. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2936. ps = &sdata->u.mesh.ps;
  2937. } else {
  2938. return;
  2939. }
  2940. /*
  2941. * actually finds last dtim_count, mac80211 will update in
  2942. * __beacon_add_tim().
  2943. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  2944. */
  2945. do_div(tsf, beacon_int);
  2946. bcns_from_dtim = do_div(tsf, dtim_period);
  2947. /* just had a DTIM */
  2948. if (!bcns_from_dtim)
  2949. dtim_count = 0;
  2950. else
  2951. dtim_count = dtim_period - bcns_from_dtim;
  2952. ps->dtim_count = dtim_count;
  2953. }
  2954. static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
  2955. struct ieee80211_chanctx *ctx)
  2956. {
  2957. struct ieee80211_sub_if_data *sdata;
  2958. u8 radar_detect = 0;
  2959. lockdep_assert_held(&local->chanctx_mtx);
  2960. if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
  2961. return 0;
  2962. list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
  2963. if (sdata->reserved_radar_required)
  2964. radar_detect |= BIT(sdata->reserved_chandef.width);
  2965. /*
  2966. * An in-place reservation context should not have any assigned vifs
  2967. * until it replaces the other context.
  2968. */
  2969. WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
  2970. !list_empty(&ctx->assigned_vifs));
  2971. list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
  2972. if (sdata->radar_required)
  2973. radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
  2974. return radar_detect;
  2975. }
  2976. int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
  2977. const struct cfg80211_chan_def *chandef,
  2978. enum ieee80211_chanctx_mode chanmode,
  2979. u8 radar_detect)
  2980. {
  2981. struct ieee80211_local *local = sdata->local;
  2982. struct ieee80211_sub_if_data *sdata_iter;
  2983. enum nl80211_iftype iftype = sdata->wdev.iftype;
  2984. struct ieee80211_chanctx *ctx;
  2985. int total = 1;
  2986. struct iface_combination_params params = {
  2987. .radar_detect = radar_detect,
  2988. };
  2989. lockdep_assert_held(&local->chanctx_mtx);
  2990. if (WARN_ON(hweight32(radar_detect) > 1))
  2991. return -EINVAL;
  2992. if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2993. !chandef->chan))
  2994. return -EINVAL;
  2995. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  2996. return -EINVAL;
  2997. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2998. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  2999. /*
  3000. * always passing this is harmless, since it'll be the
  3001. * same value that cfg80211 finds if it finds the same
  3002. * interface ... and that's always allowed
  3003. */
  3004. params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
  3005. }
  3006. /* Always allow software iftypes */
  3007. if (cfg80211_iftype_allowed(local->hw.wiphy, iftype, 0, 1)) {
  3008. if (radar_detect)
  3009. return -EINVAL;
  3010. return 0;
  3011. }
  3012. if (chandef)
  3013. params.num_different_channels = 1;
  3014. if (iftype != NL80211_IFTYPE_UNSPECIFIED)
  3015. params.iftype_num[iftype] = 1;
  3016. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3017. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3018. continue;
  3019. params.radar_detect |=
  3020. ieee80211_chanctx_radar_detect(local, ctx);
  3021. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
  3022. params.num_different_channels++;
  3023. continue;
  3024. }
  3025. if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  3026. cfg80211_chandef_compatible(chandef,
  3027. &ctx->conf.def))
  3028. continue;
  3029. params.num_different_channels++;
  3030. }
  3031. list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
  3032. struct wireless_dev *wdev_iter;
  3033. wdev_iter = &sdata_iter->wdev;
  3034. if (sdata_iter == sdata ||
  3035. !ieee80211_sdata_running(sdata_iter) ||
  3036. cfg80211_iftype_allowed(local->hw.wiphy,
  3037. wdev_iter->iftype, 0, 1))
  3038. continue;
  3039. params.iftype_num[wdev_iter->iftype]++;
  3040. total++;
  3041. }
  3042. if (total == 1 && !params.radar_detect)
  3043. return 0;
  3044. return cfg80211_check_combinations(local->hw.wiphy, &params);
  3045. }
  3046. static void
  3047. ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
  3048. void *data)
  3049. {
  3050. u32 *max_num_different_channels = data;
  3051. *max_num_different_channels = max(*max_num_different_channels,
  3052. c->num_different_channels);
  3053. }
  3054. int ieee80211_max_num_channels(struct ieee80211_local *local)
  3055. {
  3056. struct ieee80211_sub_if_data *sdata;
  3057. struct ieee80211_chanctx *ctx;
  3058. u32 max_num_different_channels = 1;
  3059. int err;
  3060. struct iface_combination_params params = {0};
  3061. lockdep_assert_held(&local->chanctx_mtx);
  3062. list_for_each_entry(ctx, &local->chanctx_list, list) {
  3063. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  3064. continue;
  3065. params.num_different_channels++;
  3066. params.radar_detect |=
  3067. ieee80211_chanctx_radar_detect(local, ctx);
  3068. }
  3069. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  3070. params.iftype_num[sdata->wdev.iftype]++;
  3071. err = cfg80211_iter_combinations(local->hw.wiphy, &params,
  3072. ieee80211_iter_max_chans,
  3073. &max_num_different_channels);
  3074. if (err < 0)
  3075. return err;
  3076. return max_num_different_channels;
  3077. }
  3078. u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
  3079. {
  3080. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  3081. *buf++ = 7; /* len */
  3082. *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
  3083. *buf++ = 0x50;
  3084. *buf++ = 0xf2;
  3085. *buf++ = 2; /* WME */
  3086. *buf++ = 0; /* WME info */
  3087. *buf++ = 1; /* WME ver */
  3088. *buf++ = qosinfo; /* U-APSD no in use */
  3089. return buf;
  3090. }
  3091. void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
  3092. unsigned long *frame_cnt,
  3093. unsigned long *byte_cnt)
  3094. {
  3095. struct txq_info *txqi = to_txq_info(txq);
  3096. u32 frag_cnt = 0, frag_bytes = 0;
  3097. struct sk_buff *skb;
  3098. skb_queue_walk(&txqi->frags, skb) {
  3099. frag_cnt++;
  3100. frag_bytes += skb->len;
  3101. }
  3102. if (frame_cnt)
  3103. *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
  3104. if (byte_cnt)
  3105. *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
  3106. }
  3107. EXPORT_SYMBOL(ieee80211_txq_get_depth);
  3108. const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
  3109. IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
  3110. IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
  3111. IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
  3112. IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
  3113. };