tx.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <[email protected]>
  5. * Copyright 2007 Johannes Berg <[email protected]>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. *
  13. * Transmit and frame generation functions.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/export.h>
  22. #include <net/net_namespace.h>
  23. #include <net/ieee80211_radiotap.h>
  24. #include <net/cfg80211.h>
  25. #include <net/mac80211.h>
  26. #include <net/codel.h>
  27. #include <net/codel_impl.h>
  28. #include <asm/unaligned.h>
  29. #include <net/fq_impl.h>
  30. #include "ieee80211_i.h"
  31. #include "driver-ops.h"
  32. #include "led.h"
  33. #include "mesh.h"
  34. #include "wep.h"
  35. #include "wpa.h"
  36. #include "wme.h"
  37. #include "rate.h"
  38. /* misc utils */
  39. static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
  40. {
  41. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  42. u64_stats_update_begin(&tstats->syncp);
  43. tstats->tx_packets++;
  44. tstats->tx_bytes += len;
  45. u64_stats_update_end(&tstats->syncp);
  46. }
  47. static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
  48. struct sk_buff *skb, int group_addr,
  49. int next_frag_len)
  50. {
  51. int rate, mrate, erp, dur, i, shift = 0;
  52. struct ieee80211_rate *txrate;
  53. struct ieee80211_local *local = tx->local;
  54. struct ieee80211_supported_band *sband;
  55. struct ieee80211_hdr *hdr;
  56. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  57. struct ieee80211_chanctx_conf *chanctx_conf;
  58. u32 rate_flags = 0;
  59. rcu_read_lock();
  60. chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
  61. if (chanctx_conf) {
  62. shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
  63. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  64. }
  65. rcu_read_unlock();
  66. /* assume HW handles this */
  67. if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
  68. return 0;
  69. /* uh huh? */
  70. if (WARN_ON_ONCE(tx->rate.idx < 0))
  71. return 0;
  72. sband = local->hw.wiphy->bands[info->band];
  73. txrate = &sband->bitrates[tx->rate.idx];
  74. erp = txrate->flags & IEEE80211_RATE_ERP_G;
  75. /*
  76. * data and mgmt (except PS Poll):
  77. * - during CFP: 32768
  78. * - during contention period:
  79. * if addr1 is group address: 0
  80. * if more fragments = 0 and addr1 is individual address: time to
  81. * transmit one ACK plus SIFS
  82. * if more fragments = 1 and addr1 is individual address: time to
  83. * transmit next fragment plus 2 x ACK plus 3 x SIFS
  84. *
  85. * IEEE 802.11, 9.6:
  86. * - control response frame (CTS or ACK) shall be transmitted using the
  87. * same rate as the immediately previous frame in the frame exchange
  88. * sequence, if this rate belongs to the PHY mandatory rates, or else
  89. * at the highest possible rate belonging to the PHY rates in the
  90. * BSSBasicRateSet
  91. */
  92. hdr = (struct ieee80211_hdr *)skb->data;
  93. if (ieee80211_is_ctl(hdr->frame_control)) {
  94. /* TODO: These control frames are not currently sent by
  95. * mac80211, but should they be implemented, this function
  96. * needs to be updated to support duration field calculation.
  97. *
  98. * RTS: time needed to transmit pending data/mgmt frame plus
  99. * one CTS frame plus one ACK frame plus 3 x SIFS
  100. * CTS: duration of immediately previous RTS minus time
  101. * required to transmit CTS and its SIFS
  102. * ACK: 0 if immediately previous directed data/mgmt had
  103. * more=0, with more=1 duration in ACK frame is duration
  104. * from previous frame minus time needed to transmit ACK
  105. * and its SIFS
  106. * PS Poll: BIT(15) | BIT(14) | aid
  107. */
  108. return 0;
  109. }
  110. /* data/mgmt */
  111. if (0 /* FIX: data/mgmt during CFP */)
  112. return cpu_to_le16(32768);
  113. if (group_addr) /* Group address as the destination - no ACK */
  114. return 0;
  115. /* Individual destination address:
  116. * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
  117. * CTS and ACK frames shall be transmitted using the highest rate in
  118. * basic rate set that is less than or equal to the rate of the
  119. * immediately previous frame and that is using the same modulation
  120. * (CCK or OFDM). If no basic rate set matches with these requirements,
  121. * the highest mandatory rate of the PHY that is less than or equal to
  122. * the rate of the previous frame is used.
  123. * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
  124. */
  125. rate = -1;
  126. /* use lowest available if everything fails */
  127. mrate = sband->bitrates[0].bitrate;
  128. for (i = 0; i < sband->n_bitrates; i++) {
  129. struct ieee80211_rate *r = &sband->bitrates[i];
  130. if (r->bitrate > txrate->bitrate)
  131. break;
  132. if ((rate_flags & r->flags) != rate_flags)
  133. continue;
  134. if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
  135. rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
  136. switch (sband->band) {
  137. case NL80211_BAND_2GHZ: {
  138. u32 flag;
  139. if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  140. flag = IEEE80211_RATE_MANDATORY_G;
  141. else
  142. flag = IEEE80211_RATE_MANDATORY_B;
  143. if (r->flags & flag)
  144. mrate = r->bitrate;
  145. break;
  146. }
  147. case NL80211_BAND_5GHZ:
  148. if (r->flags & IEEE80211_RATE_MANDATORY_A)
  149. mrate = r->bitrate;
  150. break;
  151. case NL80211_BAND_60GHZ:
  152. /* TODO, for now fall through */
  153. case NUM_NL80211_BANDS:
  154. WARN_ON(1);
  155. break;
  156. }
  157. }
  158. if (rate == -1) {
  159. /* No matching basic rate found; use highest suitable mandatory
  160. * PHY rate */
  161. rate = DIV_ROUND_UP(mrate, 1 << shift);
  162. }
  163. /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
  164. if (ieee80211_is_data_qos(hdr->frame_control) &&
  165. *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
  166. dur = 0;
  167. else
  168. /* Time needed to transmit ACK
  169. * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
  170. * to closest integer */
  171. dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
  172. tx->sdata->vif.bss_conf.use_short_preamble,
  173. shift);
  174. if (next_frag_len) {
  175. /* Frame is fragmented: duration increases with time needed to
  176. * transmit next fragment plus ACK and 2 x SIFS. */
  177. dur *= 2; /* ACK + SIFS */
  178. /* next fragment */
  179. dur += ieee80211_frame_duration(sband->band, next_frag_len,
  180. txrate->bitrate, erp,
  181. tx->sdata->vif.bss_conf.use_short_preamble,
  182. shift);
  183. }
  184. return cpu_to_le16(dur);
  185. }
  186. /* tx handlers */
  187. static ieee80211_tx_result debug_noinline
  188. ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
  189. {
  190. struct ieee80211_local *local = tx->local;
  191. struct ieee80211_if_managed *ifmgd;
  192. /* driver doesn't support power save */
  193. if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
  194. return TX_CONTINUE;
  195. /* hardware does dynamic power save */
  196. if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
  197. return TX_CONTINUE;
  198. /* dynamic power save disabled */
  199. if (local->hw.conf.dynamic_ps_timeout <= 0)
  200. return TX_CONTINUE;
  201. /* we are scanning, don't enable power save */
  202. if (local->scanning)
  203. return TX_CONTINUE;
  204. if (!local->ps_sdata)
  205. return TX_CONTINUE;
  206. /* No point if we're going to suspend */
  207. if (local->quiescing)
  208. return TX_CONTINUE;
  209. /* dynamic ps is supported only in managed mode */
  210. if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
  211. return TX_CONTINUE;
  212. ifmgd = &tx->sdata->u.mgd;
  213. /*
  214. * Don't wakeup from power save if u-apsd is enabled, voip ac has
  215. * u-apsd enabled and the frame is in voip class. This effectively
  216. * means that even if all access categories have u-apsd enabled, in
  217. * practise u-apsd is only used with the voip ac. This is a
  218. * workaround for the case when received voip class packets do not
  219. * have correct qos tag for some reason, due the network or the
  220. * peer application.
  221. *
  222. * Note: ifmgd->uapsd_queues access is racy here. If the value is
  223. * changed via debugfs, user needs to reassociate manually to have
  224. * everything in sync.
  225. */
  226. if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
  227. (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
  228. skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
  229. return TX_CONTINUE;
  230. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  231. ieee80211_stop_queues_by_reason(&local->hw,
  232. IEEE80211_MAX_QUEUE_MAP,
  233. IEEE80211_QUEUE_STOP_REASON_PS,
  234. false);
  235. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  236. ieee80211_queue_work(&local->hw,
  237. &local->dynamic_ps_disable_work);
  238. }
  239. /* Don't restart the timer if we're not disassociated */
  240. if (!ifmgd->associated)
  241. return TX_CONTINUE;
  242. mod_timer(&local->dynamic_ps_timer, jiffies +
  243. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  244. return TX_CONTINUE;
  245. }
  246. static ieee80211_tx_result debug_noinline
  247. ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
  248. {
  249. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  250. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  251. bool assoc = false;
  252. if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
  253. return TX_CONTINUE;
  254. if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
  255. test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
  256. !ieee80211_is_probe_req(hdr->frame_control) &&
  257. !ieee80211_is_nullfunc(hdr->frame_control))
  258. /*
  259. * When software scanning only nullfunc frames (to notify
  260. * the sleep state to the AP) and probe requests (for the
  261. * active scan) are allowed, all other frames should not be
  262. * sent and we should not get here, but if we do
  263. * nonetheless, drop them to avoid sending them
  264. * off-channel. See the link below and
  265. * ieee80211_start_scan() for more.
  266. *
  267. * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
  268. */
  269. return TX_DROP;
  270. if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
  271. return TX_CONTINUE;
  272. if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
  273. return TX_CONTINUE;
  274. if (tx->flags & IEEE80211_TX_PS_BUFFERED)
  275. return TX_CONTINUE;
  276. if (tx->sta)
  277. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  278. if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
  279. if (unlikely(!assoc &&
  280. ieee80211_is_data(hdr->frame_control))) {
  281. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  282. sdata_info(tx->sdata,
  283. "dropped data frame to not associated station %pM\n",
  284. hdr->addr1);
  285. #endif
  286. I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
  287. return TX_DROP;
  288. }
  289. } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
  290. ieee80211_is_data(hdr->frame_control) &&
  291. !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
  292. /*
  293. * No associated STAs - no need to send multicast
  294. * frames.
  295. */
  296. return TX_DROP;
  297. }
  298. return TX_CONTINUE;
  299. }
  300. /* This function is called whenever the AP is about to exceed the maximum limit
  301. * of buffered frames for power saving STAs. This situation should not really
  302. * happen often during normal operation, so dropping the oldest buffered packet
  303. * from each queue should be OK to make some room for new frames. */
  304. static void purge_old_ps_buffers(struct ieee80211_local *local)
  305. {
  306. int total = 0, purged = 0;
  307. struct sk_buff *skb;
  308. struct ieee80211_sub_if_data *sdata;
  309. struct sta_info *sta;
  310. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  311. struct ps_data *ps;
  312. if (sdata->vif.type == NL80211_IFTYPE_AP)
  313. ps = &sdata->u.ap.ps;
  314. else if (ieee80211_vif_is_mesh(&sdata->vif))
  315. ps = &sdata->u.mesh.ps;
  316. else
  317. continue;
  318. skb = skb_dequeue(&ps->bc_buf);
  319. if (skb) {
  320. purged++;
  321. ieee80211_free_txskb(&local->hw, skb);
  322. }
  323. total += skb_queue_len(&ps->bc_buf);
  324. }
  325. /*
  326. * Drop one frame from each station from the lowest-priority
  327. * AC that has frames at all.
  328. */
  329. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  330. int ac;
  331. for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
  332. skb = skb_dequeue(&sta->ps_tx_buf[ac]);
  333. total += skb_queue_len(&sta->ps_tx_buf[ac]);
  334. if (skb) {
  335. purged++;
  336. ieee80211_free_txskb(&local->hw, skb);
  337. break;
  338. }
  339. }
  340. }
  341. local->total_ps_buffered = total;
  342. ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
  343. }
  344. static ieee80211_tx_result
  345. ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
  346. {
  347. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  348. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  349. struct ps_data *ps;
  350. /*
  351. * broadcast/multicast frame
  352. *
  353. * If any of the associated/peer stations is in power save mode,
  354. * the frame is buffered to be sent after DTIM beacon frame.
  355. * This is done either by the hardware or us.
  356. */
  357. /* powersaving STAs currently only in AP/VLAN/mesh mode */
  358. if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  359. tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  360. if (!tx->sdata->bss)
  361. return TX_CONTINUE;
  362. ps = &tx->sdata->bss->ps;
  363. } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
  364. ps = &tx->sdata->u.mesh.ps;
  365. } else {
  366. return TX_CONTINUE;
  367. }
  368. /* no buffering for ordered frames */
  369. if (ieee80211_has_order(hdr->frame_control))
  370. return TX_CONTINUE;
  371. if (ieee80211_is_probe_req(hdr->frame_control))
  372. return TX_CONTINUE;
  373. if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
  374. info->hw_queue = tx->sdata->vif.cab_queue;
  375. /* no stations in PS mode and no buffered packets */
  376. if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
  377. return TX_CONTINUE;
  378. info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
  379. /* device releases frame after DTIM beacon */
  380. if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
  381. return TX_CONTINUE;
  382. /* buffered in mac80211 */
  383. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  384. purge_old_ps_buffers(tx->local);
  385. if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
  386. ps_dbg(tx->sdata,
  387. "BC TX buffer full - dropping the oldest frame\n");
  388. ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
  389. } else
  390. tx->local->total_ps_buffered++;
  391. skb_queue_tail(&ps->bc_buf, tx->skb);
  392. return TX_QUEUED;
  393. }
  394. static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
  395. struct sk_buff *skb)
  396. {
  397. if (!ieee80211_is_mgmt(fc))
  398. return 0;
  399. if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
  400. return 0;
  401. if (!ieee80211_is_robust_mgmt_frame(skb))
  402. return 0;
  403. return 1;
  404. }
  405. static ieee80211_tx_result
  406. ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
  407. {
  408. struct sta_info *sta = tx->sta;
  409. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  410. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  411. struct ieee80211_local *local = tx->local;
  412. if (unlikely(!sta))
  413. return TX_CONTINUE;
  414. if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
  415. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  416. test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
  417. !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
  418. int ac = skb_get_queue_mapping(tx->skb);
  419. if (ieee80211_is_mgmt(hdr->frame_control) &&
  420. !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
  421. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
  422. return TX_CONTINUE;
  423. }
  424. ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
  425. sta->sta.addr, sta->sta.aid, ac);
  426. if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
  427. purge_old_ps_buffers(tx->local);
  428. /* sync with ieee80211_sta_ps_deliver_wakeup */
  429. spin_lock(&sta->ps_lock);
  430. /*
  431. * STA woke up the meantime and all the frames on ps_tx_buf have
  432. * been queued to pending queue. No reordering can happen, go
  433. * ahead and Tx the packet.
  434. */
  435. if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
  436. !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
  437. !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
  438. spin_unlock(&sta->ps_lock);
  439. return TX_CONTINUE;
  440. }
  441. if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
  442. struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
  443. ps_dbg(tx->sdata,
  444. "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
  445. sta->sta.addr, ac);
  446. ieee80211_free_txskb(&local->hw, old);
  447. } else
  448. tx->local->total_ps_buffered++;
  449. info->control.jiffies = jiffies;
  450. info->control.vif = &tx->sdata->vif;
  451. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  452. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  453. skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
  454. spin_unlock(&sta->ps_lock);
  455. if (!timer_pending(&local->sta_cleanup))
  456. mod_timer(&local->sta_cleanup,
  457. round_jiffies(jiffies +
  458. STA_INFO_CLEANUP_INTERVAL));
  459. /*
  460. * We queued up some frames, so the TIM bit might
  461. * need to be set, recalculate it.
  462. */
  463. sta_info_recalc_tim(sta);
  464. return TX_QUEUED;
  465. } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
  466. ps_dbg(tx->sdata,
  467. "STA %pM in PS mode, but polling/in SP -> send frame\n",
  468. sta->sta.addr);
  469. }
  470. return TX_CONTINUE;
  471. }
  472. static ieee80211_tx_result debug_noinline
  473. ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
  474. {
  475. if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
  476. return TX_CONTINUE;
  477. if (tx->flags & IEEE80211_TX_UNICAST)
  478. return ieee80211_tx_h_unicast_ps_buf(tx);
  479. else
  480. return ieee80211_tx_h_multicast_ps_buf(tx);
  481. }
  482. static ieee80211_tx_result debug_noinline
  483. ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
  484. {
  485. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  486. if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
  487. if (tx->sdata->control_port_no_encrypt)
  488. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  489. info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
  490. info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  491. }
  492. return TX_CONTINUE;
  493. }
  494. static ieee80211_tx_result debug_noinline
  495. ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
  496. {
  497. struct ieee80211_key *key;
  498. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  499. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  500. if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
  501. tx->key = NULL;
  502. else if (tx->sta &&
  503. (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
  504. tx->key = key;
  505. else if (ieee80211_is_group_privacy_action(tx->skb) &&
  506. (key = rcu_dereference(tx->sdata->default_multicast_key)))
  507. tx->key = key;
  508. else if (ieee80211_is_mgmt(hdr->frame_control) &&
  509. is_multicast_ether_addr(hdr->addr1) &&
  510. ieee80211_is_robust_mgmt_frame(tx->skb) &&
  511. (key = rcu_dereference(tx->sdata->default_mgmt_key)))
  512. tx->key = key;
  513. else if (is_multicast_ether_addr(hdr->addr1) &&
  514. (key = rcu_dereference(tx->sdata->default_multicast_key)))
  515. tx->key = key;
  516. else if (!is_multicast_ether_addr(hdr->addr1) &&
  517. (key = rcu_dereference(tx->sdata->default_unicast_key)))
  518. tx->key = key;
  519. else
  520. tx->key = NULL;
  521. if (tx->key) {
  522. bool skip_hw = false;
  523. /* TODO: add threshold stuff again */
  524. switch (tx->key->conf.cipher) {
  525. case WLAN_CIPHER_SUITE_WEP40:
  526. case WLAN_CIPHER_SUITE_WEP104:
  527. case WLAN_CIPHER_SUITE_TKIP:
  528. if (!ieee80211_is_data_present(hdr->frame_control))
  529. tx->key = NULL;
  530. break;
  531. case WLAN_CIPHER_SUITE_CCMP:
  532. case WLAN_CIPHER_SUITE_CCMP_256:
  533. case WLAN_CIPHER_SUITE_GCMP:
  534. case WLAN_CIPHER_SUITE_GCMP_256:
  535. if (!ieee80211_is_data_present(hdr->frame_control) &&
  536. !ieee80211_use_mfp(hdr->frame_control, tx->sta,
  537. tx->skb) &&
  538. !ieee80211_is_group_privacy_action(tx->skb))
  539. tx->key = NULL;
  540. else
  541. skip_hw = (tx->key->conf.flags &
  542. IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
  543. ieee80211_is_mgmt(hdr->frame_control);
  544. break;
  545. case WLAN_CIPHER_SUITE_AES_CMAC:
  546. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  547. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  548. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  549. if (!ieee80211_is_mgmt(hdr->frame_control))
  550. tx->key = NULL;
  551. break;
  552. }
  553. if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
  554. !ieee80211_is_deauth(hdr->frame_control)))
  555. return TX_DROP;
  556. if (!skip_hw && tx->key &&
  557. tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  558. info->control.hw_key = &tx->key->conf;
  559. }
  560. return TX_CONTINUE;
  561. }
  562. static ieee80211_tx_result debug_noinline
  563. ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
  564. {
  565. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  566. struct ieee80211_hdr *hdr = (void *)tx->skb->data;
  567. struct ieee80211_supported_band *sband;
  568. u32 len;
  569. struct ieee80211_tx_rate_control txrc;
  570. struct ieee80211_sta_rates *ratetbl = NULL;
  571. bool assoc = false;
  572. memset(&txrc, 0, sizeof(txrc));
  573. sband = tx->local->hw.wiphy->bands[info->band];
  574. len = min_t(u32, tx->skb->len + FCS_LEN,
  575. tx->local->hw.wiphy->frag_threshold);
  576. /* set up the tx rate control struct we give the RC algo */
  577. txrc.hw = &tx->local->hw;
  578. txrc.sband = sband;
  579. txrc.bss_conf = &tx->sdata->vif.bss_conf;
  580. txrc.skb = tx->skb;
  581. txrc.reported_rate.idx = -1;
  582. txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
  583. if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
  584. txrc.max_rate_idx = -1;
  585. else
  586. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  587. if (tx->sdata->rc_has_mcs_mask[info->band])
  588. txrc.rate_idx_mcs_mask =
  589. tx->sdata->rc_rateidx_mcs_mask[info->band];
  590. txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
  591. tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  592. tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  593. tx->sdata->vif.type == NL80211_IFTYPE_OCB);
  594. /* set up RTS protection if desired */
  595. if (len > tx->local->hw.wiphy->rts_threshold) {
  596. txrc.rts = true;
  597. }
  598. info->control.use_rts = txrc.rts;
  599. info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
  600. /*
  601. * Use short preamble if the BSS can handle it, but not for
  602. * management frames unless we know the receiver can handle
  603. * that -- the management frame might be to a station that
  604. * just wants a probe response.
  605. */
  606. if (tx->sdata->vif.bss_conf.use_short_preamble &&
  607. (ieee80211_is_data(hdr->frame_control) ||
  608. (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
  609. txrc.short_preamble = true;
  610. info->control.short_preamble = txrc.short_preamble;
  611. /* don't ask rate control when rate already injected via radiotap */
  612. if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
  613. return TX_CONTINUE;
  614. if (tx->sta)
  615. assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
  616. /*
  617. * Lets not bother rate control if we're associated and cannot
  618. * talk to the sta. This should not happen.
  619. */
  620. if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
  621. !rate_usable_index_exists(sband, &tx->sta->sta),
  622. "%s: Dropped data frame as no usable bitrate found while "
  623. "scanning and associated. Target station: "
  624. "%pM on %d GHz band\n",
  625. tx->sdata->name, hdr->addr1,
  626. info->band ? 5 : 2))
  627. return TX_DROP;
  628. /*
  629. * If we're associated with the sta at this point we know we can at
  630. * least send the frame at the lowest bit rate.
  631. */
  632. rate_control_get_rate(tx->sdata, tx->sta, &txrc);
  633. if (tx->sta && !info->control.skip_table)
  634. ratetbl = rcu_dereference(tx->sta->sta.rates);
  635. if (unlikely(info->control.rates[0].idx < 0)) {
  636. if (ratetbl) {
  637. struct ieee80211_tx_rate rate = {
  638. .idx = ratetbl->rate[0].idx,
  639. .flags = ratetbl->rate[0].flags,
  640. .count = ratetbl->rate[0].count
  641. };
  642. if (ratetbl->rate[0].idx < 0)
  643. return TX_DROP;
  644. tx->rate = rate;
  645. } else {
  646. return TX_DROP;
  647. }
  648. } else {
  649. tx->rate = info->control.rates[0];
  650. }
  651. if (txrc.reported_rate.idx < 0) {
  652. txrc.reported_rate = tx->rate;
  653. if (tx->sta && ieee80211_is_data(hdr->frame_control))
  654. tx->sta->tx_stats.last_rate = txrc.reported_rate;
  655. } else if (tx->sta)
  656. tx->sta->tx_stats.last_rate = txrc.reported_rate;
  657. if (ratetbl)
  658. return TX_CONTINUE;
  659. if (unlikely(!info->control.rates[0].count))
  660. info->control.rates[0].count = 1;
  661. if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
  662. (info->flags & IEEE80211_TX_CTL_NO_ACK)))
  663. info->control.rates[0].count = 1;
  664. return TX_CONTINUE;
  665. }
  666. static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
  667. {
  668. u16 *seq = &sta->tid_seq[tid];
  669. __le16 ret = cpu_to_le16(*seq);
  670. /* Increase the sequence number. */
  671. *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
  672. return ret;
  673. }
  674. static ieee80211_tx_result debug_noinline
  675. ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
  676. {
  677. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  678. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
  679. u8 *qc;
  680. int tid;
  681. /*
  682. * Packet injection may want to control the sequence
  683. * number, if we have no matching interface then we
  684. * neither assign one ourselves nor ask the driver to.
  685. */
  686. if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
  687. return TX_CONTINUE;
  688. if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
  689. return TX_CONTINUE;
  690. if (ieee80211_hdrlen(hdr->frame_control) < 24)
  691. return TX_CONTINUE;
  692. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  693. return TX_CONTINUE;
  694. /*
  695. * Anything but QoS data that has a sequence number field
  696. * (is long enough) gets a sequence number from the global
  697. * counter. QoS data frames with a multicast destination
  698. * also use the global counter (802.11-2012 9.3.2.10).
  699. */
  700. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  701. is_multicast_ether_addr(hdr->addr1)) {
  702. /* driver should assign sequence number */
  703. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  704. /* for pure STA mode without beacons, we can do it */
  705. hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
  706. tx->sdata->sequence_number += 0x10;
  707. if (tx->sta)
  708. tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
  709. return TX_CONTINUE;
  710. }
  711. /*
  712. * This should be true for injected/management frames only, for
  713. * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
  714. * above since they are not QoS-data frames.
  715. */
  716. if (!tx->sta)
  717. return TX_CONTINUE;
  718. /* include per-STA, per-TID sequence counter */
  719. qc = ieee80211_get_qos_ctl(hdr);
  720. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  721. tx->sta->tx_stats.msdu[tid]++;
  722. hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
  723. return TX_CONTINUE;
  724. }
  725. static int ieee80211_fragment(struct ieee80211_tx_data *tx,
  726. struct sk_buff *skb, int hdrlen,
  727. int frag_threshold)
  728. {
  729. struct ieee80211_local *local = tx->local;
  730. struct ieee80211_tx_info *info;
  731. struct sk_buff *tmp;
  732. int per_fragm = frag_threshold - hdrlen - FCS_LEN;
  733. int pos = hdrlen + per_fragm;
  734. int rem = skb->len - hdrlen - per_fragm;
  735. if (WARN_ON(rem < 0))
  736. return -EINVAL;
  737. /* first fragment was already added to queue by caller */
  738. while (rem) {
  739. int fraglen = per_fragm;
  740. if (fraglen > rem)
  741. fraglen = rem;
  742. rem -= fraglen;
  743. tmp = dev_alloc_skb(local->tx_headroom +
  744. frag_threshold +
  745. tx->sdata->encrypt_headroom +
  746. IEEE80211_ENCRYPT_TAILROOM);
  747. if (!tmp)
  748. return -ENOMEM;
  749. __skb_queue_tail(&tx->skbs, tmp);
  750. skb_reserve(tmp,
  751. local->tx_headroom + tx->sdata->encrypt_headroom);
  752. /* copy control information */
  753. memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
  754. info = IEEE80211_SKB_CB(tmp);
  755. info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
  756. IEEE80211_TX_CTL_FIRST_FRAGMENT);
  757. if (rem)
  758. info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
  759. skb_copy_queue_mapping(tmp, skb);
  760. tmp->priority = skb->priority;
  761. tmp->dev = skb->dev;
  762. /* copy header and data */
  763. memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
  764. memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
  765. pos += fraglen;
  766. }
  767. /* adjust first fragment's length */
  768. skb_trim(skb, hdrlen + per_fragm);
  769. return 0;
  770. }
  771. static ieee80211_tx_result debug_noinline
  772. ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
  773. {
  774. struct sk_buff *skb = tx->skb;
  775. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  776. struct ieee80211_hdr *hdr = (void *)skb->data;
  777. int frag_threshold = tx->local->hw.wiphy->frag_threshold;
  778. int hdrlen;
  779. int fragnum;
  780. /* no matter what happens, tx->skb moves to tx->skbs */
  781. __skb_queue_tail(&tx->skbs, skb);
  782. tx->skb = NULL;
  783. if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
  784. return TX_CONTINUE;
  785. if (tx->local->ops->set_frag_threshold)
  786. return TX_CONTINUE;
  787. /*
  788. * Warn when submitting a fragmented A-MPDU frame and drop it.
  789. * This scenario is handled in ieee80211_tx_prepare but extra
  790. * caution taken here as fragmented ampdu may cause Tx stop.
  791. */
  792. if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
  793. return TX_DROP;
  794. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  795. /* internal error, why isn't DONTFRAG set? */
  796. if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
  797. return TX_DROP;
  798. /*
  799. * Now fragment the frame. This will allocate all the fragments and
  800. * chain them (using skb as the first fragment) to skb->next.
  801. * During transmission, we will remove the successfully transmitted
  802. * fragments from this list. When the low-level driver rejects one
  803. * of the fragments then we will simply pretend to accept the skb
  804. * but store it away as pending.
  805. */
  806. if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
  807. return TX_DROP;
  808. /* update duration/seq/flags of fragments */
  809. fragnum = 0;
  810. skb_queue_walk(&tx->skbs, skb) {
  811. const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
  812. hdr = (void *)skb->data;
  813. info = IEEE80211_SKB_CB(skb);
  814. if (!skb_queue_is_last(&tx->skbs, skb)) {
  815. hdr->frame_control |= morefrags;
  816. /*
  817. * No multi-rate retries for fragmented frames, that
  818. * would completely throw off the NAV at other STAs.
  819. */
  820. info->control.rates[1].idx = -1;
  821. info->control.rates[2].idx = -1;
  822. info->control.rates[3].idx = -1;
  823. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
  824. info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  825. } else {
  826. hdr->frame_control &= ~morefrags;
  827. }
  828. hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
  829. fragnum++;
  830. }
  831. return TX_CONTINUE;
  832. }
  833. static ieee80211_tx_result debug_noinline
  834. ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
  835. {
  836. struct sk_buff *skb;
  837. int ac = -1;
  838. if (!tx->sta)
  839. return TX_CONTINUE;
  840. skb_queue_walk(&tx->skbs, skb) {
  841. ac = skb_get_queue_mapping(skb);
  842. tx->sta->tx_stats.bytes[ac] += skb->len;
  843. }
  844. if (ac >= 0)
  845. tx->sta->tx_stats.packets[ac]++;
  846. return TX_CONTINUE;
  847. }
  848. static ieee80211_tx_result debug_noinline
  849. ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
  850. {
  851. if (!tx->key)
  852. return TX_CONTINUE;
  853. switch (tx->key->conf.cipher) {
  854. case WLAN_CIPHER_SUITE_WEP40:
  855. case WLAN_CIPHER_SUITE_WEP104:
  856. return ieee80211_crypto_wep_encrypt(tx);
  857. case WLAN_CIPHER_SUITE_TKIP:
  858. return ieee80211_crypto_tkip_encrypt(tx);
  859. case WLAN_CIPHER_SUITE_CCMP:
  860. return ieee80211_crypto_ccmp_encrypt(
  861. tx, IEEE80211_CCMP_MIC_LEN);
  862. case WLAN_CIPHER_SUITE_CCMP_256:
  863. return ieee80211_crypto_ccmp_encrypt(
  864. tx, IEEE80211_CCMP_256_MIC_LEN);
  865. case WLAN_CIPHER_SUITE_AES_CMAC:
  866. return ieee80211_crypto_aes_cmac_encrypt(tx);
  867. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  868. return ieee80211_crypto_aes_cmac_256_encrypt(tx);
  869. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  870. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  871. return ieee80211_crypto_aes_gmac_encrypt(tx);
  872. case WLAN_CIPHER_SUITE_GCMP:
  873. case WLAN_CIPHER_SUITE_GCMP_256:
  874. return ieee80211_crypto_gcmp_encrypt(tx);
  875. default:
  876. return ieee80211_crypto_hw_encrypt(tx);
  877. }
  878. return TX_DROP;
  879. }
  880. static ieee80211_tx_result debug_noinline
  881. ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
  882. {
  883. struct sk_buff *skb;
  884. struct ieee80211_hdr *hdr;
  885. int next_len;
  886. bool group_addr;
  887. skb_queue_walk(&tx->skbs, skb) {
  888. hdr = (void *) skb->data;
  889. if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
  890. break; /* must not overwrite AID */
  891. if (!skb_queue_is_last(&tx->skbs, skb)) {
  892. struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
  893. next_len = next->len;
  894. } else
  895. next_len = 0;
  896. group_addr = is_multicast_ether_addr(hdr->addr1);
  897. hdr->duration_id =
  898. ieee80211_duration(tx, skb, group_addr, next_len);
  899. }
  900. return TX_CONTINUE;
  901. }
  902. /* actual transmit path */
  903. static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
  904. struct sk_buff *skb,
  905. struct ieee80211_tx_info *info,
  906. struct tid_ampdu_tx *tid_tx,
  907. int tid)
  908. {
  909. bool queued = false;
  910. bool reset_agg_timer = false;
  911. struct sk_buff *purge_skb = NULL;
  912. if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  913. info->flags |= IEEE80211_TX_CTL_AMPDU;
  914. reset_agg_timer = true;
  915. } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
  916. /*
  917. * nothing -- this aggregation session is being started
  918. * but that might still fail with the driver
  919. */
  920. } else if (!tx->sta->sta.txq[tid]) {
  921. spin_lock(&tx->sta->lock);
  922. /*
  923. * Need to re-check now, because we may get here
  924. *
  925. * 1) in the window during which the setup is actually
  926. * already done, but not marked yet because not all
  927. * packets are spliced over to the driver pending
  928. * queue yet -- if this happened we acquire the lock
  929. * either before or after the splice happens, but
  930. * need to recheck which of these cases happened.
  931. *
  932. * 2) during session teardown, if the OPERATIONAL bit
  933. * was cleared due to the teardown but the pointer
  934. * hasn't been assigned NULL yet (or we loaded it
  935. * before it was assigned) -- in this case it may
  936. * now be NULL which means we should just let the
  937. * packet pass through because splicing the frames
  938. * back is already done.
  939. */
  940. tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
  941. if (!tid_tx) {
  942. /* do nothing, let packet pass through */
  943. } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
  944. info->flags |= IEEE80211_TX_CTL_AMPDU;
  945. reset_agg_timer = true;
  946. } else {
  947. queued = true;
  948. if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
  949. clear_sta_flag(tx->sta, WLAN_STA_SP);
  950. ps_dbg(tx->sta->sdata,
  951. "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
  952. tx->sta->sta.addr, tx->sta->sta.aid);
  953. }
  954. info->control.vif = &tx->sdata->vif;
  955. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  956. info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
  957. __skb_queue_tail(&tid_tx->pending, skb);
  958. if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
  959. purge_skb = __skb_dequeue(&tid_tx->pending);
  960. }
  961. spin_unlock(&tx->sta->lock);
  962. if (purge_skb)
  963. ieee80211_free_txskb(&tx->local->hw, purge_skb);
  964. }
  965. /* reset session timer */
  966. if (reset_agg_timer && tid_tx->timeout)
  967. tid_tx->last_tx = jiffies;
  968. return queued;
  969. }
  970. /*
  971. * initialises @tx
  972. * pass %NULL for the station if unknown, a valid pointer if known
  973. * or an ERR_PTR() if the station is known not to exist
  974. */
  975. static ieee80211_tx_result
  976. ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
  977. struct ieee80211_tx_data *tx,
  978. struct sta_info *sta, struct sk_buff *skb)
  979. {
  980. struct ieee80211_local *local = sdata->local;
  981. struct ieee80211_hdr *hdr;
  982. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  983. int tid;
  984. u8 *qc;
  985. memset(tx, 0, sizeof(*tx));
  986. tx->skb = skb;
  987. tx->local = local;
  988. tx->sdata = sdata;
  989. __skb_queue_head_init(&tx->skbs);
  990. /*
  991. * If this flag is set to true anywhere, and we get here,
  992. * we are doing the needed processing, so remove the flag
  993. * now.
  994. */
  995. info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  996. hdr = (struct ieee80211_hdr *) skb->data;
  997. if (likely(sta)) {
  998. if (!IS_ERR(sta))
  999. tx->sta = sta;
  1000. } else {
  1001. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  1002. tx->sta = rcu_dereference(sdata->u.vlan.sta);
  1003. if (!tx->sta && sdata->wdev.use_4addr)
  1004. return TX_DROP;
  1005. } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  1006. IEEE80211_TX_CTL_INJECTED) ||
  1007. tx->sdata->control_port_protocol == tx->skb->protocol) {
  1008. tx->sta = sta_info_get_bss(sdata, hdr->addr1);
  1009. }
  1010. if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
  1011. tx->sta = sta_info_get(sdata, hdr->addr1);
  1012. }
  1013. if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
  1014. !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
  1015. ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
  1016. !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
  1017. struct tid_ampdu_tx *tid_tx;
  1018. qc = ieee80211_get_qos_ctl(hdr);
  1019. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  1020. tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
  1021. if (tid_tx) {
  1022. bool queued;
  1023. queued = ieee80211_tx_prep_agg(tx, skb, info,
  1024. tid_tx, tid);
  1025. if (unlikely(queued))
  1026. return TX_QUEUED;
  1027. }
  1028. }
  1029. if (is_multicast_ether_addr(hdr->addr1)) {
  1030. tx->flags &= ~IEEE80211_TX_UNICAST;
  1031. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1032. } else
  1033. tx->flags |= IEEE80211_TX_UNICAST;
  1034. if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
  1035. if (!(tx->flags & IEEE80211_TX_UNICAST) ||
  1036. skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
  1037. info->flags & IEEE80211_TX_CTL_AMPDU)
  1038. info->flags |= IEEE80211_TX_CTL_DONTFRAG;
  1039. }
  1040. if (!tx->sta)
  1041. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1042. else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
  1043. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1044. ieee80211_check_fast_xmit(tx->sta);
  1045. }
  1046. info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
  1047. return TX_CONTINUE;
  1048. }
  1049. static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
  1050. struct ieee80211_vif *vif,
  1051. struct sta_info *sta,
  1052. struct sk_buff *skb)
  1053. {
  1054. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1055. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1056. struct ieee80211_txq *txq = NULL;
  1057. if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
  1058. (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
  1059. return NULL;
  1060. if (!ieee80211_is_data(hdr->frame_control))
  1061. return NULL;
  1062. if (sta) {
  1063. u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
  1064. if (!sta->uploaded)
  1065. return NULL;
  1066. txq = sta->sta.txq[tid];
  1067. } else if (vif) {
  1068. txq = vif->txq;
  1069. }
  1070. if (!txq)
  1071. return NULL;
  1072. return to_txq_info(txq);
  1073. }
  1074. static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
  1075. {
  1076. IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
  1077. }
  1078. static u32 codel_skb_len_func(const struct sk_buff *skb)
  1079. {
  1080. return skb->len;
  1081. }
  1082. static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
  1083. {
  1084. const struct ieee80211_tx_info *info;
  1085. info = (const struct ieee80211_tx_info *)skb->cb;
  1086. return info->control.enqueue_time;
  1087. }
  1088. static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
  1089. void *ctx)
  1090. {
  1091. struct ieee80211_local *local;
  1092. struct txq_info *txqi;
  1093. struct fq *fq;
  1094. struct fq_flow *flow;
  1095. txqi = ctx;
  1096. local = vif_to_sdata(txqi->txq.vif)->local;
  1097. fq = &local->fq;
  1098. if (cvars == &txqi->def_cvars)
  1099. flow = &txqi->def_flow;
  1100. else
  1101. flow = &fq->flows[cvars - local->cvars];
  1102. return fq_flow_dequeue(fq, flow);
  1103. }
  1104. static void codel_drop_func(struct sk_buff *skb,
  1105. void *ctx)
  1106. {
  1107. struct ieee80211_local *local;
  1108. struct ieee80211_hw *hw;
  1109. struct txq_info *txqi;
  1110. txqi = ctx;
  1111. local = vif_to_sdata(txqi->txq.vif)->local;
  1112. hw = &local->hw;
  1113. ieee80211_free_txskb(hw, skb);
  1114. }
  1115. static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
  1116. struct fq_tin *tin,
  1117. struct fq_flow *flow)
  1118. {
  1119. struct ieee80211_local *local;
  1120. struct txq_info *txqi;
  1121. struct codel_vars *cvars;
  1122. struct codel_params *cparams;
  1123. struct codel_stats *cstats;
  1124. local = container_of(fq, struct ieee80211_local, fq);
  1125. txqi = container_of(tin, struct txq_info, tin);
  1126. cparams = &local->cparams;
  1127. cstats = &txqi->cstats;
  1128. if (flow == &txqi->def_flow)
  1129. cvars = &txqi->def_cvars;
  1130. else
  1131. cvars = &local->cvars[flow - fq->flows];
  1132. return codel_dequeue(txqi,
  1133. &flow->backlog,
  1134. cparams,
  1135. cvars,
  1136. cstats,
  1137. codel_skb_len_func,
  1138. codel_skb_time_func,
  1139. codel_drop_func,
  1140. codel_dequeue_func);
  1141. }
  1142. static void fq_skb_free_func(struct fq *fq,
  1143. struct fq_tin *tin,
  1144. struct fq_flow *flow,
  1145. struct sk_buff *skb)
  1146. {
  1147. struct ieee80211_local *local;
  1148. local = container_of(fq, struct ieee80211_local, fq);
  1149. ieee80211_free_txskb(&local->hw, skb);
  1150. }
  1151. static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
  1152. struct fq_tin *tin,
  1153. int idx,
  1154. struct sk_buff *skb)
  1155. {
  1156. struct txq_info *txqi;
  1157. txqi = container_of(tin, struct txq_info, tin);
  1158. return &txqi->def_flow;
  1159. }
  1160. static void ieee80211_txq_enqueue(struct ieee80211_local *local,
  1161. struct txq_info *txqi,
  1162. struct sk_buff *skb)
  1163. {
  1164. struct fq *fq = &local->fq;
  1165. struct fq_tin *tin = &txqi->tin;
  1166. ieee80211_set_skb_enqueue_time(skb);
  1167. fq_tin_enqueue(fq, tin, skb,
  1168. fq_skb_free_func,
  1169. fq_flow_get_default_func);
  1170. }
  1171. void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
  1172. struct sta_info *sta,
  1173. struct txq_info *txqi, int tid)
  1174. {
  1175. fq_tin_init(&txqi->tin);
  1176. fq_flow_init(&txqi->def_flow);
  1177. codel_vars_init(&txqi->def_cvars);
  1178. codel_stats_init(&txqi->cstats);
  1179. __skb_queue_head_init(&txqi->frags);
  1180. txqi->txq.vif = &sdata->vif;
  1181. if (sta) {
  1182. txqi->txq.sta = &sta->sta;
  1183. sta->sta.txq[tid] = &txqi->txq;
  1184. txqi->txq.tid = tid;
  1185. txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
  1186. } else {
  1187. sdata->vif.txq = &txqi->txq;
  1188. txqi->txq.tid = 0;
  1189. txqi->txq.ac = IEEE80211_AC_BE;
  1190. }
  1191. }
  1192. void ieee80211_txq_purge(struct ieee80211_local *local,
  1193. struct txq_info *txqi)
  1194. {
  1195. struct fq *fq = &local->fq;
  1196. struct fq_tin *tin = &txqi->tin;
  1197. fq_tin_reset(fq, tin, fq_skb_free_func);
  1198. ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
  1199. }
  1200. int ieee80211_txq_setup_flows(struct ieee80211_local *local)
  1201. {
  1202. struct fq *fq = &local->fq;
  1203. int ret;
  1204. int i;
  1205. bool supp_vht = false;
  1206. enum nl80211_band band;
  1207. if (!local->ops->wake_tx_queue)
  1208. return 0;
  1209. ret = fq_init(fq, 4096);
  1210. if (ret)
  1211. return ret;
  1212. /*
  1213. * If the hardware doesn't support VHT, it is safe to limit the maximum
  1214. * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
  1215. */
  1216. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1217. struct ieee80211_supported_band *sband;
  1218. sband = local->hw.wiphy->bands[band];
  1219. if (!sband)
  1220. continue;
  1221. supp_vht = supp_vht || sband->vht_cap.vht_supported;
  1222. }
  1223. if (!supp_vht)
  1224. fq->memory_limit = 4 << 20; /* 4 Mbytes */
  1225. codel_params_init(&local->cparams);
  1226. local->cparams.interval = MS2TIME(100);
  1227. local->cparams.target = MS2TIME(20);
  1228. local->cparams.ecn = true;
  1229. local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
  1230. GFP_KERNEL);
  1231. if (!local->cvars) {
  1232. spin_lock_bh(&fq->lock);
  1233. fq_reset(fq, fq_skb_free_func);
  1234. spin_unlock_bh(&fq->lock);
  1235. return -ENOMEM;
  1236. }
  1237. for (i = 0; i < fq->flows_cnt; i++)
  1238. codel_vars_init(&local->cvars[i]);
  1239. return 0;
  1240. }
  1241. void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
  1242. {
  1243. struct fq *fq = &local->fq;
  1244. if (!local->ops->wake_tx_queue)
  1245. return;
  1246. kfree(local->cvars);
  1247. local->cvars = NULL;
  1248. spin_lock_bh(&fq->lock);
  1249. fq_reset(fq, fq_skb_free_func);
  1250. spin_unlock_bh(&fq->lock);
  1251. }
  1252. static bool ieee80211_queue_skb(struct ieee80211_local *local,
  1253. struct ieee80211_sub_if_data *sdata,
  1254. struct sta_info *sta,
  1255. struct sk_buff *skb)
  1256. {
  1257. struct fq *fq = &local->fq;
  1258. struct ieee80211_vif *vif;
  1259. struct txq_info *txqi;
  1260. if (!local->ops->wake_tx_queue ||
  1261. sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1262. return false;
  1263. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1264. sdata = container_of(sdata->bss,
  1265. struct ieee80211_sub_if_data, u.ap);
  1266. vif = &sdata->vif;
  1267. txqi = ieee80211_get_txq(local, vif, sta, skb);
  1268. if (!txqi)
  1269. return false;
  1270. spin_lock_bh(&fq->lock);
  1271. ieee80211_txq_enqueue(local, txqi, skb);
  1272. spin_unlock_bh(&fq->lock);
  1273. drv_wake_tx_queue(local, txqi);
  1274. return true;
  1275. }
  1276. static bool ieee80211_tx_frags(struct ieee80211_local *local,
  1277. struct ieee80211_vif *vif,
  1278. struct ieee80211_sta *sta,
  1279. struct sk_buff_head *skbs,
  1280. bool txpending)
  1281. {
  1282. struct ieee80211_tx_control control = {};
  1283. struct sk_buff *skb, *tmp;
  1284. unsigned long flags;
  1285. skb_queue_walk_safe(skbs, skb, tmp) {
  1286. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1287. int q = info->hw_queue;
  1288. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1289. if (WARN_ON_ONCE(q >= local->hw.queues)) {
  1290. __skb_unlink(skb, skbs);
  1291. ieee80211_free_txskb(&local->hw, skb);
  1292. continue;
  1293. }
  1294. #endif
  1295. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1296. if (local->queue_stop_reasons[q] ||
  1297. (!txpending && !skb_queue_empty(&local->pending[q]))) {
  1298. if (unlikely(info->flags &
  1299. IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
  1300. if (local->queue_stop_reasons[q] &
  1301. ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
  1302. /*
  1303. * Drop off-channel frames if queues
  1304. * are stopped for any reason other
  1305. * than off-channel operation. Never
  1306. * queue them.
  1307. */
  1308. spin_unlock_irqrestore(
  1309. &local->queue_stop_reason_lock,
  1310. flags);
  1311. ieee80211_purge_tx_queue(&local->hw,
  1312. skbs);
  1313. return true;
  1314. }
  1315. } else {
  1316. /*
  1317. * Since queue is stopped, queue up frames for
  1318. * later transmission from the tx-pending
  1319. * tasklet when the queue is woken again.
  1320. */
  1321. if (txpending)
  1322. skb_queue_splice_init(skbs,
  1323. &local->pending[q]);
  1324. else
  1325. skb_queue_splice_tail_init(skbs,
  1326. &local->pending[q]);
  1327. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1328. flags);
  1329. return false;
  1330. }
  1331. }
  1332. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1333. info->control.vif = vif;
  1334. control.sta = sta;
  1335. __skb_unlink(skb, skbs);
  1336. drv_tx(local, &control, skb);
  1337. }
  1338. return true;
  1339. }
  1340. /*
  1341. * Returns false if the frame couldn't be transmitted but was queued instead.
  1342. */
  1343. static bool __ieee80211_tx(struct ieee80211_local *local,
  1344. struct sk_buff_head *skbs, int led_len,
  1345. struct sta_info *sta, bool txpending)
  1346. {
  1347. struct ieee80211_tx_info *info;
  1348. struct ieee80211_sub_if_data *sdata;
  1349. struct ieee80211_vif *vif;
  1350. struct ieee80211_sta *pubsta;
  1351. struct sk_buff *skb;
  1352. bool result = true;
  1353. __le16 fc;
  1354. if (WARN_ON(skb_queue_empty(skbs)))
  1355. return true;
  1356. skb = skb_peek(skbs);
  1357. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  1358. info = IEEE80211_SKB_CB(skb);
  1359. sdata = vif_to_sdata(info->control.vif);
  1360. if (sta && !sta->uploaded)
  1361. sta = NULL;
  1362. if (sta)
  1363. pubsta = &sta->sta;
  1364. else
  1365. pubsta = NULL;
  1366. switch (sdata->vif.type) {
  1367. case NL80211_IFTYPE_MONITOR:
  1368. if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
  1369. vif = &sdata->vif;
  1370. break;
  1371. }
  1372. sdata = rcu_dereference(local->monitor_sdata);
  1373. if (sdata) {
  1374. vif = &sdata->vif;
  1375. info->hw_queue =
  1376. vif->hw_queue[skb_get_queue_mapping(skb)];
  1377. } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  1378. ieee80211_purge_tx_queue(&local->hw, skbs);
  1379. return true;
  1380. } else
  1381. vif = NULL;
  1382. break;
  1383. case NL80211_IFTYPE_AP_VLAN:
  1384. sdata = container_of(sdata->bss,
  1385. struct ieee80211_sub_if_data, u.ap);
  1386. /* fall through */
  1387. default:
  1388. vif = &sdata->vif;
  1389. break;
  1390. }
  1391. result = ieee80211_tx_frags(local, vif, pubsta, skbs,
  1392. txpending);
  1393. ieee80211_tpt_led_trig_tx(local, fc, led_len);
  1394. WARN_ON_ONCE(!skb_queue_empty(skbs));
  1395. return result;
  1396. }
  1397. /*
  1398. * Invoke TX handlers, return 0 on success and non-zero if the
  1399. * frame was dropped or queued.
  1400. *
  1401. * The handlers are split into an early and late part. The latter is everything
  1402. * that can be sensitive to reordering, and will be deferred to after packets
  1403. * are dequeued from the intermediate queues (when they are enabled).
  1404. */
  1405. static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
  1406. {
  1407. ieee80211_tx_result res = TX_DROP;
  1408. #define CALL_TXH(txh) \
  1409. do { \
  1410. res = txh(tx); \
  1411. if (res != TX_CONTINUE) \
  1412. goto txh_done; \
  1413. } while (0)
  1414. CALL_TXH(ieee80211_tx_h_dynamic_ps);
  1415. CALL_TXH(ieee80211_tx_h_check_assoc);
  1416. CALL_TXH(ieee80211_tx_h_ps_buf);
  1417. CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
  1418. CALL_TXH(ieee80211_tx_h_select_key);
  1419. if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
  1420. CALL_TXH(ieee80211_tx_h_rate_ctrl);
  1421. txh_done:
  1422. if (unlikely(res == TX_DROP)) {
  1423. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1424. if (tx->skb)
  1425. ieee80211_free_txskb(&tx->local->hw, tx->skb);
  1426. else
  1427. ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
  1428. return -1;
  1429. } else if (unlikely(res == TX_QUEUED)) {
  1430. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1431. return -1;
  1432. }
  1433. return 0;
  1434. }
  1435. /*
  1436. * Late handlers can be called while the sta lock is held. Handlers that can
  1437. * cause packets to be generated will cause deadlock!
  1438. */
  1439. static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
  1440. {
  1441. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
  1442. ieee80211_tx_result res = TX_CONTINUE;
  1443. if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
  1444. __skb_queue_tail(&tx->skbs, tx->skb);
  1445. tx->skb = NULL;
  1446. goto txh_done;
  1447. }
  1448. CALL_TXH(ieee80211_tx_h_michael_mic_add);
  1449. CALL_TXH(ieee80211_tx_h_sequence);
  1450. CALL_TXH(ieee80211_tx_h_fragment);
  1451. /* handlers after fragment must be aware of tx info fragmentation! */
  1452. CALL_TXH(ieee80211_tx_h_stats);
  1453. CALL_TXH(ieee80211_tx_h_encrypt);
  1454. if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
  1455. CALL_TXH(ieee80211_tx_h_calculate_duration);
  1456. #undef CALL_TXH
  1457. txh_done:
  1458. if (unlikely(res == TX_DROP)) {
  1459. I802_DEBUG_INC(tx->local->tx_handlers_drop);
  1460. if (tx->skb)
  1461. ieee80211_free_txskb(&tx->local->hw, tx->skb);
  1462. else
  1463. ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
  1464. return -1;
  1465. } else if (unlikely(res == TX_QUEUED)) {
  1466. I802_DEBUG_INC(tx->local->tx_handlers_queued);
  1467. return -1;
  1468. }
  1469. return 0;
  1470. }
  1471. static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
  1472. {
  1473. int r = invoke_tx_handlers_early(tx);
  1474. if (r)
  1475. return r;
  1476. return invoke_tx_handlers_late(tx);
  1477. }
  1478. bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
  1479. struct ieee80211_vif *vif, struct sk_buff *skb,
  1480. int band, struct ieee80211_sta **sta)
  1481. {
  1482. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1483. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1484. struct ieee80211_tx_data tx;
  1485. struct sk_buff *skb2;
  1486. if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
  1487. return false;
  1488. info->band = band;
  1489. info->control.vif = vif;
  1490. info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
  1491. if (invoke_tx_handlers(&tx))
  1492. return false;
  1493. if (sta) {
  1494. if (tx.sta)
  1495. *sta = &tx.sta->sta;
  1496. else
  1497. *sta = NULL;
  1498. }
  1499. /* this function isn't suitable for fragmented data frames */
  1500. skb2 = __skb_dequeue(&tx.skbs);
  1501. if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
  1502. ieee80211_free_txskb(hw, skb2);
  1503. ieee80211_purge_tx_queue(hw, &tx.skbs);
  1504. return false;
  1505. }
  1506. return true;
  1507. }
  1508. EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
  1509. /*
  1510. * Returns false if the frame couldn't be transmitted but was queued instead.
  1511. */
  1512. static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
  1513. struct sta_info *sta, struct sk_buff *skb,
  1514. bool txpending)
  1515. {
  1516. struct ieee80211_local *local = sdata->local;
  1517. struct ieee80211_tx_data tx;
  1518. ieee80211_tx_result res_prepare;
  1519. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1520. bool result = true;
  1521. int led_len;
  1522. if (unlikely(skb->len < 10)) {
  1523. dev_kfree_skb(skb);
  1524. return true;
  1525. }
  1526. /* initialises tx */
  1527. led_len = skb->len;
  1528. res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
  1529. if (unlikely(res_prepare == TX_DROP)) {
  1530. ieee80211_free_txskb(&local->hw, skb);
  1531. return true;
  1532. } else if (unlikely(res_prepare == TX_QUEUED)) {
  1533. return true;
  1534. }
  1535. /* set up hw_queue value early */
  1536. if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
  1537. !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  1538. info->hw_queue =
  1539. sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
  1540. if (invoke_tx_handlers_early(&tx))
  1541. return true;
  1542. if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
  1543. return true;
  1544. if (!invoke_tx_handlers_late(&tx))
  1545. result = __ieee80211_tx(local, &tx.skbs, led_len,
  1546. tx.sta, txpending);
  1547. return result;
  1548. }
  1549. /* device xmit handlers */
  1550. static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
  1551. struct sk_buff *skb,
  1552. int head_need, bool may_encrypt)
  1553. {
  1554. struct ieee80211_local *local = sdata->local;
  1555. struct ieee80211_hdr *hdr;
  1556. bool enc_tailroom;
  1557. int tail_need = 0;
  1558. hdr = (struct ieee80211_hdr *) skb->data;
  1559. enc_tailroom = may_encrypt &&
  1560. (sdata->crypto_tx_tailroom_needed_cnt ||
  1561. ieee80211_is_mgmt(hdr->frame_control));
  1562. if (enc_tailroom) {
  1563. tail_need = IEEE80211_ENCRYPT_TAILROOM;
  1564. tail_need -= skb_tailroom(skb);
  1565. tail_need = max_t(int, tail_need, 0);
  1566. }
  1567. if (skb_cloned(skb) &&
  1568. (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
  1569. !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
  1570. I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
  1571. else if (head_need || tail_need)
  1572. I802_DEBUG_INC(local->tx_expand_skb_head);
  1573. else
  1574. return 0;
  1575. if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
  1576. wiphy_debug(local->hw.wiphy,
  1577. "failed to reallocate TX buffer\n");
  1578. return -ENOMEM;
  1579. }
  1580. return 0;
  1581. }
  1582. void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
  1583. struct sta_info *sta, struct sk_buff *skb)
  1584. {
  1585. struct ieee80211_local *local = sdata->local;
  1586. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1587. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1588. int headroom;
  1589. bool may_encrypt;
  1590. may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
  1591. headroom = local->tx_headroom;
  1592. if (may_encrypt)
  1593. headroom += sdata->encrypt_headroom;
  1594. headroom -= skb_headroom(skb);
  1595. headroom = max_t(int, 0, headroom);
  1596. if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
  1597. ieee80211_free_txskb(&local->hw, skb);
  1598. return;
  1599. }
  1600. hdr = (struct ieee80211_hdr *) skb->data;
  1601. info->control.vif = &sdata->vif;
  1602. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  1603. if (ieee80211_is_data(hdr->frame_control) &&
  1604. is_unicast_ether_addr(hdr->addr1)) {
  1605. if (mesh_nexthop_resolve(sdata, skb))
  1606. return; /* skb queued: don't free */
  1607. } else {
  1608. ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
  1609. }
  1610. }
  1611. ieee80211_set_qos_hdr(sdata, skb);
  1612. ieee80211_tx(sdata, sta, skb, false);
  1613. }
  1614. static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
  1615. struct sk_buff *skb)
  1616. {
  1617. struct ieee80211_radiotap_iterator iterator;
  1618. struct ieee80211_radiotap_header *rthdr =
  1619. (struct ieee80211_radiotap_header *) skb->data;
  1620. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1621. struct ieee80211_supported_band *sband =
  1622. local->hw.wiphy->bands[info->band];
  1623. int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
  1624. NULL);
  1625. u16 txflags;
  1626. u16 rate = 0;
  1627. bool rate_found = false;
  1628. u8 rate_retries = 0;
  1629. u16 rate_flags = 0;
  1630. u8 mcs_known, mcs_flags, mcs_bw;
  1631. u16 vht_known;
  1632. u8 vht_mcs = 0, vht_nss = 0;
  1633. int i;
  1634. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1635. IEEE80211_TX_CTL_DONTFRAG;
  1636. /*
  1637. * for every radiotap entry that is present
  1638. * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
  1639. * entries present, or -EINVAL on error)
  1640. */
  1641. while (!ret) {
  1642. ret = ieee80211_radiotap_iterator_next(&iterator);
  1643. if (ret)
  1644. continue;
  1645. /* see if this argument is something we can use */
  1646. switch (iterator.this_arg_index) {
  1647. /*
  1648. * You must take care when dereferencing iterator.this_arg
  1649. * for multibyte types... the pointer is not aligned. Use
  1650. * get_unaligned((type *)iterator.this_arg) to dereference
  1651. * iterator.this_arg for type "type" safely on all arches.
  1652. */
  1653. case IEEE80211_RADIOTAP_FLAGS:
  1654. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
  1655. /*
  1656. * this indicates that the skb we have been
  1657. * handed has the 32-bit FCS CRC at the end...
  1658. * we should react to that by snipping it off
  1659. * because it will be recomputed and added
  1660. * on transmission
  1661. */
  1662. if (skb->len < (iterator._max_length + FCS_LEN))
  1663. return false;
  1664. skb_trim(skb, skb->len - FCS_LEN);
  1665. }
  1666. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
  1667. info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1668. if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
  1669. info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
  1670. break;
  1671. case IEEE80211_RADIOTAP_TX_FLAGS:
  1672. txflags = get_unaligned_le16(iterator.this_arg);
  1673. if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
  1674. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  1675. break;
  1676. case IEEE80211_RADIOTAP_RATE:
  1677. rate = *iterator.this_arg;
  1678. rate_flags = 0;
  1679. rate_found = true;
  1680. break;
  1681. case IEEE80211_RADIOTAP_DATA_RETRIES:
  1682. rate_retries = *iterator.this_arg;
  1683. break;
  1684. case IEEE80211_RADIOTAP_MCS:
  1685. mcs_known = iterator.this_arg[0];
  1686. mcs_flags = iterator.this_arg[1];
  1687. if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
  1688. break;
  1689. rate_found = true;
  1690. rate = iterator.this_arg[2];
  1691. rate_flags = IEEE80211_TX_RC_MCS;
  1692. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
  1693. mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
  1694. rate_flags |= IEEE80211_TX_RC_SHORT_GI;
  1695. mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
  1696. if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
  1697. mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
  1698. rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1699. break;
  1700. case IEEE80211_RADIOTAP_VHT:
  1701. vht_known = get_unaligned_le16(iterator.this_arg);
  1702. rate_found = true;
  1703. rate_flags = IEEE80211_TX_RC_VHT_MCS;
  1704. if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
  1705. (iterator.this_arg[2] &
  1706. IEEE80211_RADIOTAP_VHT_FLAG_SGI))
  1707. rate_flags |= IEEE80211_TX_RC_SHORT_GI;
  1708. if (vht_known &
  1709. IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
  1710. if (iterator.this_arg[3] == 1)
  1711. rate_flags |=
  1712. IEEE80211_TX_RC_40_MHZ_WIDTH;
  1713. else if (iterator.this_arg[3] == 4)
  1714. rate_flags |=
  1715. IEEE80211_TX_RC_80_MHZ_WIDTH;
  1716. else if (iterator.this_arg[3] == 11)
  1717. rate_flags |=
  1718. IEEE80211_TX_RC_160_MHZ_WIDTH;
  1719. }
  1720. vht_mcs = iterator.this_arg[4] >> 4;
  1721. vht_nss = iterator.this_arg[4] & 0xF;
  1722. break;
  1723. /*
  1724. * Please update the file
  1725. * Documentation/networking/mac80211-injection.txt
  1726. * when parsing new fields here.
  1727. */
  1728. default:
  1729. break;
  1730. }
  1731. }
  1732. if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
  1733. return false;
  1734. if (rate_found) {
  1735. info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
  1736. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1737. info->control.rates[i].idx = -1;
  1738. info->control.rates[i].flags = 0;
  1739. info->control.rates[i].count = 0;
  1740. }
  1741. if (rate_flags & IEEE80211_TX_RC_MCS) {
  1742. info->control.rates[0].idx = rate;
  1743. } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
  1744. ieee80211_rate_set_vht(info->control.rates, vht_mcs,
  1745. vht_nss);
  1746. } else {
  1747. for (i = 0; i < sband->n_bitrates; i++) {
  1748. if (rate * 5 != sband->bitrates[i].bitrate)
  1749. continue;
  1750. info->control.rates[0].idx = i;
  1751. break;
  1752. }
  1753. }
  1754. if (info->control.rates[0].idx < 0)
  1755. info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
  1756. info->control.rates[0].flags = rate_flags;
  1757. info->control.rates[0].count = min_t(u8, rate_retries + 1,
  1758. local->hw.max_rate_tries);
  1759. }
  1760. /*
  1761. * remove the radiotap header
  1762. * iterator->_max_length was sanity-checked against
  1763. * skb->len by iterator init
  1764. */
  1765. skb_pull(skb, iterator._max_length);
  1766. return true;
  1767. }
  1768. netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
  1769. struct net_device *dev)
  1770. {
  1771. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1772. struct ieee80211_chanctx_conf *chanctx_conf;
  1773. struct ieee80211_radiotap_header *prthdr =
  1774. (struct ieee80211_radiotap_header *)skb->data;
  1775. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1776. struct ieee80211_hdr *hdr;
  1777. struct ieee80211_sub_if_data *tmp_sdata, *sdata;
  1778. struct cfg80211_chan_def *chandef;
  1779. u16 len_rthdr;
  1780. int hdrlen;
  1781. /* check for not even having the fixed radiotap header part */
  1782. if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
  1783. goto fail; /* too short to be possibly valid */
  1784. /* is it a header version we can trust to find length from? */
  1785. if (unlikely(prthdr->it_version))
  1786. goto fail; /* only version 0 is supported */
  1787. /* then there must be a radiotap header with a length we can use */
  1788. len_rthdr = ieee80211_get_radiotap_len(skb->data);
  1789. /* does the skb contain enough to deliver on the alleged length? */
  1790. if (unlikely(skb->len < len_rthdr))
  1791. goto fail; /* skb too short for claimed rt header extent */
  1792. /*
  1793. * fix up the pointers accounting for the radiotap
  1794. * header still being in there. We are being given
  1795. * a precooked IEEE80211 header so no need for
  1796. * normal processing
  1797. */
  1798. skb_set_mac_header(skb, len_rthdr);
  1799. /*
  1800. * these are just fixed to the end of the rt area since we
  1801. * don't have any better information and at this point, nobody cares
  1802. */
  1803. skb_set_network_header(skb, len_rthdr);
  1804. skb_set_transport_header(skb, len_rthdr);
  1805. if (skb->len < len_rthdr + 2)
  1806. goto fail;
  1807. hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
  1808. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1809. if (skb->len < len_rthdr + hdrlen)
  1810. goto fail;
  1811. /*
  1812. * Initialize skb->protocol if the injected frame is a data frame
  1813. * carrying a rfc1042 header
  1814. */
  1815. if (ieee80211_is_data(hdr->frame_control) &&
  1816. skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
  1817. u8 *payload = (u8 *)hdr + hdrlen;
  1818. if (ether_addr_equal(payload, rfc1042_header))
  1819. skb->protocol = cpu_to_be16((payload[6] << 8) |
  1820. payload[7]);
  1821. }
  1822. memset(info, 0, sizeof(*info));
  1823. info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1824. IEEE80211_TX_CTL_INJECTED;
  1825. rcu_read_lock();
  1826. /*
  1827. * We process outgoing injected frames that have a local address
  1828. * we handle as though they are non-injected frames.
  1829. * This code here isn't entirely correct, the local MAC address
  1830. * isn't always enough to find the interface to use; for proper
  1831. * VLAN/WDS support we will need a different mechanism (which
  1832. * likely isn't going to be monitor interfaces).
  1833. */
  1834. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1835. list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
  1836. if (!ieee80211_sdata_running(tmp_sdata))
  1837. continue;
  1838. if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1839. tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1840. tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
  1841. continue;
  1842. if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
  1843. sdata = tmp_sdata;
  1844. break;
  1845. }
  1846. }
  1847. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1848. if (!chanctx_conf) {
  1849. tmp_sdata = rcu_dereference(local->monitor_sdata);
  1850. if (tmp_sdata)
  1851. chanctx_conf =
  1852. rcu_dereference(tmp_sdata->vif.chanctx_conf);
  1853. }
  1854. if (chanctx_conf)
  1855. chandef = &chanctx_conf->def;
  1856. else if (!local->use_chanctx)
  1857. chandef = &local->_oper_chandef;
  1858. else
  1859. goto fail_rcu;
  1860. /*
  1861. * Frame injection is not allowed if beaconing is not allowed
  1862. * or if we need radar detection. Beaconing is usually not allowed when
  1863. * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
  1864. * Passive scan is also used in world regulatory domains where
  1865. * your country is not known and as such it should be treated as
  1866. * NO TX unless the channel is explicitly allowed in which case
  1867. * your current regulatory domain would not have the passive scan
  1868. * flag.
  1869. *
  1870. * Since AP mode uses monitor interfaces to inject/TX management
  1871. * frames we can make AP mode the exception to this rule once it
  1872. * supports radar detection as its implementation can deal with
  1873. * radar detection by itself. We can do that later by adding a
  1874. * monitor flag interfaces used for AP support.
  1875. */
  1876. if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
  1877. sdata->vif.type))
  1878. goto fail_rcu;
  1879. info->band = chandef->chan->band;
  1880. /* process and remove the injection radiotap header */
  1881. if (!ieee80211_parse_tx_radiotap(local, skb))
  1882. goto fail_rcu;
  1883. ieee80211_xmit(sdata, NULL, skb);
  1884. rcu_read_unlock();
  1885. return NETDEV_TX_OK;
  1886. fail_rcu:
  1887. rcu_read_unlock();
  1888. fail:
  1889. dev_kfree_skb(skb);
  1890. return NETDEV_TX_OK; /* meaning, we dealt with the skb */
  1891. }
  1892. static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
  1893. {
  1894. u16 ethertype = (skb->data[12] << 8) | skb->data[13];
  1895. return ethertype == ETH_P_TDLS &&
  1896. skb->len > 14 &&
  1897. skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
  1898. }
  1899. static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
  1900. struct sk_buff *skb,
  1901. struct sta_info **sta_out)
  1902. {
  1903. struct sta_info *sta;
  1904. switch (sdata->vif.type) {
  1905. case NL80211_IFTYPE_AP_VLAN:
  1906. sta = rcu_dereference(sdata->u.vlan.sta);
  1907. if (sta) {
  1908. *sta_out = sta;
  1909. return 0;
  1910. } else if (sdata->wdev.use_4addr) {
  1911. return -ENOLINK;
  1912. }
  1913. /* fall through */
  1914. case NL80211_IFTYPE_AP:
  1915. case NL80211_IFTYPE_OCB:
  1916. case NL80211_IFTYPE_ADHOC:
  1917. if (is_multicast_ether_addr(skb->data)) {
  1918. *sta_out = ERR_PTR(-ENOENT);
  1919. return 0;
  1920. }
  1921. sta = sta_info_get_bss(sdata, skb->data);
  1922. break;
  1923. case NL80211_IFTYPE_WDS:
  1924. sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
  1925. break;
  1926. #ifdef CONFIG_MAC80211_MESH
  1927. case NL80211_IFTYPE_MESH_POINT:
  1928. /* determined much later */
  1929. *sta_out = NULL;
  1930. return 0;
  1931. #endif
  1932. case NL80211_IFTYPE_STATION:
  1933. if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
  1934. sta = sta_info_get(sdata, skb->data);
  1935. if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
  1936. if (test_sta_flag(sta,
  1937. WLAN_STA_TDLS_PEER_AUTH)) {
  1938. *sta_out = sta;
  1939. return 0;
  1940. }
  1941. /*
  1942. * TDLS link during setup - throw out frames to
  1943. * peer. Allow TDLS-setup frames to unauthorized
  1944. * peers for the special case of a link teardown
  1945. * after a TDLS sta is removed due to being
  1946. * unreachable.
  1947. */
  1948. if (!ieee80211_is_tdls_setup(skb))
  1949. return -EINVAL;
  1950. }
  1951. }
  1952. sta = sta_info_get(sdata, sdata->u.mgd.bssid);
  1953. if (!sta)
  1954. return -ENOLINK;
  1955. break;
  1956. default:
  1957. return -EINVAL;
  1958. }
  1959. *sta_out = sta ?: ERR_PTR(-ENOENT);
  1960. return 0;
  1961. }
  1962. /**
  1963. * ieee80211_build_hdr - build 802.11 header in the given frame
  1964. * @sdata: virtual interface to build the header for
  1965. * @skb: the skb to build the header in
  1966. * @info_flags: skb flags to set
  1967. *
  1968. * This function takes the skb with 802.3 header and reformats the header to
  1969. * the appropriate IEEE 802.11 header based on which interface the packet is
  1970. * being transmitted on.
  1971. *
  1972. * Note that this function also takes care of the TX status request and
  1973. * potential unsharing of the SKB - this needs to be interleaved with the
  1974. * header building.
  1975. *
  1976. * The function requires the read-side RCU lock held
  1977. *
  1978. * Returns: the (possibly reallocated) skb or an ERR_PTR() code
  1979. */
  1980. static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
  1981. struct sk_buff *skb, u32 info_flags,
  1982. struct sta_info *sta)
  1983. {
  1984. struct ieee80211_local *local = sdata->local;
  1985. struct ieee80211_tx_info *info;
  1986. int head_need;
  1987. u16 ethertype, hdrlen, meshhdrlen = 0;
  1988. __le16 fc;
  1989. struct ieee80211_hdr hdr;
  1990. struct ieee80211s_hdr mesh_hdr __maybe_unused;
  1991. struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
  1992. const u8 *encaps_data;
  1993. int encaps_len, skip_header_bytes;
  1994. bool wme_sta = false, authorized = false;
  1995. bool tdls_peer;
  1996. bool multicast;
  1997. u16 info_id = 0;
  1998. struct ieee80211_chanctx_conf *chanctx_conf;
  1999. struct ieee80211_sub_if_data *ap_sdata;
  2000. enum nl80211_band band;
  2001. int ret;
  2002. if (IS_ERR(sta))
  2003. sta = NULL;
  2004. /* convert Ethernet header to proper 802.11 header (based on
  2005. * operation mode) */
  2006. ethertype = (skb->data[12] << 8) | skb->data[13];
  2007. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  2008. switch (sdata->vif.type) {
  2009. case NL80211_IFTYPE_AP_VLAN:
  2010. if (sdata->wdev.use_4addr) {
  2011. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  2012. /* RA TA DA SA */
  2013. memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
  2014. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2015. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2016. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  2017. hdrlen = 30;
  2018. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  2019. wme_sta = sta->sta.wme;
  2020. }
  2021. ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2022. u.ap);
  2023. chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
  2024. if (!chanctx_conf) {
  2025. ret = -ENOTCONN;
  2026. goto free;
  2027. }
  2028. band = chanctx_conf->def.chan->band;
  2029. if (sdata->wdev.use_4addr)
  2030. break;
  2031. /* fall through */
  2032. case NL80211_IFTYPE_AP:
  2033. if (sdata->vif.type == NL80211_IFTYPE_AP)
  2034. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2035. if (!chanctx_conf) {
  2036. ret = -ENOTCONN;
  2037. goto free;
  2038. }
  2039. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  2040. /* DA BSSID SA */
  2041. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2042. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2043. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  2044. hdrlen = 24;
  2045. band = chanctx_conf->def.chan->band;
  2046. break;
  2047. case NL80211_IFTYPE_WDS:
  2048. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  2049. /* RA TA DA SA */
  2050. memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
  2051. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2052. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2053. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  2054. hdrlen = 30;
  2055. /*
  2056. * This is the exception! WDS style interfaces are prohibited
  2057. * when channel contexts are in used so this must be valid
  2058. */
  2059. band = local->hw.conf.chandef.chan->band;
  2060. break;
  2061. #ifdef CONFIG_MAC80211_MESH
  2062. case NL80211_IFTYPE_MESH_POINT:
  2063. if (!is_multicast_ether_addr(skb->data)) {
  2064. struct sta_info *next_hop;
  2065. bool mpp_lookup = true;
  2066. mpath = mesh_path_lookup(sdata, skb->data);
  2067. if (mpath) {
  2068. mpp_lookup = false;
  2069. next_hop = rcu_dereference(mpath->next_hop);
  2070. if (!next_hop ||
  2071. !(mpath->flags & (MESH_PATH_ACTIVE |
  2072. MESH_PATH_RESOLVING)))
  2073. mpp_lookup = true;
  2074. }
  2075. if (mpp_lookup) {
  2076. mppath = mpp_path_lookup(sdata, skb->data);
  2077. if (mppath)
  2078. mppath->exp_time = jiffies;
  2079. }
  2080. if (mppath && mpath)
  2081. mesh_path_del(sdata, mpath->dst);
  2082. }
  2083. /*
  2084. * Use address extension if it is a packet from
  2085. * another interface or if we know the destination
  2086. * is being proxied by a portal (i.e. portal address
  2087. * differs from proxied address)
  2088. */
  2089. if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
  2090. !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
  2091. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  2092. skb->data, skb->data + ETH_ALEN);
  2093. meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
  2094. NULL, NULL);
  2095. } else {
  2096. /* DS -> MBSS (802.11-2012 13.11.3.3).
  2097. * For unicast with unknown forwarding information,
  2098. * destination might be in the MBSS or if that fails
  2099. * forwarded to another mesh gate. In either case
  2100. * resolution will be handled in ieee80211_xmit(), so
  2101. * leave the original DA. This also works for mcast */
  2102. const u8 *mesh_da = skb->data;
  2103. if (mppath)
  2104. mesh_da = mppath->mpp;
  2105. else if (mpath)
  2106. mesh_da = mpath->dst;
  2107. hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
  2108. mesh_da, sdata->vif.addr);
  2109. if (is_multicast_ether_addr(mesh_da))
  2110. /* DA TA mSA AE:SA */
  2111. meshhdrlen = ieee80211_new_mesh_header(
  2112. sdata, &mesh_hdr,
  2113. skb->data + ETH_ALEN, NULL);
  2114. else
  2115. /* RA TA mDA mSA AE:DA SA */
  2116. meshhdrlen = ieee80211_new_mesh_header(
  2117. sdata, &mesh_hdr, skb->data,
  2118. skb->data + ETH_ALEN);
  2119. }
  2120. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2121. if (!chanctx_conf) {
  2122. ret = -ENOTCONN;
  2123. goto free;
  2124. }
  2125. band = chanctx_conf->def.chan->band;
  2126. break;
  2127. #endif
  2128. case NL80211_IFTYPE_STATION:
  2129. /* we already did checks when looking up the RA STA */
  2130. tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
  2131. if (tdls_peer) {
  2132. /* DA SA BSSID */
  2133. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2134. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2135. memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
  2136. hdrlen = 24;
  2137. } else if (sdata->u.mgd.use_4addr &&
  2138. cpu_to_be16(ethertype) != sdata->control_port_protocol) {
  2139. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2140. IEEE80211_FCTL_TODS);
  2141. /* RA TA DA SA */
  2142. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  2143. memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
  2144. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2145. memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
  2146. hdrlen = 30;
  2147. } else {
  2148. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  2149. /* BSSID SA DA */
  2150. memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
  2151. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2152. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  2153. hdrlen = 24;
  2154. }
  2155. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2156. if (!chanctx_conf) {
  2157. ret = -ENOTCONN;
  2158. goto free;
  2159. }
  2160. band = chanctx_conf->def.chan->band;
  2161. break;
  2162. case NL80211_IFTYPE_OCB:
  2163. /* DA SA BSSID */
  2164. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2165. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2166. eth_broadcast_addr(hdr.addr3);
  2167. hdrlen = 24;
  2168. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2169. if (!chanctx_conf) {
  2170. ret = -ENOTCONN;
  2171. goto free;
  2172. }
  2173. band = chanctx_conf->def.chan->band;
  2174. break;
  2175. case NL80211_IFTYPE_ADHOC:
  2176. /* DA SA BSSID */
  2177. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  2178. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  2179. memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
  2180. hdrlen = 24;
  2181. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2182. if (!chanctx_conf) {
  2183. ret = -ENOTCONN;
  2184. goto free;
  2185. }
  2186. band = chanctx_conf->def.chan->band;
  2187. break;
  2188. default:
  2189. ret = -EINVAL;
  2190. goto free;
  2191. }
  2192. multicast = is_multicast_ether_addr(hdr.addr1);
  2193. /* sta is always NULL for mesh */
  2194. if (sta) {
  2195. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  2196. wme_sta = sta->sta.wme;
  2197. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2198. /* For mesh, the use of the QoS header is mandatory */
  2199. wme_sta = true;
  2200. }
  2201. /* receiver does QoS (which also means we do) use it */
  2202. if (wme_sta) {
  2203. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2204. hdrlen += 2;
  2205. }
  2206. /*
  2207. * Drop unicast frames to unauthorised stations unless they are
  2208. * EAPOL frames from the local station.
  2209. */
  2210. if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
  2211. (sdata->vif.type != NL80211_IFTYPE_OCB) &&
  2212. !multicast && !authorized &&
  2213. (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
  2214. !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
  2215. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2216. net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
  2217. sdata->name, hdr.addr1);
  2218. #endif
  2219. I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
  2220. ret = -EPERM;
  2221. goto free;
  2222. }
  2223. if (unlikely(!multicast && skb->sk &&
  2224. skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
  2225. struct sk_buff *ack_skb = skb_clone_sk(skb);
  2226. if (ack_skb) {
  2227. unsigned long flags;
  2228. int id;
  2229. spin_lock_irqsave(&local->ack_status_lock, flags);
  2230. id = idr_alloc(&local->ack_status_frames, ack_skb,
  2231. 1, 0x10000, GFP_ATOMIC);
  2232. spin_unlock_irqrestore(&local->ack_status_lock, flags);
  2233. if (id >= 0) {
  2234. info_id = id;
  2235. info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  2236. } else {
  2237. kfree_skb(ack_skb);
  2238. }
  2239. }
  2240. }
  2241. /*
  2242. * If the skb is shared we need to obtain our own copy.
  2243. */
  2244. if (skb_shared(skb)) {
  2245. struct sk_buff *tmp_skb = skb;
  2246. /* can't happen -- skb is a clone if info_id != 0 */
  2247. WARN_ON(info_id);
  2248. skb = skb_clone(skb, GFP_ATOMIC);
  2249. kfree_skb(tmp_skb);
  2250. if (!skb) {
  2251. ret = -ENOMEM;
  2252. goto free;
  2253. }
  2254. }
  2255. hdr.frame_control = fc;
  2256. hdr.duration_id = 0;
  2257. hdr.seq_ctrl = 0;
  2258. skip_header_bytes = ETH_HLEN;
  2259. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  2260. encaps_data = bridge_tunnel_header;
  2261. encaps_len = sizeof(bridge_tunnel_header);
  2262. skip_header_bytes -= 2;
  2263. } else if (ethertype >= ETH_P_802_3_MIN) {
  2264. encaps_data = rfc1042_header;
  2265. encaps_len = sizeof(rfc1042_header);
  2266. skip_header_bytes -= 2;
  2267. } else {
  2268. encaps_data = NULL;
  2269. encaps_len = 0;
  2270. }
  2271. skb_pull(skb, skip_header_bytes);
  2272. head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
  2273. /*
  2274. * So we need to modify the skb header and hence need a copy of
  2275. * that. The head_need variable above doesn't, so far, include
  2276. * the needed header space that we don't need right away. If we
  2277. * can, then we don't reallocate right now but only after the
  2278. * frame arrives at the master device (if it does...)
  2279. *
  2280. * If we cannot, however, then we will reallocate to include all
  2281. * the ever needed space. Also, if we need to reallocate it anyway,
  2282. * make it big enough for everything we may ever need.
  2283. */
  2284. if (head_need > 0 || skb_cloned(skb)) {
  2285. head_need += sdata->encrypt_headroom;
  2286. head_need += local->tx_headroom;
  2287. head_need = max_t(int, 0, head_need);
  2288. if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
  2289. ieee80211_free_txskb(&local->hw, skb);
  2290. skb = NULL;
  2291. return ERR_PTR(-ENOMEM);
  2292. }
  2293. }
  2294. if (encaps_data)
  2295. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  2296. #ifdef CONFIG_MAC80211_MESH
  2297. if (meshhdrlen > 0)
  2298. memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
  2299. #endif
  2300. if (ieee80211_is_data_qos(fc)) {
  2301. __le16 *qos_control;
  2302. qos_control = (__le16 *) skb_push(skb, 2);
  2303. memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
  2304. /*
  2305. * Maybe we could actually set some fields here, for now just
  2306. * initialise to zero to indicate no special operation.
  2307. */
  2308. *qos_control = 0;
  2309. } else
  2310. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  2311. skb_reset_mac_header(skb);
  2312. info = IEEE80211_SKB_CB(skb);
  2313. memset(info, 0, sizeof(*info));
  2314. info->flags = info_flags;
  2315. info->ack_frame_id = info_id;
  2316. info->band = band;
  2317. return skb;
  2318. free:
  2319. kfree_skb(skb);
  2320. return ERR_PTR(ret);
  2321. }
  2322. /*
  2323. * fast-xmit overview
  2324. *
  2325. * The core idea of this fast-xmit is to remove per-packet checks by checking
  2326. * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
  2327. * checks that are needed to get the sta->fast_tx pointer assigned, after which
  2328. * much less work can be done per packet. For example, fragmentation must be
  2329. * disabled or the fast_tx pointer will not be set. All the conditions are seen
  2330. * in the code here.
  2331. *
  2332. * Once assigned, the fast_tx data structure also caches the per-packet 802.11
  2333. * header and other data to aid packet processing in ieee80211_xmit_fast().
  2334. *
  2335. * The most difficult part of this is that when any of these assumptions
  2336. * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
  2337. * ieee80211_check_fast_xmit() or friends) is required to reset the data,
  2338. * since the per-packet code no longer checks the conditions. This is reflected
  2339. * by the calls to these functions throughout the rest of the code, and must be
  2340. * maintained if any of the TX path checks change.
  2341. */
  2342. void ieee80211_check_fast_xmit(struct sta_info *sta)
  2343. {
  2344. struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
  2345. struct ieee80211_local *local = sta->local;
  2346. struct ieee80211_sub_if_data *sdata = sta->sdata;
  2347. struct ieee80211_hdr *hdr = (void *)build.hdr;
  2348. struct ieee80211_chanctx_conf *chanctx_conf;
  2349. __le16 fc;
  2350. if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
  2351. return;
  2352. /* Locking here protects both the pointer itself, and against concurrent
  2353. * invocations winning data access races to, e.g., the key pointer that
  2354. * is used.
  2355. * Without it, the invocation of this function right after the key
  2356. * pointer changes wouldn't be sufficient, as another CPU could access
  2357. * the pointer, then stall, and then do the cache update after the CPU
  2358. * that invalidated the key.
  2359. * With the locking, such scenarios cannot happen as the check for the
  2360. * key and the fast-tx assignment are done atomically, so the CPU that
  2361. * modifies the key will either wait or other one will see the key
  2362. * cleared/changed already.
  2363. */
  2364. spin_lock_bh(&sta->lock);
  2365. if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
  2366. !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
  2367. sdata->vif.type == NL80211_IFTYPE_STATION)
  2368. goto out;
  2369. if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  2370. goto out;
  2371. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  2372. test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
  2373. test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
  2374. test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
  2375. goto out;
  2376. if (sdata->noack_map)
  2377. goto out;
  2378. /* fast-xmit doesn't handle fragmentation at all */
  2379. if (local->hw.wiphy->frag_threshold != (u32)-1 &&
  2380. !local->ops->set_frag_threshold)
  2381. goto out;
  2382. rcu_read_lock();
  2383. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2384. if (!chanctx_conf) {
  2385. rcu_read_unlock();
  2386. goto out;
  2387. }
  2388. build.band = chanctx_conf->def.chan->band;
  2389. rcu_read_unlock();
  2390. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  2391. switch (sdata->vif.type) {
  2392. case NL80211_IFTYPE_ADHOC:
  2393. /* DA SA BSSID */
  2394. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2395. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2396. memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
  2397. build.hdr_len = 24;
  2398. break;
  2399. case NL80211_IFTYPE_STATION:
  2400. if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
  2401. /* DA SA BSSID */
  2402. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2403. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2404. memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  2405. build.hdr_len = 24;
  2406. break;
  2407. }
  2408. if (sdata->u.mgd.use_4addr) {
  2409. /* non-regular ethertype cannot use the fastpath */
  2410. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2411. IEEE80211_FCTL_TODS);
  2412. /* RA TA DA SA */
  2413. memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  2414. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2415. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2416. build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  2417. build.hdr_len = 30;
  2418. break;
  2419. }
  2420. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  2421. /* BSSID SA DA */
  2422. memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  2423. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2424. build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  2425. build.hdr_len = 24;
  2426. break;
  2427. case NL80211_IFTYPE_AP_VLAN:
  2428. if (sdata->wdev.use_4addr) {
  2429. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  2430. IEEE80211_FCTL_TODS);
  2431. /* RA TA DA SA */
  2432. memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
  2433. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2434. build.da_offs = offsetof(struct ieee80211_hdr, addr3);
  2435. build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  2436. build.hdr_len = 30;
  2437. break;
  2438. }
  2439. /* fall through */
  2440. case NL80211_IFTYPE_AP:
  2441. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  2442. /* DA BSSID SA */
  2443. build.da_offs = offsetof(struct ieee80211_hdr, addr1);
  2444. memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2445. build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
  2446. build.hdr_len = 24;
  2447. break;
  2448. default:
  2449. /* not handled on fast-xmit */
  2450. goto out;
  2451. }
  2452. if (sta->sta.wme) {
  2453. build.hdr_len += 2;
  2454. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2455. }
  2456. /* We store the key here so there's no point in using rcu_dereference()
  2457. * but that's fine because the code that changes the pointers will call
  2458. * this function after doing so. For a single CPU that would be enough,
  2459. * for multiple see the comment above.
  2460. */
  2461. build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
  2462. if (!build.key)
  2463. build.key = rcu_access_pointer(sdata->default_unicast_key);
  2464. if (build.key) {
  2465. bool gen_iv, iv_spc, mmic;
  2466. gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
  2467. iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  2468. mmic = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2469. /* don't handle software crypto */
  2470. if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  2471. goto out;
  2472. switch (build.key->conf.cipher) {
  2473. case WLAN_CIPHER_SUITE_CCMP:
  2474. case WLAN_CIPHER_SUITE_CCMP_256:
  2475. /* add fixed key ID */
  2476. if (gen_iv) {
  2477. (build.hdr + build.hdr_len)[3] =
  2478. 0x20 | (build.key->conf.keyidx << 6);
  2479. build.pn_offs = build.hdr_len;
  2480. }
  2481. if (gen_iv || iv_spc)
  2482. build.hdr_len += IEEE80211_CCMP_HDR_LEN;
  2483. break;
  2484. case WLAN_CIPHER_SUITE_GCMP:
  2485. case WLAN_CIPHER_SUITE_GCMP_256:
  2486. /* add fixed key ID */
  2487. if (gen_iv) {
  2488. (build.hdr + build.hdr_len)[3] =
  2489. 0x20 | (build.key->conf.keyidx << 6);
  2490. build.pn_offs = build.hdr_len;
  2491. }
  2492. if (gen_iv || iv_spc)
  2493. build.hdr_len += IEEE80211_GCMP_HDR_LEN;
  2494. break;
  2495. case WLAN_CIPHER_SUITE_TKIP:
  2496. /* cannot handle MMIC or IV generation in xmit-fast */
  2497. if (mmic || gen_iv)
  2498. goto out;
  2499. if (iv_spc)
  2500. build.hdr_len += IEEE80211_TKIP_IV_LEN;
  2501. break;
  2502. case WLAN_CIPHER_SUITE_WEP40:
  2503. case WLAN_CIPHER_SUITE_WEP104:
  2504. /* cannot handle IV generation in fast-xmit */
  2505. if (gen_iv)
  2506. goto out;
  2507. if (iv_spc)
  2508. build.hdr_len += IEEE80211_WEP_IV_LEN;
  2509. break;
  2510. case WLAN_CIPHER_SUITE_AES_CMAC:
  2511. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  2512. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  2513. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  2514. WARN(1,
  2515. "management cipher suite 0x%x enabled for data\n",
  2516. build.key->conf.cipher);
  2517. goto out;
  2518. default:
  2519. /* we don't know how to generate IVs for this at all */
  2520. if (WARN_ON(gen_iv))
  2521. goto out;
  2522. /* pure hardware keys are OK, of course */
  2523. if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
  2524. break;
  2525. /* cipher scheme might require space allocation */
  2526. if (iv_spc &&
  2527. build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
  2528. goto out;
  2529. if (iv_spc)
  2530. build.hdr_len += build.key->conf.iv_len;
  2531. }
  2532. fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  2533. }
  2534. hdr->frame_control = fc;
  2535. memcpy(build.hdr + build.hdr_len,
  2536. rfc1042_header, sizeof(rfc1042_header));
  2537. build.hdr_len += sizeof(rfc1042_header);
  2538. fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
  2539. /* if the kmemdup fails, continue w/o fast_tx */
  2540. if (!fast_tx)
  2541. goto out;
  2542. out:
  2543. /* we might have raced against another call to this function */
  2544. old = rcu_dereference_protected(sta->fast_tx,
  2545. lockdep_is_held(&sta->lock));
  2546. rcu_assign_pointer(sta->fast_tx, fast_tx);
  2547. if (old)
  2548. kfree_rcu(old, rcu_head);
  2549. spin_unlock_bh(&sta->lock);
  2550. }
  2551. void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
  2552. {
  2553. struct sta_info *sta;
  2554. rcu_read_lock();
  2555. list_for_each_entry_rcu(sta, &local->sta_list, list)
  2556. ieee80211_check_fast_xmit(sta);
  2557. rcu_read_unlock();
  2558. }
  2559. void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
  2560. {
  2561. struct ieee80211_local *local = sdata->local;
  2562. struct sta_info *sta;
  2563. rcu_read_lock();
  2564. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  2565. if (sdata != sta->sdata &&
  2566. (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
  2567. continue;
  2568. ieee80211_check_fast_xmit(sta);
  2569. }
  2570. rcu_read_unlock();
  2571. }
  2572. void ieee80211_clear_fast_xmit(struct sta_info *sta)
  2573. {
  2574. struct ieee80211_fast_tx *fast_tx;
  2575. spin_lock_bh(&sta->lock);
  2576. fast_tx = rcu_dereference_protected(sta->fast_tx,
  2577. lockdep_is_held(&sta->lock));
  2578. RCU_INIT_POINTER(sta->fast_tx, NULL);
  2579. spin_unlock_bh(&sta->lock);
  2580. if (fast_tx)
  2581. kfree_rcu(fast_tx, rcu_head);
  2582. }
  2583. static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
  2584. struct sk_buff *skb, int headroom,
  2585. int *subframe_len)
  2586. {
  2587. int amsdu_len = *subframe_len + sizeof(struct ethhdr);
  2588. int padding = (4 - amsdu_len) & 3;
  2589. if (skb_headroom(skb) < headroom || skb_tailroom(skb) < padding) {
  2590. I802_DEBUG_INC(local->tx_expand_skb_head);
  2591. if (pskb_expand_head(skb, headroom, padding, GFP_ATOMIC)) {
  2592. wiphy_debug(local->hw.wiphy,
  2593. "failed to reallocate TX buffer\n");
  2594. return false;
  2595. }
  2596. }
  2597. if (padding) {
  2598. *subframe_len += padding;
  2599. memset(skb_put(skb, padding), 0, padding);
  2600. }
  2601. return true;
  2602. }
  2603. static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
  2604. struct ieee80211_fast_tx *fast_tx,
  2605. struct sk_buff *skb)
  2606. {
  2607. struct ieee80211_local *local = sdata->local;
  2608. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2609. struct ieee80211_hdr *hdr;
  2610. struct ethhdr amsdu_hdr;
  2611. int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
  2612. int subframe_len = skb->len - hdr_len;
  2613. void *data;
  2614. u8 *qc;
  2615. if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
  2616. return false;
  2617. if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
  2618. return true;
  2619. if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(amsdu_hdr),
  2620. &subframe_len))
  2621. return false;
  2622. amsdu_hdr.h_proto = cpu_to_be16(subframe_len);
  2623. memcpy(amsdu_hdr.h_source, skb->data + fast_tx->sa_offs, ETH_ALEN);
  2624. memcpy(amsdu_hdr.h_dest, skb->data + fast_tx->da_offs, ETH_ALEN);
  2625. data = skb_push(skb, sizeof(amsdu_hdr));
  2626. memmove(data, data + sizeof(amsdu_hdr), hdr_len);
  2627. memcpy(data + hdr_len, &amsdu_hdr, sizeof(amsdu_hdr));
  2628. hdr = data;
  2629. qc = ieee80211_get_qos_ctl(hdr);
  2630. *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  2631. info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
  2632. return true;
  2633. }
  2634. static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
  2635. struct sta_info *sta,
  2636. struct ieee80211_fast_tx *fast_tx,
  2637. struct sk_buff *skb)
  2638. {
  2639. struct ieee80211_local *local = sdata->local;
  2640. struct fq *fq = &local->fq;
  2641. struct fq_tin *tin;
  2642. struct fq_flow *flow;
  2643. u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  2644. struct ieee80211_txq *txq = sta->sta.txq[tid];
  2645. struct txq_info *txqi;
  2646. struct sk_buff **frag_tail, *head;
  2647. int subframe_len = skb->len - ETH_ALEN;
  2648. u8 max_subframes = sta->sta.max_amsdu_subframes;
  2649. int max_frags = local->hw.max_tx_fragments;
  2650. int max_amsdu_len = sta->sta.max_amsdu_len;
  2651. __be16 len;
  2652. void *data;
  2653. bool ret = false;
  2654. unsigned int orig_len;
  2655. int n = 1, nfrags;
  2656. if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
  2657. return false;
  2658. if (!txq)
  2659. return false;
  2660. txqi = to_txq_info(txq);
  2661. if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
  2662. return false;
  2663. if (sta->sta.max_rc_amsdu_len)
  2664. max_amsdu_len = min_t(int, max_amsdu_len,
  2665. sta->sta.max_rc_amsdu_len);
  2666. spin_lock_bh(&fq->lock);
  2667. /* TODO: Ideally aggregation should be done on dequeue to remain
  2668. * responsive to environment changes.
  2669. */
  2670. tin = &txqi->tin;
  2671. flow = fq_flow_classify(fq, tin, skb, fq_flow_get_default_func);
  2672. head = skb_peek_tail(&flow->queue);
  2673. if (!head)
  2674. goto out;
  2675. orig_len = head->len;
  2676. if (skb->len + head->len > max_amsdu_len)
  2677. goto out;
  2678. if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
  2679. goto out;
  2680. nfrags = 1 + skb_shinfo(skb)->nr_frags;
  2681. nfrags += 1 + skb_shinfo(head)->nr_frags;
  2682. frag_tail = &skb_shinfo(head)->frag_list;
  2683. while (*frag_tail) {
  2684. nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
  2685. frag_tail = &(*frag_tail)->next;
  2686. n++;
  2687. }
  2688. if (max_subframes && n > max_subframes)
  2689. goto out;
  2690. if (max_frags && nfrags > max_frags)
  2691. goto out;
  2692. if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) + 2,
  2693. &subframe_len))
  2694. goto out;
  2695. ret = true;
  2696. data = skb_push(skb, ETH_ALEN + 2);
  2697. memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
  2698. data += 2 * ETH_ALEN;
  2699. len = cpu_to_be16(subframe_len);
  2700. memcpy(data, &len, 2);
  2701. memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
  2702. head->len += skb->len;
  2703. head->data_len += skb->len;
  2704. *frag_tail = skb;
  2705. flow->backlog += head->len - orig_len;
  2706. tin->backlog_bytes += head->len - orig_len;
  2707. fq_recalc_backlog(fq, tin, flow);
  2708. out:
  2709. spin_unlock_bh(&fq->lock);
  2710. return ret;
  2711. }
  2712. /*
  2713. * Can be called while the sta lock is held. Anything that can cause packets to
  2714. * be generated will cause deadlock!
  2715. */
  2716. static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
  2717. struct sta_info *sta, u8 pn_offs,
  2718. struct ieee80211_key *key,
  2719. struct sk_buff *skb)
  2720. {
  2721. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2722. struct ieee80211_hdr *hdr = (void *)skb->data;
  2723. u8 tid = IEEE80211_NUM_TIDS;
  2724. if (key)
  2725. info->control.hw_key = &key->conf;
  2726. ieee80211_tx_stats(skb->dev, skb->len);
  2727. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  2728. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  2729. hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
  2730. } else {
  2731. info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
  2732. hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
  2733. sdata->sequence_number += 0x10;
  2734. }
  2735. if (skb_shinfo(skb)->gso_size)
  2736. sta->tx_stats.msdu[tid] +=
  2737. DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
  2738. else
  2739. sta->tx_stats.msdu[tid]++;
  2740. info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
  2741. /* statistics normally done by ieee80211_tx_h_stats (but that
  2742. * has to consider fragmentation, so is more complex)
  2743. */
  2744. sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
  2745. sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
  2746. if (pn_offs) {
  2747. u64 pn;
  2748. u8 *crypto_hdr = skb->data + pn_offs;
  2749. switch (key->conf.cipher) {
  2750. case WLAN_CIPHER_SUITE_CCMP:
  2751. case WLAN_CIPHER_SUITE_CCMP_256:
  2752. case WLAN_CIPHER_SUITE_GCMP:
  2753. case WLAN_CIPHER_SUITE_GCMP_256:
  2754. pn = atomic64_inc_return(&key->conf.tx_pn);
  2755. crypto_hdr[0] = pn;
  2756. crypto_hdr[1] = pn >> 8;
  2757. crypto_hdr[4] = pn >> 16;
  2758. crypto_hdr[5] = pn >> 24;
  2759. crypto_hdr[6] = pn >> 32;
  2760. crypto_hdr[7] = pn >> 40;
  2761. break;
  2762. }
  2763. }
  2764. }
  2765. static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
  2766. struct sta_info *sta,
  2767. struct ieee80211_fast_tx *fast_tx,
  2768. struct sk_buff *skb)
  2769. {
  2770. struct ieee80211_local *local = sdata->local;
  2771. u16 ethertype = (skb->data[12] << 8) | skb->data[13];
  2772. int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
  2773. int hw_headroom = sdata->local->hw.extra_tx_headroom;
  2774. struct ethhdr eth;
  2775. struct ieee80211_tx_info *info;
  2776. struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
  2777. struct ieee80211_tx_data tx;
  2778. ieee80211_tx_result r;
  2779. struct tid_ampdu_tx *tid_tx = NULL;
  2780. u8 tid = IEEE80211_NUM_TIDS;
  2781. /* control port protocol needs a lot of special handling */
  2782. if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
  2783. return false;
  2784. /* only RFC 1042 SNAP */
  2785. if (ethertype < ETH_P_802_3_MIN)
  2786. return false;
  2787. /* don't handle TX status request here either */
  2788. if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
  2789. return false;
  2790. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  2791. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  2792. tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
  2793. if (tid_tx) {
  2794. if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
  2795. return false;
  2796. if (tid_tx->timeout)
  2797. tid_tx->last_tx = jiffies;
  2798. }
  2799. }
  2800. /* after this point (skb is modified) we cannot return false */
  2801. if (skb_shared(skb)) {
  2802. struct sk_buff *tmp_skb = skb;
  2803. skb = skb_clone(skb, GFP_ATOMIC);
  2804. kfree_skb(tmp_skb);
  2805. if (!skb)
  2806. return true;
  2807. }
  2808. if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
  2809. ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
  2810. return true;
  2811. /* will not be crypto-handled beyond what we do here, so use false
  2812. * as the may-encrypt argument for the resize to not account for
  2813. * more room than we already have in 'extra_head'
  2814. */
  2815. if (unlikely(ieee80211_skb_resize(sdata, skb,
  2816. max_t(int, extra_head + hw_headroom -
  2817. skb_headroom(skb), 0),
  2818. false))) {
  2819. kfree_skb(skb);
  2820. return true;
  2821. }
  2822. memcpy(&eth, skb->data, ETH_HLEN - 2);
  2823. hdr = (void *)skb_push(skb, extra_head);
  2824. memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
  2825. memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
  2826. memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
  2827. info = IEEE80211_SKB_CB(skb);
  2828. memset(info, 0, sizeof(*info));
  2829. info->band = fast_tx->band;
  2830. info->control.vif = &sdata->vif;
  2831. info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
  2832. IEEE80211_TX_CTL_DONTFRAG |
  2833. (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
  2834. info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
  2835. if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
  2836. tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
  2837. *ieee80211_get_qos_ctl(hdr) = tid;
  2838. }
  2839. __skb_queue_head_init(&tx.skbs);
  2840. tx.flags = IEEE80211_TX_UNICAST;
  2841. tx.local = local;
  2842. tx.sdata = sdata;
  2843. tx.sta = sta;
  2844. tx.key = fast_tx->key;
  2845. if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
  2846. tx.skb = skb;
  2847. r = ieee80211_tx_h_rate_ctrl(&tx);
  2848. skb = tx.skb;
  2849. tx.skb = NULL;
  2850. if (r != TX_CONTINUE) {
  2851. if (r != TX_QUEUED)
  2852. kfree_skb(skb);
  2853. return true;
  2854. }
  2855. }
  2856. if (ieee80211_queue_skb(local, sdata, sta, skb))
  2857. return true;
  2858. ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
  2859. fast_tx->key, skb);
  2860. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2861. sdata = container_of(sdata->bss,
  2862. struct ieee80211_sub_if_data, u.ap);
  2863. __skb_queue_tail(&tx.skbs, skb);
  2864. ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
  2865. return true;
  2866. }
  2867. struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
  2868. struct ieee80211_txq *txq)
  2869. {
  2870. struct ieee80211_local *local = hw_to_local(hw);
  2871. struct txq_info *txqi = container_of(txq, struct txq_info, txq);
  2872. struct ieee80211_hdr *hdr;
  2873. struct sk_buff *skb = NULL;
  2874. struct fq *fq = &local->fq;
  2875. struct fq_tin *tin = &txqi->tin;
  2876. struct ieee80211_tx_info *info;
  2877. struct ieee80211_tx_data tx;
  2878. ieee80211_tx_result r;
  2879. struct ieee80211_vif *vif;
  2880. spin_lock_bh(&fq->lock);
  2881. if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
  2882. goto out;
  2883. /* Make sure fragments stay together. */
  2884. skb = __skb_dequeue(&txqi->frags);
  2885. if (skb)
  2886. goto out;
  2887. begin:
  2888. skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
  2889. if (!skb)
  2890. goto out;
  2891. hdr = (struct ieee80211_hdr *)skb->data;
  2892. info = IEEE80211_SKB_CB(skb);
  2893. memset(&tx, 0, sizeof(tx));
  2894. __skb_queue_head_init(&tx.skbs);
  2895. tx.local = local;
  2896. tx.skb = skb;
  2897. tx.sdata = vif_to_sdata(info->control.vif);
  2898. if (txq->sta)
  2899. tx.sta = container_of(txq->sta, struct sta_info, sta);
  2900. /*
  2901. * The key can be removed while the packet was queued, so need to call
  2902. * this here to get the current key.
  2903. */
  2904. r = ieee80211_tx_h_select_key(&tx);
  2905. if (r != TX_CONTINUE) {
  2906. ieee80211_free_txskb(&local->hw, skb);
  2907. goto begin;
  2908. }
  2909. if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
  2910. info->flags |= IEEE80211_TX_CTL_AMPDU;
  2911. else
  2912. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  2913. if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
  2914. struct sta_info *sta = container_of(txq->sta, struct sta_info,
  2915. sta);
  2916. u8 pn_offs = 0;
  2917. if (tx.key &&
  2918. (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  2919. pn_offs = ieee80211_hdrlen(hdr->frame_control);
  2920. ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
  2921. tx.key, skb);
  2922. } else {
  2923. if (invoke_tx_handlers_late(&tx))
  2924. goto begin;
  2925. skb = __skb_dequeue(&tx.skbs);
  2926. if (!skb_queue_empty(&tx.skbs))
  2927. skb_queue_splice_tail(&tx.skbs, &txqi->frags);
  2928. }
  2929. if (skb && skb_has_frag_list(skb) &&
  2930. !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
  2931. if (skb_linearize(skb)) {
  2932. ieee80211_free_txskb(&local->hw, skb);
  2933. goto begin;
  2934. }
  2935. }
  2936. switch (tx.sdata->vif.type) {
  2937. case NL80211_IFTYPE_MONITOR:
  2938. if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
  2939. vif = &tx.sdata->vif;
  2940. break;
  2941. }
  2942. tx.sdata = rcu_dereference(local->monitor_sdata);
  2943. if (tx.sdata) {
  2944. vif = &tx.sdata->vif;
  2945. info->hw_queue =
  2946. vif->hw_queue[skb_get_queue_mapping(skb)];
  2947. } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  2948. ieee80211_free_txskb(&local->hw, skb);
  2949. goto begin;
  2950. } else {
  2951. vif = NULL;
  2952. }
  2953. break;
  2954. case NL80211_IFTYPE_AP_VLAN:
  2955. tx.sdata = container_of(tx.sdata->bss,
  2956. struct ieee80211_sub_if_data, u.ap);
  2957. /* fall through */
  2958. default:
  2959. vif = &tx.sdata->vif;
  2960. break;
  2961. }
  2962. IEEE80211_SKB_CB(skb)->control.vif = vif;
  2963. out:
  2964. spin_unlock_bh(&fq->lock);
  2965. return skb;
  2966. }
  2967. EXPORT_SYMBOL(ieee80211_tx_dequeue);
  2968. void __ieee80211_subif_start_xmit(struct sk_buff *skb,
  2969. struct net_device *dev,
  2970. u32 info_flags)
  2971. {
  2972. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  2973. struct sta_info *sta;
  2974. struct sk_buff *next;
  2975. if (unlikely(skb->len < ETH_HLEN)) {
  2976. kfree_skb(skb);
  2977. return;
  2978. }
  2979. rcu_read_lock();
  2980. if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
  2981. goto out_free;
  2982. if (!IS_ERR_OR_NULL(sta)) {
  2983. struct ieee80211_fast_tx *fast_tx;
  2984. fast_tx = rcu_dereference(sta->fast_tx);
  2985. if (fast_tx &&
  2986. ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
  2987. goto out;
  2988. }
  2989. if (skb_is_gso(skb)) {
  2990. struct sk_buff *segs;
  2991. segs = skb_gso_segment(skb, 0);
  2992. if (IS_ERR(segs)) {
  2993. goto out_free;
  2994. } else if (segs) {
  2995. consume_skb(skb);
  2996. skb = segs;
  2997. }
  2998. } else {
  2999. /* we cannot process non-linear frames on this path */
  3000. if (skb_linearize(skb)) {
  3001. kfree_skb(skb);
  3002. goto out;
  3003. }
  3004. /* the frame could be fragmented, software-encrypted, and other
  3005. * things so we cannot really handle checksum offload with it -
  3006. * fix it up in software before we handle anything else.
  3007. */
  3008. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3009. skb_set_transport_header(skb,
  3010. skb_checksum_start_offset(skb));
  3011. if (skb_checksum_help(skb))
  3012. goto out_free;
  3013. }
  3014. }
  3015. next = skb;
  3016. while (next) {
  3017. skb = next;
  3018. next = skb->next;
  3019. skb->prev = NULL;
  3020. skb->next = NULL;
  3021. skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
  3022. if (IS_ERR(skb))
  3023. goto out;
  3024. ieee80211_tx_stats(dev, skb->len);
  3025. ieee80211_xmit(sdata, sta, skb);
  3026. }
  3027. goto out;
  3028. out_free:
  3029. kfree_skb(skb);
  3030. out:
  3031. rcu_read_unlock();
  3032. }
  3033. /**
  3034. * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
  3035. * @skb: packet to be sent
  3036. * @dev: incoming interface
  3037. *
  3038. * On failure skb will be freed.
  3039. */
  3040. netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
  3041. struct net_device *dev)
  3042. {
  3043. __ieee80211_subif_start_xmit(skb, dev, 0);
  3044. return NETDEV_TX_OK;
  3045. }
  3046. struct sk_buff *
  3047. ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
  3048. struct sk_buff *skb, u32 info_flags)
  3049. {
  3050. struct ieee80211_hdr *hdr;
  3051. struct ieee80211_tx_data tx = {
  3052. .local = sdata->local,
  3053. .sdata = sdata,
  3054. };
  3055. struct sta_info *sta;
  3056. rcu_read_lock();
  3057. if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
  3058. kfree_skb(skb);
  3059. skb = ERR_PTR(-EINVAL);
  3060. goto out;
  3061. }
  3062. skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
  3063. if (IS_ERR(skb))
  3064. goto out;
  3065. hdr = (void *)skb->data;
  3066. tx.sta = sta_info_get(sdata, hdr->addr1);
  3067. tx.skb = skb;
  3068. if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
  3069. rcu_read_unlock();
  3070. kfree_skb(skb);
  3071. return ERR_PTR(-EINVAL);
  3072. }
  3073. out:
  3074. rcu_read_unlock();
  3075. return skb;
  3076. }
  3077. /*
  3078. * ieee80211_clear_tx_pending may not be called in a context where
  3079. * it is possible that it packets could come in again.
  3080. */
  3081. void ieee80211_clear_tx_pending(struct ieee80211_local *local)
  3082. {
  3083. struct sk_buff *skb;
  3084. int i;
  3085. for (i = 0; i < local->hw.queues; i++) {
  3086. while ((skb = skb_dequeue(&local->pending[i])) != NULL)
  3087. ieee80211_free_txskb(&local->hw, skb);
  3088. }
  3089. }
  3090. /*
  3091. * Returns false if the frame couldn't be transmitted but was queued instead,
  3092. * which in this case means re-queued -- take as an indication to stop sending
  3093. * more pending frames.
  3094. */
  3095. static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
  3096. struct sk_buff *skb)
  3097. {
  3098. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  3099. struct ieee80211_sub_if_data *sdata;
  3100. struct sta_info *sta;
  3101. struct ieee80211_hdr *hdr;
  3102. bool result;
  3103. struct ieee80211_chanctx_conf *chanctx_conf;
  3104. sdata = vif_to_sdata(info->control.vif);
  3105. if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
  3106. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3107. if (unlikely(!chanctx_conf)) {
  3108. dev_kfree_skb(skb);
  3109. return true;
  3110. }
  3111. info->band = chanctx_conf->def.chan->band;
  3112. result = ieee80211_tx(sdata, NULL, skb, true);
  3113. } else {
  3114. struct sk_buff_head skbs;
  3115. __skb_queue_head_init(&skbs);
  3116. __skb_queue_tail(&skbs, skb);
  3117. hdr = (struct ieee80211_hdr *)skb->data;
  3118. sta = sta_info_get(sdata, hdr->addr1);
  3119. result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
  3120. }
  3121. return result;
  3122. }
  3123. /*
  3124. * Transmit all pending packets. Called from tasklet.
  3125. */
  3126. void ieee80211_tx_pending(unsigned long data)
  3127. {
  3128. struct ieee80211_local *local = (struct ieee80211_local *)data;
  3129. unsigned long flags;
  3130. int i;
  3131. bool txok;
  3132. rcu_read_lock();
  3133. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  3134. for (i = 0; i < local->hw.queues; i++) {
  3135. /*
  3136. * If queue is stopped by something other than due to pending
  3137. * frames, or we have no pending frames, proceed to next queue.
  3138. */
  3139. if (local->queue_stop_reasons[i] ||
  3140. skb_queue_empty(&local->pending[i]))
  3141. continue;
  3142. while (!skb_queue_empty(&local->pending[i])) {
  3143. struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
  3144. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  3145. if (WARN_ON(!info->control.vif)) {
  3146. ieee80211_free_txskb(&local->hw, skb);
  3147. continue;
  3148. }
  3149. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  3150. flags);
  3151. txok = ieee80211_tx_pending_skb(local, skb);
  3152. spin_lock_irqsave(&local->queue_stop_reason_lock,
  3153. flags);
  3154. if (!txok)
  3155. break;
  3156. }
  3157. if (skb_queue_empty(&local->pending[i]))
  3158. ieee80211_propagate_queue_wake(local, i);
  3159. }
  3160. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  3161. rcu_read_unlock();
  3162. }
  3163. /* functions for drivers to get certain frames */
  3164. static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  3165. struct ps_data *ps, struct sk_buff *skb,
  3166. bool is_template)
  3167. {
  3168. u8 *pos, *tim;
  3169. int aid0 = 0;
  3170. int i, have_bits = 0, n1, n2;
  3171. /* Generate bitmap for TIM only if there are any STAs in power save
  3172. * mode. */
  3173. if (atomic_read(&ps->num_sta_ps) > 0)
  3174. /* in the hope that this is faster than
  3175. * checking byte-for-byte */
  3176. have_bits = !bitmap_empty((unsigned long *)ps->tim,
  3177. IEEE80211_MAX_AID+1);
  3178. if (!is_template) {
  3179. if (ps->dtim_count == 0)
  3180. ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
  3181. else
  3182. ps->dtim_count--;
  3183. }
  3184. tim = pos = (u8 *) skb_put(skb, 6);
  3185. *pos++ = WLAN_EID_TIM;
  3186. *pos++ = 4;
  3187. *pos++ = ps->dtim_count;
  3188. *pos++ = sdata->vif.bss_conf.dtim_period;
  3189. if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
  3190. aid0 = 1;
  3191. ps->dtim_bc_mc = aid0 == 1;
  3192. if (have_bits) {
  3193. /* Find largest even number N1 so that bits numbered 1 through
  3194. * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
  3195. * (N2 + 1) x 8 through 2007 are 0. */
  3196. n1 = 0;
  3197. for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
  3198. if (ps->tim[i]) {
  3199. n1 = i & 0xfe;
  3200. break;
  3201. }
  3202. }
  3203. n2 = n1;
  3204. for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
  3205. if (ps->tim[i]) {
  3206. n2 = i;
  3207. break;
  3208. }
  3209. }
  3210. /* Bitmap control */
  3211. *pos++ = n1 | aid0;
  3212. /* Part Virt Bitmap */
  3213. skb_put(skb, n2 - n1);
  3214. memcpy(pos, ps->tim + n1, n2 - n1 + 1);
  3215. tim[1] = n2 - n1 + 4;
  3216. } else {
  3217. *pos++ = aid0; /* Bitmap control */
  3218. *pos++ = 0; /* Part Virt Bitmap */
  3219. }
  3220. }
  3221. static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
  3222. struct ps_data *ps, struct sk_buff *skb,
  3223. bool is_template)
  3224. {
  3225. struct ieee80211_local *local = sdata->local;
  3226. /*
  3227. * Not very nice, but we want to allow the driver to call
  3228. * ieee80211_beacon_get() as a response to the set_tim()
  3229. * callback. That, however, is already invoked under the
  3230. * sta_lock to guarantee consistent and race-free update
  3231. * of the tim bitmap in mac80211 and the driver.
  3232. */
  3233. if (local->tim_in_locked_section) {
  3234. __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
  3235. } else {
  3236. spin_lock_bh(&local->tim_lock);
  3237. __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
  3238. spin_unlock_bh(&local->tim_lock);
  3239. }
  3240. return 0;
  3241. }
  3242. static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
  3243. struct beacon_data *beacon)
  3244. {
  3245. struct probe_resp *resp;
  3246. u8 *beacon_data;
  3247. size_t beacon_data_len;
  3248. int i;
  3249. u8 count = beacon->csa_current_counter;
  3250. switch (sdata->vif.type) {
  3251. case NL80211_IFTYPE_AP:
  3252. beacon_data = beacon->tail;
  3253. beacon_data_len = beacon->tail_len;
  3254. break;
  3255. case NL80211_IFTYPE_ADHOC:
  3256. beacon_data = beacon->head;
  3257. beacon_data_len = beacon->head_len;
  3258. break;
  3259. case NL80211_IFTYPE_MESH_POINT:
  3260. beacon_data = beacon->head;
  3261. beacon_data_len = beacon->head_len;
  3262. break;
  3263. default:
  3264. return;
  3265. }
  3266. rcu_read_lock();
  3267. for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
  3268. resp = rcu_dereference(sdata->u.ap.probe_resp);
  3269. if (beacon->csa_counter_offsets[i]) {
  3270. if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
  3271. beacon_data_len)) {
  3272. rcu_read_unlock();
  3273. return;
  3274. }
  3275. beacon_data[beacon->csa_counter_offsets[i]] = count;
  3276. }
  3277. if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
  3278. resp->data[resp->csa_counter_offsets[i]] = count;
  3279. }
  3280. rcu_read_unlock();
  3281. }
  3282. static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
  3283. {
  3284. beacon->csa_current_counter--;
  3285. /* the counter should never reach 0 */
  3286. WARN_ON_ONCE(!beacon->csa_current_counter);
  3287. return beacon->csa_current_counter;
  3288. }
  3289. u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
  3290. {
  3291. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3292. struct beacon_data *beacon = NULL;
  3293. u8 count = 0;
  3294. rcu_read_lock();
  3295. if (sdata->vif.type == NL80211_IFTYPE_AP)
  3296. beacon = rcu_dereference(sdata->u.ap.beacon);
  3297. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  3298. beacon = rcu_dereference(sdata->u.ibss.presp);
  3299. else if (ieee80211_vif_is_mesh(&sdata->vif))
  3300. beacon = rcu_dereference(sdata->u.mesh.beacon);
  3301. if (!beacon)
  3302. goto unlock;
  3303. count = __ieee80211_csa_update_counter(beacon);
  3304. unlock:
  3305. rcu_read_unlock();
  3306. return count;
  3307. }
  3308. EXPORT_SYMBOL(ieee80211_csa_update_counter);
  3309. bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
  3310. {
  3311. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3312. struct beacon_data *beacon = NULL;
  3313. u8 *beacon_data;
  3314. size_t beacon_data_len;
  3315. int ret = false;
  3316. if (!ieee80211_sdata_running(sdata))
  3317. return false;
  3318. rcu_read_lock();
  3319. if (vif->type == NL80211_IFTYPE_AP) {
  3320. struct ieee80211_if_ap *ap = &sdata->u.ap;
  3321. beacon = rcu_dereference(ap->beacon);
  3322. if (WARN_ON(!beacon || !beacon->tail))
  3323. goto out;
  3324. beacon_data = beacon->tail;
  3325. beacon_data_len = beacon->tail_len;
  3326. } else if (vif->type == NL80211_IFTYPE_ADHOC) {
  3327. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  3328. beacon = rcu_dereference(ifibss->presp);
  3329. if (!beacon)
  3330. goto out;
  3331. beacon_data = beacon->head;
  3332. beacon_data_len = beacon->head_len;
  3333. } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
  3334. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  3335. beacon = rcu_dereference(ifmsh->beacon);
  3336. if (!beacon)
  3337. goto out;
  3338. beacon_data = beacon->head;
  3339. beacon_data_len = beacon->head_len;
  3340. } else {
  3341. WARN_ON(1);
  3342. goto out;
  3343. }
  3344. if (!beacon->csa_counter_offsets[0])
  3345. goto out;
  3346. if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
  3347. goto out;
  3348. if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
  3349. ret = true;
  3350. out:
  3351. rcu_read_unlock();
  3352. return ret;
  3353. }
  3354. EXPORT_SYMBOL(ieee80211_csa_is_complete);
  3355. static struct sk_buff *
  3356. __ieee80211_beacon_get(struct ieee80211_hw *hw,
  3357. struct ieee80211_vif *vif,
  3358. struct ieee80211_mutable_offsets *offs,
  3359. bool is_template)
  3360. {
  3361. struct ieee80211_local *local = hw_to_local(hw);
  3362. struct beacon_data *beacon = NULL;
  3363. struct sk_buff *skb = NULL;
  3364. struct ieee80211_tx_info *info;
  3365. struct ieee80211_sub_if_data *sdata = NULL;
  3366. enum nl80211_band band;
  3367. struct ieee80211_tx_rate_control txrc;
  3368. struct ieee80211_chanctx_conf *chanctx_conf;
  3369. int csa_off_base = 0;
  3370. rcu_read_lock();
  3371. sdata = vif_to_sdata(vif);
  3372. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3373. if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
  3374. goto out;
  3375. if (offs)
  3376. memset(offs, 0, sizeof(*offs));
  3377. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  3378. struct ieee80211_if_ap *ap = &sdata->u.ap;
  3379. beacon = rcu_dereference(ap->beacon);
  3380. if (beacon) {
  3381. if (beacon->csa_counter_offsets[0]) {
  3382. if (!is_template)
  3383. __ieee80211_csa_update_counter(beacon);
  3384. ieee80211_set_csa(sdata, beacon);
  3385. }
  3386. /*
  3387. * headroom, head length,
  3388. * tail length and maximum TIM length
  3389. */
  3390. skb = dev_alloc_skb(local->tx_headroom +
  3391. beacon->head_len +
  3392. beacon->tail_len + 256 +
  3393. local->hw.extra_beacon_tailroom);
  3394. if (!skb)
  3395. goto out;
  3396. skb_reserve(skb, local->tx_headroom);
  3397. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  3398. beacon->head_len);
  3399. ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
  3400. is_template);
  3401. if (offs) {
  3402. offs->tim_offset = beacon->head_len;
  3403. offs->tim_length = skb->len - beacon->head_len;
  3404. /* for AP the csa offsets are from tail */
  3405. csa_off_base = skb->len;
  3406. }
  3407. if (beacon->tail)
  3408. memcpy(skb_put(skb, beacon->tail_len),
  3409. beacon->tail, beacon->tail_len);
  3410. } else
  3411. goto out;
  3412. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  3413. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  3414. struct ieee80211_hdr *hdr;
  3415. beacon = rcu_dereference(ifibss->presp);
  3416. if (!beacon)
  3417. goto out;
  3418. if (beacon->csa_counter_offsets[0]) {
  3419. if (!is_template)
  3420. __ieee80211_csa_update_counter(beacon);
  3421. ieee80211_set_csa(sdata, beacon);
  3422. }
  3423. skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
  3424. local->hw.extra_beacon_tailroom);
  3425. if (!skb)
  3426. goto out;
  3427. skb_reserve(skb, local->tx_headroom);
  3428. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  3429. beacon->head_len);
  3430. hdr = (struct ieee80211_hdr *) skb->data;
  3431. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  3432. IEEE80211_STYPE_BEACON);
  3433. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  3434. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  3435. beacon = rcu_dereference(ifmsh->beacon);
  3436. if (!beacon)
  3437. goto out;
  3438. if (beacon->csa_counter_offsets[0]) {
  3439. if (!is_template)
  3440. /* TODO: For mesh csa_counter is in TU, so
  3441. * decrementing it by one isn't correct, but
  3442. * for now we leave it consistent with overall
  3443. * mac80211's behavior.
  3444. */
  3445. __ieee80211_csa_update_counter(beacon);
  3446. ieee80211_set_csa(sdata, beacon);
  3447. }
  3448. if (ifmsh->sync_ops)
  3449. ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
  3450. skb = dev_alloc_skb(local->tx_headroom +
  3451. beacon->head_len +
  3452. 256 + /* TIM IE */
  3453. beacon->tail_len +
  3454. local->hw.extra_beacon_tailroom);
  3455. if (!skb)
  3456. goto out;
  3457. skb_reserve(skb, local->tx_headroom);
  3458. memcpy(skb_put(skb, beacon->head_len), beacon->head,
  3459. beacon->head_len);
  3460. ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
  3461. if (offs) {
  3462. offs->tim_offset = beacon->head_len;
  3463. offs->tim_length = skb->len - beacon->head_len;
  3464. }
  3465. memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
  3466. beacon->tail_len);
  3467. } else {
  3468. WARN_ON(1);
  3469. goto out;
  3470. }
  3471. /* CSA offsets */
  3472. if (offs && beacon) {
  3473. int i;
  3474. for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
  3475. u16 csa_off = beacon->csa_counter_offsets[i];
  3476. if (!csa_off)
  3477. continue;
  3478. offs->csa_counter_offs[i] = csa_off_base + csa_off;
  3479. }
  3480. }
  3481. band = chanctx_conf->def.chan->band;
  3482. info = IEEE80211_SKB_CB(skb);
  3483. info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  3484. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  3485. info->band = band;
  3486. memset(&txrc, 0, sizeof(txrc));
  3487. txrc.hw = hw;
  3488. txrc.sband = local->hw.wiphy->bands[band];
  3489. txrc.bss_conf = &sdata->vif.bss_conf;
  3490. txrc.skb = skb;
  3491. txrc.reported_rate.idx = -1;
  3492. txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
  3493. if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
  3494. txrc.max_rate_idx = -1;
  3495. else
  3496. txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
  3497. txrc.bss = true;
  3498. rate_control_get_rate(sdata, NULL, &txrc);
  3499. info->control.vif = vif;
  3500. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
  3501. IEEE80211_TX_CTL_ASSIGN_SEQ |
  3502. IEEE80211_TX_CTL_FIRST_FRAGMENT;
  3503. out:
  3504. rcu_read_unlock();
  3505. return skb;
  3506. }
  3507. struct sk_buff *
  3508. ieee80211_beacon_get_template(struct ieee80211_hw *hw,
  3509. struct ieee80211_vif *vif,
  3510. struct ieee80211_mutable_offsets *offs)
  3511. {
  3512. return __ieee80211_beacon_get(hw, vif, offs, true);
  3513. }
  3514. EXPORT_SYMBOL(ieee80211_beacon_get_template);
  3515. struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
  3516. struct ieee80211_vif *vif,
  3517. u16 *tim_offset, u16 *tim_length)
  3518. {
  3519. struct ieee80211_mutable_offsets offs = {};
  3520. struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
  3521. struct sk_buff *copy;
  3522. struct ieee80211_supported_band *sband;
  3523. int shift;
  3524. if (!bcn)
  3525. return bcn;
  3526. if (tim_offset)
  3527. *tim_offset = offs.tim_offset;
  3528. if (tim_length)
  3529. *tim_length = offs.tim_length;
  3530. if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
  3531. !hw_to_local(hw)->monitors)
  3532. return bcn;
  3533. /* send a copy to monitor interfaces */
  3534. copy = skb_copy(bcn, GFP_ATOMIC);
  3535. if (!copy)
  3536. return bcn;
  3537. shift = ieee80211_vif_get_shift(vif);
  3538. sband = ieee80211_get_sband(vif_to_sdata(vif));
  3539. if (!sband)
  3540. return bcn;
  3541. ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
  3542. return bcn;
  3543. }
  3544. EXPORT_SYMBOL(ieee80211_beacon_get_tim);
  3545. struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
  3546. struct ieee80211_vif *vif)
  3547. {
  3548. struct ieee80211_if_ap *ap = NULL;
  3549. struct sk_buff *skb = NULL;
  3550. struct probe_resp *presp = NULL;
  3551. struct ieee80211_hdr *hdr;
  3552. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3553. if (sdata->vif.type != NL80211_IFTYPE_AP)
  3554. return NULL;
  3555. rcu_read_lock();
  3556. ap = &sdata->u.ap;
  3557. presp = rcu_dereference(ap->probe_resp);
  3558. if (!presp)
  3559. goto out;
  3560. skb = dev_alloc_skb(presp->len);
  3561. if (!skb)
  3562. goto out;
  3563. memcpy(skb_put(skb, presp->len), presp->data, presp->len);
  3564. hdr = (struct ieee80211_hdr *) skb->data;
  3565. memset(hdr->addr1, 0, sizeof(hdr->addr1));
  3566. out:
  3567. rcu_read_unlock();
  3568. return skb;
  3569. }
  3570. EXPORT_SYMBOL(ieee80211_proberesp_get);
  3571. struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
  3572. struct ieee80211_vif *vif)
  3573. {
  3574. struct ieee80211_sub_if_data *sdata;
  3575. struct ieee80211_if_managed *ifmgd;
  3576. struct ieee80211_pspoll *pspoll;
  3577. struct ieee80211_local *local;
  3578. struct sk_buff *skb;
  3579. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  3580. return NULL;
  3581. sdata = vif_to_sdata(vif);
  3582. ifmgd = &sdata->u.mgd;
  3583. local = sdata->local;
  3584. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  3585. if (!skb)
  3586. return NULL;
  3587. skb_reserve(skb, local->hw.extra_tx_headroom);
  3588. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  3589. memset(pspoll, 0, sizeof(*pspoll));
  3590. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  3591. IEEE80211_STYPE_PSPOLL);
  3592. pspoll->aid = cpu_to_le16(ifmgd->aid);
  3593. /* aid in PS-Poll has its two MSBs each set to 1 */
  3594. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  3595. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  3596. memcpy(pspoll->ta, vif->addr, ETH_ALEN);
  3597. return skb;
  3598. }
  3599. EXPORT_SYMBOL(ieee80211_pspoll_get);
  3600. struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
  3601. struct ieee80211_vif *vif)
  3602. {
  3603. struct ieee80211_hdr_3addr *nullfunc;
  3604. struct ieee80211_sub_if_data *sdata;
  3605. struct ieee80211_if_managed *ifmgd;
  3606. struct ieee80211_local *local;
  3607. struct sk_buff *skb;
  3608. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  3609. return NULL;
  3610. sdata = vif_to_sdata(vif);
  3611. ifmgd = &sdata->u.mgd;
  3612. local = sdata->local;
  3613. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
  3614. if (!skb)
  3615. return NULL;
  3616. skb_reserve(skb, local->hw.extra_tx_headroom);
  3617. nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
  3618. sizeof(*nullfunc));
  3619. memset(nullfunc, 0, sizeof(*nullfunc));
  3620. nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  3621. IEEE80211_STYPE_NULLFUNC |
  3622. IEEE80211_FCTL_TODS);
  3623. memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
  3624. memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
  3625. memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
  3626. return skb;
  3627. }
  3628. EXPORT_SYMBOL(ieee80211_nullfunc_get);
  3629. struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
  3630. const u8 *src_addr,
  3631. const u8 *ssid, size_t ssid_len,
  3632. size_t tailroom)
  3633. {
  3634. struct ieee80211_local *local = hw_to_local(hw);
  3635. struct ieee80211_hdr_3addr *hdr;
  3636. struct sk_buff *skb;
  3637. size_t ie_ssid_len;
  3638. u8 *pos;
  3639. ie_ssid_len = 2 + ssid_len;
  3640. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
  3641. ie_ssid_len + tailroom);
  3642. if (!skb)
  3643. return NULL;
  3644. skb_reserve(skb, local->hw.extra_tx_headroom);
  3645. hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
  3646. memset(hdr, 0, sizeof(*hdr));
  3647. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  3648. IEEE80211_STYPE_PROBE_REQ);
  3649. eth_broadcast_addr(hdr->addr1);
  3650. memcpy(hdr->addr2, src_addr, ETH_ALEN);
  3651. eth_broadcast_addr(hdr->addr3);
  3652. pos = skb_put(skb, ie_ssid_len);
  3653. *pos++ = WLAN_EID_SSID;
  3654. *pos++ = ssid_len;
  3655. if (ssid_len)
  3656. memcpy(pos, ssid, ssid_len);
  3657. pos += ssid_len;
  3658. return skb;
  3659. }
  3660. EXPORT_SYMBOL(ieee80211_probereq_get);
  3661. void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3662. const void *frame, size_t frame_len,
  3663. const struct ieee80211_tx_info *frame_txctl,
  3664. struct ieee80211_rts *rts)
  3665. {
  3666. const struct ieee80211_hdr *hdr = frame;
  3667. rts->frame_control =
  3668. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
  3669. rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
  3670. frame_txctl);
  3671. memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
  3672. memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
  3673. }
  3674. EXPORT_SYMBOL(ieee80211_rts_get);
  3675. void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3676. const void *frame, size_t frame_len,
  3677. const struct ieee80211_tx_info *frame_txctl,
  3678. struct ieee80211_cts *cts)
  3679. {
  3680. const struct ieee80211_hdr *hdr = frame;
  3681. cts->frame_control =
  3682. cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
  3683. cts->duration = ieee80211_ctstoself_duration(hw, vif,
  3684. frame_len, frame_txctl);
  3685. memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
  3686. }
  3687. EXPORT_SYMBOL(ieee80211_ctstoself_get);
  3688. struct sk_buff *
  3689. ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
  3690. struct ieee80211_vif *vif)
  3691. {
  3692. struct ieee80211_local *local = hw_to_local(hw);
  3693. struct sk_buff *skb = NULL;
  3694. struct ieee80211_tx_data tx;
  3695. struct ieee80211_sub_if_data *sdata;
  3696. struct ps_data *ps;
  3697. struct ieee80211_tx_info *info;
  3698. struct ieee80211_chanctx_conf *chanctx_conf;
  3699. sdata = vif_to_sdata(vif);
  3700. rcu_read_lock();
  3701. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3702. if (!chanctx_conf)
  3703. goto out;
  3704. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  3705. struct beacon_data *beacon =
  3706. rcu_dereference(sdata->u.ap.beacon);
  3707. if (!beacon || !beacon->head)
  3708. goto out;
  3709. ps = &sdata->u.ap.ps;
  3710. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  3711. ps = &sdata->u.mesh.ps;
  3712. } else {
  3713. goto out;
  3714. }
  3715. if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
  3716. goto out; /* send buffered bc/mc only after DTIM beacon */
  3717. while (1) {
  3718. skb = skb_dequeue(&ps->bc_buf);
  3719. if (!skb)
  3720. goto out;
  3721. local->total_ps_buffered--;
  3722. if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
  3723. struct ieee80211_hdr *hdr =
  3724. (struct ieee80211_hdr *) skb->data;
  3725. /* more buffered multicast/broadcast frames ==> set
  3726. * MoreData flag in IEEE 802.11 header to inform PS
  3727. * STAs */
  3728. hdr->frame_control |=
  3729. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  3730. }
  3731. if (sdata->vif.type == NL80211_IFTYPE_AP)
  3732. sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
  3733. if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
  3734. break;
  3735. ieee80211_free_txskb(hw, skb);
  3736. }
  3737. info = IEEE80211_SKB_CB(skb);
  3738. tx.flags |= IEEE80211_TX_PS_BUFFERED;
  3739. info->band = chanctx_conf->def.chan->band;
  3740. if (invoke_tx_handlers(&tx))
  3741. skb = NULL;
  3742. out:
  3743. rcu_read_unlock();
  3744. return skb;
  3745. }
  3746. EXPORT_SYMBOL(ieee80211_get_buffered_bc);
  3747. int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  3748. {
  3749. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  3750. struct ieee80211_sub_if_data *sdata = sta->sdata;
  3751. struct ieee80211_local *local = sdata->local;
  3752. int ret;
  3753. u32 queues;
  3754. lockdep_assert_held(&local->sta_mtx);
  3755. /* only some cases are supported right now */
  3756. switch (sdata->vif.type) {
  3757. case NL80211_IFTYPE_STATION:
  3758. case NL80211_IFTYPE_AP:
  3759. case NL80211_IFTYPE_AP_VLAN:
  3760. break;
  3761. default:
  3762. WARN_ON(1);
  3763. return -EINVAL;
  3764. }
  3765. if (WARN_ON(tid >= IEEE80211_NUM_UPS))
  3766. return -EINVAL;
  3767. if (sta->reserved_tid == tid) {
  3768. ret = 0;
  3769. goto out;
  3770. }
  3771. if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
  3772. sdata_err(sdata, "TID reservation already active\n");
  3773. ret = -EALREADY;
  3774. goto out;
  3775. }
  3776. ieee80211_stop_vif_queues(sdata->local, sdata,
  3777. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  3778. synchronize_net();
  3779. /* Tear down BA sessions so we stop aggregating on this TID */
  3780. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
  3781. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  3782. __ieee80211_stop_tx_ba_session(sta, tid,
  3783. AGG_STOP_LOCAL_REQUEST);
  3784. }
  3785. queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
  3786. __ieee80211_flush_queues(local, sdata, queues, false);
  3787. sta->reserved_tid = tid;
  3788. ieee80211_wake_vif_queues(local, sdata,
  3789. IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
  3790. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
  3791. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  3792. ret = 0;
  3793. out:
  3794. return ret;
  3795. }
  3796. EXPORT_SYMBOL(ieee80211_reserve_tid);
  3797. void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
  3798. {
  3799. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  3800. struct ieee80211_sub_if_data *sdata = sta->sdata;
  3801. lockdep_assert_held(&sdata->local->sta_mtx);
  3802. /* only some cases are supported right now */
  3803. switch (sdata->vif.type) {
  3804. case NL80211_IFTYPE_STATION:
  3805. case NL80211_IFTYPE_AP:
  3806. case NL80211_IFTYPE_AP_VLAN:
  3807. break;
  3808. default:
  3809. WARN_ON(1);
  3810. return;
  3811. }
  3812. if (tid != sta->reserved_tid) {
  3813. sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
  3814. return;
  3815. }
  3816. sta->reserved_tid = IEEE80211_TID_UNRESERVED;
  3817. }
  3818. EXPORT_SYMBOL(ieee80211_unreserve_tid);
  3819. void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
  3820. struct sk_buff *skb, int tid,
  3821. enum nl80211_band band)
  3822. {
  3823. int ac = ieee802_1d_to_ac[tid & 7];
  3824. skb_reset_mac_header(skb);
  3825. skb_set_queue_mapping(skb, ac);
  3826. skb->priority = tid;
  3827. skb->dev = sdata->dev;
  3828. /*
  3829. * The other path calling ieee80211_xmit is from the tasklet,
  3830. * and while we can handle concurrent transmissions locking
  3831. * requirements are that we do not come into tx with bhs on.
  3832. */
  3833. local_bh_disable();
  3834. IEEE80211_SKB_CB(skb)->band = band;
  3835. ieee80211_xmit(sdata, NULL, skb);
  3836. local_bh_enable();
  3837. }