tx.c 118 KB

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