rx.c 87 KB

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