rx.c 96 KB

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