rx.c 86 KB

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