rx.c 90 KB

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