rx.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566
  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-2010 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018 Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/jiffies.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/export.h>
  22. #include <linux/bitops.h>
  23. #include <net/mac80211.h>
  24. #include <net/ieee80211_radiotap.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "led.h"
  29. #include "mesh.h"
  30. #include "wep.h"
  31. #include "wpa.h"
  32. #include "tkip.h"
  33. #include "wme.h"
  34. #include "rate.h"
  35. static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
  36. {
  37. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  38. u64_stats_update_begin(&tstats->syncp);
  39. tstats->rx_packets++;
  40. tstats->rx_bytes += len;
  41. u64_stats_update_end(&tstats->syncp);
  42. }
  43. static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. if (ieee80211_is_data(fc)) {
  48. if (len < 24) /* drop incorrect hdr len (data) */
  49. return NULL;
  50. if (ieee80211_has_a4(fc))
  51. return NULL;
  52. if (ieee80211_has_tods(fc))
  53. return hdr->addr1;
  54. if (ieee80211_has_fromds(fc))
  55. return hdr->addr2;
  56. return hdr->addr3;
  57. }
  58. if (ieee80211_is_mgmt(fc)) {
  59. if (len < 24) /* drop incorrect hdr len (mgmt) */
  60. return NULL;
  61. return hdr->addr3;
  62. }
  63. if (ieee80211_is_ctl(fc)) {
  64. if (ieee80211_is_pspoll(fc))
  65. return hdr->addr1;
  66. if (ieee80211_is_back_req(fc)) {
  67. switch (type) {
  68. case NL80211_IFTYPE_STATION:
  69. return hdr->addr2;
  70. case NL80211_IFTYPE_AP:
  71. case NL80211_IFTYPE_AP_VLAN:
  72. return hdr->addr1;
  73. default:
  74. break; /* fall through to the return */
  75. }
  76. }
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * monitor mode reception
  82. *
  83. * This function cleans up the SKB, i.e. it removes all the stuff
  84. * only useful for monitoring.
  85. */
  86. static void remove_monitor_info(struct sk_buff *skb,
  87. unsigned int present_fcs_len,
  88. unsigned int rtap_space)
  89. {
  90. if (present_fcs_len)
  91. __pskb_trim(skb, skb->len - present_fcs_len);
  92. __pskb_pull(skb, rtap_space);
  93. }
  94. static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
  95. unsigned int rtap_space)
  96. {
  97. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  98. struct ieee80211_hdr *hdr;
  99. hdr = (void *)(skb->data + rtap_space);
  100. if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
  101. RX_FLAG_FAILED_PLCP_CRC |
  102. RX_FLAG_ONLY_MONITOR))
  103. return true;
  104. if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
  105. return true;
  106. if (ieee80211_is_ctl(hdr->frame_control) &&
  107. !ieee80211_is_pspoll(hdr->frame_control) &&
  108. !ieee80211_is_back_req(hdr->frame_control))
  109. return true;
  110. return false;
  111. }
  112. static int
  113. ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
  114. struct ieee80211_rx_status *status,
  115. struct sk_buff *skb)
  116. {
  117. int len;
  118. /* always present fields */
  119. len = sizeof(struct ieee80211_radiotap_header) + 8;
  120. /* allocate extra bitmaps */
  121. if (status->chains)
  122. len += 4 * hweight8(status->chains);
  123. if (ieee80211_have_rx_timestamp(status)) {
  124. len = ALIGN(len, 8);
  125. len += 8;
  126. }
  127. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
  128. len += 1;
  129. /* antenna field, if we don't have per-chain info */
  130. if (!status->chains)
  131. len += 1;
  132. /* padding for RX_FLAGS if necessary */
  133. len = ALIGN(len, 2);
  134. if (status->encoding == RX_ENC_HT) /* HT info */
  135. len += 3;
  136. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  137. len = ALIGN(len, 4);
  138. len += 8;
  139. }
  140. if (status->encoding == RX_ENC_VHT) {
  141. len = ALIGN(len, 2);
  142. len += 12;
  143. }
  144. if (local->hw.radiotap_timestamp.units_pos >= 0) {
  145. len = ALIGN(len, 8);
  146. len += 12;
  147. }
  148. if (status->encoding == RX_ENC_HE &&
  149. status->flag & RX_FLAG_RADIOTAP_HE) {
  150. len = ALIGN(len, 2);
  151. len += 12;
  152. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
  153. }
  154. if (status->encoding == RX_ENC_HE &&
  155. status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  156. len = ALIGN(len, 2);
  157. len += 12;
  158. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
  159. }
  160. if (status->chains) {
  161. /* antenna and antenna signal fields */
  162. len += 2 * hweight8(status->chains);
  163. }
  164. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  165. struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
  166. /* vendor presence bitmap */
  167. len += 4;
  168. /* alignment for fixed 6-byte vendor data header */
  169. len = ALIGN(len, 2);
  170. /* vendor data header */
  171. len += 6;
  172. if (WARN_ON(rtap->align == 0))
  173. rtap->align = 1;
  174. len = ALIGN(len, rtap->align);
  175. len += rtap->len + rtap->pad;
  176. }
  177. return len;
  178. }
  179. static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
  180. struct sk_buff *skb,
  181. int rtap_space)
  182. {
  183. struct {
  184. struct ieee80211_hdr_3addr hdr;
  185. u8 category;
  186. u8 action_code;
  187. } __packed action;
  188. if (!sdata)
  189. return;
  190. BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
  191. if (skb->len < rtap_space + sizeof(action) +
  192. VHT_MUMIMO_GROUPS_DATA_LEN)
  193. return;
  194. if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
  195. return;
  196. skb_copy_bits(skb, rtap_space, &action, sizeof(action));
  197. if (!ieee80211_is_action(action.hdr.frame_control))
  198. return;
  199. if (action.category != WLAN_CATEGORY_VHT)
  200. return;
  201. if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
  202. return;
  203. if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
  204. return;
  205. skb = skb_copy(skb, GFP_ATOMIC);
  206. if (!skb)
  207. return;
  208. skb_queue_tail(&sdata->skb_queue, skb);
  209. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  210. }
  211. /*
  212. * ieee80211_add_rx_radiotap_header - add radiotap header
  213. *
  214. * add a radiotap header containing all the fields which the hardware provided.
  215. */
  216. static void
  217. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  218. struct sk_buff *skb,
  219. struct ieee80211_rate *rate,
  220. int rtap_len, bool has_fcs)
  221. {
  222. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  223. struct ieee80211_radiotap_header *rthdr;
  224. unsigned char *pos;
  225. __le32 *it_present;
  226. u32 it_present_val;
  227. u16 rx_flags = 0;
  228. u16 channel_flags = 0;
  229. int mpdulen, chain;
  230. unsigned long chains = status->chains;
  231. struct ieee80211_vendor_radiotap rtap = {};
  232. struct ieee80211_radiotap_he he = {};
  233. struct ieee80211_radiotap_he_mu he_mu = {};
  234. if (status->flag & RX_FLAG_RADIOTAP_HE) {
  235. he = *(struct ieee80211_radiotap_he *)skb->data;
  236. skb_pull(skb, sizeof(he));
  237. WARN_ON_ONCE(status->encoding != RX_ENC_HE);
  238. }
  239. if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  240. he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
  241. skb_pull(skb, sizeof(he_mu));
  242. }
  243. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  244. rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
  245. /* rtap.len and rtap.pad are undone immediately */
  246. skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
  247. }
  248. mpdulen = skb->len;
  249. if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
  250. mpdulen += FCS_LEN;
  251. rthdr = skb_push(skb, rtap_len);
  252. memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
  253. it_present = &rthdr->it_present;
  254. /* radiotap header, set always present flags */
  255. rthdr->it_len = cpu_to_le16(rtap_len);
  256. it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
  257. BIT(IEEE80211_RADIOTAP_CHANNEL) |
  258. BIT(IEEE80211_RADIOTAP_RX_FLAGS);
  259. if (!status->chains)
  260. it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
  261. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  262. it_present_val |=
  263. BIT(IEEE80211_RADIOTAP_EXT) |
  264. BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
  265. put_unaligned_le32(it_present_val, it_present);
  266. it_present++;
  267. it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
  268. BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  269. }
  270. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  271. it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
  272. BIT(IEEE80211_RADIOTAP_EXT);
  273. put_unaligned_le32(it_present_val, it_present);
  274. it_present++;
  275. it_present_val = rtap.present;
  276. }
  277. put_unaligned_le32(it_present_val, it_present);
  278. pos = (void *)(it_present + 1);
  279. /* the order of the following fields is important */
  280. /* IEEE80211_RADIOTAP_TSFT */
  281. if (ieee80211_have_rx_timestamp(status)) {
  282. /* padding */
  283. while ((pos - (u8 *)rthdr) & 7)
  284. *pos++ = 0;
  285. put_unaligned_le64(
  286. ieee80211_calculate_rx_timestamp(local, status,
  287. mpdulen, 0),
  288. pos);
  289. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  290. pos += 8;
  291. }
  292. /* IEEE80211_RADIOTAP_FLAGS */
  293. if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
  294. *pos |= IEEE80211_RADIOTAP_F_FCS;
  295. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  296. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  297. if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
  298. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  299. pos++;
  300. /* IEEE80211_RADIOTAP_RATE */
  301. if (!rate || status->encoding != RX_ENC_LEGACY) {
  302. /*
  303. * Without rate information don't add it. If we have,
  304. * MCS information is a separate field in radiotap,
  305. * added below. The byte here is needed as padding
  306. * for the channel though, so initialise it to 0.
  307. */
  308. *pos = 0;
  309. } else {
  310. int shift = 0;
  311. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  312. if (status->bw == RATE_INFO_BW_10)
  313. shift = 1;
  314. else if (status->bw == RATE_INFO_BW_5)
  315. shift = 2;
  316. *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
  317. }
  318. pos++;
  319. /* IEEE80211_RADIOTAP_CHANNEL */
  320. put_unaligned_le16(status->freq, pos);
  321. pos += 2;
  322. if (status->bw == RATE_INFO_BW_10)
  323. channel_flags |= IEEE80211_CHAN_HALF;
  324. else if (status->bw == RATE_INFO_BW_5)
  325. channel_flags |= IEEE80211_CHAN_QUARTER;
  326. if (status->band == NL80211_BAND_5GHZ)
  327. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
  328. else if (status->encoding != RX_ENC_LEGACY)
  329. channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
  330. else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
  331. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
  332. else if (rate)
  333. channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
  334. else
  335. channel_flags |= IEEE80211_CHAN_2GHZ;
  336. put_unaligned_le16(channel_flags, pos);
  337. pos += 2;
  338. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  339. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
  340. !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  341. *pos = status->signal;
  342. rthdr->it_present |=
  343. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  344. pos++;
  345. }
  346. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  347. if (!status->chains) {
  348. /* IEEE80211_RADIOTAP_ANTENNA */
  349. *pos = status->antenna;
  350. pos++;
  351. }
  352. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  353. /* IEEE80211_RADIOTAP_RX_FLAGS */
  354. /* ensure 2 byte alignment for the 2 byte field as required */
  355. if ((pos - (u8 *)rthdr) & 1)
  356. *pos++ = 0;
  357. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  358. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  359. put_unaligned_le16(rx_flags, pos);
  360. pos += 2;
  361. if (status->encoding == RX_ENC_HT) {
  362. unsigned int stbc;
  363. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  364. *pos++ = local->hw.radiotap_mcs_details;
  365. *pos = 0;
  366. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  367. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  368. if (status->bw == RATE_INFO_BW_40)
  369. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  370. if (status->enc_flags & RX_ENC_FLAG_HT_GF)
  371. *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
  372. if (status->enc_flags & RX_ENC_FLAG_LDPC)
  373. *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
  374. stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
  375. *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
  376. pos++;
  377. *pos++ = status->rate_idx;
  378. }
  379. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  380. u16 flags = 0;
  381. /* ensure 4 byte alignment */
  382. while ((pos - (u8 *)rthdr) & 3)
  383. pos++;
  384. rthdr->it_present |=
  385. cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
  386. put_unaligned_le32(status->ampdu_reference, pos);
  387. pos += 4;
  388. if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
  389. flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
  390. if (status->flag & RX_FLAG_AMPDU_IS_LAST)
  391. flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
  392. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
  393. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
  394. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  395. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
  396. if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
  397. flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
  398. if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
  399. flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
  400. put_unaligned_le16(flags, pos);
  401. pos += 2;
  402. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  403. *pos++ = status->ampdu_delimiter_crc;
  404. else
  405. *pos++ = 0;
  406. *pos++ = 0;
  407. }
  408. if (status->encoding == RX_ENC_VHT) {
  409. u16 known = local->hw.radiotap_vht_details;
  410. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
  411. put_unaligned_le16(known, pos);
  412. pos += 2;
  413. /* flags */
  414. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  415. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
  416. /* in VHT, STBC is binary */
  417. if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
  418. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
  419. if (status->enc_flags & RX_ENC_FLAG_BF)
  420. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
  421. pos++;
  422. /* bandwidth */
  423. switch (status->bw) {
  424. case RATE_INFO_BW_80:
  425. *pos++ = 4;
  426. break;
  427. case RATE_INFO_BW_160:
  428. *pos++ = 11;
  429. break;
  430. case RATE_INFO_BW_40:
  431. *pos++ = 1;
  432. break;
  433. default:
  434. *pos++ = 0;
  435. }
  436. /* MCS/NSS */
  437. *pos = (status->rate_idx << 4) | status->nss;
  438. pos += 4;
  439. /* coding field */
  440. if (status->enc_flags & RX_ENC_FLAG_LDPC)
  441. *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
  442. pos++;
  443. /* group ID */
  444. pos++;
  445. /* partial_aid */
  446. pos += 2;
  447. }
  448. if (local->hw.radiotap_timestamp.units_pos >= 0) {
  449. u16 accuracy = 0;
  450. u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
  451. rthdr->it_present |=
  452. cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
  453. /* ensure 8 byte alignment */
  454. while ((pos - (u8 *)rthdr) & 7)
  455. pos++;
  456. put_unaligned_le64(status->device_timestamp, pos);
  457. pos += sizeof(u64);
  458. if (local->hw.radiotap_timestamp.accuracy >= 0) {
  459. accuracy = local->hw.radiotap_timestamp.accuracy;
  460. flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
  461. }
  462. put_unaligned_le16(accuracy, pos);
  463. pos += sizeof(u16);
  464. *pos++ = local->hw.radiotap_timestamp.units_pos;
  465. *pos++ = flags;
  466. }
  467. if (status->encoding == RX_ENC_HE &&
  468. status->flag & RX_FLAG_RADIOTAP_HE) {
  469. #define HE_PREP(f, val) cpu_to_le16(FIELD_PREP(IEEE80211_RADIOTAP_HE_##f, val))
  470. if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
  471. he.data6 |= HE_PREP(DATA6_NSTS,
  472. FIELD_GET(RX_ENC_FLAG_STBC_MASK,
  473. status->enc_flags));
  474. he.data3 |= HE_PREP(DATA3_STBC, 1);
  475. } else {
  476. he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
  477. }
  478. #define CHECK_GI(s) \
  479. BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
  480. (int)NL80211_RATE_INFO_HE_GI_##s)
  481. CHECK_GI(0_8);
  482. CHECK_GI(1_6);
  483. CHECK_GI(3_2);
  484. he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
  485. he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
  486. he.data3 |= HE_PREP(DATA3_CODING,
  487. !!(status->enc_flags & RX_ENC_FLAG_LDPC));
  488. he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
  489. switch (status->bw) {
  490. case RATE_INFO_BW_20:
  491. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  492. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
  493. break;
  494. case RATE_INFO_BW_40:
  495. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  496. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
  497. break;
  498. case RATE_INFO_BW_80:
  499. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  500. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
  501. break;
  502. case RATE_INFO_BW_160:
  503. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  504. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
  505. break;
  506. case RATE_INFO_BW_HE_RU:
  507. #define CHECK_RU_ALLOC(s) \
  508. BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
  509. NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
  510. CHECK_RU_ALLOC(26);
  511. CHECK_RU_ALLOC(52);
  512. CHECK_RU_ALLOC(106);
  513. CHECK_RU_ALLOC(242);
  514. CHECK_RU_ALLOC(484);
  515. CHECK_RU_ALLOC(996);
  516. CHECK_RU_ALLOC(2x996);
  517. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  518. status->he_ru + 4);
  519. break;
  520. default:
  521. WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
  522. }
  523. /* ensure 2 byte alignment */
  524. while ((pos - (u8 *)rthdr) & 1)
  525. pos++;
  526. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
  527. memcpy(pos, &he, sizeof(he));
  528. pos += sizeof(he);
  529. }
  530. if (status->encoding == RX_ENC_HE &&
  531. status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  532. /* ensure 2 byte alignment */
  533. while ((pos - (u8 *)rthdr) & 1)
  534. pos++;
  535. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
  536. memcpy(pos, &he_mu, sizeof(he_mu));
  537. pos += sizeof(he_mu);
  538. }
  539. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  540. *pos++ = status->chain_signal[chain];
  541. *pos++ = chain;
  542. }
  543. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  544. /* ensure 2 byte alignment for the vendor field as required */
  545. if ((pos - (u8 *)rthdr) & 1)
  546. *pos++ = 0;
  547. *pos++ = rtap.oui[0];
  548. *pos++ = rtap.oui[1];
  549. *pos++ = rtap.oui[2];
  550. *pos++ = rtap.subns;
  551. put_unaligned_le16(rtap.len, pos);
  552. pos += 2;
  553. /* align the actual payload as requested */
  554. while ((pos - (u8 *)rthdr) & (rtap.align - 1))
  555. *pos++ = 0;
  556. /* data (and possible padding) already follows */
  557. }
  558. }
  559. static struct sk_buff *
  560. ieee80211_make_monitor_skb(struct ieee80211_local *local,
  561. struct sk_buff **origskb,
  562. struct ieee80211_rate *rate,
  563. int rtap_space, bool use_origskb)
  564. {
  565. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
  566. int rt_hdrlen, needed_headroom;
  567. struct sk_buff *skb;
  568. /* room for the radiotap header based on driver features */
  569. rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
  570. needed_headroom = rt_hdrlen - rtap_space;
  571. if (use_origskb) {
  572. /* only need to expand headroom if necessary */
  573. skb = *origskb;
  574. *origskb = NULL;
  575. /*
  576. * This shouldn't trigger often because most devices have an
  577. * RX header they pull before we get here, and that should
  578. * be big enough for our radiotap information. We should
  579. * probably export the length to drivers so that we can have
  580. * them allocate enough headroom to start with.
  581. */
  582. if (skb_headroom(skb) < needed_headroom &&
  583. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  584. dev_kfree_skb(skb);
  585. return NULL;
  586. }
  587. } else {
  588. /*
  589. * Need to make a copy and possibly remove radiotap header
  590. * and FCS from the original.
  591. */
  592. skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
  593. if (!skb)
  594. return NULL;
  595. }
  596. /* prepend radiotap information */
  597. ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
  598. skb_reset_mac_header(skb);
  599. skb->ip_summed = CHECKSUM_UNNECESSARY;
  600. skb->pkt_type = PACKET_OTHERHOST;
  601. skb->protocol = htons(ETH_P_802_2);
  602. return skb;
  603. }
  604. /*
  605. * This function copies a received frame to all monitor interfaces and
  606. * returns a cleaned-up SKB that no longer includes the FCS nor the
  607. * radiotap header the driver might have added.
  608. */
  609. static struct sk_buff *
  610. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  611. struct ieee80211_rate *rate)
  612. {
  613. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  614. struct ieee80211_sub_if_data *sdata;
  615. struct sk_buff *monskb = NULL;
  616. int present_fcs_len = 0;
  617. unsigned int rtap_space = 0;
  618. struct ieee80211_sub_if_data *monitor_sdata =
  619. rcu_dereference(local->monitor_sdata);
  620. bool only_monitor = false;
  621. if (status->flag & RX_FLAG_RADIOTAP_HE)
  622. rtap_space += sizeof(struct ieee80211_radiotap_he);
  623. if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
  624. rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
  625. if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
  626. struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
  627. rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
  628. }
  629. /*
  630. * First, we may need to make a copy of the skb because
  631. * (1) we need to modify it for radiotap (if not present), and
  632. * (2) the other RX handlers will modify the skb we got.
  633. *
  634. * We don't need to, of course, if we aren't going to return
  635. * the SKB because it has a bad FCS/PLCP checksum.
  636. */
  637. if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
  638. if (unlikely(origskb->len <= FCS_LEN)) {
  639. /* driver bug */
  640. WARN_ON(1);
  641. dev_kfree_skb(origskb);
  642. return NULL;
  643. }
  644. present_fcs_len = FCS_LEN;
  645. }
  646. /* ensure hdr->frame_control and vendor radiotap data are in skb head */
  647. if (!pskb_may_pull(origskb, 2 + rtap_space)) {
  648. dev_kfree_skb(origskb);
  649. return NULL;
  650. }
  651. only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
  652. if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
  653. if (only_monitor) {
  654. dev_kfree_skb(origskb);
  655. return NULL;
  656. }
  657. remove_monitor_info(origskb, present_fcs_len, rtap_space);
  658. return origskb;
  659. }
  660. ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
  661. list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
  662. bool last_monitor = list_is_last(&sdata->u.mntr.list,
  663. &local->mon_list);
  664. if (!monskb)
  665. monskb = ieee80211_make_monitor_skb(local, &origskb,
  666. rate, rtap_space,
  667. only_monitor &&
  668. last_monitor);
  669. if (monskb) {
  670. struct sk_buff *skb;
  671. if (last_monitor) {
  672. skb = monskb;
  673. monskb = NULL;
  674. } else {
  675. skb = skb_clone(monskb, GFP_ATOMIC);
  676. }
  677. if (skb) {
  678. skb->dev = sdata->dev;
  679. ieee80211_rx_stats(skb->dev, skb->len);
  680. netif_receive_skb(skb);
  681. }
  682. }
  683. if (last_monitor)
  684. break;
  685. }
  686. /* this happens if last_monitor was erroneously false */
  687. dev_kfree_skb(monskb);
  688. /* ditto */
  689. if (!origskb)
  690. return NULL;
  691. remove_monitor_info(origskb, present_fcs_len, rtap_space);
  692. return origskb;
  693. }
  694. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  695. {
  696. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  697. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  698. int tid, seqno_idx, security_idx;
  699. /* does the frame have a qos control field? */
  700. if (ieee80211_is_data_qos(hdr->frame_control)) {
  701. u8 *qc = ieee80211_get_qos_ctl(hdr);
  702. /* frame has qos control */
  703. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  704. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  705. status->rx_flags |= IEEE80211_RX_AMSDU;
  706. seqno_idx = tid;
  707. security_idx = tid;
  708. } else {
  709. /*
  710. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  711. *
  712. * Sequence numbers for management frames, QoS data
  713. * frames with a broadcast/multicast address in the
  714. * Address 1 field, and all non-QoS data frames sent
  715. * by QoS STAs are assigned using an additional single
  716. * modulo-4096 counter, [...]
  717. *
  718. * We also use that counter for non-QoS STAs.
  719. */
  720. seqno_idx = IEEE80211_NUM_TIDS;
  721. security_idx = 0;
  722. if (ieee80211_is_mgmt(hdr->frame_control))
  723. security_idx = IEEE80211_NUM_TIDS;
  724. tid = 0;
  725. }
  726. rx->seqno_idx = seqno_idx;
  727. rx->security_idx = security_idx;
  728. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  729. * For now, set skb->priority to 0 for other cases. */
  730. rx->skb->priority = (tid > 7) ? 0 : tid;
  731. }
  732. /**
  733. * DOC: Packet alignment
  734. *
  735. * Drivers always need to pass packets that are aligned to two-byte boundaries
  736. * to the stack.
  737. *
  738. * Additionally, should, if possible, align the payload data in a way that
  739. * guarantees that the contained IP header is aligned to a four-byte
  740. * boundary. In the case of regular frames, this simply means aligning the
  741. * payload to a four-byte boundary (because either the IP header is directly
  742. * contained, or IV/RFC1042 headers that have a length divisible by four are
  743. * in front of it). If the payload data is not properly aligned and the
  744. * architecture doesn't support efficient unaligned operations, mac80211
  745. * will align the data.
  746. *
  747. * With A-MSDU frames, however, the payload data address must yield two modulo
  748. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  749. * push the IP header further back to a multiple of four again. Thankfully, the
  750. * specs were sane enough this time around to require padding each A-MSDU
  751. * subframe to a length that is a multiple of four.
  752. *
  753. * Padding like Atheros hardware adds which is between the 802.11 header and
  754. * the payload is not supported, the driver is required to move the 802.11
  755. * header to be directly in front of the payload in that case.
  756. */
  757. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  758. {
  759. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  760. WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
  761. #endif
  762. }
  763. /* rx handlers */
  764. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  765. {
  766. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  767. if (is_multicast_ether_addr(hdr->addr1))
  768. return 0;
  769. return ieee80211_is_robust_mgmt_frame(skb);
  770. }
  771. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  772. {
  773. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  774. if (!is_multicast_ether_addr(hdr->addr1))
  775. return 0;
  776. return ieee80211_is_robust_mgmt_frame(skb);
  777. }
  778. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  779. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  780. {
  781. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  782. struct ieee80211_mmie *mmie;
  783. struct ieee80211_mmie_16 *mmie16;
  784. if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
  785. return -1;
  786. if (!ieee80211_is_robust_mgmt_frame(skb))
  787. return -1; /* not a robust management frame */
  788. mmie = (struct ieee80211_mmie *)
  789. (skb->data + skb->len - sizeof(*mmie));
  790. if (mmie->element_id == WLAN_EID_MMIE &&
  791. mmie->length == sizeof(*mmie) - 2)
  792. return le16_to_cpu(mmie->key_id);
  793. mmie16 = (struct ieee80211_mmie_16 *)
  794. (skb->data + skb->len - sizeof(*mmie16));
  795. if (skb->len >= 24 + sizeof(*mmie16) &&
  796. mmie16->element_id == WLAN_EID_MMIE &&
  797. mmie16->length == sizeof(*mmie16) - 2)
  798. return le16_to_cpu(mmie16->key_id);
  799. return -1;
  800. }
  801. static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
  802. struct sk_buff *skb)
  803. {
  804. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  805. __le16 fc;
  806. int hdrlen;
  807. u8 keyid;
  808. fc = hdr->frame_control;
  809. hdrlen = ieee80211_hdrlen(fc);
  810. if (skb->len < hdrlen + cs->hdr_len)
  811. return -EINVAL;
  812. skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
  813. keyid &= cs->key_idx_mask;
  814. keyid >>= cs->key_idx_shift;
  815. return keyid;
  816. }
  817. static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  818. {
  819. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  820. char *dev_addr = rx->sdata->vif.addr;
  821. if (ieee80211_is_data(hdr->frame_control)) {
  822. if (is_multicast_ether_addr(hdr->addr1)) {
  823. if (ieee80211_has_tods(hdr->frame_control) ||
  824. !ieee80211_has_fromds(hdr->frame_control))
  825. return RX_DROP_MONITOR;
  826. if (ether_addr_equal(hdr->addr3, dev_addr))
  827. return RX_DROP_MONITOR;
  828. } else {
  829. if (!ieee80211_has_a4(hdr->frame_control))
  830. return RX_DROP_MONITOR;
  831. if (ether_addr_equal(hdr->addr4, dev_addr))
  832. return RX_DROP_MONITOR;
  833. }
  834. }
  835. /* If there is not an established peer link and this is not a peer link
  836. * establisment frame, beacon or probe, drop the frame.
  837. */
  838. if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
  839. struct ieee80211_mgmt *mgmt;
  840. if (!ieee80211_is_mgmt(hdr->frame_control))
  841. return RX_DROP_MONITOR;
  842. if (ieee80211_is_action(hdr->frame_control)) {
  843. u8 category;
  844. /* make sure category field is present */
  845. if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
  846. return RX_DROP_MONITOR;
  847. mgmt = (struct ieee80211_mgmt *)hdr;
  848. category = mgmt->u.action.category;
  849. if (category != WLAN_CATEGORY_MESH_ACTION &&
  850. category != WLAN_CATEGORY_SELF_PROTECTED)
  851. return RX_DROP_MONITOR;
  852. return RX_CONTINUE;
  853. }
  854. if (ieee80211_is_probe_req(hdr->frame_control) ||
  855. ieee80211_is_probe_resp(hdr->frame_control) ||
  856. ieee80211_is_beacon(hdr->frame_control) ||
  857. ieee80211_is_auth(hdr->frame_control))
  858. return RX_CONTINUE;
  859. return RX_DROP_MONITOR;
  860. }
  861. return RX_CONTINUE;
  862. }
  863. static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
  864. int index)
  865. {
  866. struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
  867. struct sk_buff *tail = skb_peek_tail(frames);
  868. struct ieee80211_rx_status *status;
  869. if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
  870. return true;
  871. if (!tail)
  872. return false;
  873. status = IEEE80211_SKB_RXCB(tail);
  874. if (status->flag & RX_FLAG_AMSDU_MORE)
  875. return false;
  876. return true;
  877. }
  878. static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
  879. struct tid_ampdu_rx *tid_agg_rx,
  880. int index,
  881. struct sk_buff_head *frames)
  882. {
  883. struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
  884. struct sk_buff *skb;
  885. struct ieee80211_rx_status *status;
  886. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  887. if (skb_queue_empty(skb_list))
  888. goto no_frame;
  889. if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  890. __skb_queue_purge(skb_list);
  891. goto no_frame;
  892. }
  893. /* release frames from the reorder ring buffer */
  894. tid_agg_rx->stored_mpdu_num--;
  895. while ((skb = __skb_dequeue(skb_list))) {
  896. status = IEEE80211_SKB_RXCB(skb);
  897. status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
  898. __skb_queue_tail(frames, skb);
  899. }
  900. no_frame:
  901. tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
  902. tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  903. }
  904. static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
  905. struct tid_ampdu_rx *tid_agg_rx,
  906. u16 head_seq_num,
  907. struct sk_buff_head *frames)
  908. {
  909. int index;
  910. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  911. while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  912. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  913. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  914. frames);
  915. }
  916. }
  917. /*
  918. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  919. * the skb was added to the buffer longer than this time ago, the earlier
  920. * frames that have not yet been received are assumed to be lost and the skb
  921. * can be released for processing. This may also release other skb's from the
  922. * reorder buffer if there are no additional gaps between the frames.
  923. *
  924. * Callers must hold tid_agg_rx->reorder_lock.
  925. */
  926. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  927. static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
  928. struct tid_ampdu_rx *tid_agg_rx,
  929. struct sk_buff_head *frames)
  930. {
  931. int index, i, j;
  932. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  933. /* release the buffer until next missing frame */
  934. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  935. if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
  936. tid_agg_rx->stored_mpdu_num) {
  937. /*
  938. * No buffers ready to be released, but check whether any
  939. * frames in the reorder buffer have timed out.
  940. */
  941. int skipped = 1;
  942. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  943. j = (j + 1) % tid_agg_rx->buf_size) {
  944. if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
  945. skipped++;
  946. continue;
  947. }
  948. if (skipped &&
  949. !time_after(jiffies, tid_agg_rx->reorder_time[j] +
  950. HT_RX_REORDER_BUF_TIMEOUT))
  951. goto set_release_timer;
  952. /* don't leave incomplete A-MSDUs around */
  953. for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
  954. i = (i + 1) % tid_agg_rx->buf_size)
  955. __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
  956. ht_dbg_ratelimited(sdata,
  957. "release an RX reorder frame due to timeout on earlier frames\n");
  958. ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
  959. frames);
  960. /*
  961. * Increment the head seq# also for the skipped slots.
  962. */
  963. tid_agg_rx->head_seq_num =
  964. (tid_agg_rx->head_seq_num +
  965. skipped) & IEEE80211_SN_MASK;
  966. skipped = 0;
  967. }
  968. } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  969. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  970. frames);
  971. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  972. }
  973. if (tid_agg_rx->stored_mpdu_num) {
  974. j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  975. for (; j != (index - 1) % tid_agg_rx->buf_size;
  976. j = (j + 1) % tid_agg_rx->buf_size) {
  977. if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
  978. break;
  979. }
  980. set_release_timer:
  981. if (!tid_agg_rx->removed)
  982. mod_timer(&tid_agg_rx->reorder_timer,
  983. tid_agg_rx->reorder_time[j] + 1 +
  984. HT_RX_REORDER_BUF_TIMEOUT);
  985. } else {
  986. del_timer(&tid_agg_rx->reorder_timer);
  987. }
  988. }
  989. /*
  990. * As this function belongs to the RX path it must be under
  991. * rcu_read_lock protection. It returns false if the frame
  992. * can be processed immediately, true if it was consumed.
  993. */
  994. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
  995. struct tid_ampdu_rx *tid_agg_rx,
  996. struct sk_buff *skb,
  997. struct sk_buff_head *frames)
  998. {
  999. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1000. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1001. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  1002. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1003. u16 head_seq_num, buf_size;
  1004. int index;
  1005. bool ret = true;
  1006. spin_lock(&tid_agg_rx->reorder_lock);
  1007. /*
  1008. * Offloaded BA sessions have no known starting sequence number so pick
  1009. * one from first Rxed frame for this tid after BA was started.
  1010. */
  1011. if (unlikely(tid_agg_rx->auto_seq)) {
  1012. tid_agg_rx->auto_seq = false;
  1013. tid_agg_rx->ssn = mpdu_seq_num;
  1014. tid_agg_rx->head_seq_num = mpdu_seq_num;
  1015. }
  1016. buf_size = tid_agg_rx->buf_size;
  1017. head_seq_num = tid_agg_rx->head_seq_num;
  1018. /*
  1019. * If the current MPDU's SN is smaller than the SSN, it shouldn't
  1020. * be reordered.
  1021. */
  1022. if (unlikely(!tid_agg_rx->started)) {
  1023. if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
  1024. ret = false;
  1025. goto out;
  1026. }
  1027. tid_agg_rx->started = true;
  1028. }
  1029. /* frame with out of date sequence number */
  1030. if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
  1031. dev_kfree_skb(skb);
  1032. goto out;
  1033. }
  1034. /*
  1035. * If frame the sequence number exceeds our buffering window
  1036. * size release some previous frames to make room for this one.
  1037. */
  1038. if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
  1039. head_seq_num = ieee80211_sn_inc(
  1040. ieee80211_sn_sub(mpdu_seq_num, buf_size));
  1041. /* release stored frames up to new head to stack */
  1042. ieee80211_release_reorder_frames(sdata, tid_agg_rx,
  1043. head_seq_num, frames);
  1044. }
  1045. /* Now the new frame is always in the range of the reordering buffer */
  1046. index = mpdu_seq_num % tid_agg_rx->buf_size;
  1047. /* check if we already stored this frame */
  1048. if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  1049. dev_kfree_skb(skb);
  1050. goto out;
  1051. }
  1052. /*
  1053. * If the current MPDU is in the right order and nothing else
  1054. * is stored we can process it directly, no need to buffer it.
  1055. * If it is first but there's something stored, we may be able
  1056. * to release frames after this one.
  1057. */
  1058. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1059. tid_agg_rx->stored_mpdu_num == 0) {
  1060. if (!(status->flag & RX_FLAG_AMSDU_MORE))
  1061. tid_agg_rx->head_seq_num =
  1062. ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  1063. ret = false;
  1064. goto out;
  1065. }
  1066. /* put the frame in the reordering buffer */
  1067. __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
  1068. if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  1069. tid_agg_rx->reorder_time[index] = jiffies;
  1070. tid_agg_rx->stored_mpdu_num++;
  1071. ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
  1072. }
  1073. out:
  1074. spin_unlock(&tid_agg_rx->reorder_lock);
  1075. return ret;
  1076. }
  1077. /*
  1078. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  1079. * true if the MPDU was buffered, false if it should be processed.
  1080. */
  1081. static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
  1082. struct sk_buff_head *frames)
  1083. {
  1084. struct sk_buff *skb = rx->skb;
  1085. struct ieee80211_local *local = rx->local;
  1086. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1087. struct sta_info *sta = rx->sta;
  1088. struct tid_ampdu_rx *tid_agg_rx;
  1089. u16 sc;
  1090. u8 tid, ack_policy;
  1091. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  1092. is_multicast_ether_addr(hdr->addr1))
  1093. goto dont_reorder;
  1094. /*
  1095. * filter the QoS data rx stream according to
  1096. * STA/TID and check if this STA/TID is on aggregation
  1097. */
  1098. if (!sta)
  1099. goto dont_reorder;
  1100. ack_policy = *ieee80211_get_qos_ctl(hdr) &
  1101. IEEE80211_QOS_CTL_ACK_POLICY_MASK;
  1102. tid = ieee80211_get_tid(hdr);
  1103. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  1104. if (!tid_agg_rx) {
  1105. if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  1106. !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
  1107. !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
  1108. ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
  1109. WLAN_BACK_RECIPIENT,
  1110. WLAN_REASON_QSTA_REQUIRE_SETUP);
  1111. goto dont_reorder;
  1112. }
  1113. /* qos null data frames are excluded */
  1114. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  1115. goto dont_reorder;
  1116. /* not part of a BA session */
  1117. if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  1118. ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
  1119. goto dont_reorder;
  1120. /* new, potentially un-ordered, ampdu frame - process it */
  1121. /* reset session timer */
  1122. if (tid_agg_rx->timeout)
  1123. tid_agg_rx->last_rx = jiffies;
  1124. /* if this mpdu is fragmented - terminate rx aggregation session */
  1125. sc = le16_to_cpu(hdr->seq_ctrl);
  1126. if (sc & IEEE80211_SCTL_FRAG) {
  1127. skb_queue_tail(&rx->sdata->skb_queue, skb);
  1128. ieee80211_queue_work(&local->hw, &rx->sdata->work);
  1129. return;
  1130. }
  1131. /*
  1132. * No locking needed -- we will only ever process one
  1133. * RX packet at a time, and thus own tid_agg_rx. All
  1134. * other code manipulating it needs to (and does) make
  1135. * sure that we cannot get to it any more before doing
  1136. * anything with it.
  1137. */
  1138. if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
  1139. frames))
  1140. return;
  1141. dont_reorder:
  1142. __skb_queue_tail(frames, skb);
  1143. }
  1144. static ieee80211_rx_result debug_noinline
  1145. ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
  1146. {
  1147. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1148. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1149. if (status->flag & RX_FLAG_DUP_VALIDATED)
  1150. return RX_CONTINUE;
  1151. /*
  1152. * Drop duplicate 802.11 retransmissions
  1153. * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
  1154. */
  1155. if (rx->skb->len < 24)
  1156. return RX_CONTINUE;
  1157. if (ieee80211_is_ctl(hdr->frame_control) ||
  1158. ieee80211_is_qos_nullfunc(hdr->frame_control) ||
  1159. is_multicast_ether_addr(hdr->addr1))
  1160. return RX_CONTINUE;
  1161. if (!rx->sta)
  1162. return RX_CONTINUE;
  1163. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  1164. rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
  1165. I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
  1166. rx->sta->rx_stats.num_duplicates++;
  1167. return RX_DROP_UNUSABLE;
  1168. } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  1169. rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
  1170. }
  1171. return RX_CONTINUE;
  1172. }
  1173. static ieee80211_rx_result debug_noinline
  1174. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  1175. {
  1176. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1177. /* Drop disallowed frame classes based on STA auth/assoc state;
  1178. * IEEE 802.11, Chap 5.5.
  1179. *
  1180. * mac80211 filters only based on association state, i.e. it drops
  1181. * Class 3 frames from not associated stations. hostapd sends
  1182. * deauth/disassoc frames when needed. In addition, hostapd is
  1183. * responsible for filtering on both auth and assoc states.
  1184. */
  1185. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1186. return ieee80211_rx_mesh_check(rx);
  1187. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  1188. ieee80211_is_pspoll(hdr->frame_control)) &&
  1189. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1190. rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
  1191. rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
  1192. (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
  1193. /*
  1194. * accept port control frames from the AP even when it's not
  1195. * yet marked ASSOC to prevent a race where we don't set the
  1196. * assoc bit quickly enough before it sends the first frame
  1197. */
  1198. if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1199. ieee80211_is_data_present(hdr->frame_control)) {
  1200. unsigned int hdrlen;
  1201. __be16 ethertype;
  1202. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1203. if (rx->skb->len < hdrlen + 8)
  1204. return RX_DROP_MONITOR;
  1205. skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
  1206. if (ethertype == rx->sdata->control_port_protocol)
  1207. return RX_CONTINUE;
  1208. }
  1209. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  1210. cfg80211_rx_spurious_frame(rx->sdata->dev,
  1211. hdr->addr2,
  1212. GFP_ATOMIC))
  1213. return RX_DROP_UNUSABLE;
  1214. return RX_DROP_MONITOR;
  1215. }
  1216. return RX_CONTINUE;
  1217. }
  1218. static ieee80211_rx_result debug_noinline
  1219. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  1220. {
  1221. struct ieee80211_local *local;
  1222. struct ieee80211_hdr *hdr;
  1223. struct sk_buff *skb;
  1224. local = rx->local;
  1225. skb = rx->skb;
  1226. hdr = (struct ieee80211_hdr *) skb->data;
  1227. if (!local->pspolling)
  1228. return RX_CONTINUE;
  1229. if (!ieee80211_has_fromds(hdr->frame_control))
  1230. /* this is not from AP */
  1231. return RX_CONTINUE;
  1232. if (!ieee80211_is_data(hdr->frame_control))
  1233. return RX_CONTINUE;
  1234. if (!ieee80211_has_moredata(hdr->frame_control)) {
  1235. /* AP has no more frames buffered for us */
  1236. local->pspolling = false;
  1237. return RX_CONTINUE;
  1238. }
  1239. /* more data bit is set, let's request a new frame from the AP */
  1240. ieee80211_send_pspoll(local, rx->sdata);
  1241. return RX_CONTINUE;
  1242. }
  1243. static void sta_ps_start(struct sta_info *sta)
  1244. {
  1245. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1246. struct ieee80211_local *local = sdata->local;
  1247. struct ps_data *ps;
  1248. int tid;
  1249. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1250. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1251. ps = &sdata->bss->ps;
  1252. else
  1253. return;
  1254. atomic_inc(&ps->num_sta_ps);
  1255. set_sta_flag(sta, WLAN_STA_PS_STA);
  1256. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  1257. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  1258. ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
  1259. sta->sta.addr, sta->sta.aid);
  1260. ieee80211_clear_fast_xmit(sta);
  1261. if (!sta->sta.txq[0])
  1262. return;
  1263. for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
  1264. if (txq_has_queue(sta->sta.txq[tid]))
  1265. set_bit(tid, &sta->txq_buffered_tids);
  1266. else
  1267. clear_bit(tid, &sta->txq_buffered_tids);
  1268. }
  1269. }
  1270. static void sta_ps_end(struct sta_info *sta)
  1271. {
  1272. ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
  1273. sta->sta.addr, sta->sta.aid);
  1274. if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  1275. /*
  1276. * Clear the flag only if the other one is still set
  1277. * so that the TX path won't start TX'ing new frames
  1278. * directly ... In the case that the driver flag isn't
  1279. * set ieee80211_sta_ps_deliver_wakeup() will clear it.
  1280. */
  1281. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1282. ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
  1283. sta->sta.addr, sta->sta.aid);
  1284. return;
  1285. }
  1286. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1287. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1288. ieee80211_sta_ps_deliver_wakeup(sta);
  1289. }
  1290. int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
  1291. {
  1292. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1293. bool in_ps;
  1294. WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
  1295. /* Don't let the same PS state be set twice */
  1296. in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
  1297. if ((start && in_ps) || (!start && !in_ps))
  1298. return -EINVAL;
  1299. if (start)
  1300. sta_ps_start(sta);
  1301. else
  1302. sta_ps_end(sta);
  1303. return 0;
  1304. }
  1305. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  1306. void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
  1307. {
  1308. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1309. if (test_sta_flag(sta, WLAN_STA_SP))
  1310. return;
  1311. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1312. ieee80211_sta_ps_deliver_poll_response(sta);
  1313. else
  1314. set_sta_flag(sta, WLAN_STA_PSPOLL);
  1315. }
  1316. EXPORT_SYMBOL(ieee80211_sta_pspoll);
  1317. void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
  1318. {
  1319. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1320. int ac = ieee80211_ac_from_tid(tid);
  1321. /*
  1322. * If this AC is not trigger-enabled do nothing unless the
  1323. * driver is calling us after it already checked.
  1324. *
  1325. * NB: This could/should check a separate bitmap of trigger-
  1326. * enabled queues, but for now we only implement uAPSD w/o
  1327. * TSPEC changes to the ACs, so they're always the same.
  1328. */
  1329. if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
  1330. tid != IEEE80211_NUM_TIDS)
  1331. return;
  1332. /* if we are in a service period, do nothing */
  1333. if (test_sta_flag(sta, WLAN_STA_SP))
  1334. return;
  1335. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1336. ieee80211_sta_ps_deliver_uapsd(sta);
  1337. else
  1338. set_sta_flag(sta, WLAN_STA_UAPSD);
  1339. }
  1340. EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
  1341. static ieee80211_rx_result debug_noinline
  1342. ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
  1343. {
  1344. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1345. struct ieee80211_hdr *hdr = (void *)rx->skb->data;
  1346. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1347. if (!rx->sta)
  1348. return RX_CONTINUE;
  1349. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1350. sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1351. return RX_CONTINUE;
  1352. /*
  1353. * The device handles station powersave, so don't do anything about
  1354. * uAPSD and PS-Poll frames (the latter shouldn't even come up from
  1355. * it to mac80211 since they're handled.)
  1356. */
  1357. if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
  1358. return RX_CONTINUE;
  1359. /*
  1360. * Don't do anything if the station isn't already asleep. In
  1361. * the uAPSD case, the station will probably be marked asleep,
  1362. * in the PS-Poll case the station must be confused ...
  1363. */
  1364. if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
  1365. return RX_CONTINUE;
  1366. if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
  1367. ieee80211_sta_pspoll(&rx->sta->sta);
  1368. /* Free PS Poll skb here instead of returning RX_DROP that would
  1369. * count as an dropped frame. */
  1370. dev_kfree_skb(rx->skb);
  1371. return RX_QUEUED;
  1372. } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1373. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1374. ieee80211_has_pm(hdr->frame_control) &&
  1375. (ieee80211_is_data_qos(hdr->frame_control) ||
  1376. ieee80211_is_qos_nullfunc(hdr->frame_control))) {
  1377. u8 tid = ieee80211_get_tid(hdr);
  1378. ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
  1379. }
  1380. return RX_CONTINUE;
  1381. }
  1382. static ieee80211_rx_result debug_noinline
  1383. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  1384. {
  1385. struct sta_info *sta = rx->sta;
  1386. struct sk_buff *skb = rx->skb;
  1387. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1388. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1389. int i;
  1390. if (!sta)
  1391. return RX_CONTINUE;
  1392. /*
  1393. * Update last_rx only for IBSS packets which are for the current
  1394. * BSSID and for station already AUTHORIZED to avoid keeping the
  1395. * current IBSS network alive in cases where other STAs start
  1396. * using different BSSID. This will also give the station another
  1397. * chance to restart the authentication/authorization in case
  1398. * something went wrong the first time.
  1399. */
  1400. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1401. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1402. NL80211_IFTYPE_ADHOC);
  1403. if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
  1404. test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
  1405. sta->rx_stats.last_rx = jiffies;
  1406. if (ieee80211_is_data(hdr->frame_control) &&
  1407. !is_multicast_ether_addr(hdr->addr1))
  1408. sta->rx_stats.last_rate =
  1409. sta_stats_encode_rate(status);
  1410. }
  1411. } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
  1412. sta->rx_stats.last_rx = jiffies;
  1413. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1414. /*
  1415. * Mesh beacons will update last_rx when if they are found to
  1416. * match the current local configuration when processed.
  1417. */
  1418. sta->rx_stats.last_rx = jiffies;
  1419. if (ieee80211_is_data(hdr->frame_control))
  1420. sta->rx_stats.last_rate = sta_stats_encode_rate(status);
  1421. }
  1422. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1423. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1424. sta->rx_stats.fragments++;
  1425. u64_stats_update_begin(&rx->sta->rx_stats.syncp);
  1426. sta->rx_stats.bytes += rx->skb->len;
  1427. u64_stats_update_end(&rx->sta->rx_stats.syncp);
  1428. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  1429. sta->rx_stats.last_signal = status->signal;
  1430. ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
  1431. }
  1432. if (status->chains) {
  1433. sta->rx_stats.chains = status->chains;
  1434. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  1435. int signal = status->chain_signal[i];
  1436. if (!(status->chains & BIT(i)))
  1437. continue;
  1438. sta->rx_stats.chain_signal_last[i] = signal;
  1439. ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
  1440. -signal);
  1441. }
  1442. }
  1443. /*
  1444. * Change STA power saving mode only at the end of a frame
  1445. * exchange sequence, and only for a data or management
  1446. * frame as specified in IEEE 802.11-2016 11.2.3.2
  1447. */
  1448. if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
  1449. !ieee80211_has_morefrags(hdr->frame_control) &&
  1450. (ieee80211_is_mgmt(hdr->frame_control) ||
  1451. ieee80211_is_data(hdr->frame_control)) &&
  1452. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1453. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1454. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  1455. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1456. if (!ieee80211_has_pm(hdr->frame_control))
  1457. sta_ps_end(sta);
  1458. } else {
  1459. if (ieee80211_has_pm(hdr->frame_control))
  1460. sta_ps_start(sta);
  1461. }
  1462. }
  1463. /* mesh power save support */
  1464. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1465. ieee80211_mps_rx_h_sta_process(sta, hdr);
  1466. /*
  1467. * Drop (qos-)data::nullfunc frames silently, since they
  1468. * are used only to control station power saving mode.
  1469. */
  1470. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1471. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1472. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1473. /*
  1474. * If we receive a 4-addr nullfunc frame from a STA
  1475. * that was not moved to a 4-addr STA vlan yet send
  1476. * the event to userspace and for older hostapd drop
  1477. * the frame to the monitor interface.
  1478. */
  1479. if (ieee80211_has_a4(hdr->frame_control) &&
  1480. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1481. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1482. !rx->sdata->u.vlan.sta))) {
  1483. if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
  1484. cfg80211_rx_unexpected_4addr_frame(
  1485. rx->sdata->dev, sta->sta.addr,
  1486. GFP_ATOMIC);
  1487. return RX_DROP_MONITOR;
  1488. }
  1489. /*
  1490. * Update counter and free packet here to avoid
  1491. * counting this as a dropped packed.
  1492. */
  1493. sta->rx_stats.packets++;
  1494. dev_kfree_skb(rx->skb);
  1495. return RX_QUEUED;
  1496. }
  1497. return RX_CONTINUE;
  1498. } /* ieee80211_rx_h_sta_process */
  1499. static ieee80211_rx_result debug_noinline
  1500. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  1501. {
  1502. struct sk_buff *skb = rx->skb;
  1503. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1504. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1505. int keyidx;
  1506. int hdrlen;
  1507. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  1508. struct ieee80211_key *sta_ptk = NULL;
  1509. int mmie_keyidx = -1;
  1510. __le16 fc;
  1511. const struct ieee80211_cipher_scheme *cs = NULL;
  1512. /*
  1513. * Key selection 101
  1514. *
  1515. * There are four types of keys:
  1516. * - GTK (group keys)
  1517. * - IGTK (group keys for management frames)
  1518. * - PTK (pairwise keys)
  1519. * - STK (station-to-station pairwise keys)
  1520. *
  1521. * When selecting a key, we have to distinguish between multicast
  1522. * (including broadcast) and unicast frames, the latter can only
  1523. * use PTKs and STKs while the former always use GTKs and IGTKs.
  1524. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  1525. * unicast frames can also use key indices like GTKs. Hence, if we
  1526. * don't have a PTK/STK we check the key index for a WEP key.
  1527. *
  1528. * Note that in a regular BSS, multicast frames are sent by the
  1529. * AP only, associated stations unicast the frame to the AP first
  1530. * which then multicasts it on their behalf.
  1531. *
  1532. * There is also a slight problem in IBSS mode: GTKs are negotiated
  1533. * with each station, that is something we don't currently handle.
  1534. * The spec seems to expect that one negotiates the same key with
  1535. * every station but there's no such requirement; VLANs could be
  1536. * possible.
  1537. */
  1538. /* start without a key */
  1539. rx->key = NULL;
  1540. fc = hdr->frame_control;
  1541. if (rx->sta) {
  1542. int keyid = rx->sta->ptk_idx;
  1543. if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
  1544. cs = rx->sta->cipher_scheme;
  1545. keyid = ieee80211_get_cs_keyid(cs, rx->skb);
  1546. if (unlikely(keyid < 0))
  1547. return RX_DROP_UNUSABLE;
  1548. }
  1549. sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
  1550. }
  1551. if (!ieee80211_has_protected(fc))
  1552. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  1553. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  1554. rx->key = sta_ptk;
  1555. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1556. (status->flag & RX_FLAG_IV_STRIPPED))
  1557. return RX_CONTINUE;
  1558. /* Skip decryption if the frame is not protected. */
  1559. if (!ieee80211_has_protected(fc))
  1560. return RX_CONTINUE;
  1561. } else if (mmie_keyidx >= 0) {
  1562. /* Broadcast/multicast robust management frame / BIP */
  1563. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1564. (status->flag & RX_FLAG_IV_STRIPPED))
  1565. return RX_CONTINUE;
  1566. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  1567. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  1568. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  1569. if (rx->sta) {
  1570. if (ieee80211_is_group_privacy_action(skb) &&
  1571. test_sta_flag(rx->sta, WLAN_STA_MFP))
  1572. return RX_DROP_MONITOR;
  1573. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  1574. }
  1575. if (!rx->key)
  1576. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  1577. } else if (!ieee80211_has_protected(fc)) {
  1578. /*
  1579. * The frame was not protected, so skip decryption. However, we
  1580. * need to set rx->key if there is a key that could have been
  1581. * used so that the frame may be dropped if encryption would
  1582. * have been expected.
  1583. */
  1584. struct ieee80211_key *key = NULL;
  1585. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1586. int i;
  1587. if (ieee80211_is_mgmt(fc) &&
  1588. is_multicast_ether_addr(hdr->addr1) &&
  1589. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  1590. rx->key = key;
  1591. else {
  1592. if (rx->sta) {
  1593. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1594. key = rcu_dereference(rx->sta->gtk[i]);
  1595. if (key)
  1596. break;
  1597. }
  1598. }
  1599. if (!key) {
  1600. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1601. key = rcu_dereference(sdata->keys[i]);
  1602. if (key)
  1603. break;
  1604. }
  1605. }
  1606. if (key)
  1607. rx->key = key;
  1608. }
  1609. return RX_CONTINUE;
  1610. } else {
  1611. u8 keyid;
  1612. /*
  1613. * The device doesn't give us the IV so we won't be
  1614. * able to look up the key. That's ok though, we
  1615. * don't need to decrypt the frame, we just won't
  1616. * be able to keep statistics accurate.
  1617. * Except for key threshold notifications, should
  1618. * we somehow allow the driver to tell us which key
  1619. * the hardware used if this flag is set?
  1620. */
  1621. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1622. (status->flag & RX_FLAG_IV_STRIPPED))
  1623. return RX_CONTINUE;
  1624. hdrlen = ieee80211_hdrlen(fc);
  1625. if (cs) {
  1626. keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
  1627. if (unlikely(keyidx < 0))
  1628. return RX_DROP_UNUSABLE;
  1629. } else {
  1630. if (rx->skb->len < 8 + hdrlen)
  1631. return RX_DROP_UNUSABLE; /* TODO: count this? */
  1632. /*
  1633. * no need to call ieee80211_wep_get_keyidx,
  1634. * it verifies a bunch of things we've done already
  1635. */
  1636. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  1637. keyidx = keyid >> 6;
  1638. }
  1639. /* check per-station GTK first, if multicast packet */
  1640. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  1641. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  1642. /* if not found, try default key */
  1643. if (!rx->key) {
  1644. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  1645. /*
  1646. * RSNA-protected unicast frames should always be
  1647. * sent with pairwise or station-to-station keys,
  1648. * but for WEP we allow using a key index as well.
  1649. */
  1650. if (rx->key &&
  1651. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1652. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  1653. !is_multicast_ether_addr(hdr->addr1))
  1654. rx->key = NULL;
  1655. }
  1656. }
  1657. if (rx->key) {
  1658. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  1659. return RX_DROP_MONITOR;
  1660. /* TODO: add threshold stuff again */
  1661. } else {
  1662. return RX_DROP_MONITOR;
  1663. }
  1664. switch (rx->key->conf.cipher) {
  1665. case WLAN_CIPHER_SUITE_WEP40:
  1666. case WLAN_CIPHER_SUITE_WEP104:
  1667. result = ieee80211_crypto_wep_decrypt(rx);
  1668. break;
  1669. case WLAN_CIPHER_SUITE_TKIP:
  1670. result = ieee80211_crypto_tkip_decrypt(rx);
  1671. break;
  1672. case WLAN_CIPHER_SUITE_CCMP:
  1673. result = ieee80211_crypto_ccmp_decrypt(
  1674. rx, IEEE80211_CCMP_MIC_LEN);
  1675. break;
  1676. case WLAN_CIPHER_SUITE_CCMP_256:
  1677. result = ieee80211_crypto_ccmp_decrypt(
  1678. rx, IEEE80211_CCMP_256_MIC_LEN);
  1679. break;
  1680. case WLAN_CIPHER_SUITE_AES_CMAC:
  1681. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  1682. break;
  1683. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1684. result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
  1685. break;
  1686. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1687. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1688. result = ieee80211_crypto_aes_gmac_decrypt(rx);
  1689. break;
  1690. case WLAN_CIPHER_SUITE_GCMP:
  1691. case WLAN_CIPHER_SUITE_GCMP_256:
  1692. result = ieee80211_crypto_gcmp_decrypt(rx);
  1693. break;
  1694. default:
  1695. result = ieee80211_crypto_hw_decrypt(rx);
  1696. }
  1697. /* the hdr variable is invalid after the decrypt handlers */
  1698. /* either the frame has been decrypted or will be dropped */
  1699. status->flag |= RX_FLAG_DECRYPTED;
  1700. return result;
  1701. }
  1702. static inline struct ieee80211_fragment_entry *
  1703. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1704. unsigned int frag, unsigned int seq, int rx_queue,
  1705. struct sk_buff **skb)
  1706. {
  1707. struct ieee80211_fragment_entry *entry;
  1708. entry = &sdata->fragments[sdata->fragment_next++];
  1709. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1710. sdata->fragment_next = 0;
  1711. if (!skb_queue_empty(&entry->skb_list))
  1712. __skb_queue_purge(&entry->skb_list);
  1713. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1714. *skb = NULL;
  1715. entry->first_frag_time = jiffies;
  1716. entry->seq = seq;
  1717. entry->rx_queue = rx_queue;
  1718. entry->last_frag = frag;
  1719. entry->check_sequential_pn = false;
  1720. entry->extra_len = 0;
  1721. return entry;
  1722. }
  1723. static inline struct ieee80211_fragment_entry *
  1724. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1725. unsigned int frag, unsigned int seq,
  1726. int rx_queue, struct ieee80211_hdr *hdr)
  1727. {
  1728. struct ieee80211_fragment_entry *entry;
  1729. int i, idx;
  1730. idx = sdata->fragment_next;
  1731. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1732. struct ieee80211_hdr *f_hdr;
  1733. idx--;
  1734. if (idx < 0)
  1735. idx = IEEE80211_FRAGMENT_MAX - 1;
  1736. entry = &sdata->fragments[idx];
  1737. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1738. entry->rx_queue != rx_queue ||
  1739. entry->last_frag + 1 != frag)
  1740. continue;
  1741. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1742. /*
  1743. * Check ftype and addresses are equal, else check next fragment
  1744. */
  1745. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1746. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1747. !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
  1748. !ether_addr_equal(hdr->addr2, f_hdr->addr2))
  1749. continue;
  1750. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1751. __skb_queue_purge(&entry->skb_list);
  1752. continue;
  1753. }
  1754. return entry;
  1755. }
  1756. return NULL;
  1757. }
  1758. static ieee80211_rx_result debug_noinline
  1759. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1760. {
  1761. struct ieee80211_hdr *hdr;
  1762. u16 sc;
  1763. __le16 fc;
  1764. unsigned int frag, seq;
  1765. struct ieee80211_fragment_entry *entry;
  1766. struct sk_buff *skb;
  1767. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1768. fc = hdr->frame_control;
  1769. if (ieee80211_is_ctl(fc))
  1770. return RX_CONTINUE;
  1771. sc = le16_to_cpu(hdr->seq_ctrl);
  1772. frag = sc & IEEE80211_SCTL_FRAG;
  1773. if (is_multicast_ether_addr(hdr->addr1)) {
  1774. I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
  1775. goto out_no_led;
  1776. }
  1777. if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
  1778. goto out;
  1779. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1780. if (skb_linearize(rx->skb))
  1781. return RX_DROP_UNUSABLE;
  1782. /*
  1783. * skb_linearize() might change the skb->data and
  1784. * previously cached variables (in this case, hdr) need to
  1785. * be refreshed with the new data.
  1786. */
  1787. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1788. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1789. if (frag == 0) {
  1790. /* This is the first fragment of a new frame. */
  1791. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1792. rx->seqno_idx, &(rx->skb));
  1793. if (rx->key &&
  1794. (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
  1795. rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
  1796. rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
  1797. rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
  1798. ieee80211_has_protected(fc)) {
  1799. int queue = rx->security_idx;
  1800. /* Store CCMP/GCMP PN so that we can verify that the
  1801. * next fragment has a sequential PN value.
  1802. */
  1803. entry->check_sequential_pn = true;
  1804. memcpy(entry->last_pn,
  1805. rx->key->u.ccmp.rx_pn[queue],
  1806. IEEE80211_CCMP_PN_LEN);
  1807. BUILD_BUG_ON(offsetof(struct ieee80211_key,
  1808. u.ccmp.rx_pn) !=
  1809. offsetof(struct ieee80211_key,
  1810. u.gcmp.rx_pn));
  1811. BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
  1812. sizeof(rx->key->u.gcmp.rx_pn[queue]));
  1813. BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
  1814. IEEE80211_GCMP_PN_LEN);
  1815. }
  1816. return RX_QUEUED;
  1817. }
  1818. /* This is a fragment for a frame that should already be pending in
  1819. * fragment cache. Add this fragment to the end of the pending entry.
  1820. */
  1821. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1822. rx->seqno_idx, hdr);
  1823. if (!entry) {
  1824. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1825. return RX_DROP_MONITOR;
  1826. }
  1827. /* "The receiver shall discard MSDUs and MMPDUs whose constituent
  1828. * MPDU PN values are not incrementing in steps of 1."
  1829. * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
  1830. * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
  1831. */
  1832. if (entry->check_sequential_pn) {
  1833. int i;
  1834. u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
  1835. int queue;
  1836. if (!rx->key ||
  1837. (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
  1838. rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
  1839. rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
  1840. rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
  1841. return RX_DROP_UNUSABLE;
  1842. memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
  1843. for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
  1844. pn[i]++;
  1845. if (pn[i])
  1846. break;
  1847. }
  1848. queue = rx->security_idx;
  1849. rpn = rx->key->u.ccmp.rx_pn[queue];
  1850. if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
  1851. return RX_DROP_UNUSABLE;
  1852. memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
  1853. }
  1854. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1855. __skb_queue_tail(&entry->skb_list, rx->skb);
  1856. entry->last_frag = frag;
  1857. entry->extra_len += rx->skb->len;
  1858. if (ieee80211_has_morefrags(fc)) {
  1859. rx->skb = NULL;
  1860. return RX_QUEUED;
  1861. }
  1862. rx->skb = __skb_dequeue(&entry->skb_list);
  1863. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1864. I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
  1865. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1866. GFP_ATOMIC))) {
  1867. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1868. __skb_queue_purge(&entry->skb_list);
  1869. return RX_DROP_UNUSABLE;
  1870. }
  1871. }
  1872. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1873. skb_put_data(rx->skb, skb->data, skb->len);
  1874. dev_kfree_skb(skb);
  1875. }
  1876. out:
  1877. ieee80211_led_rx(rx->local);
  1878. out_no_led:
  1879. if (rx->sta)
  1880. rx->sta->rx_stats.packets++;
  1881. return RX_CONTINUE;
  1882. }
  1883. static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1884. {
  1885. if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1886. return -EACCES;
  1887. return 0;
  1888. }
  1889. static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1890. {
  1891. struct sk_buff *skb = rx->skb;
  1892. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1893. /*
  1894. * Pass through unencrypted frames if the hardware has
  1895. * decrypted them already.
  1896. */
  1897. if (status->flag & RX_FLAG_DECRYPTED)
  1898. return 0;
  1899. /* Drop unencrypted frames if key is set. */
  1900. if (unlikely(!ieee80211_has_protected(fc) &&
  1901. !ieee80211_is_nullfunc(fc) &&
  1902. ieee80211_is_data(fc) && rx->key))
  1903. return -EACCES;
  1904. return 0;
  1905. }
  1906. static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1907. {
  1908. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1909. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1910. __le16 fc = hdr->frame_control;
  1911. /*
  1912. * Pass through unencrypted frames if the hardware has
  1913. * decrypted them already.
  1914. */
  1915. if (status->flag & RX_FLAG_DECRYPTED)
  1916. return 0;
  1917. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1918. if (unlikely(!ieee80211_has_protected(fc) &&
  1919. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1920. rx->key)) {
  1921. if (ieee80211_is_deauth(fc) ||
  1922. ieee80211_is_disassoc(fc))
  1923. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1924. rx->skb->data,
  1925. rx->skb->len);
  1926. return -EACCES;
  1927. }
  1928. /* BIP does not use Protected field, so need to check MMIE */
  1929. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1930. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1931. if (ieee80211_is_deauth(fc) ||
  1932. ieee80211_is_disassoc(fc))
  1933. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1934. rx->skb->data,
  1935. rx->skb->len);
  1936. return -EACCES;
  1937. }
  1938. /*
  1939. * When using MFP, Action frames are not allowed prior to
  1940. * having configured keys.
  1941. */
  1942. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1943. ieee80211_is_robust_mgmt_frame(rx->skb)))
  1944. return -EACCES;
  1945. }
  1946. return 0;
  1947. }
  1948. static int
  1949. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1950. {
  1951. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1952. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1953. bool check_port_control = false;
  1954. struct ethhdr *ehdr;
  1955. int ret;
  1956. *port_control = false;
  1957. if (ieee80211_has_a4(hdr->frame_control) &&
  1958. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1959. return -1;
  1960. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1961. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1962. if (!sdata->u.mgd.use_4addr)
  1963. return -1;
  1964. else
  1965. check_port_control = true;
  1966. }
  1967. if (is_multicast_ether_addr(hdr->addr1) &&
  1968. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1969. return -1;
  1970. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1971. if (ret < 0)
  1972. return ret;
  1973. ehdr = (struct ethhdr *) rx->skb->data;
  1974. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1975. *port_control = true;
  1976. else if (check_port_control)
  1977. return -1;
  1978. return 0;
  1979. }
  1980. /*
  1981. * requires that rx->skb is a frame with ethernet header
  1982. */
  1983. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1984. {
  1985. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1986. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1987. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1988. /*
  1989. * Allow EAPOL frames to us/the PAE group address regardless
  1990. * of whether the frame was encrypted or not.
  1991. */
  1992. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1993. (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
  1994. ether_addr_equal(ehdr->h_dest, pae_group_addr)))
  1995. return true;
  1996. if (ieee80211_802_1x_port_control(rx) ||
  1997. ieee80211_drop_unencrypted(rx, fc))
  1998. return false;
  1999. return true;
  2000. }
  2001. static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
  2002. struct ieee80211_rx_data *rx)
  2003. {
  2004. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2005. struct net_device *dev = sdata->dev;
  2006. if (unlikely((skb->protocol == sdata->control_port_protocol ||
  2007. skb->protocol == cpu_to_be16(ETH_P_PREAUTH)) &&
  2008. sdata->control_port_over_nl80211)) {
  2009. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2010. bool noencrypt = status->flag & RX_FLAG_DECRYPTED;
  2011. cfg80211_rx_control_port(dev, skb, noencrypt);
  2012. dev_kfree_skb(skb);
  2013. } else {
  2014. /* deliver to local stack */
  2015. if (rx->napi)
  2016. napi_gro_receive(rx->napi, skb);
  2017. else
  2018. netif_receive_skb(skb);
  2019. }
  2020. }
  2021. /*
  2022. * requires that rx->skb is a frame with ethernet header
  2023. */
  2024. static void
  2025. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  2026. {
  2027. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2028. struct net_device *dev = sdata->dev;
  2029. struct sk_buff *skb, *xmit_skb;
  2030. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  2031. struct sta_info *dsta;
  2032. skb = rx->skb;
  2033. xmit_skb = NULL;
  2034. ieee80211_rx_stats(dev, skb->len);
  2035. if (rx->sta) {
  2036. /* The seqno index has the same property as needed
  2037. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  2038. * for non-QoS-data frames. Here we know it's a data
  2039. * frame, so count MSDUs.
  2040. */
  2041. u64_stats_update_begin(&rx->sta->rx_stats.syncp);
  2042. rx->sta->rx_stats.msdu[rx->seqno_idx]++;
  2043. u64_stats_update_end(&rx->sta->rx_stats.syncp);
  2044. }
  2045. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  2046. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  2047. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  2048. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  2049. if (is_multicast_ether_addr(ehdr->h_dest) &&
  2050. ieee80211_vif_get_num_mcast_if(sdata) != 0) {
  2051. /*
  2052. * send multicast frames both to higher layers in
  2053. * local net stack and back to the wireless medium
  2054. */
  2055. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  2056. if (!xmit_skb)
  2057. net_info_ratelimited("%s: failed to clone multicast frame\n",
  2058. dev->name);
  2059. } else if (!is_multicast_ether_addr(ehdr->h_dest)) {
  2060. dsta = sta_info_get(sdata, skb->data);
  2061. if (dsta) {
  2062. /*
  2063. * The destination station is associated to
  2064. * this AP (in this VLAN), so send the frame
  2065. * directly to it and do not pass it to local
  2066. * net stack.
  2067. */
  2068. xmit_skb = skb;
  2069. skb = NULL;
  2070. }
  2071. }
  2072. }
  2073. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2074. if (skb) {
  2075. /* 'align' will only take the values 0 or 2 here since all
  2076. * frames are required to be aligned to 2-byte boundaries
  2077. * when being passed to mac80211; the code here works just
  2078. * as well if that isn't true, but mac80211 assumes it can
  2079. * access fields as 2-byte aligned (e.g. for ether_addr_equal)
  2080. */
  2081. int align;
  2082. align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
  2083. if (align) {
  2084. if (WARN_ON(skb_headroom(skb) < 3)) {
  2085. dev_kfree_skb(skb);
  2086. skb = NULL;
  2087. } else {
  2088. u8 *data = skb->data;
  2089. size_t len = skb_headlen(skb);
  2090. skb->data -= align;
  2091. memmove(skb->data, data, len);
  2092. skb_set_tail_pointer(skb, len);
  2093. }
  2094. }
  2095. }
  2096. #endif
  2097. if (skb) {
  2098. skb->protocol = eth_type_trans(skb, dev);
  2099. memset(skb->cb, 0, sizeof(skb->cb));
  2100. ieee80211_deliver_skb_to_local_stack(skb, rx);
  2101. }
  2102. if (xmit_skb) {
  2103. /*
  2104. * Send to wireless media and increase priority by 256 to
  2105. * keep the received priority instead of reclassifying
  2106. * the frame (see cfg80211_classify8021d).
  2107. */
  2108. xmit_skb->priority += 256;
  2109. xmit_skb->protocol = htons(ETH_P_802_3);
  2110. skb_reset_network_header(xmit_skb);
  2111. skb_reset_mac_header(xmit_skb);
  2112. dev_queue_xmit(xmit_skb);
  2113. }
  2114. }
  2115. static ieee80211_rx_result debug_noinline
  2116. __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
  2117. {
  2118. struct net_device *dev = rx->sdata->dev;
  2119. struct sk_buff *skb = rx->skb;
  2120. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2121. __le16 fc = hdr->frame_control;
  2122. struct sk_buff_head frame_list;
  2123. struct ethhdr ethhdr;
  2124. const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
  2125. if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
  2126. check_da = NULL;
  2127. check_sa = NULL;
  2128. } else switch (rx->sdata->vif.type) {
  2129. case NL80211_IFTYPE_AP:
  2130. case NL80211_IFTYPE_AP_VLAN:
  2131. check_da = NULL;
  2132. break;
  2133. case NL80211_IFTYPE_STATION:
  2134. if (!rx->sta ||
  2135. !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
  2136. check_sa = NULL;
  2137. break;
  2138. case NL80211_IFTYPE_MESH_POINT:
  2139. check_sa = NULL;
  2140. break;
  2141. default:
  2142. break;
  2143. }
  2144. skb->dev = dev;
  2145. __skb_queue_head_init(&frame_list);
  2146. if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
  2147. rx->sdata->vif.addr,
  2148. rx->sdata->vif.type,
  2149. data_offset))
  2150. return RX_DROP_UNUSABLE;
  2151. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  2152. rx->sdata->vif.type,
  2153. rx->local->hw.extra_tx_headroom,
  2154. check_da, check_sa);
  2155. while (!skb_queue_empty(&frame_list)) {
  2156. rx->skb = __skb_dequeue(&frame_list);
  2157. if (!ieee80211_frame_allowed(rx, fc)) {
  2158. dev_kfree_skb(rx->skb);
  2159. continue;
  2160. }
  2161. ieee80211_deliver_skb(rx);
  2162. }
  2163. return RX_QUEUED;
  2164. }
  2165. static ieee80211_rx_result debug_noinline
  2166. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  2167. {
  2168. struct sk_buff *skb = rx->skb;
  2169. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2170. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2171. __le16 fc = hdr->frame_control;
  2172. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  2173. return RX_CONTINUE;
  2174. if (unlikely(!ieee80211_is_data(fc)))
  2175. return RX_CONTINUE;
  2176. if (unlikely(!ieee80211_is_data_present(fc)))
  2177. return RX_DROP_MONITOR;
  2178. if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
  2179. switch (rx->sdata->vif.type) {
  2180. case NL80211_IFTYPE_AP_VLAN:
  2181. if (!rx->sdata->u.vlan.sta)
  2182. return RX_DROP_UNUSABLE;
  2183. break;
  2184. case NL80211_IFTYPE_STATION:
  2185. if (!rx->sdata->u.mgd.use_4addr)
  2186. return RX_DROP_UNUSABLE;
  2187. break;
  2188. default:
  2189. return RX_DROP_UNUSABLE;
  2190. }
  2191. }
  2192. if (is_multicast_ether_addr(hdr->addr1))
  2193. return RX_DROP_UNUSABLE;
  2194. return __ieee80211_rx_h_amsdu(rx, 0);
  2195. }
  2196. #ifdef CONFIG_MAC80211_MESH
  2197. static ieee80211_rx_result
  2198. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  2199. {
  2200. struct ieee80211_hdr *fwd_hdr, *hdr;
  2201. struct ieee80211_tx_info *info;
  2202. struct ieee80211s_hdr *mesh_hdr;
  2203. struct sk_buff *skb = rx->skb, *fwd_skb;
  2204. struct ieee80211_local *local = rx->local;
  2205. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2206. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2207. u16 ac, q, hdrlen;
  2208. hdr = (struct ieee80211_hdr *) skb->data;
  2209. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  2210. /* make sure fixed part of mesh header is there, also checks skb len */
  2211. if (!pskb_may_pull(rx->skb, hdrlen + 6))
  2212. return RX_DROP_MONITOR;
  2213. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  2214. /* make sure full mesh header is there, also checks skb len */
  2215. if (!pskb_may_pull(rx->skb,
  2216. hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
  2217. return RX_DROP_MONITOR;
  2218. /* reload pointers */
  2219. hdr = (struct ieee80211_hdr *) skb->data;
  2220. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  2221. if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
  2222. return RX_DROP_MONITOR;
  2223. /* frame is in RMC, don't forward */
  2224. if (ieee80211_is_data(hdr->frame_control) &&
  2225. is_multicast_ether_addr(hdr->addr1) &&
  2226. mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
  2227. return RX_DROP_MONITOR;
  2228. if (!ieee80211_is_data(hdr->frame_control))
  2229. return RX_CONTINUE;
  2230. if (!mesh_hdr->ttl)
  2231. return RX_DROP_MONITOR;
  2232. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  2233. struct mesh_path *mppath;
  2234. char *proxied_addr;
  2235. char *mpp_addr;
  2236. if (is_multicast_ether_addr(hdr->addr1)) {
  2237. mpp_addr = hdr->addr3;
  2238. proxied_addr = mesh_hdr->eaddr1;
  2239. } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
  2240. MESH_FLAGS_AE_A5_A6) {
  2241. /* has_a4 already checked in ieee80211_rx_mesh_check */
  2242. mpp_addr = hdr->addr4;
  2243. proxied_addr = mesh_hdr->eaddr2;
  2244. } else {
  2245. return RX_DROP_MONITOR;
  2246. }
  2247. rcu_read_lock();
  2248. mppath = mpp_path_lookup(sdata, proxied_addr);
  2249. if (!mppath) {
  2250. mpp_path_add(sdata, proxied_addr, mpp_addr);
  2251. } else {
  2252. spin_lock_bh(&mppath->state_lock);
  2253. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  2254. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  2255. mppath->exp_time = jiffies;
  2256. spin_unlock_bh(&mppath->state_lock);
  2257. }
  2258. rcu_read_unlock();
  2259. }
  2260. /* Frame has reached destination. Don't forward */
  2261. if (!is_multicast_ether_addr(hdr->addr1) &&
  2262. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  2263. return RX_CONTINUE;
  2264. ac = ieee80211_select_queue_80211(sdata, skb, hdr);
  2265. q = sdata->vif.hw_queue[ac];
  2266. if (ieee80211_queue_stopped(&local->hw, q)) {
  2267. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  2268. return RX_DROP_MONITOR;
  2269. }
  2270. skb_set_queue_mapping(skb, q);
  2271. if (!--mesh_hdr->ttl) {
  2272. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  2273. goto out;
  2274. }
  2275. if (!ifmsh->mshcfg.dot11MeshForwarding)
  2276. goto out;
  2277. fwd_skb = skb_copy_expand(skb, local->tx_headroom +
  2278. sdata->encrypt_headroom, 0, GFP_ATOMIC);
  2279. if (!fwd_skb)
  2280. goto out;
  2281. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  2282. fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
  2283. info = IEEE80211_SKB_CB(fwd_skb);
  2284. memset(info, 0, sizeof(*info));
  2285. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  2286. info->control.vif = &rx->sdata->vif;
  2287. info->control.jiffies = jiffies;
  2288. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  2289. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  2290. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2291. /* update power mode indication when forwarding */
  2292. ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
  2293. } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
  2294. /* mesh power mode flags updated in mesh_nexthop_lookup */
  2295. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  2296. } else {
  2297. /* unable to resolve next hop */
  2298. mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
  2299. fwd_hdr->addr3, 0,
  2300. WLAN_REASON_MESH_PATH_NOFORWARD,
  2301. fwd_hdr->addr2);
  2302. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  2303. kfree_skb(fwd_skb);
  2304. return RX_DROP_MONITOR;
  2305. }
  2306. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  2307. ieee80211_add_pending_skb(local, fwd_skb);
  2308. out:
  2309. if (is_multicast_ether_addr(hdr->addr1))
  2310. return RX_CONTINUE;
  2311. return RX_DROP_MONITOR;
  2312. }
  2313. #endif
  2314. static ieee80211_rx_result debug_noinline
  2315. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  2316. {
  2317. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2318. struct ieee80211_local *local = rx->local;
  2319. struct net_device *dev = sdata->dev;
  2320. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  2321. __le16 fc = hdr->frame_control;
  2322. bool port_control;
  2323. int err;
  2324. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  2325. return RX_CONTINUE;
  2326. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  2327. return RX_DROP_MONITOR;
  2328. /*
  2329. * Send unexpected-4addr-frame event to hostapd. For older versions,
  2330. * also drop the frame to cooked monitor interfaces.
  2331. */
  2332. if (ieee80211_has_a4(hdr->frame_control) &&
  2333. sdata->vif.type == NL80211_IFTYPE_AP) {
  2334. if (rx->sta &&
  2335. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  2336. cfg80211_rx_unexpected_4addr_frame(
  2337. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  2338. return RX_DROP_MONITOR;
  2339. }
  2340. err = __ieee80211_data_to_8023(rx, &port_control);
  2341. if (unlikely(err))
  2342. return RX_DROP_UNUSABLE;
  2343. if (!ieee80211_frame_allowed(rx, fc))
  2344. return RX_DROP_MONITOR;
  2345. /* directly handle TDLS channel switch requests/responses */
  2346. if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
  2347. cpu_to_be16(ETH_P_TDLS))) {
  2348. struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
  2349. if (pskb_may_pull(rx->skb,
  2350. offsetof(struct ieee80211_tdls_data, u)) &&
  2351. tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
  2352. tf->category == WLAN_CATEGORY_TDLS &&
  2353. (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
  2354. tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
  2355. skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
  2356. schedule_work(&local->tdls_chsw_work);
  2357. if (rx->sta)
  2358. rx->sta->rx_stats.packets++;
  2359. return RX_QUEUED;
  2360. }
  2361. }
  2362. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  2363. unlikely(port_control) && sdata->bss) {
  2364. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2365. u.ap);
  2366. dev = sdata->dev;
  2367. rx->sdata = sdata;
  2368. }
  2369. rx->skb->dev = dev;
  2370. if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
  2371. local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  2372. !is_multicast_ether_addr(
  2373. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  2374. (!local->scanning &&
  2375. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
  2376. mod_timer(&local->dynamic_ps_timer, jiffies +
  2377. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  2378. ieee80211_deliver_skb(rx);
  2379. return RX_QUEUED;
  2380. }
  2381. static ieee80211_rx_result debug_noinline
  2382. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
  2383. {
  2384. struct sk_buff *skb = rx->skb;
  2385. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  2386. struct tid_ampdu_rx *tid_agg_rx;
  2387. u16 start_seq_num;
  2388. u16 tid;
  2389. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  2390. return RX_CONTINUE;
  2391. if (ieee80211_is_back_req(bar->frame_control)) {
  2392. struct {
  2393. __le16 control, start_seq_num;
  2394. } __packed bar_data;
  2395. struct ieee80211_event event = {
  2396. .type = BAR_RX_EVENT,
  2397. };
  2398. if (!rx->sta)
  2399. return RX_DROP_MONITOR;
  2400. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  2401. &bar_data, sizeof(bar_data)))
  2402. return RX_DROP_MONITOR;
  2403. tid = le16_to_cpu(bar_data.control) >> 12;
  2404. if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
  2405. !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
  2406. ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
  2407. WLAN_BACK_RECIPIENT,
  2408. WLAN_REASON_QSTA_REQUIRE_SETUP);
  2409. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  2410. if (!tid_agg_rx)
  2411. return RX_DROP_MONITOR;
  2412. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  2413. event.u.ba.tid = tid;
  2414. event.u.ba.ssn = start_seq_num;
  2415. event.u.ba.sta = &rx->sta->sta;
  2416. /* reset session timer */
  2417. if (tid_agg_rx->timeout)
  2418. mod_timer(&tid_agg_rx->session_timer,
  2419. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2420. spin_lock(&tid_agg_rx->reorder_lock);
  2421. /* release stored frames up to start of BAR */
  2422. ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
  2423. start_seq_num, frames);
  2424. spin_unlock(&tid_agg_rx->reorder_lock);
  2425. drv_event_callback(rx->local, rx->sdata, &event);
  2426. kfree_skb(skb);
  2427. return RX_QUEUED;
  2428. }
  2429. /*
  2430. * After this point, we only want management frames,
  2431. * so we can drop all remaining control frames to
  2432. * cooked monitor interfaces.
  2433. */
  2434. return RX_DROP_MONITOR;
  2435. }
  2436. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  2437. struct ieee80211_mgmt *mgmt,
  2438. size_t len)
  2439. {
  2440. struct ieee80211_local *local = sdata->local;
  2441. struct sk_buff *skb;
  2442. struct ieee80211_mgmt *resp;
  2443. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  2444. /* Not to own unicast address */
  2445. return;
  2446. }
  2447. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  2448. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  2449. /* Not from the current AP or not associated yet. */
  2450. return;
  2451. }
  2452. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  2453. /* Too short SA Query request frame */
  2454. return;
  2455. }
  2456. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  2457. if (skb == NULL)
  2458. return;
  2459. skb_reserve(skb, local->hw.extra_tx_headroom);
  2460. resp = skb_put_zero(skb, 24);
  2461. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2462. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  2463. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  2464. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2465. IEEE80211_STYPE_ACTION);
  2466. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  2467. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  2468. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  2469. memcpy(resp->u.action.u.sa_query.trans_id,
  2470. mgmt->u.action.u.sa_query.trans_id,
  2471. WLAN_SA_QUERY_TR_ID_LEN);
  2472. ieee80211_tx_skb(sdata, skb);
  2473. }
  2474. static ieee80211_rx_result debug_noinline
  2475. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  2476. {
  2477. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2478. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2479. /*
  2480. * From here on, look only at management frames.
  2481. * Data and control frames are already handled,
  2482. * and unknown (reserved) frames are useless.
  2483. */
  2484. if (rx->skb->len < 24)
  2485. return RX_DROP_MONITOR;
  2486. if (!ieee80211_is_mgmt(mgmt->frame_control))
  2487. return RX_DROP_MONITOR;
  2488. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  2489. ieee80211_is_beacon(mgmt->frame_control) &&
  2490. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  2491. int sig = 0;
  2492. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
  2493. !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
  2494. sig = status->signal;
  2495. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  2496. rx->skb->data, rx->skb->len,
  2497. status->freq, sig);
  2498. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  2499. }
  2500. if (ieee80211_drop_unencrypted_mgmt(rx))
  2501. return RX_DROP_UNUSABLE;
  2502. return RX_CONTINUE;
  2503. }
  2504. static ieee80211_rx_result debug_noinline
  2505. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  2506. {
  2507. struct ieee80211_local *local = rx->local;
  2508. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2509. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2510. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2511. int len = rx->skb->len;
  2512. if (!ieee80211_is_action(mgmt->frame_control))
  2513. return RX_CONTINUE;
  2514. /* drop too small frames */
  2515. if (len < IEEE80211_MIN_ACTION_SIZE)
  2516. return RX_DROP_UNUSABLE;
  2517. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
  2518. mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
  2519. mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  2520. return RX_DROP_UNUSABLE;
  2521. switch (mgmt->u.action.category) {
  2522. case WLAN_CATEGORY_HT:
  2523. /* reject HT action frames from stations not supporting HT */
  2524. if (!rx->sta->sta.ht_cap.ht_supported)
  2525. goto invalid;
  2526. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2527. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2528. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2529. sdata->vif.type != NL80211_IFTYPE_AP &&
  2530. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2531. break;
  2532. /* verify action & smps_control/chanwidth are present */
  2533. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2534. goto invalid;
  2535. switch (mgmt->u.action.u.ht_smps.action) {
  2536. case WLAN_HT_ACTION_SMPS: {
  2537. struct ieee80211_supported_band *sband;
  2538. enum ieee80211_smps_mode smps_mode;
  2539. struct sta_opmode_info sta_opmode = {};
  2540. /* convert to HT capability */
  2541. switch (mgmt->u.action.u.ht_smps.smps_control) {
  2542. case WLAN_HT_SMPS_CONTROL_DISABLED:
  2543. smps_mode = IEEE80211_SMPS_OFF;
  2544. break;
  2545. case WLAN_HT_SMPS_CONTROL_STATIC:
  2546. smps_mode = IEEE80211_SMPS_STATIC;
  2547. break;
  2548. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  2549. smps_mode = IEEE80211_SMPS_DYNAMIC;
  2550. break;
  2551. default:
  2552. goto invalid;
  2553. }
  2554. /* if no change do nothing */
  2555. if (rx->sta->sta.smps_mode == smps_mode)
  2556. goto handled;
  2557. rx->sta->sta.smps_mode = smps_mode;
  2558. sta_opmode.smps_mode =
  2559. ieee80211_smps_mode_to_smps_mode(smps_mode);
  2560. sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
  2561. sband = rx->local->hw.wiphy->bands[status->band];
  2562. rate_control_rate_update(local, sband, rx->sta,
  2563. IEEE80211_RC_SMPS_CHANGED);
  2564. cfg80211_sta_opmode_change_notify(sdata->dev,
  2565. rx->sta->addr,
  2566. &sta_opmode,
  2567. GFP_KERNEL);
  2568. goto handled;
  2569. }
  2570. case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
  2571. struct ieee80211_supported_band *sband;
  2572. u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
  2573. enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
  2574. struct sta_opmode_info sta_opmode = {};
  2575. /* If it doesn't support 40 MHz it can't change ... */
  2576. if (!(rx->sta->sta.ht_cap.cap &
  2577. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  2578. goto handled;
  2579. if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
  2580. max_bw = IEEE80211_STA_RX_BW_20;
  2581. else
  2582. max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
  2583. /* set cur_max_bandwidth and recalc sta bw */
  2584. rx->sta->cur_max_bandwidth = max_bw;
  2585. new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
  2586. if (rx->sta->sta.bandwidth == new_bw)
  2587. goto handled;
  2588. rx->sta->sta.bandwidth = new_bw;
  2589. sband = rx->local->hw.wiphy->bands[status->band];
  2590. sta_opmode.bw =
  2591. ieee80211_sta_rx_bw_to_chan_width(rx->sta);
  2592. sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
  2593. rate_control_rate_update(local, sband, rx->sta,
  2594. IEEE80211_RC_BW_CHANGED);
  2595. cfg80211_sta_opmode_change_notify(sdata->dev,
  2596. rx->sta->addr,
  2597. &sta_opmode,
  2598. GFP_KERNEL);
  2599. goto handled;
  2600. }
  2601. default:
  2602. goto invalid;
  2603. }
  2604. break;
  2605. case WLAN_CATEGORY_PUBLIC:
  2606. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2607. goto invalid;
  2608. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2609. break;
  2610. if (!rx->sta)
  2611. break;
  2612. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  2613. break;
  2614. if (mgmt->u.action.u.ext_chan_switch.action_code !=
  2615. WLAN_PUB_ACTION_EXT_CHANSW_ANN)
  2616. break;
  2617. if (len < offsetof(struct ieee80211_mgmt,
  2618. u.action.u.ext_chan_switch.variable))
  2619. goto invalid;
  2620. goto queue;
  2621. case WLAN_CATEGORY_VHT:
  2622. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2623. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2624. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2625. sdata->vif.type != NL80211_IFTYPE_AP &&
  2626. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2627. break;
  2628. /* verify action code is present */
  2629. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2630. goto invalid;
  2631. switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
  2632. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  2633. /* verify opmode is present */
  2634. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2635. goto invalid;
  2636. goto queue;
  2637. }
  2638. case WLAN_VHT_ACTION_GROUPID_MGMT: {
  2639. if (len < IEEE80211_MIN_ACTION_SIZE + 25)
  2640. goto invalid;
  2641. goto queue;
  2642. }
  2643. default:
  2644. break;
  2645. }
  2646. break;
  2647. case WLAN_CATEGORY_BACK:
  2648. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2649. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2650. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2651. sdata->vif.type != NL80211_IFTYPE_AP &&
  2652. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2653. break;
  2654. /* verify action_code is present */
  2655. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2656. break;
  2657. switch (mgmt->u.action.u.addba_req.action_code) {
  2658. case WLAN_ACTION_ADDBA_REQ:
  2659. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2660. sizeof(mgmt->u.action.u.addba_req)))
  2661. goto invalid;
  2662. break;
  2663. case WLAN_ACTION_ADDBA_RESP:
  2664. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2665. sizeof(mgmt->u.action.u.addba_resp)))
  2666. goto invalid;
  2667. break;
  2668. case WLAN_ACTION_DELBA:
  2669. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2670. sizeof(mgmt->u.action.u.delba)))
  2671. goto invalid;
  2672. break;
  2673. default:
  2674. goto invalid;
  2675. }
  2676. goto queue;
  2677. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2678. /* verify action_code is present */
  2679. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2680. break;
  2681. switch (mgmt->u.action.u.measurement.action_code) {
  2682. case WLAN_ACTION_SPCT_MSR_REQ:
  2683. if (status->band != NL80211_BAND_5GHZ)
  2684. break;
  2685. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2686. sizeof(mgmt->u.action.u.measurement)))
  2687. break;
  2688. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2689. break;
  2690. ieee80211_process_measurement_req(sdata, mgmt, len);
  2691. goto handled;
  2692. case WLAN_ACTION_SPCT_CHL_SWITCH: {
  2693. u8 *bssid;
  2694. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2695. sizeof(mgmt->u.action.u.chan_switch)))
  2696. break;
  2697. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2698. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2699. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2700. break;
  2701. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2702. bssid = sdata->u.mgd.bssid;
  2703. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2704. bssid = sdata->u.ibss.bssid;
  2705. else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  2706. bssid = mgmt->sa;
  2707. else
  2708. break;
  2709. if (!ether_addr_equal(mgmt->bssid, bssid))
  2710. break;
  2711. goto queue;
  2712. }
  2713. }
  2714. break;
  2715. case WLAN_CATEGORY_SA_QUERY:
  2716. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2717. sizeof(mgmt->u.action.u.sa_query)))
  2718. break;
  2719. switch (mgmt->u.action.u.sa_query.action) {
  2720. case WLAN_ACTION_SA_QUERY_REQUEST:
  2721. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2722. break;
  2723. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2724. goto handled;
  2725. }
  2726. break;
  2727. case WLAN_CATEGORY_SELF_PROTECTED:
  2728. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2729. sizeof(mgmt->u.action.u.self_prot.action_code)))
  2730. break;
  2731. switch (mgmt->u.action.u.self_prot.action_code) {
  2732. case WLAN_SP_MESH_PEERING_OPEN:
  2733. case WLAN_SP_MESH_PEERING_CLOSE:
  2734. case WLAN_SP_MESH_PEERING_CONFIRM:
  2735. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2736. goto invalid;
  2737. if (sdata->u.mesh.user_mpm)
  2738. /* userspace handles this frame */
  2739. break;
  2740. goto queue;
  2741. case WLAN_SP_MGK_INFORM:
  2742. case WLAN_SP_MGK_ACK:
  2743. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2744. goto invalid;
  2745. break;
  2746. }
  2747. break;
  2748. case WLAN_CATEGORY_MESH_ACTION:
  2749. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2750. sizeof(mgmt->u.action.u.mesh_action.action_code)))
  2751. break;
  2752. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2753. break;
  2754. if (mesh_action_is_path_sel(mgmt) &&
  2755. !mesh_path_sel_is_hwmp(sdata))
  2756. break;
  2757. goto queue;
  2758. }
  2759. return RX_CONTINUE;
  2760. invalid:
  2761. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2762. /* will return in the next handlers */
  2763. return RX_CONTINUE;
  2764. handled:
  2765. if (rx->sta)
  2766. rx->sta->rx_stats.packets++;
  2767. dev_kfree_skb(rx->skb);
  2768. return RX_QUEUED;
  2769. queue:
  2770. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2771. ieee80211_queue_work(&local->hw, &sdata->work);
  2772. if (rx->sta)
  2773. rx->sta->rx_stats.packets++;
  2774. return RX_QUEUED;
  2775. }
  2776. static ieee80211_rx_result debug_noinline
  2777. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2778. {
  2779. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2780. int sig = 0;
  2781. /* skip known-bad action frames and return them in the next handler */
  2782. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2783. return RX_CONTINUE;
  2784. /*
  2785. * Getting here means the kernel doesn't know how to handle
  2786. * it, but maybe userspace does ... include returned frames
  2787. * so userspace can register for those to know whether ones
  2788. * it transmitted were processed or returned.
  2789. */
  2790. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
  2791. !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
  2792. sig = status->signal;
  2793. if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
  2794. rx->skb->data, rx->skb->len, 0)) {
  2795. if (rx->sta)
  2796. rx->sta->rx_stats.packets++;
  2797. dev_kfree_skb(rx->skb);
  2798. return RX_QUEUED;
  2799. }
  2800. return RX_CONTINUE;
  2801. }
  2802. static ieee80211_rx_result debug_noinline
  2803. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2804. {
  2805. struct ieee80211_local *local = rx->local;
  2806. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2807. struct sk_buff *nskb;
  2808. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2809. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2810. if (!ieee80211_is_action(mgmt->frame_control))
  2811. return RX_CONTINUE;
  2812. /*
  2813. * For AP mode, hostapd is responsible for handling any action
  2814. * frames that we didn't handle, including returning unknown
  2815. * ones. For all other modes we will return them to the sender,
  2816. * setting the 0x80 bit in the action category, as required by
  2817. * 802.11-2012 9.24.4.
  2818. * Newer versions of hostapd shall also use the management frame
  2819. * registration mechanisms, but older ones still use cooked
  2820. * monitor interfaces so push all frames there.
  2821. */
  2822. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2823. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2824. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2825. return RX_DROP_MONITOR;
  2826. if (is_multicast_ether_addr(mgmt->da))
  2827. return RX_DROP_MONITOR;
  2828. /* do not return rejected action frames */
  2829. if (mgmt->u.action.category & 0x80)
  2830. return RX_DROP_UNUSABLE;
  2831. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2832. GFP_ATOMIC);
  2833. if (nskb) {
  2834. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2835. nmgmt->u.action.category |= 0x80;
  2836. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2837. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2838. memset(nskb->cb, 0, sizeof(nskb->cb));
  2839. if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  2840. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
  2841. info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
  2842. IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
  2843. IEEE80211_TX_CTL_NO_CCK_RATE;
  2844. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  2845. info->hw_queue =
  2846. local->hw.offchannel_tx_hw_queue;
  2847. }
  2848. __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
  2849. status->band, 0);
  2850. }
  2851. dev_kfree_skb(rx->skb);
  2852. return RX_QUEUED;
  2853. }
  2854. static ieee80211_rx_result debug_noinline
  2855. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2856. {
  2857. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2858. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2859. __le16 stype;
  2860. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2861. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2862. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2863. sdata->vif.type != NL80211_IFTYPE_OCB &&
  2864. sdata->vif.type != NL80211_IFTYPE_STATION)
  2865. return RX_DROP_MONITOR;
  2866. switch (stype) {
  2867. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2868. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2869. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2870. /* process for all: mesh, mlme, ibss */
  2871. break;
  2872. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2873. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2874. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2875. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2876. if (is_multicast_ether_addr(mgmt->da) &&
  2877. !is_broadcast_ether_addr(mgmt->da))
  2878. return RX_DROP_MONITOR;
  2879. /* process only for station */
  2880. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2881. return RX_DROP_MONITOR;
  2882. break;
  2883. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2884. /* process only for ibss and mesh */
  2885. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2886. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2887. return RX_DROP_MONITOR;
  2888. break;
  2889. default:
  2890. return RX_DROP_MONITOR;
  2891. }
  2892. /* queue up frame and kick off work to process it */
  2893. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2894. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2895. if (rx->sta)
  2896. rx->sta->rx_stats.packets++;
  2897. return RX_QUEUED;
  2898. }
  2899. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2900. struct ieee80211_rate *rate)
  2901. {
  2902. struct ieee80211_sub_if_data *sdata;
  2903. struct ieee80211_local *local = rx->local;
  2904. struct sk_buff *skb = rx->skb, *skb2;
  2905. struct net_device *prev_dev = NULL;
  2906. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2907. int needed_headroom;
  2908. /*
  2909. * If cooked monitor has been processed already, then
  2910. * don't do it again. If not, set the flag.
  2911. */
  2912. if (rx->flags & IEEE80211_RX_CMNTR)
  2913. goto out_free_skb;
  2914. rx->flags |= IEEE80211_RX_CMNTR;
  2915. /* If there are no cooked monitor interfaces, just free the SKB */
  2916. if (!local->cooked_mntrs)
  2917. goto out_free_skb;
  2918. /* vendor data is long removed here */
  2919. status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
  2920. /* room for the radiotap header based on driver features */
  2921. needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
  2922. if (skb_headroom(skb) < needed_headroom &&
  2923. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2924. goto out_free_skb;
  2925. /* prepend radiotap information */
  2926. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2927. false);
  2928. skb_reset_mac_header(skb);
  2929. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2930. skb->pkt_type = PACKET_OTHERHOST;
  2931. skb->protocol = htons(ETH_P_802_2);
  2932. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2933. if (!ieee80211_sdata_running(sdata))
  2934. continue;
  2935. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2936. !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
  2937. continue;
  2938. if (prev_dev) {
  2939. skb2 = skb_clone(skb, GFP_ATOMIC);
  2940. if (skb2) {
  2941. skb2->dev = prev_dev;
  2942. netif_receive_skb(skb2);
  2943. }
  2944. }
  2945. prev_dev = sdata->dev;
  2946. ieee80211_rx_stats(sdata->dev, skb->len);
  2947. }
  2948. if (prev_dev) {
  2949. skb->dev = prev_dev;
  2950. netif_receive_skb(skb);
  2951. return;
  2952. }
  2953. out_free_skb:
  2954. dev_kfree_skb(skb);
  2955. }
  2956. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2957. ieee80211_rx_result res)
  2958. {
  2959. switch (res) {
  2960. case RX_DROP_MONITOR:
  2961. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2962. if (rx->sta)
  2963. rx->sta->rx_stats.dropped++;
  2964. /* fall through */
  2965. case RX_CONTINUE: {
  2966. struct ieee80211_rate *rate = NULL;
  2967. struct ieee80211_supported_band *sband;
  2968. struct ieee80211_rx_status *status;
  2969. status = IEEE80211_SKB_RXCB((rx->skb));
  2970. sband = rx->local->hw.wiphy->bands[status->band];
  2971. if (status->encoding == RX_ENC_LEGACY)
  2972. rate = &sband->bitrates[status->rate_idx];
  2973. ieee80211_rx_cooked_monitor(rx, rate);
  2974. break;
  2975. }
  2976. case RX_DROP_UNUSABLE:
  2977. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2978. if (rx->sta)
  2979. rx->sta->rx_stats.dropped++;
  2980. dev_kfree_skb(rx->skb);
  2981. break;
  2982. case RX_QUEUED:
  2983. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2984. break;
  2985. }
  2986. }
  2987. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
  2988. struct sk_buff_head *frames)
  2989. {
  2990. ieee80211_rx_result res = RX_DROP_MONITOR;
  2991. struct sk_buff *skb;
  2992. #define CALL_RXH(rxh) \
  2993. do { \
  2994. res = rxh(rx); \
  2995. if (res != RX_CONTINUE) \
  2996. goto rxh_next; \
  2997. } while (0)
  2998. /* Lock here to avoid hitting all of the data used in the RX
  2999. * path (e.g. key data, station data, ...) concurrently when
  3000. * a frame is released from the reorder buffer due to timeout
  3001. * from the timer, potentially concurrently with RX from the
  3002. * driver.
  3003. */
  3004. spin_lock_bh(&rx->local->rx_path_lock);
  3005. while ((skb = __skb_dequeue(frames))) {
  3006. /*
  3007. * all the other fields are valid across frames
  3008. * that belong to an aMPDU since they are on the
  3009. * same TID from the same station
  3010. */
  3011. rx->skb = skb;
  3012. CALL_RXH(ieee80211_rx_h_check_more_data);
  3013. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
  3014. CALL_RXH(ieee80211_rx_h_sta_process);
  3015. CALL_RXH(ieee80211_rx_h_decrypt);
  3016. CALL_RXH(ieee80211_rx_h_defragment);
  3017. CALL_RXH(ieee80211_rx_h_michael_mic_verify);
  3018. /* must be after MMIC verify so header is counted in MPDU mic */
  3019. #ifdef CONFIG_MAC80211_MESH
  3020. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  3021. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  3022. #endif
  3023. CALL_RXH(ieee80211_rx_h_amsdu);
  3024. CALL_RXH(ieee80211_rx_h_data);
  3025. /* special treatment -- needs the queue */
  3026. res = ieee80211_rx_h_ctrl(rx, frames);
  3027. if (res != RX_CONTINUE)
  3028. goto rxh_next;
  3029. CALL_RXH(ieee80211_rx_h_mgmt_check);
  3030. CALL_RXH(ieee80211_rx_h_action);
  3031. CALL_RXH(ieee80211_rx_h_userspace_mgmt);
  3032. CALL_RXH(ieee80211_rx_h_action_return);
  3033. CALL_RXH(ieee80211_rx_h_mgmt);
  3034. rxh_next:
  3035. ieee80211_rx_handlers_result(rx, res);
  3036. #undef CALL_RXH
  3037. }
  3038. spin_unlock_bh(&rx->local->rx_path_lock);
  3039. }
  3040. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  3041. {
  3042. struct sk_buff_head reorder_release;
  3043. ieee80211_rx_result res = RX_DROP_MONITOR;
  3044. __skb_queue_head_init(&reorder_release);
  3045. #define CALL_RXH(rxh) \
  3046. do { \
  3047. res = rxh(rx); \
  3048. if (res != RX_CONTINUE) \
  3049. goto rxh_next; \
  3050. } while (0)
  3051. CALL_RXH(ieee80211_rx_h_check_dup);
  3052. CALL_RXH(ieee80211_rx_h_check);
  3053. ieee80211_rx_reorder_ampdu(rx, &reorder_release);
  3054. ieee80211_rx_handlers(rx, &reorder_release);
  3055. return;
  3056. rxh_next:
  3057. ieee80211_rx_handlers_result(rx, res);
  3058. #undef CALL_RXH
  3059. }
  3060. /*
  3061. * This function makes calls into the RX path, therefore
  3062. * it has to be invoked under RCU read lock.
  3063. */
  3064. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  3065. {
  3066. struct sk_buff_head frames;
  3067. struct ieee80211_rx_data rx = {
  3068. .sta = sta,
  3069. .sdata = sta->sdata,
  3070. .local = sta->local,
  3071. /* This is OK -- must be QoS data frame */
  3072. .security_idx = tid,
  3073. .seqno_idx = tid,
  3074. .napi = NULL, /* must be NULL to not have races */
  3075. };
  3076. struct tid_ampdu_rx *tid_agg_rx;
  3077. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  3078. if (!tid_agg_rx)
  3079. return;
  3080. __skb_queue_head_init(&frames);
  3081. spin_lock(&tid_agg_rx->reorder_lock);
  3082. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  3083. spin_unlock(&tid_agg_rx->reorder_lock);
  3084. if (!skb_queue_empty(&frames)) {
  3085. struct ieee80211_event event = {
  3086. .type = BA_FRAME_TIMEOUT,
  3087. .u.ba.tid = tid,
  3088. .u.ba.sta = &sta->sta,
  3089. };
  3090. drv_event_callback(rx.local, rx.sdata, &event);
  3091. }
  3092. ieee80211_rx_handlers(&rx, &frames);
  3093. }
  3094. void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
  3095. u16 ssn, u64 filtered,
  3096. u16 received_mpdus)
  3097. {
  3098. struct sta_info *sta;
  3099. struct tid_ampdu_rx *tid_agg_rx;
  3100. struct sk_buff_head frames;
  3101. struct ieee80211_rx_data rx = {
  3102. /* This is OK -- must be QoS data frame */
  3103. .security_idx = tid,
  3104. .seqno_idx = tid,
  3105. };
  3106. int i, diff;
  3107. if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
  3108. return;
  3109. __skb_queue_head_init(&frames);
  3110. sta = container_of(pubsta, struct sta_info, sta);
  3111. rx.sta = sta;
  3112. rx.sdata = sta->sdata;
  3113. rx.local = sta->local;
  3114. rcu_read_lock();
  3115. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  3116. if (!tid_agg_rx)
  3117. goto out;
  3118. spin_lock_bh(&tid_agg_rx->reorder_lock);
  3119. if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
  3120. int release;
  3121. /* release all frames in the reorder buffer */
  3122. release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
  3123. IEEE80211_SN_MODULO;
  3124. ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
  3125. release, &frames);
  3126. /* update ssn to match received ssn */
  3127. tid_agg_rx->head_seq_num = ssn;
  3128. } else {
  3129. ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
  3130. &frames);
  3131. }
  3132. /* handle the case that received ssn is behind the mac ssn.
  3133. * it can be tid_agg_rx->buf_size behind and still be valid */
  3134. diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
  3135. if (diff >= tid_agg_rx->buf_size) {
  3136. tid_agg_rx->reorder_buf_filtered = 0;
  3137. goto release;
  3138. }
  3139. filtered = filtered >> diff;
  3140. ssn += diff;
  3141. /* update bitmap */
  3142. for (i = 0; i < tid_agg_rx->buf_size; i++) {
  3143. int index = (ssn + i) % tid_agg_rx->buf_size;
  3144. tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
  3145. if (filtered & BIT_ULL(i))
  3146. tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
  3147. }
  3148. /* now process also frames that the filter marking released */
  3149. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  3150. release:
  3151. spin_unlock_bh(&tid_agg_rx->reorder_lock);
  3152. ieee80211_rx_handlers(&rx, &frames);
  3153. out:
  3154. rcu_read_unlock();
  3155. }
  3156. EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
  3157. /* main receive path */
  3158. static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
  3159. {
  3160. struct ieee80211_sub_if_data *sdata = rx->sdata;
  3161. struct sk_buff *skb = rx->skb;
  3162. struct ieee80211_hdr *hdr = (void *)skb->data;
  3163. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3164. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  3165. bool multicast = is_multicast_ether_addr(hdr->addr1);
  3166. switch (sdata->vif.type) {
  3167. case NL80211_IFTYPE_STATION:
  3168. if (!bssid && !sdata->u.mgd.use_4addr)
  3169. return false;
  3170. if (multicast)
  3171. return true;
  3172. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3173. case NL80211_IFTYPE_ADHOC:
  3174. if (!bssid)
  3175. return false;
  3176. if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
  3177. ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
  3178. return false;
  3179. if (ieee80211_is_beacon(hdr->frame_control))
  3180. return true;
  3181. if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
  3182. return false;
  3183. if (!multicast &&
  3184. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  3185. return false;
  3186. if (!rx->sta) {
  3187. int rate_idx;
  3188. if (status->encoding != RX_ENC_LEGACY)
  3189. rate_idx = 0; /* TODO: HT/VHT rates */
  3190. else
  3191. rate_idx = status->rate_idx;
  3192. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  3193. BIT(rate_idx));
  3194. }
  3195. return true;
  3196. case NL80211_IFTYPE_OCB:
  3197. if (!bssid)
  3198. return false;
  3199. if (!ieee80211_is_data_present(hdr->frame_control))
  3200. return false;
  3201. if (!is_broadcast_ether_addr(bssid))
  3202. return false;
  3203. if (!multicast &&
  3204. !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
  3205. return false;
  3206. if (!rx->sta) {
  3207. int rate_idx;
  3208. if (status->encoding != RX_ENC_LEGACY)
  3209. rate_idx = 0; /* TODO: HT rates */
  3210. else
  3211. rate_idx = status->rate_idx;
  3212. ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
  3213. BIT(rate_idx));
  3214. }
  3215. return true;
  3216. case NL80211_IFTYPE_MESH_POINT:
  3217. if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
  3218. return false;
  3219. if (multicast)
  3220. return true;
  3221. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3222. case NL80211_IFTYPE_AP_VLAN:
  3223. case NL80211_IFTYPE_AP:
  3224. if (!bssid)
  3225. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3226. if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
  3227. /*
  3228. * Accept public action frames even when the
  3229. * BSSID doesn't match, this is used for P2P
  3230. * and location updates. Note that mac80211
  3231. * itself never looks at these frames.
  3232. */
  3233. if (!multicast &&
  3234. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  3235. return false;
  3236. if (ieee80211_is_public_action(hdr, skb->len))
  3237. return true;
  3238. return ieee80211_is_beacon(hdr->frame_control);
  3239. }
  3240. if (!ieee80211_has_tods(hdr->frame_control)) {
  3241. /* ignore data frames to TDLS-peers */
  3242. if (ieee80211_is_data(hdr->frame_control))
  3243. return false;
  3244. /* ignore action frames to TDLS-peers */
  3245. if (ieee80211_is_action(hdr->frame_control) &&
  3246. !is_broadcast_ether_addr(bssid) &&
  3247. !ether_addr_equal(bssid, hdr->addr1))
  3248. return false;
  3249. }
  3250. /*
  3251. * 802.11-2016 Table 9-26 says that for data frames, A1 must be
  3252. * the BSSID - we've checked that already but may have accepted
  3253. * the wildcard (ff:ff:ff:ff:ff:ff).
  3254. *
  3255. * It also says:
  3256. * The BSSID of the Data frame is determined as follows:
  3257. * a) If the STA is contained within an AP or is associated
  3258. * with an AP, the BSSID is the address currently in use
  3259. * by the STA contained in the AP.
  3260. *
  3261. * So we should not accept data frames with an address that's
  3262. * multicast.
  3263. *
  3264. * Accepting it also opens a security problem because stations
  3265. * could encrypt it with the GTK and inject traffic that way.
  3266. */
  3267. if (ieee80211_is_data(hdr->frame_control) && multicast)
  3268. return false;
  3269. return true;
  3270. case NL80211_IFTYPE_WDS:
  3271. if (bssid || !ieee80211_is_data(hdr->frame_control))
  3272. return false;
  3273. return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
  3274. case NL80211_IFTYPE_P2P_DEVICE:
  3275. return ieee80211_is_public_action(hdr, skb->len) ||
  3276. ieee80211_is_probe_req(hdr->frame_control) ||
  3277. ieee80211_is_probe_resp(hdr->frame_control) ||
  3278. ieee80211_is_beacon(hdr->frame_control);
  3279. case NL80211_IFTYPE_NAN:
  3280. /* Currently no frames on NAN interface are allowed */
  3281. return false;
  3282. default:
  3283. break;
  3284. }
  3285. WARN_ON_ONCE(1);
  3286. return false;
  3287. }
  3288. void ieee80211_check_fast_rx(struct sta_info *sta)
  3289. {
  3290. struct ieee80211_sub_if_data *sdata = sta->sdata;
  3291. struct ieee80211_local *local = sdata->local;
  3292. struct ieee80211_key *key;
  3293. struct ieee80211_fast_rx fastrx = {
  3294. .dev = sdata->dev,
  3295. .vif_type = sdata->vif.type,
  3296. .control_port_protocol = sdata->control_port_protocol,
  3297. }, *old, *new = NULL;
  3298. bool assign = false;
  3299. /* use sparse to check that we don't return without updating */
  3300. __acquire(check_fast_rx);
  3301. BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
  3302. BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
  3303. ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
  3304. ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
  3305. fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
  3306. /* fast-rx doesn't do reordering */
  3307. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
  3308. !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
  3309. goto clear;
  3310. switch (sdata->vif.type) {
  3311. case NL80211_IFTYPE_STATION:
  3312. if (sta->sta.tdls) {
  3313. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
  3314. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  3315. fastrx.expected_ds_bits = 0;
  3316. } else {
  3317. fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
  3318. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
  3319. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
  3320. fastrx.expected_ds_bits =
  3321. cpu_to_le16(IEEE80211_FCTL_FROMDS);
  3322. }
  3323. if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
  3324. fastrx.expected_ds_bits |=
  3325. cpu_to_le16(IEEE80211_FCTL_TODS);
  3326. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
  3327. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  3328. }
  3329. if (!sdata->u.mgd.powersave)
  3330. break;
  3331. /* software powersave is a huge mess, avoid all of it */
  3332. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
  3333. goto clear;
  3334. if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
  3335. !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
  3336. goto clear;
  3337. break;
  3338. case NL80211_IFTYPE_AP_VLAN:
  3339. case NL80211_IFTYPE_AP:
  3340. /* parallel-rx requires this, at least with calls to
  3341. * ieee80211_sta_ps_transition()
  3342. */
  3343. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  3344. goto clear;
  3345. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
  3346. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  3347. fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
  3348. fastrx.internal_forward =
  3349. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  3350. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
  3351. !sdata->u.vlan.sta);
  3352. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  3353. sdata->u.vlan.sta) {
  3354. fastrx.expected_ds_bits |=
  3355. cpu_to_le16(IEEE80211_FCTL_FROMDS);
  3356. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  3357. fastrx.internal_forward = 0;
  3358. }
  3359. break;
  3360. default:
  3361. goto clear;
  3362. }
  3363. if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  3364. goto clear;
  3365. rcu_read_lock();
  3366. key = rcu_dereference(sta->ptk[sta->ptk_idx]);
  3367. if (key) {
  3368. switch (key->conf.cipher) {
  3369. case WLAN_CIPHER_SUITE_TKIP:
  3370. /* we don't want to deal with MMIC in fast-rx */
  3371. goto clear_rcu;
  3372. case WLAN_CIPHER_SUITE_CCMP:
  3373. case WLAN_CIPHER_SUITE_CCMP_256:
  3374. case WLAN_CIPHER_SUITE_GCMP:
  3375. case WLAN_CIPHER_SUITE_GCMP_256:
  3376. break;
  3377. default:
  3378. /* we also don't want to deal with WEP or cipher scheme
  3379. * since those require looking up the key idx in the
  3380. * frame, rather than assuming the PTK is used
  3381. * (we need to revisit this once we implement the real
  3382. * PTK index, which is now valid in the spec, but we
  3383. * haven't implemented that part yet)
  3384. */
  3385. goto clear_rcu;
  3386. }
  3387. fastrx.key = true;
  3388. fastrx.icv_len = key->conf.icv_len;
  3389. }
  3390. assign = true;
  3391. clear_rcu:
  3392. rcu_read_unlock();
  3393. clear:
  3394. __release(check_fast_rx);
  3395. if (assign)
  3396. new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
  3397. spin_lock_bh(&sta->lock);
  3398. old = rcu_dereference_protected(sta->fast_rx, true);
  3399. rcu_assign_pointer(sta->fast_rx, new);
  3400. spin_unlock_bh(&sta->lock);
  3401. if (old)
  3402. kfree_rcu(old, rcu_head);
  3403. }
  3404. void ieee80211_clear_fast_rx(struct sta_info *sta)
  3405. {
  3406. struct ieee80211_fast_rx *old;
  3407. spin_lock_bh(&sta->lock);
  3408. old = rcu_dereference_protected(sta->fast_rx, true);
  3409. RCU_INIT_POINTER(sta->fast_rx, NULL);
  3410. spin_unlock_bh(&sta->lock);
  3411. if (old)
  3412. kfree_rcu(old, rcu_head);
  3413. }
  3414. void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
  3415. {
  3416. struct ieee80211_local *local = sdata->local;
  3417. struct sta_info *sta;
  3418. lockdep_assert_held(&local->sta_mtx);
  3419. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  3420. if (sdata != sta->sdata &&
  3421. (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
  3422. continue;
  3423. ieee80211_check_fast_rx(sta);
  3424. }
  3425. }
  3426. void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
  3427. {
  3428. struct ieee80211_local *local = sdata->local;
  3429. mutex_lock(&local->sta_mtx);
  3430. __ieee80211_check_fast_rx_iface(sdata);
  3431. mutex_unlock(&local->sta_mtx);
  3432. }
  3433. static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
  3434. struct ieee80211_fast_rx *fast_rx)
  3435. {
  3436. struct sk_buff *skb = rx->skb;
  3437. struct ieee80211_hdr *hdr = (void *)skb->data;
  3438. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3439. struct sta_info *sta = rx->sta;
  3440. int orig_len = skb->len;
  3441. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  3442. int snap_offs = hdrlen;
  3443. struct {
  3444. u8 snap[sizeof(rfc1042_header)];
  3445. __be16 proto;
  3446. } *payload __aligned(2);
  3447. struct {
  3448. u8 da[ETH_ALEN];
  3449. u8 sa[ETH_ALEN];
  3450. } addrs __aligned(2);
  3451. struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
  3452. if (fast_rx->uses_rss)
  3453. stats = this_cpu_ptr(sta->pcpu_rx_stats);
  3454. /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
  3455. * to a common data structure; drivers can implement that per queue
  3456. * but we don't have that information in mac80211
  3457. */
  3458. if (!(status->flag & RX_FLAG_DUP_VALIDATED))
  3459. return false;
  3460. #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
  3461. /* If using encryption, we also need to have:
  3462. * - PN_VALIDATED: similar, but the implementation is tricky
  3463. * - DECRYPTED: necessary for PN_VALIDATED
  3464. */
  3465. if (fast_rx->key &&
  3466. (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
  3467. return false;
  3468. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  3469. return false;
  3470. if (unlikely(ieee80211_is_frag(hdr)))
  3471. return false;
  3472. /* Since our interface address cannot be multicast, this
  3473. * implicitly also rejects multicast frames without the
  3474. * explicit check.
  3475. *
  3476. * We shouldn't get any *data* frames not addressed to us
  3477. * (AP mode will accept multicast *management* frames), but
  3478. * punting here will make it go through the full checks in
  3479. * ieee80211_accept_frame().
  3480. */
  3481. if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
  3482. return false;
  3483. if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
  3484. IEEE80211_FCTL_TODS)) !=
  3485. fast_rx->expected_ds_bits)
  3486. return false;
  3487. /* assign the key to drop unencrypted frames (later)
  3488. * and strip the IV/MIC if necessary
  3489. */
  3490. if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
  3491. /* GCMP header length is the same */
  3492. snap_offs += IEEE80211_CCMP_HDR_LEN;
  3493. }
  3494. if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
  3495. if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
  3496. goto drop;
  3497. payload = (void *)(skb->data + snap_offs);
  3498. if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
  3499. return false;
  3500. /* Don't handle these here since they require special code.
  3501. * Accept AARP and IPX even though they should come with a
  3502. * bridge-tunnel header - but if we get them this way then
  3503. * there's little point in discarding them.
  3504. */
  3505. if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
  3506. payload->proto == fast_rx->control_port_protocol))
  3507. return false;
  3508. }
  3509. /* after this point, don't punt to the slowpath! */
  3510. if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
  3511. pskb_trim(skb, skb->len - fast_rx->icv_len))
  3512. goto drop;
  3513. if (unlikely(fast_rx->sta_notify)) {
  3514. ieee80211_sta_rx_notify(rx->sdata, hdr);
  3515. fast_rx->sta_notify = false;
  3516. }
  3517. /* statistics part of ieee80211_rx_h_sta_process() */
  3518. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  3519. stats->last_signal = status->signal;
  3520. if (!fast_rx->uses_rss)
  3521. ewma_signal_add(&sta->rx_stats_avg.signal,
  3522. -status->signal);
  3523. }
  3524. if (status->chains) {
  3525. int i;
  3526. stats->chains = status->chains;
  3527. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  3528. int signal = status->chain_signal[i];
  3529. if (!(status->chains & BIT(i)))
  3530. continue;
  3531. stats->chain_signal_last[i] = signal;
  3532. if (!fast_rx->uses_rss)
  3533. ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
  3534. -signal);
  3535. }
  3536. }
  3537. /* end of statistics */
  3538. if (rx->key && !ieee80211_has_protected(hdr->frame_control))
  3539. goto drop;
  3540. if (status->rx_flags & IEEE80211_RX_AMSDU) {
  3541. if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
  3542. RX_QUEUED)
  3543. goto drop;
  3544. return true;
  3545. }
  3546. stats->last_rx = jiffies;
  3547. stats->last_rate = sta_stats_encode_rate(status);
  3548. stats->fragments++;
  3549. stats->packets++;
  3550. /* do the header conversion - first grab the addresses */
  3551. ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
  3552. ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
  3553. /* remove the SNAP but leave the ethertype */
  3554. skb_pull(skb, snap_offs + sizeof(rfc1042_header));
  3555. /* push the addresses in front */
  3556. memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
  3557. skb->dev = fast_rx->dev;
  3558. ieee80211_rx_stats(fast_rx->dev, skb->len);
  3559. /* The seqno index has the same property as needed
  3560. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  3561. * for non-QoS-data frames. Here we know it's a data
  3562. * frame, so count MSDUs.
  3563. */
  3564. u64_stats_update_begin(&stats->syncp);
  3565. stats->msdu[rx->seqno_idx]++;
  3566. stats->bytes += orig_len;
  3567. u64_stats_update_end(&stats->syncp);
  3568. if (fast_rx->internal_forward) {
  3569. struct sk_buff *xmit_skb = NULL;
  3570. bool multicast = is_multicast_ether_addr(skb->data);
  3571. if (multicast) {
  3572. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  3573. } else if (sta_info_get(rx->sdata, skb->data)) {
  3574. xmit_skb = skb;
  3575. skb = NULL;
  3576. }
  3577. if (xmit_skb) {
  3578. /*
  3579. * Send to wireless media and increase priority by 256
  3580. * to keep the received priority instead of
  3581. * reclassifying the frame (see cfg80211_classify8021d).
  3582. */
  3583. xmit_skb->priority += 256;
  3584. xmit_skb->protocol = htons(ETH_P_802_3);
  3585. skb_reset_network_header(xmit_skb);
  3586. skb_reset_mac_header(xmit_skb);
  3587. dev_queue_xmit(xmit_skb);
  3588. }
  3589. if (!skb)
  3590. return true;
  3591. }
  3592. /* deliver to local stack */
  3593. skb->protocol = eth_type_trans(skb, fast_rx->dev);
  3594. memset(skb->cb, 0, sizeof(skb->cb));
  3595. if (rx->napi)
  3596. napi_gro_receive(rx->napi, skb);
  3597. else
  3598. netif_receive_skb(skb);
  3599. return true;
  3600. drop:
  3601. dev_kfree_skb(skb);
  3602. stats->dropped++;
  3603. return true;
  3604. }
  3605. /*
  3606. * This function returns whether or not the SKB
  3607. * was destined for RX processing or not, which,
  3608. * if consume is true, is equivalent to whether
  3609. * or not the skb was consumed.
  3610. */
  3611. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  3612. struct sk_buff *skb, bool consume)
  3613. {
  3614. struct ieee80211_local *local = rx->local;
  3615. struct ieee80211_sub_if_data *sdata = rx->sdata;
  3616. rx->skb = skb;
  3617. /* See if we can do fast-rx; if we have to copy we already lost,
  3618. * so punt in that case. We should never have to deliver a data
  3619. * frame to multiple interfaces anyway.
  3620. *
  3621. * We skip the ieee80211_accept_frame() call and do the necessary
  3622. * checking inside ieee80211_invoke_fast_rx().
  3623. */
  3624. if (consume && rx->sta) {
  3625. struct ieee80211_fast_rx *fast_rx;
  3626. fast_rx = rcu_dereference(rx->sta->fast_rx);
  3627. if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
  3628. return true;
  3629. }
  3630. if (!ieee80211_accept_frame(rx))
  3631. return false;
  3632. if (!consume) {
  3633. skb = skb_copy(skb, GFP_ATOMIC);
  3634. if (!skb) {
  3635. if (net_ratelimit())
  3636. wiphy_debug(local->hw.wiphy,
  3637. "failed to copy skb for %s\n",
  3638. sdata->name);
  3639. return true;
  3640. }
  3641. rx->skb = skb;
  3642. }
  3643. ieee80211_invoke_rx_handlers(rx);
  3644. return true;
  3645. }
  3646. /*
  3647. * This is the actual Rx frames handler. as it belongs to Rx path it must
  3648. * be called with rcu_read_lock protection.
  3649. */
  3650. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  3651. struct ieee80211_sta *pubsta,
  3652. struct sk_buff *skb,
  3653. struct napi_struct *napi)
  3654. {
  3655. struct ieee80211_local *local = hw_to_local(hw);
  3656. struct ieee80211_sub_if_data *sdata;
  3657. struct ieee80211_hdr *hdr;
  3658. __le16 fc;
  3659. struct ieee80211_rx_data rx;
  3660. struct ieee80211_sub_if_data *prev;
  3661. struct rhlist_head *tmp;
  3662. int err = 0;
  3663. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  3664. memset(&rx, 0, sizeof(rx));
  3665. rx.skb = skb;
  3666. rx.local = local;
  3667. rx.napi = napi;
  3668. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  3669. I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
  3670. if (ieee80211_is_mgmt(fc)) {
  3671. /* drop frame if too short for header */
  3672. if (skb->len < ieee80211_hdrlen(fc))
  3673. err = -ENOBUFS;
  3674. else
  3675. err = skb_linearize(skb);
  3676. } else {
  3677. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  3678. }
  3679. if (err) {
  3680. dev_kfree_skb(skb);
  3681. return;
  3682. }
  3683. hdr = (struct ieee80211_hdr *)skb->data;
  3684. ieee80211_parse_qos(&rx);
  3685. ieee80211_verify_alignment(&rx);
  3686. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
  3687. ieee80211_is_beacon(hdr->frame_control)))
  3688. ieee80211_scan_rx(local, skb);
  3689. if (ieee80211_is_data(fc)) {
  3690. struct sta_info *sta, *prev_sta;
  3691. if (pubsta) {
  3692. rx.sta = container_of(pubsta, struct sta_info, sta);
  3693. rx.sdata = rx.sta->sdata;
  3694. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3695. return;
  3696. goto out;
  3697. }
  3698. prev_sta = NULL;
  3699. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  3700. if (!prev_sta) {
  3701. prev_sta = sta;
  3702. continue;
  3703. }
  3704. rx.sta = prev_sta;
  3705. rx.sdata = prev_sta->sdata;
  3706. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  3707. prev_sta = sta;
  3708. }
  3709. if (prev_sta) {
  3710. rx.sta = prev_sta;
  3711. rx.sdata = prev_sta->sdata;
  3712. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3713. return;
  3714. goto out;
  3715. }
  3716. }
  3717. prev = NULL;
  3718. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  3719. if (!ieee80211_sdata_running(sdata))
  3720. continue;
  3721. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  3722. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  3723. continue;
  3724. /*
  3725. * frame is destined for this interface, but if it's
  3726. * not also for the previous one we handle that after
  3727. * the loop to avoid copying the SKB once too much
  3728. */
  3729. if (!prev) {
  3730. prev = sdata;
  3731. continue;
  3732. }
  3733. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  3734. rx.sdata = prev;
  3735. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  3736. prev = sdata;
  3737. }
  3738. if (prev) {
  3739. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  3740. rx.sdata = prev;
  3741. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3742. return;
  3743. }
  3744. out:
  3745. dev_kfree_skb(skb);
  3746. }
  3747. /*
  3748. * This is the receive path handler. It is called by a low level driver when an
  3749. * 802.11 MPDU is received from the hardware.
  3750. */
  3751. void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
  3752. struct sk_buff *skb, struct napi_struct *napi)
  3753. {
  3754. struct ieee80211_local *local = hw_to_local(hw);
  3755. struct ieee80211_rate *rate = NULL;
  3756. struct ieee80211_supported_band *sband;
  3757. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3758. WARN_ON_ONCE(softirq_count() == 0);
  3759. if (WARN_ON(status->band >= NUM_NL80211_BANDS))
  3760. goto drop;
  3761. sband = local->hw.wiphy->bands[status->band];
  3762. if (WARN_ON(!sband))
  3763. goto drop;
  3764. /*
  3765. * If we're suspending, it is possible although not too likely
  3766. * that we'd be receiving frames after having already partially
  3767. * quiesced the stack. We can't process such frames then since
  3768. * that might, for example, cause stations to be added or other
  3769. * driver callbacks be invoked.
  3770. */
  3771. if (unlikely(local->quiescing || local->suspended))
  3772. goto drop;
  3773. /* We might be during a HW reconfig, prevent Rx for the same reason */
  3774. if (unlikely(local->in_reconfig))
  3775. goto drop;
  3776. /*
  3777. * The same happens when we're not even started,
  3778. * but that's worth a warning.
  3779. */
  3780. if (WARN_ON(!local->started))
  3781. goto drop;
  3782. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  3783. /*
  3784. * Validate the rate, unless a PLCP error means that
  3785. * we probably can't have a valid rate here anyway.
  3786. */
  3787. switch (status->encoding) {
  3788. case RX_ENC_HT:
  3789. /*
  3790. * rate_idx is MCS index, which can be [0-76]
  3791. * as documented on:
  3792. *
  3793. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  3794. *
  3795. * Anything else would be some sort of driver or
  3796. * hardware error. The driver should catch hardware
  3797. * errors.
  3798. */
  3799. if (WARN(status->rate_idx > 76,
  3800. "Rate marked as an HT rate but passed "
  3801. "status->rate_idx is not "
  3802. "an MCS index [0-76]: %d (0x%02x)\n",
  3803. status->rate_idx,
  3804. status->rate_idx))
  3805. goto drop;
  3806. break;
  3807. case RX_ENC_VHT:
  3808. if (WARN_ONCE(status->rate_idx > 9 ||
  3809. !status->nss ||
  3810. status->nss > 8,
  3811. "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
  3812. status->rate_idx, status->nss))
  3813. goto drop;
  3814. break;
  3815. case RX_ENC_HE:
  3816. if (WARN_ONCE(status->rate_idx > 11 ||
  3817. !status->nss ||
  3818. status->nss > 8,
  3819. "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
  3820. status->rate_idx, status->nss))
  3821. goto drop;
  3822. break;
  3823. default:
  3824. WARN_ON_ONCE(1);
  3825. /* fall through */
  3826. case RX_ENC_LEGACY:
  3827. if (WARN_ON(status->rate_idx >= sband->n_bitrates))
  3828. goto drop;
  3829. rate = &sband->bitrates[status->rate_idx];
  3830. }
  3831. }
  3832. status->rx_flags = 0;
  3833. /*
  3834. * key references and virtual interfaces are protected using RCU
  3835. * and this requires that we are in a read-side RCU section during
  3836. * receive processing
  3837. */
  3838. rcu_read_lock();
  3839. /*
  3840. * Frames with failed FCS/PLCP checksum are not returned,
  3841. * all other frames are returned without radiotap header
  3842. * if it was previously present.
  3843. * Also, frames with less than 16 bytes are dropped.
  3844. */
  3845. skb = ieee80211_rx_monitor(local, skb, rate);
  3846. if (!skb) {
  3847. rcu_read_unlock();
  3848. return;
  3849. }
  3850. ieee80211_tpt_led_trig_rx(local,
  3851. ((struct ieee80211_hdr *)skb->data)->frame_control,
  3852. skb->len);
  3853. __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
  3854. rcu_read_unlock();
  3855. return;
  3856. drop:
  3857. kfree_skb(skb);
  3858. }
  3859. EXPORT_SYMBOL(ieee80211_rx_napi);
  3860. /* This is a version of the rx handler that can be called from hard irq
  3861. * context. Post the skb on the queue and schedule the tasklet */
  3862. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  3863. {
  3864. struct ieee80211_local *local = hw_to_local(hw);
  3865. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3866. skb->pkt_type = IEEE80211_RX_MSG;
  3867. skb_queue_tail(&local->skb_queue, skb);
  3868. tasklet_schedule(&local->tasklet);
  3869. }
  3870. EXPORT_SYMBOL(ieee80211_rx_irqsafe);