tx.c 122 KB

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