tx.c 124 KB

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