rx.c 102 KB

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