rx.c 95 KB

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