rx.c 87 KB

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