rx.c 116 KB

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