tx.c 130 KB

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