tx.c 122 KB

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