rx.c 102 KB

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