rx.c 127 KB

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