tx.c 130 KB

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