tx.c 131 KB

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