rx.c 128 KB

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