rx.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  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. /* frame is in RMC, don't forward */
  1896. if (ieee80211_is_data(hdr->frame_control) &&
  1897. is_multicast_ether_addr(hdr->addr1) &&
  1898. mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
  1899. return RX_DROP_MONITOR;
  1900. if (!ieee80211_is_data(hdr->frame_control) ||
  1901. !(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1902. return RX_CONTINUE;
  1903. if (!mesh_hdr->ttl)
  1904. return RX_DROP_MONITOR;
  1905. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1906. struct mesh_path *mppath;
  1907. char *proxied_addr;
  1908. char *mpp_addr;
  1909. if (is_multicast_ether_addr(hdr->addr1)) {
  1910. mpp_addr = hdr->addr3;
  1911. proxied_addr = mesh_hdr->eaddr1;
  1912. } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
  1913. /* has_a4 already checked in ieee80211_rx_mesh_check */
  1914. mpp_addr = hdr->addr4;
  1915. proxied_addr = mesh_hdr->eaddr2;
  1916. } else {
  1917. return RX_DROP_MONITOR;
  1918. }
  1919. rcu_read_lock();
  1920. mppath = mpp_path_lookup(sdata, proxied_addr);
  1921. if (!mppath) {
  1922. mpp_path_add(sdata, proxied_addr, mpp_addr);
  1923. } else {
  1924. spin_lock_bh(&mppath->state_lock);
  1925. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  1926. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1927. spin_unlock_bh(&mppath->state_lock);
  1928. }
  1929. rcu_read_unlock();
  1930. }
  1931. /* Frame has reached destination. Don't forward */
  1932. if (!is_multicast_ether_addr(hdr->addr1) &&
  1933. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  1934. return RX_CONTINUE;
  1935. q = ieee80211_select_queue_80211(sdata, skb, hdr);
  1936. if (ieee80211_queue_stopped(&local->hw, q)) {
  1937. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  1938. return RX_DROP_MONITOR;
  1939. }
  1940. skb_set_queue_mapping(skb, q);
  1941. if (!--mesh_hdr->ttl) {
  1942. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  1943. goto out;
  1944. }
  1945. if (!ifmsh->mshcfg.dot11MeshForwarding)
  1946. goto out;
  1947. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1948. if (!fwd_skb) {
  1949. net_info_ratelimited("%s: failed to clone mesh frame\n",
  1950. sdata->name);
  1951. goto out;
  1952. }
  1953. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1954. fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
  1955. info = IEEE80211_SKB_CB(fwd_skb);
  1956. memset(info, 0, sizeof(*info));
  1957. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1958. info->control.vif = &rx->sdata->vif;
  1959. info->control.jiffies = jiffies;
  1960. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  1961. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  1962. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1963. /* update power mode indication when forwarding */
  1964. ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
  1965. } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
  1966. /* mesh power mode flags updated in mesh_nexthop_lookup */
  1967. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  1968. } else {
  1969. /* unable to resolve next hop */
  1970. mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
  1971. fwd_hdr->addr3, 0,
  1972. WLAN_REASON_MESH_PATH_NOFORWARD,
  1973. fwd_hdr->addr2);
  1974. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  1975. kfree_skb(fwd_skb);
  1976. return RX_DROP_MONITOR;
  1977. }
  1978. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  1979. ieee80211_add_pending_skb(local, fwd_skb);
  1980. out:
  1981. if (is_multicast_ether_addr(hdr->addr1) ||
  1982. sdata->dev->flags & IFF_PROMISC)
  1983. return RX_CONTINUE;
  1984. else
  1985. return RX_DROP_MONITOR;
  1986. }
  1987. #endif
  1988. static ieee80211_rx_result debug_noinline
  1989. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1990. {
  1991. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1992. struct ieee80211_local *local = rx->local;
  1993. struct net_device *dev = sdata->dev;
  1994. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1995. __le16 fc = hdr->frame_control;
  1996. bool port_control;
  1997. int err;
  1998. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1999. return RX_CONTINUE;
  2000. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  2001. return RX_DROP_MONITOR;
  2002. if (rx->sta) {
  2003. /* The seqno index has the same property as needed
  2004. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  2005. * for non-QoS-data frames. Here we know it's a data
  2006. * frame, so count MSDUs.
  2007. */
  2008. rx->sta->rx_msdu[rx->seqno_idx]++;
  2009. }
  2010. /*
  2011. * Send unexpected-4addr-frame event to hostapd. For older versions,
  2012. * also drop the frame to cooked monitor interfaces.
  2013. */
  2014. if (ieee80211_has_a4(hdr->frame_control) &&
  2015. sdata->vif.type == NL80211_IFTYPE_AP) {
  2016. if (rx->sta &&
  2017. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  2018. cfg80211_rx_unexpected_4addr_frame(
  2019. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  2020. return RX_DROP_MONITOR;
  2021. }
  2022. err = __ieee80211_data_to_8023(rx, &port_control);
  2023. if (unlikely(err))
  2024. return RX_DROP_UNUSABLE;
  2025. if (!ieee80211_frame_allowed(rx, fc))
  2026. return RX_DROP_MONITOR;
  2027. /* directly handle TDLS channel switch requests/responses */
  2028. if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
  2029. cpu_to_be16(ETH_P_TDLS))) {
  2030. struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
  2031. if (pskb_may_pull(rx->skb,
  2032. offsetof(struct ieee80211_tdls_data, u)) &&
  2033. tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
  2034. tf->category == WLAN_CATEGORY_TDLS &&
  2035. (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
  2036. tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
  2037. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TDLS_CHSW;
  2038. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2039. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2040. if (rx->sta)
  2041. rx->sta->rx_packets++;
  2042. return RX_QUEUED;
  2043. }
  2044. }
  2045. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  2046. unlikely(port_control) && sdata->bss) {
  2047. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2048. u.ap);
  2049. dev = sdata->dev;
  2050. rx->sdata = sdata;
  2051. }
  2052. rx->skb->dev = dev;
  2053. dev->stats.rx_packets++;
  2054. dev->stats.rx_bytes += rx->skb->len;
  2055. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  2056. !is_multicast_ether_addr(
  2057. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  2058. (!local->scanning &&
  2059. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  2060. mod_timer(&local->dynamic_ps_timer, jiffies +
  2061. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  2062. }
  2063. ieee80211_deliver_skb(rx);
  2064. return RX_QUEUED;
  2065. }
  2066. static ieee80211_rx_result debug_noinline
  2067. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
  2068. {
  2069. struct sk_buff *skb = rx->skb;
  2070. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  2071. struct tid_ampdu_rx *tid_agg_rx;
  2072. u16 start_seq_num;
  2073. u16 tid;
  2074. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  2075. return RX_CONTINUE;
  2076. if (ieee80211_is_back_req(bar->frame_control)) {
  2077. struct {
  2078. __le16 control, start_seq_num;
  2079. } __packed bar_data;
  2080. if (!rx->sta)
  2081. return RX_DROP_MONITOR;
  2082. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  2083. &bar_data, sizeof(bar_data)))
  2084. return RX_DROP_MONITOR;
  2085. tid = le16_to_cpu(bar_data.control) >> 12;
  2086. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  2087. if (!tid_agg_rx)
  2088. return RX_DROP_MONITOR;
  2089. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  2090. /* reset session timer */
  2091. if (tid_agg_rx->timeout)
  2092. mod_timer(&tid_agg_rx->session_timer,
  2093. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2094. spin_lock(&tid_agg_rx->reorder_lock);
  2095. /* release stored frames up to start of BAR */
  2096. ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
  2097. start_seq_num, frames);
  2098. spin_unlock(&tid_agg_rx->reorder_lock);
  2099. kfree_skb(skb);
  2100. return RX_QUEUED;
  2101. }
  2102. /*
  2103. * After this point, we only want management frames,
  2104. * so we can drop all remaining control frames to
  2105. * cooked monitor interfaces.
  2106. */
  2107. return RX_DROP_MONITOR;
  2108. }
  2109. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  2110. struct ieee80211_mgmt *mgmt,
  2111. size_t len)
  2112. {
  2113. struct ieee80211_local *local = sdata->local;
  2114. struct sk_buff *skb;
  2115. struct ieee80211_mgmt *resp;
  2116. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  2117. /* Not to own unicast address */
  2118. return;
  2119. }
  2120. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  2121. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  2122. /* Not from the current AP or not associated yet. */
  2123. return;
  2124. }
  2125. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  2126. /* Too short SA Query request frame */
  2127. return;
  2128. }
  2129. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  2130. if (skb == NULL)
  2131. return;
  2132. skb_reserve(skb, local->hw.extra_tx_headroom);
  2133. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  2134. memset(resp, 0, 24);
  2135. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2136. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  2137. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  2138. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2139. IEEE80211_STYPE_ACTION);
  2140. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  2141. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  2142. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  2143. memcpy(resp->u.action.u.sa_query.trans_id,
  2144. mgmt->u.action.u.sa_query.trans_id,
  2145. WLAN_SA_QUERY_TR_ID_LEN);
  2146. ieee80211_tx_skb(sdata, skb);
  2147. }
  2148. static ieee80211_rx_result debug_noinline
  2149. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  2150. {
  2151. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2152. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2153. /*
  2154. * From here on, look only at management frames.
  2155. * Data and control frames are already handled,
  2156. * and unknown (reserved) frames are useless.
  2157. */
  2158. if (rx->skb->len < 24)
  2159. return RX_DROP_MONITOR;
  2160. if (!ieee80211_is_mgmt(mgmt->frame_control))
  2161. return RX_DROP_MONITOR;
  2162. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  2163. ieee80211_is_beacon(mgmt->frame_control) &&
  2164. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  2165. int sig = 0;
  2166. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  2167. sig = status->signal;
  2168. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  2169. rx->skb->data, rx->skb->len,
  2170. status->freq, sig);
  2171. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  2172. }
  2173. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  2174. return RX_DROP_MONITOR;
  2175. if (ieee80211_drop_unencrypted_mgmt(rx))
  2176. return RX_DROP_UNUSABLE;
  2177. return RX_CONTINUE;
  2178. }
  2179. static ieee80211_rx_result debug_noinline
  2180. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  2181. {
  2182. struct ieee80211_local *local = rx->local;
  2183. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2184. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2185. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2186. int len = rx->skb->len;
  2187. if (!ieee80211_is_action(mgmt->frame_control))
  2188. return RX_CONTINUE;
  2189. /* drop too small frames */
  2190. if (len < IEEE80211_MIN_ACTION_SIZE)
  2191. return RX_DROP_UNUSABLE;
  2192. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
  2193. mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
  2194. mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  2195. return RX_DROP_UNUSABLE;
  2196. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  2197. return RX_DROP_UNUSABLE;
  2198. switch (mgmt->u.action.category) {
  2199. case WLAN_CATEGORY_HT:
  2200. /* reject HT action frames from stations not supporting HT */
  2201. if (!rx->sta->sta.ht_cap.ht_supported)
  2202. goto invalid;
  2203. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2204. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2205. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2206. sdata->vif.type != NL80211_IFTYPE_AP &&
  2207. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2208. break;
  2209. /* verify action & smps_control/chanwidth are present */
  2210. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2211. goto invalid;
  2212. switch (mgmt->u.action.u.ht_smps.action) {
  2213. case WLAN_HT_ACTION_SMPS: {
  2214. struct ieee80211_supported_band *sband;
  2215. enum ieee80211_smps_mode smps_mode;
  2216. /* convert to HT capability */
  2217. switch (mgmt->u.action.u.ht_smps.smps_control) {
  2218. case WLAN_HT_SMPS_CONTROL_DISABLED:
  2219. smps_mode = IEEE80211_SMPS_OFF;
  2220. break;
  2221. case WLAN_HT_SMPS_CONTROL_STATIC:
  2222. smps_mode = IEEE80211_SMPS_STATIC;
  2223. break;
  2224. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  2225. smps_mode = IEEE80211_SMPS_DYNAMIC;
  2226. break;
  2227. default:
  2228. goto invalid;
  2229. }
  2230. /* if no change do nothing */
  2231. if (rx->sta->sta.smps_mode == smps_mode)
  2232. goto handled;
  2233. rx->sta->sta.smps_mode = smps_mode;
  2234. sband = rx->local->hw.wiphy->bands[status->band];
  2235. rate_control_rate_update(local, sband, rx->sta,
  2236. IEEE80211_RC_SMPS_CHANGED);
  2237. goto handled;
  2238. }
  2239. case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
  2240. struct ieee80211_supported_band *sband;
  2241. u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
  2242. enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
  2243. /* If it doesn't support 40 MHz it can't change ... */
  2244. if (!(rx->sta->sta.ht_cap.cap &
  2245. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  2246. goto handled;
  2247. if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
  2248. max_bw = IEEE80211_STA_RX_BW_20;
  2249. else
  2250. max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
  2251. /* set cur_max_bandwidth and recalc sta bw */
  2252. rx->sta->cur_max_bandwidth = max_bw;
  2253. new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
  2254. if (rx->sta->sta.bandwidth == new_bw)
  2255. goto handled;
  2256. rx->sta->sta.bandwidth = new_bw;
  2257. sband = rx->local->hw.wiphy->bands[status->band];
  2258. rate_control_rate_update(local, sband, rx->sta,
  2259. IEEE80211_RC_BW_CHANGED);
  2260. goto handled;
  2261. }
  2262. default:
  2263. goto invalid;
  2264. }
  2265. break;
  2266. case WLAN_CATEGORY_PUBLIC:
  2267. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2268. goto invalid;
  2269. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2270. break;
  2271. if (!rx->sta)
  2272. break;
  2273. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  2274. break;
  2275. if (mgmt->u.action.u.ext_chan_switch.action_code !=
  2276. WLAN_PUB_ACTION_EXT_CHANSW_ANN)
  2277. break;
  2278. if (len < offsetof(struct ieee80211_mgmt,
  2279. u.action.u.ext_chan_switch.variable))
  2280. goto invalid;
  2281. goto queue;
  2282. case WLAN_CATEGORY_VHT:
  2283. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2284. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2285. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2286. sdata->vif.type != NL80211_IFTYPE_AP &&
  2287. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2288. break;
  2289. /* verify action code is present */
  2290. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2291. goto invalid;
  2292. switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
  2293. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  2294. u8 opmode;
  2295. /* verify opmode is present */
  2296. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2297. goto invalid;
  2298. opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
  2299. ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
  2300. opmode, status->band,
  2301. false);
  2302. goto handled;
  2303. }
  2304. default:
  2305. break;
  2306. }
  2307. break;
  2308. case WLAN_CATEGORY_BACK:
  2309. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2310. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2311. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2312. sdata->vif.type != NL80211_IFTYPE_AP &&
  2313. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2314. break;
  2315. /* verify action_code is present */
  2316. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2317. break;
  2318. switch (mgmt->u.action.u.addba_req.action_code) {
  2319. case WLAN_ACTION_ADDBA_REQ:
  2320. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2321. sizeof(mgmt->u.action.u.addba_req)))
  2322. goto invalid;
  2323. break;
  2324. case WLAN_ACTION_ADDBA_RESP:
  2325. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2326. sizeof(mgmt->u.action.u.addba_resp)))
  2327. goto invalid;
  2328. break;
  2329. case WLAN_ACTION_DELBA:
  2330. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2331. sizeof(mgmt->u.action.u.delba)))
  2332. goto invalid;
  2333. break;
  2334. default:
  2335. goto invalid;
  2336. }
  2337. goto queue;
  2338. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2339. /* verify action_code is present */
  2340. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2341. break;
  2342. switch (mgmt->u.action.u.measurement.action_code) {
  2343. case WLAN_ACTION_SPCT_MSR_REQ:
  2344. if (status->band != IEEE80211_BAND_5GHZ)
  2345. break;
  2346. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2347. sizeof(mgmt->u.action.u.measurement)))
  2348. break;
  2349. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2350. break;
  2351. ieee80211_process_measurement_req(sdata, mgmt, len);
  2352. goto handled;
  2353. case WLAN_ACTION_SPCT_CHL_SWITCH: {
  2354. u8 *bssid;
  2355. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2356. sizeof(mgmt->u.action.u.chan_switch)))
  2357. break;
  2358. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2359. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2360. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2361. break;
  2362. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2363. bssid = sdata->u.mgd.bssid;
  2364. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2365. bssid = sdata->u.ibss.bssid;
  2366. else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  2367. bssid = mgmt->sa;
  2368. else
  2369. break;
  2370. if (!ether_addr_equal(mgmt->bssid, bssid))
  2371. break;
  2372. goto queue;
  2373. }
  2374. }
  2375. break;
  2376. case WLAN_CATEGORY_SA_QUERY:
  2377. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2378. sizeof(mgmt->u.action.u.sa_query)))
  2379. break;
  2380. switch (mgmt->u.action.u.sa_query.action) {
  2381. case WLAN_ACTION_SA_QUERY_REQUEST:
  2382. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2383. break;
  2384. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2385. goto handled;
  2386. }
  2387. break;
  2388. case WLAN_CATEGORY_SELF_PROTECTED:
  2389. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2390. sizeof(mgmt->u.action.u.self_prot.action_code)))
  2391. break;
  2392. switch (mgmt->u.action.u.self_prot.action_code) {
  2393. case WLAN_SP_MESH_PEERING_OPEN:
  2394. case WLAN_SP_MESH_PEERING_CLOSE:
  2395. case WLAN_SP_MESH_PEERING_CONFIRM:
  2396. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2397. goto invalid;
  2398. if (sdata->u.mesh.user_mpm)
  2399. /* userspace handles this frame */
  2400. break;
  2401. goto queue;
  2402. case WLAN_SP_MGK_INFORM:
  2403. case WLAN_SP_MGK_ACK:
  2404. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2405. goto invalid;
  2406. break;
  2407. }
  2408. break;
  2409. case WLAN_CATEGORY_MESH_ACTION:
  2410. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2411. sizeof(mgmt->u.action.u.mesh_action.action_code)))
  2412. break;
  2413. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2414. break;
  2415. if (mesh_action_is_path_sel(mgmt) &&
  2416. !mesh_path_sel_is_hwmp(sdata))
  2417. break;
  2418. goto queue;
  2419. }
  2420. return RX_CONTINUE;
  2421. invalid:
  2422. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2423. /* will return in the next handlers */
  2424. return RX_CONTINUE;
  2425. handled:
  2426. if (rx->sta)
  2427. rx->sta->rx_packets++;
  2428. dev_kfree_skb(rx->skb);
  2429. return RX_QUEUED;
  2430. queue:
  2431. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2432. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2433. ieee80211_queue_work(&local->hw, &sdata->work);
  2434. if (rx->sta)
  2435. rx->sta->rx_packets++;
  2436. return RX_QUEUED;
  2437. }
  2438. static ieee80211_rx_result debug_noinline
  2439. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2440. {
  2441. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2442. int sig = 0;
  2443. /* skip known-bad action frames and return them in the next handler */
  2444. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2445. return RX_CONTINUE;
  2446. /*
  2447. * Getting here means the kernel doesn't know how to handle
  2448. * it, but maybe userspace does ... include returned frames
  2449. * so userspace can register for those to know whether ones
  2450. * it transmitted were processed or returned.
  2451. */
  2452. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  2453. sig = status->signal;
  2454. if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
  2455. rx->skb->data, rx->skb->len, 0)) {
  2456. if (rx->sta)
  2457. rx->sta->rx_packets++;
  2458. dev_kfree_skb(rx->skb);
  2459. return RX_QUEUED;
  2460. }
  2461. return RX_CONTINUE;
  2462. }
  2463. static ieee80211_rx_result debug_noinline
  2464. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2465. {
  2466. struct ieee80211_local *local = rx->local;
  2467. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2468. struct sk_buff *nskb;
  2469. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2470. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2471. if (!ieee80211_is_action(mgmt->frame_control))
  2472. return RX_CONTINUE;
  2473. /*
  2474. * For AP mode, hostapd is responsible for handling any action
  2475. * frames that we didn't handle, including returning unknown
  2476. * ones. For all other modes we will return them to the sender,
  2477. * setting the 0x80 bit in the action category, as required by
  2478. * 802.11-2012 9.24.4.
  2479. * Newer versions of hostapd shall also use the management frame
  2480. * registration mechanisms, but older ones still use cooked
  2481. * monitor interfaces so push all frames there.
  2482. */
  2483. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2484. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2485. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2486. return RX_DROP_MONITOR;
  2487. if (is_multicast_ether_addr(mgmt->da))
  2488. return RX_DROP_MONITOR;
  2489. /* do not return rejected action frames */
  2490. if (mgmt->u.action.category & 0x80)
  2491. return RX_DROP_UNUSABLE;
  2492. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2493. GFP_ATOMIC);
  2494. if (nskb) {
  2495. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2496. nmgmt->u.action.category |= 0x80;
  2497. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2498. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2499. memset(nskb->cb, 0, sizeof(nskb->cb));
  2500. if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  2501. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
  2502. info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
  2503. IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
  2504. IEEE80211_TX_CTL_NO_CCK_RATE;
  2505. if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
  2506. info->hw_queue =
  2507. local->hw.offchannel_tx_hw_queue;
  2508. }
  2509. __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
  2510. status->band);
  2511. }
  2512. dev_kfree_skb(rx->skb);
  2513. return RX_QUEUED;
  2514. }
  2515. static ieee80211_rx_result debug_noinline
  2516. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2517. {
  2518. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2519. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2520. __le16 stype;
  2521. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2522. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2523. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2524. sdata->vif.type != NL80211_IFTYPE_OCB &&
  2525. sdata->vif.type != NL80211_IFTYPE_STATION)
  2526. return RX_DROP_MONITOR;
  2527. switch (stype) {
  2528. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2529. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2530. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2531. /* process for all: mesh, mlme, ibss */
  2532. break;
  2533. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2534. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2535. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2536. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2537. if (is_multicast_ether_addr(mgmt->da) &&
  2538. !is_broadcast_ether_addr(mgmt->da))
  2539. return RX_DROP_MONITOR;
  2540. /* process only for station */
  2541. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2542. return RX_DROP_MONITOR;
  2543. break;
  2544. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2545. /* process only for ibss and mesh */
  2546. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2547. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2548. return RX_DROP_MONITOR;
  2549. break;
  2550. default:
  2551. return RX_DROP_MONITOR;
  2552. }
  2553. /* queue up frame and kick off work to process it */
  2554. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2555. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2556. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2557. if (rx->sta)
  2558. rx->sta->rx_packets++;
  2559. return RX_QUEUED;
  2560. }
  2561. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2562. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2563. struct ieee80211_rate *rate)
  2564. {
  2565. struct ieee80211_sub_if_data *sdata;
  2566. struct ieee80211_local *local = rx->local;
  2567. struct sk_buff *skb = rx->skb, *skb2;
  2568. struct net_device *prev_dev = NULL;
  2569. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2570. int needed_headroom;
  2571. /*
  2572. * If cooked monitor has been processed already, then
  2573. * don't do it again. If not, set the flag.
  2574. */
  2575. if (rx->flags & IEEE80211_RX_CMNTR)
  2576. goto out_free_skb;
  2577. rx->flags |= IEEE80211_RX_CMNTR;
  2578. /* If there are no cooked monitor interfaces, just free the SKB */
  2579. if (!local->cooked_mntrs)
  2580. goto out_free_skb;
  2581. /* vendor data is long removed here */
  2582. status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
  2583. /* room for the radiotap header based on driver features */
  2584. needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
  2585. if (skb_headroom(skb) < needed_headroom &&
  2586. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2587. goto out_free_skb;
  2588. /* prepend radiotap information */
  2589. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2590. false);
  2591. skb_set_mac_header(skb, 0);
  2592. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2593. skb->pkt_type = PACKET_OTHERHOST;
  2594. skb->protocol = htons(ETH_P_802_2);
  2595. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2596. if (!ieee80211_sdata_running(sdata))
  2597. continue;
  2598. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2599. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2600. continue;
  2601. if (prev_dev) {
  2602. skb2 = skb_clone(skb, GFP_ATOMIC);
  2603. if (skb2) {
  2604. skb2->dev = prev_dev;
  2605. netif_receive_skb(skb2);
  2606. }
  2607. }
  2608. prev_dev = sdata->dev;
  2609. sdata->dev->stats.rx_packets++;
  2610. sdata->dev->stats.rx_bytes += skb->len;
  2611. }
  2612. if (prev_dev) {
  2613. skb->dev = prev_dev;
  2614. netif_receive_skb(skb);
  2615. return;
  2616. }
  2617. out_free_skb:
  2618. dev_kfree_skb(skb);
  2619. }
  2620. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2621. ieee80211_rx_result res)
  2622. {
  2623. switch (res) {
  2624. case RX_DROP_MONITOR:
  2625. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2626. if (rx->sta)
  2627. rx->sta->rx_dropped++;
  2628. /* fall through */
  2629. case RX_CONTINUE: {
  2630. struct ieee80211_rate *rate = NULL;
  2631. struct ieee80211_supported_band *sband;
  2632. struct ieee80211_rx_status *status;
  2633. status = IEEE80211_SKB_RXCB((rx->skb));
  2634. sband = rx->local->hw.wiphy->bands[status->band];
  2635. if (!(status->flag & RX_FLAG_HT) &&
  2636. !(status->flag & RX_FLAG_VHT))
  2637. rate = &sband->bitrates[status->rate_idx];
  2638. ieee80211_rx_cooked_monitor(rx, rate);
  2639. break;
  2640. }
  2641. case RX_DROP_UNUSABLE:
  2642. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2643. if (rx->sta)
  2644. rx->sta->rx_dropped++;
  2645. dev_kfree_skb(rx->skb);
  2646. break;
  2647. case RX_QUEUED:
  2648. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2649. break;
  2650. }
  2651. }
  2652. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
  2653. struct sk_buff_head *frames)
  2654. {
  2655. ieee80211_rx_result res = RX_DROP_MONITOR;
  2656. struct sk_buff *skb;
  2657. #define CALL_RXH(rxh) \
  2658. do { \
  2659. res = rxh(rx); \
  2660. if (res != RX_CONTINUE) \
  2661. goto rxh_next; \
  2662. } while (0);
  2663. spin_lock_bh(&rx->local->rx_path_lock);
  2664. while ((skb = __skb_dequeue(frames))) {
  2665. /*
  2666. * all the other fields are valid across frames
  2667. * that belong to an aMPDU since they are on the
  2668. * same TID from the same station
  2669. */
  2670. rx->skb = skb;
  2671. CALL_RXH(ieee80211_rx_h_check_more_data)
  2672. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
  2673. CALL_RXH(ieee80211_rx_h_sta_process)
  2674. CALL_RXH(ieee80211_rx_h_decrypt)
  2675. CALL_RXH(ieee80211_rx_h_defragment)
  2676. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2677. /* must be after MMIC verify so header is counted in MPDU mic */
  2678. #ifdef CONFIG_MAC80211_MESH
  2679. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2680. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2681. #endif
  2682. CALL_RXH(ieee80211_rx_h_amsdu)
  2683. CALL_RXH(ieee80211_rx_h_data)
  2684. /* special treatment -- needs the queue */
  2685. res = ieee80211_rx_h_ctrl(rx, frames);
  2686. if (res != RX_CONTINUE)
  2687. goto rxh_next;
  2688. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2689. CALL_RXH(ieee80211_rx_h_action)
  2690. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2691. CALL_RXH(ieee80211_rx_h_action_return)
  2692. CALL_RXH(ieee80211_rx_h_mgmt)
  2693. rxh_next:
  2694. ieee80211_rx_handlers_result(rx, res);
  2695. #undef CALL_RXH
  2696. }
  2697. spin_unlock_bh(&rx->local->rx_path_lock);
  2698. }
  2699. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2700. {
  2701. struct sk_buff_head reorder_release;
  2702. ieee80211_rx_result res = RX_DROP_MONITOR;
  2703. __skb_queue_head_init(&reorder_release);
  2704. #define CALL_RXH(rxh) \
  2705. do { \
  2706. res = rxh(rx); \
  2707. if (res != RX_CONTINUE) \
  2708. goto rxh_next; \
  2709. } while (0);
  2710. CALL_RXH(ieee80211_rx_h_check_dup)
  2711. CALL_RXH(ieee80211_rx_h_check)
  2712. ieee80211_rx_reorder_ampdu(rx, &reorder_release);
  2713. ieee80211_rx_handlers(rx, &reorder_release);
  2714. return;
  2715. rxh_next:
  2716. ieee80211_rx_handlers_result(rx, res);
  2717. #undef CALL_RXH
  2718. }
  2719. /*
  2720. * This function makes calls into the RX path, therefore
  2721. * it has to be invoked under RCU read lock.
  2722. */
  2723. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2724. {
  2725. struct sk_buff_head frames;
  2726. struct ieee80211_rx_data rx = {
  2727. .sta = sta,
  2728. .sdata = sta->sdata,
  2729. .local = sta->local,
  2730. /* This is OK -- must be QoS data frame */
  2731. .security_idx = tid,
  2732. .seqno_idx = tid,
  2733. .flags = 0,
  2734. };
  2735. struct tid_ampdu_rx *tid_agg_rx;
  2736. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2737. if (!tid_agg_rx)
  2738. return;
  2739. __skb_queue_head_init(&frames);
  2740. spin_lock(&tid_agg_rx->reorder_lock);
  2741. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  2742. spin_unlock(&tid_agg_rx->reorder_lock);
  2743. ieee80211_rx_handlers(&rx, &frames);
  2744. }
  2745. /* main receive path */
  2746. static bool prepare_for_handlers(struct ieee80211_rx_data *rx,
  2747. struct ieee80211_hdr *hdr)
  2748. {
  2749. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2750. struct sk_buff *skb = rx->skb;
  2751. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2752. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2753. int multicast = is_multicast_ether_addr(hdr->addr1);
  2754. switch (sdata->vif.type) {
  2755. case NL80211_IFTYPE_STATION:
  2756. if (!bssid && !sdata->u.mgd.use_4addr)
  2757. return false;
  2758. if (!multicast &&
  2759. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2760. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2761. sdata->u.mgd.use_4addr)
  2762. return false;
  2763. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2764. }
  2765. break;
  2766. case NL80211_IFTYPE_ADHOC:
  2767. if (!bssid)
  2768. return false;
  2769. if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
  2770. ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
  2771. return false;
  2772. if (ieee80211_is_beacon(hdr->frame_control)) {
  2773. return true;
  2774. } else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2775. return false;
  2776. } else if (!multicast &&
  2777. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2778. if (!(sdata->dev->flags & IFF_PROMISC))
  2779. return false;
  2780. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2781. } else if (!rx->sta) {
  2782. int rate_idx;
  2783. if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
  2784. rate_idx = 0; /* TODO: HT/VHT rates */
  2785. else
  2786. rate_idx = status->rate_idx;
  2787. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  2788. BIT(rate_idx));
  2789. }
  2790. break;
  2791. case NL80211_IFTYPE_OCB:
  2792. if (!bssid)
  2793. return false;
  2794. if (ieee80211_is_beacon(hdr->frame_control)) {
  2795. return false;
  2796. } else if (!is_broadcast_ether_addr(bssid)) {
  2797. ocb_dbg(sdata, "BSSID mismatch in OCB mode!\n");
  2798. return false;
  2799. } else if (!multicast &&
  2800. !ether_addr_equal(sdata->dev->dev_addr,
  2801. hdr->addr1)) {
  2802. /* if we are in promisc mode we also accept
  2803. * packets not destined for us
  2804. */
  2805. if (!(sdata->dev->flags & IFF_PROMISC))
  2806. return false;
  2807. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  2808. } else if (!rx->sta) {
  2809. int rate_idx;
  2810. if (status->flag & RX_FLAG_HT)
  2811. rate_idx = 0; /* TODO: HT rates */
  2812. else
  2813. rate_idx = status->rate_idx;
  2814. ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
  2815. BIT(rate_idx));
  2816. }
  2817. break;
  2818. case NL80211_IFTYPE_MESH_POINT:
  2819. if (!multicast &&
  2820. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2821. if (!(sdata->dev->flags & IFF_PROMISC))
  2822. return false;
  2823. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2824. }
  2825. break;
  2826. case NL80211_IFTYPE_AP_VLAN:
  2827. case NL80211_IFTYPE_AP:
  2828. if (!bssid) {
  2829. if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2830. return false;
  2831. } else if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
  2832. /*
  2833. * Accept public action frames even when the
  2834. * BSSID doesn't match, this is used for P2P
  2835. * and location updates. Note that mac80211
  2836. * itself never looks at these frames.
  2837. */
  2838. if (!multicast &&
  2839. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2840. return false;
  2841. if (ieee80211_is_public_action(hdr, skb->len))
  2842. return true;
  2843. if (!ieee80211_is_beacon(hdr->frame_control))
  2844. return false;
  2845. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2846. } else if (!ieee80211_has_tods(hdr->frame_control)) {
  2847. /* ignore data frames to TDLS-peers */
  2848. if (ieee80211_is_data(hdr->frame_control))
  2849. return false;
  2850. /* ignore action frames to TDLS-peers */
  2851. if (ieee80211_is_action(hdr->frame_control) &&
  2852. !ether_addr_equal(bssid, hdr->addr1))
  2853. return false;
  2854. }
  2855. break;
  2856. case NL80211_IFTYPE_WDS:
  2857. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2858. return false;
  2859. if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
  2860. return false;
  2861. break;
  2862. case NL80211_IFTYPE_P2P_DEVICE:
  2863. if (!ieee80211_is_public_action(hdr, skb->len) &&
  2864. !ieee80211_is_probe_req(hdr->frame_control) &&
  2865. !ieee80211_is_probe_resp(hdr->frame_control) &&
  2866. !ieee80211_is_beacon(hdr->frame_control))
  2867. return false;
  2868. if (!ether_addr_equal(sdata->vif.addr, hdr->addr1) &&
  2869. !multicast)
  2870. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2871. break;
  2872. default:
  2873. /* should never get here */
  2874. WARN_ON_ONCE(1);
  2875. break;
  2876. }
  2877. return true;
  2878. }
  2879. /*
  2880. * This function returns whether or not the SKB
  2881. * was destined for RX processing or not, which,
  2882. * if consume is true, is equivalent to whether
  2883. * or not the skb was consumed.
  2884. */
  2885. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2886. struct sk_buff *skb, bool consume)
  2887. {
  2888. struct ieee80211_local *local = rx->local;
  2889. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2890. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2891. struct ieee80211_hdr *hdr = (void *)skb->data;
  2892. rx->skb = skb;
  2893. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2894. if (!prepare_for_handlers(rx, hdr))
  2895. return false;
  2896. if (!consume) {
  2897. skb = skb_copy(skb, GFP_ATOMIC);
  2898. if (!skb) {
  2899. if (net_ratelimit())
  2900. wiphy_debug(local->hw.wiphy,
  2901. "failed to copy skb for %s\n",
  2902. sdata->name);
  2903. return true;
  2904. }
  2905. rx->skb = skb;
  2906. }
  2907. ieee80211_invoke_rx_handlers(rx);
  2908. return true;
  2909. }
  2910. /*
  2911. * This is the actual Rx frames handler. as it belongs to Rx path it must
  2912. * be called with rcu_read_lock protection.
  2913. */
  2914. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2915. struct sk_buff *skb)
  2916. {
  2917. struct ieee80211_local *local = hw_to_local(hw);
  2918. struct ieee80211_sub_if_data *sdata;
  2919. struct ieee80211_hdr *hdr;
  2920. __le16 fc;
  2921. struct ieee80211_rx_data rx;
  2922. struct ieee80211_sub_if_data *prev;
  2923. struct sta_info *sta, *tmp, *prev_sta;
  2924. int err = 0;
  2925. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2926. memset(&rx, 0, sizeof(rx));
  2927. rx.skb = skb;
  2928. rx.local = local;
  2929. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2930. local->dot11ReceivedFragmentCount++;
  2931. if (ieee80211_is_mgmt(fc)) {
  2932. /* drop frame if too short for header */
  2933. if (skb->len < ieee80211_hdrlen(fc))
  2934. err = -ENOBUFS;
  2935. else
  2936. err = skb_linearize(skb);
  2937. } else {
  2938. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2939. }
  2940. if (err) {
  2941. dev_kfree_skb(skb);
  2942. return;
  2943. }
  2944. hdr = (struct ieee80211_hdr *)skb->data;
  2945. ieee80211_parse_qos(&rx);
  2946. ieee80211_verify_alignment(&rx);
  2947. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
  2948. ieee80211_is_beacon(hdr->frame_control)))
  2949. ieee80211_scan_rx(local, skb);
  2950. if (ieee80211_is_data(fc)) {
  2951. prev_sta = NULL;
  2952. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  2953. if (!prev_sta) {
  2954. prev_sta = sta;
  2955. continue;
  2956. }
  2957. rx.sta = prev_sta;
  2958. rx.sdata = prev_sta->sdata;
  2959. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2960. prev_sta = sta;
  2961. }
  2962. if (prev_sta) {
  2963. rx.sta = prev_sta;
  2964. rx.sdata = prev_sta->sdata;
  2965. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2966. return;
  2967. goto out;
  2968. }
  2969. }
  2970. prev = NULL;
  2971. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2972. if (!ieee80211_sdata_running(sdata))
  2973. continue;
  2974. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2975. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2976. continue;
  2977. /*
  2978. * frame is destined for this interface, but if it's
  2979. * not also for the previous one we handle that after
  2980. * the loop to avoid copying the SKB once too much
  2981. */
  2982. if (!prev) {
  2983. prev = sdata;
  2984. continue;
  2985. }
  2986. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2987. rx.sdata = prev;
  2988. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2989. prev = sdata;
  2990. }
  2991. if (prev) {
  2992. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2993. rx.sdata = prev;
  2994. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2995. return;
  2996. }
  2997. out:
  2998. dev_kfree_skb(skb);
  2999. }
  3000. /*
  3001. * This is the receive path handler. It is called by a low level driver when an
  3002. * 802.11 MPDU is received from the hardware.
  3003. */
  3004. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  3005. {
  3006. struct ieee80211_local *local = hw_to_local(hw);
  3007. struct ieee80211_rate *rate = NULL;
  3008. struct ieee80211_supported_band *sband;
  3009. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3010. WARN_ON_ONCE(softirq_count() == 0);
  3011. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  3012. goto drop;
  3013. sband = local->hw.wiphy->bands[status->band];
  3014. if (WARN_ON(!sband))
  3015. goto drop;
  3016. /*
  3017. * If we're suspending, it is possible although not too likely
  3018. * that we'd be receiving frames after having already partially
  3019. * quiesced the stack. We can't process such frames then since
  3020. * that might, for example, cause stations to be added or other
  3021. * driver callbacks be invoked.
  3022. */
  3023. if (unlikely(local->quiescing || local->suspended))
  3024. goto drop;
  3025. /* We might be during a HW reconfig, prevent Rx for the same reason */
  3026. if (unlikely(local->in_reconfig))
  3027. goto drop;
  3028. /*
  3029. * The same happens when we're not even started,
  3030. * but that's worth a warning.
  3031. */
  3032. if (WARN_ON(!local->started))
  3033. goto drop;
  3034. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  3035. /*
  3036. * Validate the rate, unless a PLCP error means that
  3037. * we probably can't have a valid rate here anyway.
  3038. */
  3039. if (status->flag & RX_FLAG_HT) {
  3040. /*
  3041. * rate_idx is MCS index, which can be [0-76]
  3042. * as documented on:
  3043. *
  3044. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  3045. *
  3046. * Anything else would be some sort of driver or
  3047. * hardware error. The driver should catch hardware
  3048. * errors.
  3049. */
  3050. if (WARN(status->rate_idx > 76,
  3051. "Rate marked as an HT rate but passed "
  3052. "status->rate_idx is not "
  3053. "an MCS index [0-76]: %d (0x%02x)\n",
  3054. status->rate_idx,
  3055. status->rate_idx))
  3056. goto drop;
  3057. } else if (status->flag & RX_FLAG_VHT) {
  3058. if (WARN_ONCE(status->rate_idx > 9 ||
  3059. !status->vht_nss ||
  3060. status->vht_nss > 8,
  3061. "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
  3062. status->rate_idx, status->vht_nss))
  3063. goto drop;
  3064. } else {
  3065. if (WARN_ON(status->rate_idx >= sband->n_bitrates))
  3066. goto drop;
  3067. rate = &sband->bitrates[status->rate_idx];
  3068. }
  3069. }
  3070. status->rx_flags = 0;
  3071. /*
  3072. * key references and virtual interfaces are protected using RCU
  3073. * and this requires that we are in a read-side RCU section during
  3074. * receive processing
  3075. */
  3076. rcu_read_lock();
  3077. /*
  3078. * Frames with failed FCS/PLCP checksum are not returned,
  3079. * all other frames are returned without radiotap header
  3080. * if it was previously present.
  3081. * Also, frames with less than 16 bytes are dropped.
  3082. */
  3083. skb = ieee80211_rx_monitor(local, skb, rate);
  3084. if (!skb) {
  3085. rcu_read_unlock();
  3086. return;
  3087. }
  3088. ieee80211_tpt_led_trig_rx(local,
  3089. ((struct ieee80211_hdr *)skb->data)->frame_control,
  3090. skb->len);
  3091. __ieee80211_rx_handle_packet(hw, skb);
  3092. rcu_read_unlock();
  3093. return;
  3094. drop:
  3095. kfree_skb(skb);
  3096. }
  3097. EXPORT_SYMBOL(ieee80211_rx);
  3098. /* This is a version of the rx handler that can be called from hard irq
  3099. * context. Post the skb on the queue and schedule the tasklet */
  3100. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  3101. {
  3102. struct ieee80211_local *local = hw_to_local(hw);
  3103. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3104. skb->pkt_type = IEEE80211_RX_MSG;
  3105. skb_queue_tail(&local->skb_queue, skb);
  3106. tasklet_schedule(&local->tasklet);
  3107. }
  3108. EXPORT_SYMBOL(ieee80211_rx_irqsafe);