rx.c 102 KB

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