rx.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649
  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. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1004. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1005. ps = &sdata->bss->ps;
  1006. else
  1007. return;
  1008. atomic_inc(&ps->num_sta_ps);
  1009. set_sta_flag(sta, WLAN_STA_PS_STA);
  1010. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  1011. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  1012. ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
  1013. sta->sta.addr, sta->sta.aid);
  1014. }
  1015. static void sta_ps_end(struct sta_info *sta)
  1016. {
  1017. ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
  1018. sta->sta.addr, sta->sta.aid);
  1019. if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  1020. /*
  1021. * Clear the flag only if the other one is still set
  1022. * so that the TX path won't start TX'ing new frames
  1023. * directly ... In the case that the driver flag isn't
  1024. * set ieee80211_sta_ps_deliver_wakeup() will clear it.
  1025. */
  1026. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1027. ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
  1028. sta->sta.addr, sta->sta.aid);
  1029. return;
  1030. }
  1031. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1032. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1033. ieee80211_sta_ps_deliver_wakeup(sta);
  1034. }
  1035. int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
  1036. {
  1037. struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
  1038. bool in_ps;
  1039. WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
  1040. /* Don't let the same PS state be set twice */
  1041. in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
  1042. if ((start && in_ps) || (!start && !in_ps))
  1043. return -EINVAL;
  1044. if (start)
  1045. sta_ps_start(sta_inf);
  1046. else
  1047. sta_ps_end(sta_inf);
  1048. return 0;
  1049. }
  1050. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  1051. static ieee80211_rx_result debug_noinline
  1052. ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
  1053. {
  1054. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1055. struct ieee80211_hdr *hdr = (void *)rx->skb->data;
  1056. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1057. int tid, ac;
  1058. if (!rx->sta || !(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1059. return RX_CONTINUE;
  1060. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1061. sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1062. return RX_CONTINUE;
  1063. /*
  1064. * The device handles station powersave, so don't do anything about
  1065. * uAPSD and PS-Poll frames (the latter shouldn't even come up from
  1066. * it to mac80211 since they're handled.)
  1067. */
  1068. if (sdata->local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  1069. return RX_CONTINUE;
  1070. /*
  1071. * Don't do anything if the station isn't already asleep. In
  1072. * the uAPSD case, the station will probably be marked asleep,
  1073. * in the PS-Poll case the station must be confused ...
  1074. */
  1075. if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
  1076. return RX_CONTINUE;
  1077. if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
  1078. if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
  1079. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1080. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  1081. else
  1082. set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
  1083. }
  1084. /* Free PS Poll skb here instead of returning RX_DROP that would
  1085. * count as an dropped frame. */
  1086. dev_kfree_skb(rx->skb);
  1087. return RX_QUEUED;
  1088. } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1089. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1090. ieee80211_has_pm(hdr->frame_control) &&
  1091. (ieee80211_is_data_qos(hdr->frame_control) ||
  1092. ieee80211_is_qos_nullfunc(hdr->frame_control))) {
  1093. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  1094. ac = ieee802_1d_to_ac[tid & 7];
  1095. /*
  1096. * If this AC is not trigger-enabled do nothing.
  1097. *
  1098. * NB: This could/should check a separate bitmap of trigger-
  1099. * enabled queues, but for now we only implement uAPSD w/o
  1100. * TSPEC changes to the ACs, so they're always the same.
  1101. */
  1102. if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
  1103. return RX_CONTINUE;
  1104. /* if we are in a service period, do nothing */
  1105. if (test_sta_flag(rx->sta, WLAN_STA_SP))
  1106. return RX_CONTINUE;
  1107. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1108. ieee80211_sta_ps_deliver_uapsd(rx->sta);
  1109. else
  1110. set_sta_flag(rx->sta, WLAN_STA_UAPSD);
  1111. }
  1112. return RX_CONTINUE;
  1113. }
  1114. static ieee80211_rx_result debug_noinline
  1115. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  1116. {
  1117. struct sta_info *sta = rx->sta;
  1118. struct sk_buff *skb = rx->skb;
  1119. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1120. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1121. int i;
  1122. if (!sta)
  1123. return RX_CONTINUE;
  1124. /*
  1125. * Update last_rx only for IBSS packets which are for the current
  1126. * BSSID and for station already AUTHORIZED to avoid keeping the
  1127. * current IBSS network alive in cases where other STAs start
  1128. * using different BSSID. This will also give the station another
  1129. * chance to restart the authentication/authorization in case
  1130. * something went wrong the first time.
  1131. */
  1132. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1133. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1134. NL80211_IFTYPE_ADHOC);
  1135. if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
  1136. test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
  1137. sta->last_rx = jiffies;
  1138. if (ieee80211_is_data(hdr->frame_control) &&
  1139. !is_multicast_ether_addr(hdr->addr1)) {
  1140. sta->last_rx_rate_idx = status->rate_idx;
  1141. sta->last_rx_rate_flag = status->flag;
  1142. sta->last_rx_rate_vht_flag = status->vht_flag;
  1143. sta->last_rx_rate_vht_nss = status->vht_nss;
  1144. }
  1145. }
  1146. } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
  1147. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1148. NL80211_IFTYPE_OCB);
  1149. /* OCB uses wild-card BSSID */
  1150. if (is_broadcast_ether_addr(bssid))
  1151. sta->last_rx = jiffies;
  1152. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1153. /*
  1154. * Mesh beacons will update last_rx when if they are found to
  1155. * match the current local configuration when processed.
  1156. */
  1157. sta->last_rx = jiffies;
  1158. if (ieee80211_is_data(hdr->frame_control)) {
  1159. sta->last_rx_rate_idx = status->rate_idx;
  1160. sta->last_rx_rate_flag = status->flag;
  1161. sta->last_rx_rate_vht_flag = status->vht_flag;
  1162. sta->last_rx_rate_vht_nss = status->vht_nss;
  1163. }
  1164. }
  1165. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1166. return RX_CONTINUE;
  1167. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1168. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1169. sta->rx_fragments++;
  1170. sta->rx_bytes += rx->skb->len;
  1171. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  1172. sta->last_signal = status->signal;
  1173. ewma_add(&sta->avg_signal, -status->signal);
  1174. }
  1175. if (status->chains) {
  1176. sta->chains = status->chains;
  1177. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  1178. int signal = status->chain_signal[i];
  1179. if (!(status->chains & BIT(i)))
  1180. continue;
  1181. sta->chain_signal_last[i] = signal;
  1182. ewma_add(&sta->chain_signal_avg[i], -signal);
  1183. }
  1184. }
  1185. /*
  1186. * Change STA power saving mode only at the end of a frame
  1187. * exchange sequence.
  1188. */
  1189. if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
  1190. !ieee80211_has_morefrags(hdr->frame_control) &&
  1191. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1192. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1193. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1194. /* PM bit is only checked in frames where it isn't reserved,
  1195. * in AP mode it's reserved in non-bufferable management frames
  1196. * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
  1197. */
  1198. (!ieee80211_is_mgmt(hdr->frame_control) ||
  1199. ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
  1200. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1201. if (!ieee80211_has_pm(hdr->frame_control))
  1202. sta_ps_end(sta);
  1203. } else {
  1204. if (ieee80211_has_pm(hdr->frame_control))
  1205. sta_ps_start(sta);
  1206. }
  1207. }
  1208. /* mesh power save support */
  1209. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1210. ieee80211_mps_rx_h_sta_process(sta, hdr);
  1211. /*
  1212. * Drop (qos-)data::nullfunc frames silently, since they
  1213. * are used only to control station power saving mode.
  1214. */
  1215. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1216. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1217. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1218. /*
  1219. * If we receive a 4-addr nullfunc frame from a STA
  1220. * that was not moved to a 4-addr STA vlan yet send
  1221. * the event to userspace and for older hostapd drop
  1222. * the frame to the monitor interface.
  1223. */
  1224. if (ieee80211_has_a4(hdr->frame_control) &&
  1225. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1226. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1227. !rx->sdata->u.vlan.sta))) {
  1228. if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
  1229. cfg80211_rx_unexpected_4addr_frame(
  1230. rx->sdata->dev, sta->sta.addr,
  1231. GFP_ATOMIC);
  1232. return RX_DROP_MONITOR;
  1233. }
  1234. /*
  1235. * Update counter and free packet here to avoid
  1236. * counting this as a dropped packed.
  1237. */
  1238. sta->rx_packets++;
  1239. dev_kfree_skb(rx->skb);
  1240. return RX_QUEUED;
  1241. }
  1242. return RX_CONTINUE;
  1243. } /* ieee80211_rx_h_sta_process */
  1244. static ieee80211_rx_result debug_noinline
  1245. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  1246. {
  1247. struct sk_buff *skb = rx->skb;
  1248. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1249. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1250. int keyidx;
  1251. int hdrlen;
  1252. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  1253. struct ieee80211_key *sta_ptk = NULL;
  1254. int mmie_keyidx = -1;
  1255. __le16 fc;
  1256. const struct ieee80211_cipher_scheme *cs = NULL;
  1257. /*
  1258. * Key selection 101
  1259. *
  1260. * There are four types of keys:
  1261. * - GTK (group keys)
  1262. * - IGTK (group keys for management frames)
  1263. * - PTK (pairwise keys)
  1264. * - STK (station-to-station pairwise keys)
  1265. *
  1266. * When selecting a key, we have to distinguish between multicast
  1267. * (including broadcast) and unicast frames, the latter can only
  1268. * use PTKs and STKs while the former always use GTKs and IGTKs.
  1269. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  1270. * unicast frames can also use key indices like GTKs. Hence, if we
  1271. * don't have a PTK/STK we check the key index for a WEP key.
  1272. *
  1273. * Note that in a regular BSS, multicast frames are sent by the
  1274. * AP only, associated stations unicast the frame to the AP first
  1275. * which then multicasts it on their behalf.
  1276. *
  1277. * There is also a slight problem in IBSS mode: GTKs are negotiated
  1278. * with each station, that is something we don't currently handle.
  1279. * The spec seems to expect that one negotiates the same key with
  1280. * every station but there's no such requirement; VLANs could be
  1281. * possible.
  1282. */
  1283. /*
  1284. * No point in finding a key and decrypting if the frame is neither
  1285. * addressed to us nor a multicast frame.
  1286. */
  1287. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1288. return RX_CONTINUE;
  1289. /* start without a key */
  1290. rx->key = NULL;
  1291. fc = hdr->frame_control;
  1292. if (rx->sta) {
  1293. int keyid = rx->sta->ptk_idx;
  1294. if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
  1295. cs = rx->sta->cipher_scheme;
  1296. keyid = iwl80211_get_cs_keyid(cs, rx->skb);
  1297. if (unlikely(keyid < 0))
  1298. return RX_DROP_UNUSABLE;
  1299. }
  1300. sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
  1301. }
  1302. if (!ieee80211_has_protected(fc))
  1303. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  1304. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  1305. rx->key = sta_ptk;
  1306. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1307. (status->flag & RX_FLAG_IV_STRIPPED))
  1308. return RX_CONTINUE;
  1309. /* Skip decryption if the frame is not protected. */
  1310. if (!ieee80211_has_protected(fc))
  1311. return RX_CONTINUE;
  1312. } else if (mmie_keyidx >= 0) {
  1313. /* Broadcast/multicast robust management frame / BIP */
  1314. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1315. (status->flag & RX_FLAG_IV_STRIPPED))
  1316. return RX_CONTINUE;
  1317. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  1318. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  1319. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  1320. if (rx->sta)
  1321. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  1322. if (!rx->key)
  1323. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  1324. } else if (!ieee80211_has_protected(fc)) {
  1325. /*
  1326. * The frame was not protected, so skip decryption. However, we
  1327. * need to set rx->key if there is a key that could have been
  1328. * used so that the frame may be dropped if encryption would
  1329. * have been expected.
  1330. */
  1331. struct ieee80211_key *key = NULL;
  1332. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1333. int i;
  1334. if (ieee80211_is_mgmt(fc) &&
  1335. is_multicast_ether_addr(hdr->addr1) &&
  1336. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  1337. rx->key = key;
  1338. else {
  1339. if (rx->sta) {
  1340. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1341. key = rcu_dereference(rx->sta->gtk[i]);
  1342. if (key)
  1343. break;
  1344. }
  1345. }
  1346. if (!key) {
  1347. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1348. key = rcu_dereference(sdata->keys[i]);
  1349. if (key)
  1350. break;
  1351. }
  1352. }
  1353. if (key)
  1354. rx->key = key;
  1355. }
  1356. return RX_CONTINUE;
  1357. } else {
  1358. u8 keyid;
  1359. /*
  1360. * The device doesn't give us the IV so we won't be
  1361. * able to look up the key. That's ok though, we
  1362. * don't need to decrypt the frame, we just won't
  1363. * be able to keep statistics accurate.
  1364. * Except for key threshold notifications, should
  1365. * we somehow allow the driver to tell us which key
  1366. * the hardware used if this flag is set?
  1367. */
  1368. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1369. (status->flag & RX_FLAG_IV_STRIPPED))
  1370. return RX_CONTINUE;
  1371. hdrlen = ieee80211_hdrlen(fc);
  1372. if (cs) {
  1373. keyidx = iwl80211_get_cs_keyid(cs, rx->skb);
  1374. if (unlikely(keyidx < 0))
  1375. return RX_DROP_UNUSABLE;
  1376. } else {
  1377. if (rx->skb->len < 8 + hdrlen)
  1378. return RX_DROP_UNUSABLE; /* TODO: count this? */
  1379. /*
  1380. * no need to call ieee80211_wep_get_keyidx,
  1381. * it verifies a bunch of things we've done already
  1382. */
  1383. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  1384. keyidx = keyid >> 6;
  1385. }
  1386. /* check per-station GTK first, if multicast packet */
  1387. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  1388. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  1389. /* if not found, try default key */
  1390. if (!rx->key) {
  1391. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  1392. /*
  1393. * RSNA-protected unicast frames should always be
  1394. * sent with pairwise or station-to-station keys,
  1395. * but for WEP we allow using a key index as well.
  1396. */
  1397. if (rx->key &&
  1398. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1399. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  1400. !is_multicast_ether_addr(hdr->addr1))
  1401. rx->key = NULL;
  1402. }
  1403. }
  1404. if (rx->key) {
  1405. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  1406. return RX_DROP_MONITOR;
  1407. rx->key->tx_rx_count++;
  1408. /* TODO: add threshold stuff again */
  1409. } else {
  1410. return RX_DROP_MONITOR;
  1411. }
  1412. switch (rx->key->conf.cipher) {
  1413. case WLAN_CIPHER_SUITE_WEP40:
  1414. case WLAN_CIPHER_SUITE_WEP104:
  1415. result = ieee80211_crypto_wep_decrypt(rx);
  1416. break;
  1417. case WLAN_CIPHER_SUITE_TKIP:
  1418. result = ieee80211_crypto_tkip_decrypt(rx);
  1419. break;
  1420. case WLAN_CIPHER_SUITE_CCMP:
  1421. result = ieee80211_crypto_ccmp_decrypt(
  1422. rx, IEEE80211_CCMP_MIC_LEN);
  1423. break;
  1424. case WLAN_CIPHER_SUITE_CCMP_256:
  1425. result = ieee80211_crypto_ccmp_decrypt(
  1426. rx, IEEE80211_CCMP_256_MIC_LEN);
  1427. break;
  1428. case WLAN_CIPHER_SUITE_AES_CMAC:
  1429. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  1430. break;
  1431. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1432. result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
  1433. break;
  1434. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1435. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1436. result = ieee80211_crypto_aes_gmac_decrypt(rx);
  1437. break;
  1438. case WLAN_CIPHER_SUITE_GCMP:
  1439. case WLAN_CIPHER_SUITE_GCMP_256:
  1440. result = ieee80211_crypto_gcmp_decrypt(rx);
  1441. break;
  1442. default:
  1443. result = ieee80211_crypto_hw_decrypt(rx);
  1444. }
  1445. /* the hdr variable is invalid after the decrypt handlers */
  1446. /* either the frame has been decrypted or will be dropped */
  1447. status->flag |= RX_FLAG_DECRYPTED;
  1448. return result;
  1449. }
  1450. static inline struct ieee80211_fragment_entry *
  1451. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1452. unsigned int frag, unsigned int seq, int rx_queue,
  1453. struct sk_buff **skb)
  1454. {
  1455. struct ieee80211_fragment_entry *entry;
  1456. entry = &sdata->fragments[sdata->fragment_next++];
  1457. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1458. sdata->fragment_next = 0;
  1459. if (!skb_queue_empty(&entry->skb_list))
  1460. __skb_queue_purge(&entry->skb_list);
  1461. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1462. *skb = NULL;
  1463. entry->first_frag_time = jiffies;
  1464. entry->seq = seq;
  1465. entry->rx_queue = rx_queue;
  1466. entry->last_frag = frag;
  1467. entry->ccmp = 0;
  1468. entry->extra_len = 0;
  1469. return entry;
  1470. }
  1471. static inline struct ieee80211_fragment_entry *
  1472. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1473. unsigned int frag, unsigned int seq,
  1474. int rx_queue, struct ieee80211_hdr *hdr)
  1475. {
  1476. struct ieee80211_fragment_entry *entry;
  1477. int i, idx;
  1478. idx = sdata->fragment_next;
  1479. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1480. struct ieee80211_hdr *f_hdr;
  1481. idx--;
  1482. if (idx < 0)
  1483. idx = IEEE80211_FRAGMENT_MAX - 1;
  1484. entry = &sdata->fragments[idx];
  1485. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1486. entry->rx_queue != rx_queue ||
  1487. entry->last_frag + 1 != frag)
  1488. continue;
  1489. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1490. /*
  1491. * Check ftype and addresses are equal, else check next fragment
  1492. */
  1493. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1494. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1495. !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
  1496. !ether_addr_equal(hdr->addr2, f_hdr->addr2))
  1497. continue;
  1498. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1499. __skb_queue_purge(&entry->skb_list);
  1500. continue;
  1501. }
  1502. return entry;
  1503. }
  1504. return NULL;
  1505. }
  1506. static ieee80211_rx_result debug_noinline
  1507. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1508. {
  1509. struct ieee80211_hdr *hdr;
  1510. u16 sc;
  1511. __le16 fc;
  1512. unsigned int frag, seq;
  1513. struct ieee80211_fragment_entry *entry;
  1514. struct sk_buff *skb;
  1515. struct ieee80211_rx_status *status;
  1516. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1517. fc = hdr->frame_control;
  1518. if (ieee80211_is_ctl(fc))
  1519. return RX_CONTINUE;
  1520. sc = le16_to_cpu(hdr->seq_ctrl);
  1521. frag = sc & IEEE80211_SCTL_FRAG;
  1522. if (is_multicast_ether_addr(hdr->addr1)) {
  1523. rx->local->dot11MulticastReceivedFrameCount++;
  1524. goto out_no_led;
  1525. }
  1526. if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
  1527. goto out;
  1528. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1529. if (skb_linearize(rx->skb))
  1530. return RX_DROP_UNUSABLE;
  1531. /*
  1532. * skb_linearize() might change the skb->data and
  1533. * previously cached variables (in this case, hdr) need to
  1534. * be refreshed with the new data.
  1535. */
  1536. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1537. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1538. if (frag == 0) {
  1539. /* This is the first fragment of a new frame. */
  1540. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1541. rx->seqno_idx, &(rx->skb));
  1542. if (rx->key &&
  1543. (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
  1544. rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256) &&
  1545. ieee80211_has_protected(fc)) {
  1546. int queue = rx->security_idx;
  1547. /* Store CCMP PN so that we can verify that the next
  1548. * fragment has a sequential PN value. */
  1549. entry->ccmp = 1;
  1550. memcpy(entry->last_pn,
  1551. rx->key->u.ccmp.rx_pn[queue],
  1552. IEEE80211_CCMP_PN_LEN);
  1553. }
  1554. return RX_QUEUED;
  1555. }
  1556. /* This is a fragment for a frame that should already be pending in
  1557. * fragment cache. Add this fragment to the end of the pending entry.
  1558. */
  1559. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1560. rx->seqno_idx, hdr);
  1561. if (!entry) {
  1562. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1563. return RX_DROP_MONITOR;
  1564. }
  1565. /* Verify that MPDUs within one MSDU have sequential PN values.
  1566. * (IEEE 802.11i, 8.3.3.4.5) */
  1567. if (entry->ccmp) {
  1568. int i;
  1569. u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
  1570. int queue;
  1571. if (!rx->key ||
  1572. (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
  1573. rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256))
  1574. return RX_DROP_UNUSABLE;
  1575. memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
  1576. for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
  1577. pn[i]++;
  1578. if (pn[i])
  1579. break;
  1580. }
  1581. queue = rx->security_idx;
  1582. rpn = rx->key->u.ccmp.rx_pn[queue];
  1583. if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
  1584. return RX_DROP_UNUSABLE;
  1585. memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
  1586. }
  1587. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1588. __skb_queue_tail(&entry->skb_list, rx->skb);
  1589. entry->last_frag = frag;
  1590. entry->extra_len += rx->skb->len;
  1591. if (ieee80211_has_morefrags(fc)) {
  1592. rx->skb = NULL;
  1593. return RX_QUEUED;
  1594. }
  1595. rx->skb = __skb_dequeue(&entry->skb_list);
  1596. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1597. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  1598. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1599. GFP_ATOMIC))) {
  1600. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1601. __skb_queue_purge(&entry->skb_list);
  1602. return RX_DROP_UNUSABLE;
  1603. }
  1604. }
  1605. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1606. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1607. dev_kfree_skb(skb);
  1608. }
  1609. /* Complete frame has been reassembled - process it now */
  1610. status = IEEE80211_SKB_RXCB(rx->skb);
  1611. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1612. out:
  1613. ieee80211_led_rx(rx->local);
  1614. out_no_led:
  1615. if (rx->sta)
  1616. rx->sta->rx_packets++;
  1617. return RX_CONTINUE;
  1618. }
  1619. static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1620. {
  1621. if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1622. return -EACCES;
  1623. return 0;
  1624. }
  1625. static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1626. {
  1627. struct sk_buff *skb = rx->skb;
  1628. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1629. /*
  1630. * Pass through unencrypted frames if the hardware has
  1631. * decrypted them already.
  1632. */
  1633. if (status->flag & RX_FLAG_DECRYPTED)
  1634. return 0;
  1635. /* Drop unencrypted frames if key is set. */
  1636. if (unlikely(!ieee80211_has_protected(fc) &&
  1637. !ieee80211_is_nullfunc(fc) &&
  1638. ieee80211_is_data(fc) &&
  1639. (rx->key || rx->sdata->drop_unencrypted)))
  1640. return -EACCES;
  1641. return 0;
  1642. }
  1643. static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1644. {
  1645. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1646. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1647. __le16 fc = hdr->frame_control;
  1648. /*
  1649. * Pass through unencrypted frames if the hardware has
  1650. * decrypted them already.
  1651. */
  1652. if (status->flag & RX_FLAG_DECRYPTED)
  1653. return 0;
  1654. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1655. if (unlikely(!ieee80211_has_protected(fc) &&
  1656. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1657. rx->key)) {
  1658. if (ieee80211_is_deauth(fc) ||
  1659. ieee80211_is_disassoc(fc))
  1660. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1661. rx->skb->data,
  1662. rx->skb->len);
  1663. return -EACCES;
  1664. }
  1665. /* BIP does not use Protected field, so need to check MMIE */
  1666. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1667. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1668. if (ieee80211_is_deauth(fc) ||
  1669. ieee80211_is_disassoc(fc))
  1670. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1671. rx->skb->data,
  1672. rx->skb->len);
  1673. return -EACCES;
  1674. }
  1675. /*
  1676. * When using MFP, Action frames are not allowed prior to
  1677. * having configured keys.
  1678. */
  1679. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1680. ieee80211_is_robust_mgmt_frame(rx->skb)))
  1681. return -EACCES;
  1682. }
  1683. return 0;
  1684. }
  1685. static int
  1686. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1687. {
  1688. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1689. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1690. bool check_port_control = false;
  1691. struct ethhdr *ehdr;
  1692. int ret;
  1693. *port_control = false;
  1694. if (ieee80211_has_a4(hdr->frame_control) &&
  1695. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1696. return -1;
  1697. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1698. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1699. if (!sdata->u.mgd.use_4addr)
  1700. return -1;
  1701. else
  1702. check_port_control = true;
  1703. }
  1704. if (is_multicast_ether_addr(hdr->addr1) &&
  1705. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1706. return -1;
  1707. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1708. if (ret < 0)
  1709. return ret;
  1710. ehdr = (struct ethhdr *) rx->skb->data;
  1711. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1712. *port_control = true;
  1713. else if (check_port_control)
  1714. return -1;
  1715. return 0;
  1716. }
  1717. /*
  1718. * requires that rx->skb is a frame with ethernet header
  1719. */
  1720. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1721. {
  1722. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1723. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1724. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1725. /*
  1726. * Allow EAPOL frames to us/the PAE group address regardless
  1727. * of whether the frame was encrypted or not.
  1728. */
  1729. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1730. (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
  1731. ether_addr_equal(ehdr->h_dest, pae_group_addr)))
  1732. return true;
  1733. if (ieee80211_802_1x_port_control(rx) ||
  1734. ieee80211_drop_unencrypted(rx, fc))
  1735. return false;
  1736. return true;
  1737. }
  1738. /*
  1739. * requires that rx->skb is a frame with ethernet header
  1740. */
  1741. static void
  1742. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1743. {
  1744. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1745. struct net_device *dev = sdata->dev;
  1746. struct sk_buff *skb, *xmit_skb;
  1747. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1748. struct sta_info *dsta;
  1749. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1750. skb = rx->skb;
  1751. xmit_skb = NULL;
  1752. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1753. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1754. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1755. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1756. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1757. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1758. /*
  1759. * send multicast frames both to higher layers in
  1760. * local net stack and back to the wireless medium
  1761. */
  1762. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1763. if (!xmit_skb)
  1764. net_info_ratelimited("%s: failed to clone multicast frame\n",
  1765. dev->name);
  1766. } else {
  1767. dsta = sta_info_get(sdata, skb->data);
  1768. if (dsta) {
  1769. /*
  1770. * The destination station is associated to
  1771. * this AP (in this VLAN), so send the frame
  1772. * directly to it and do not pass it to local
  1773. * net stack.
  1774. */
  1775. xmit_skb = skb;
  1776. skb = NULL;
  1777. }
  1778. }
  1779. }
  1780. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1781. if (skb) {
  1782. /* 'align' will only take the values 0 or 2 here since all
  1783. * frames are required to be aligned to 2-byte boundaries
  1784. * when being passed to mac80211; the code here works just
  1785. * as well if that isn't true, but mac80211 assumes it can
  1786. * access fields as 2-byte aligned (e.g. for ether_addr_equal)
  1787. */
  1788. int align;
  1789. align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
  1790. if (align) {
  1791. if (WARN_ON(skb_headroom(skb) < 3)) {
  1792. dev_kfree_skb(skb);
  1793. skb = NULL;
  1794. } else {
  1795. u8 *data = skb->data;
  1796. size_t len = skb_headlen(skb);
  1797. skb->data -= align;
  1798. memmove(skb->data, data, len);
  1799. skb_set_tail_pointer(skb, len);
  1800. }
  1801. }
  1802. }
  1803. #endif
  1804. if (skb) {
  1805. /* deliver to local stack */
  1806. skb->protocol = eth_type_trans(skb, dev);
  1807. memset(skb->cb, 0, sizeof(skb->cb));
  1808. if (rx->local->napi)
  1809. napi_gro_receive(rx->local->napi, skb);
  1810. else
  1811. netif_receive_skb(skb);
  1812. }
  1813. if (xmit_skb) {
  1814. /*
  1815. * Send to wireless media and increase priority by 256 to
  1816. * keep the received priority instead of reclassifying
  1817. * the frame (see cfg80211_classify8021d).
  1818. */
  1819. xmit_skb->priority += 256;
  1820. xmit_skb->protocol = htons(ETH_P_802_3);
  1821. skb_reset_network_header(xmit_skb);
  1822. skb_reset_mac_header(xmit_skb);
  1823. dev_queue_xmit(xmit_skb);
  1824. }
  1825. }
  1826. static ieee80211_rx_result debug_noinline
  1827. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1828. {
  1829. struct net_device *dev = rx->sdata->dev;
  1830. struct sk_buff *skb = rx->skb;
  1831. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1832. __le16 fc = hdr->frame_control;
  1833. struct sk_buff_head frame_list;
  1834. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1835. if (unlikely(!ieee80211_is_data(fc)))
  1836. return RX_CONTINUE;
  1837. if (unlikely(!ieee80211_is_data_present(fc)))
  1838. return RX_DROP_MONITOR;
  1839. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1840. return RX_CONTINUE;
  1841. if (ieee80211_has_a4(hdr->frame_control) &&
  1842. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1843. !rx->sdata->u.vlan.sta)
  1844. return RX_DROP_UNUSABLE;
  1845. if (is_multicast_ether_addr(hdr->addr1) &&
  1846. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1847. rx->sdata->u.vlan.sta) ||
  1848. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1849. rx->sdata->u.mgd.use_4addr)))
  1850. return RX_DROP_UNUSABLE;
  1851. skb->dev = dev;
  1852. __skb_queue_head_init(&frame_list);
  1853. if (skb_linearize(skb))
  1854. return RX_DROP_UNUSABLE;
  1855. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1856. rx->sdata->vif.type,
  1857. rx->local->hw.extra_tx_headroom, true);
  1858. while (!skb_queue_empty(&frame_list)) {
  1859. rx->skb = __skb_dequeue(&frame_list);
  1860. if (!ieee80211_frame_allowed(rx, fc)) {
  1861. dev_kfree_skb(rx->skb);
  1862. continue;
  1863. }
  1864. dev->stats.rx_packets++;
  1865. dev->stats.rx_bytes += rx->skb->len;
  1866. ieee80211_deliver_skb(rx);
  1867. }
  1868. return RX_QUEUED;
  1869. }
  1870. #ifdef CONFIG_MAC80211_MESH
  1871. static ieee80211_rx_result
  1872. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1873. {
  1874. struct ieee80211_hdr *fwd_hdr, *hdr;
  1875. struct ieee80211_tx_info *info;
  1876. struct ieee80211s_hdr *mesh_hdr;
  1877. struct sk_buff *skb = rx->skb, *fwd_skb;
  1878. struct ieee80211_local *local = rx->local;
  1879. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1880. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1881. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1882. u16 q, hdrlen;
  1883. hdr = (struct ieee80211_hdr *) skb->data;
  1884. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1885. /* make sure fixed part of mesh header is there, also checks skb len */
  1886. if (!pskb_may_pull(rx->skb, hdrlen + 6))
  1887. return RX_DROP_MONITOR;
  1888. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1889. /* make sure full mesh header is there, also checks skb len */
  1890. if (!pskb_may_pull(rx->skb,
  1891. hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
  1892. return RX_DROP_MONITOR;
  1893. /* reload pointers */
  1894. hdr = (struct ieee80211_hdr *) skb->data;
  1895. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1896. if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
  1897. return RX_DROP_MONITOR;
  1898. /* frame is in RMC, don't forward */
  1899. if (ieee80211_is_data(hdr->frame_control) &&
  1900. is_multicast_ether_addr(hdr->addr1) &&
  1901. mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
  1902. return RX_DROP_MONITOR;
  1903. if (!ieee80211_is_data(hdr->frame_control) ||
  1904. !(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1905. return RX_CONTINUE;
  1906. if (!mesh_hdr->ttl)
  1907. return RX_DROP_MONITOR;
  1908. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1909. struct mesh_path *mppath;
  1910. char *proxied_addr;
  1911. char *mpp_addr;
  1912. if (is_multicast_ether_addr(hdr->addr1)) {
  1913. mpp_addr = hdr->addr3;
  1914. proxied_addr = mesh_hdr->eaddr1;
  1915. } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
  1916. /* has_a4 already checked in ieee80211_rx_mesh_check */
  1917. mpp_addr = hdr->addr4;
  1918. proxied_addr = mesh_hdr->eaddr2;
  1919. } else {
  1920. return RX_DROP_MONITOR;
  1921. }
  1922. rcu_read_lock();
  1923. mppath = mpp_path_lookup(sdata, proxied_addr);
  1924. if (!mppath) {
  1925. mpp_path_add(sdata, proxied_addr, mpp_addr);
  1926. } else {
  1927. spin_lock_bh(&mppath->state_lock);
  1928. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  1929. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1930. spin_unlock_bh(&mppath->state_lock);
  1931. }
  1932. rcu_read_unlock();
  1933. }
  1934. /* Frame has reached destination. Don't forward */
  1935. if (!is_multicast_ether_addr(hdr->addr1) &&
  1936. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  1937. return RX_CONTINUE;
  1938. q = ieee80211_select_queue_80211(sdata, skb, hdr);
  1939. if (ieee80211_queue_stopped(&local->hw, q)) {
  1940. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  1941. return RX_DROP_MONITOR;
  1942. }
  1943. skb_set_queue_mapping(skb, q);
  1944. if (!--mesh_hdr->ttl) {
  1945. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  1946. goto out;
  1947. }
  1948. if (!ifmsh->mshcfg.dot11MeshForwarding)
  1949. goto out;
  1950. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1951. if (!fwd_skb) {
  1952. net_info_ratelimited("%s: failed to clone mesh frame\n",
  1953. sdata->name);
  1954. goto out;
  1955. }
  1956. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1957. fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
  1958. info = IEEE80211_SKB_CB(fwd_skb);
  1959. memset(info, 0, sizeof(*info));
  1960. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1961. info->control.vif = &rx->sdata->vif;
  1962. info->control.jiffies = jiffies;
  1963. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  1964. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  1965. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1966. /* update power mode indication when forwarding */
  1967. ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
  1968. } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
  1969. /* mesh power mode flags updated in mesh_nexthop_lookup */
  1970. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  1971. } else {
  1972. /* unable to resolve next hop */
  1973. mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
  1974. fwd_hdr->addr3, 0,
  1975. WLAN_REASON_MESH_PATH_NOFORWARD,
  1976. fwd_hdr->addr2);
  1977. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  1978. kfree_skb(fwd_skb);
  1979. return RX_DROP_MONITOR;
  1980. }
  1981. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  1982. ieee80211_add_pending_skb(local, fwd_skb);
  1983. out:
  1984. if (is_multicast_ether_addr(hdr->addr1) ||
  1985. sdata->dev->flags & IFF_PROMISC)
  1986. return RX_CONTINUE;
  1987. else
  1988. return RX_DROP_MONITOR;
  1989. }
  1990. #endif
  1991. static ieee80211_rx_result debug_noinline
  1992. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1993. {
  1994. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1995. struct ieee80211_local *local = rx->local;
  1996. struct net_device *dev = sdata->dev;
  1997. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1998. __le16 fc = hdr->frame_control;
  1999. bool port_control;
  2000. int err;
  2001. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  2002. return RX_CONTINUE;
  2003. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  2004. return RX_DROP_MONITOR;
  2005. if (rx->sta) {
  2006. /* The seqno index has the same property as needed
  2007. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  2008. * for non-QoS-data frames. Here we know it's a data
  2009. * frame, so count MSDUs.
  2010. */
  2011. rx->sta->rx_msdu[rx->seqno_idx]++;
  2012. }
  2013. /*
  2014. * Send unexpected-4addr-frame event to hostapd. For older versions,
  2015. * also drop the frame to cooked monitor interfaces.
  2016. */
  2017. if (ieee80211_has_a4(hdr->frame_control) &&
  2018. sdata->vif.type == NL80211_IFTYPE_AP) {
  2019. if (rx->sta &&
  2020. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  2021. cfg80211_rx_unexpected_4addr_frame(
  2022. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  2023. return RX_DROP_MONITOR;
  2024. }
  2025. err = __ieee80211_data_to_8023(rx, &port_control);
  2026. if (unlikely(err))
  2027. return RX_DROP_UNUSABLE;
  2028. if (!ieee80211_frame_allowed(rx, fc))
  2029. return RX_DROP_MONITOR;
  2030. /* directly handle TDLS channel switch requests/responses */
  2031. if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
  2032. cpu_to_be16(ETH_P_TDLS))) {
  2033. struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
  2034. if (pskb_may_pull(rx->skb,
  2035. offsetof(struct ieee80211_tdls_data, u)) &&
  2036. tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
  2037. tf->category == WLAN_CATEGORY_TDLS &&
  2038. (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
  2039. tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
  2040. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TDLS_CHSW;
  2041. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2042. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2043. if (rx->sta)
  2044. rx->sta->rx_packets++;
  2045. return RX_QUEUED;
  2046. }
  2047. }
  2048. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  2049. unlikely(port_control) && sdata->bss) {
  2050. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2051. u.ap);
  2052. dev = sdata->dev;
  2053. rx->sdata = sdata;
  2054. }
  2055. rx->skb->dev = dev;
  2056. dev->stats.rx_packets++;
  2057. dev->stats.rx_bytes += rx->skb->len;
  2058. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  2059. !is_multicast_ether_addr(
  2060. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  2061. (!local->scanning &&
  2062. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  2063. mod_timer(&local->dynamic_ps_timer, jiffies +
  2064. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  2065. }
  2066. ieee80211_deliver_skb(rx);
  2067. return RX_QUEUED;
  2068. }
  2069. static ieee80211_rx_result debug_noinline
  2070. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
  2071. {
  2072. struct sk_buff *skb = rx->skb;
  2073. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  2074. struct tid_ampdu_rx *tid_agg_rx;
  2075. u16 start_seq_num;
  2076. u16 tid;
  2077. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  2078. return RX_CONTINUE;
  2079. if (ieee80211_is_back_req(bar->frame_control)) {
  2080. struct {
  2081. __le16 control, start_seq_num;
  2082. } __packed bar_data;
  2083. if (!rx->sta)
  2084. return RX_DROP_MONITOR;
  2085. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  2086. &bar_data, sizeof(bar_data)))
  2087. return RX_DROP_MONITOR;
  2088. tid = le16_to_cpu(bar_data.control) >> 12;
  2089. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  2090. if (!tid_agg_rx)
  2091. return RX_DROP_MONITOR;
  2092. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  2093. /* reset session timer */
  2094. if (tid_agg_rx->timeout)
  2095. mod_timer(&tid_agg_rx->session_timer,
  2096. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2097. spin_lock(&tid_agg_rx->reorder_lock);
  2098. /* release stored frames up to start of BAR */
  2099. ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
  2100. start_seq_num, frames);
  2101. spin_unlock(&tid_agg_rx->reorder_lock);
  2102. kfree_skb(skb);
  2103. return RX_QUEUED;
  2104. }
  2105. /*
  2106. * After this point, we only want management frames,
  2107. * so we can drop all remaining control frames to
  2108. * cooked monitor interfaces.
  2109. */
  2110. return RX_DROP_MONITOR;
  2111. }
  2112. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  2113. struct ieee80211_mgmt *mgmt,
  2114. size_t len)
  2115. {
  2116. struct ieee80211_local *local = sdata->local;
  2117. struct sk_buff *skb;
  2118. struct ieee80211_mgmt *resp;
  2119. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  2120. /* Not to own unicast address */
  2121. return;
  2122. }
  2123. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  2124. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  2125. /* Not from the current AP or not associated yet. */
  2126. return;
  2127. }
  2128. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  2129. /* Too short SA Query request frame */
  2130. return;
  2131. }
  2132. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  2133. if (skb == NULL)
  2134. return;
  2135. skb_reserve(skb, local->hw.extra_tx_headroom);
  2136. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  2137. memset(resp, 0, 24);
  2138. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2139. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  2140. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  2141. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2142. IEEE80211_STYPE_ACTION);
  2143. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  2144. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  2145. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  2146. memcpy(resp->u.action.u.sa_query.trans_id,
  2147. mgmt->u.action.u.sa_query.trans_id,
  2148. WLAN_SA_QUERY_TR_ID_LEN);
  2149. ieee80211_tx_skb(sdata, skb);
  2150. }
  2151. static ieee80211_rx_result debug_noinline
  2152. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  2153. {
  2154. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2155. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2156. /*
  2157. * From here on, look only at management frames.
  2158. * Data and control frames are already handled,
  2159. * and unknown (reserved) frames are useless.
  2160. */
  2161. if (rx->skb->len < 24)
  2162. return RX_DROP_MONITOR;
  2163. if (!ieee80211_is_mgmt(mgmt->frame_control))
  2164. return RX_DROP_MONITOR;
  2165. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  2166. ieee80211_is_beacon(mgmt->frame_control) &&
  2167. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  2168. int sig = 0;
  2169. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  2170. sig = status->signal;
  2171. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  2172. rx->skb->data, rx->skb->len,
  2173. status->freq, sig);
  2174. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  2175. }
  2176. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  2177. return RX_DROP_MONITOR;
  2178. if (ieee80211_drop_unencrypted_mgmt(rx))
  2179. return RX_DROP_UNUSABLE;
  2180. return RX_CONTINUE;
  2181. }
  2182. static ieee80211_rx_result debug_noinline
  2183. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  2184. {
  2185. struct ieee80211_local *local = rx->local;
  2186. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2187. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2188. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2189. int len = rx->skb->len;
  2190. if (!ieee80211_is_action(mgmt->frame_control))
  2191. return RX_CONTINUE;
  2192. /* drop too small frames */
  2193. if (len < IEEE80211_MIN_ACTION_SIZE)
  2194. return RX_DROP_UNUSABLE;
  2195. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
  2196. mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
  2197. mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  2198. return RX_DROP_UNUSABLE;
  2199. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  2200. return RX_DROP_UNUSABLE;
  2201. switch (mgmt->u.action.category) {
  2202. case WLAN_CATEGORY_HT:
  2203. /* reject HT action frames from stations not supporting HT */
  2204. if (!rx->sta->sta.ht_cap.ht_supported)
  2205. goto invalid;
  2206. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2207. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2208. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2209. sdata->vif.type != NL80211_IFTYPE_AP &&
  2210. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2211. break;
  2212. /* verify action & smps_control/chanwidth are present */
  2213. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2214. goto invalid;
  2215. switch (mgmt->u.action.u.ht_smps.action) {
  2216. case WLAN_HT_ACTION_SMPS: {
  2217. struct ieee80211_supported_band *sband;
  2218. enum ieee80211_smps_mode smps_mode;
  2219. /* convert to HT capability */
  2220. switch (mgmt->u.action.u.ht_smps.smps_control) {
  2221. case WLAN_HT_SMPS_CONTROL_DISABLED:
  2222. smps_mode = IEEE80211_SMPS_OFF;
  2223. break;
  2224. case WLAN_HT_SMPS_CONTROL_STATIC:
  2225. smps_mode = IEEE80211_SMPS_STATIC;
  2226. break;
  2227. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  2228. smps_mode = IEEE80211_SMPS_DYNAMIC;
  2229. break;
  2230. default:
  2231. goto invalid;
  2232. }
  2233. /* if no change do nothing */
  2234. if (rx->sta->sta.smps_mode == smps_mode)
  2235. goto handled;
  2236. rx->sta->sta.smps_mode = smps_mode;
  2237. sband = rx->local->hw.wiphy->bands[status->band];
  2238. rate_control_rate_update(local, sband, rx->sta,
  2239. IEEE80211_RC_SMPS_CHANGED);
  2240. goto handled;
  2241. }
  2242. case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
  2243. struct ieee80211_supported_band *sband;
  2244. u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
  2245. enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
  2246. /* If it doesn't support 40 MHz it can't change ... */
  2247. if (!(rx->sta->sta.ht_cap.cap &
  2248. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  2249. goto handled;
  2250. if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
  2251. max_bw = IEEE80211_STA_RX_BW_20;
  2252. else
  2253. max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
  2254. /* set cur_max_bandwidth and recalc sta bw */
  2255. rx->sta->cur_max_bandwidth = max_bw;
  2256. new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
  2257. if (rx->sta->sta.bandwidth == new_bw)
  2258. goto handled;
  2259. rx->sta->sta.bandwidth = new_bw;
  2260. sband = rx->local->hw.wiphy->bands[status->band];
  2261. rate_control_rate_update(local, sband, rx->sta,
  2262. IEEE80211_RC_BW_CHANGED);
  2263. goto handled;
  2264. }
  2265. default:
  2266. goto invalid;
  2267. }
  2268. break;
  2269. case WLAN_CATEGORY_PUBLIC:
  2270. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2271. goto invalid;
  2272. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2273. break;
  2274. if (!rx->sta)
  2275. break;
  2276. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  2277. break;
  2278. if (mgmt->u.action.u.ext_chan_switch.action_code !=
  2279. WLAN_PUB_ACTION_EXT_CHANSW_ANN)
  2280. break;
  2281. if (len < offsetof(struct ieee80211_mgmt,
  2282. u.action.u.ext_chan_switch.variable))
  2283. goto invalid;
  2284. goto queue;
  2285. case WLAN_CATEGORY_VHT:
  2286. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2287. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2288. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2289. sdata->vif.type != NL80211_IFTYPE_AP &&
  2290. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2291. break;
  2292. /* verify action code is present */
  2293. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2294. goto invalid;
  2295. switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
  2296. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  2297. u8 opmode;
  2298. /* verify opmode is present */
  2299. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2300. goto invalid;
  2301. opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
  2302. ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
  2303. opmode, status->band,
  2304. false);
  2305. goto handled;
  2306. }
  2307. default:
  2308. break;
  2309. }
  2310. break;
  2311. case WLAN_CATEGORY_BACK:
  2312. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2313. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2314. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2315. sdata->vif.type != NL80211_IFTYPE_AP &&
  2316. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2317. break;
  2318. /* verify action_code is present */
  2319. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2320. break;
  2321. switch (mgmt->u.action.u.addba_req.action_code) {
  2322. case WLAN_ACTION_ADDBA_REQ:
  2323. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2324. sizeof(mgmt->u.action.u.addba_req)))
  2325. goto invalid;
  2326. break;
  2327. case WLAN_ACTION_ADDBA_RESP:
  2328. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2329. sizeof(mgmt->u.action.u.addba_resp)))
  2330. goto invalid;
  2331. break;
  2332. case WLAN_ACTION_DELBA:
  2333. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2334. sizeof(mgmt->u.action.u.delba)))
  2335. goto invalid;
  2336. break;
  2337. default:
  2338. goto invalid;
  2339. }
  2340. goto queue;
  2341. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2342. /* verify action_code is present */
  2343. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2344. break;
  2345. switch (mgmt->u.action.u.measurement.action_code) {
  2346. case WLAN_ACTION_SPCT_MSR_REQ:
  2347. if (status->band != IEEE80211_BAND_5GHZ)
  2348. break;
  2349. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2350. sizeof(mgmt->u.action.u.measurement)))
  2351. break;
  2352. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2353. break;
  2354. ieee80211_process_measurement_req(sdata, mgmt, len);
  2355. goto handled;
  2356. case WLAN_ACTION_SPCT_CHL_SWITCH: {
  2357. u8 *bssid;
  2358. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2359. sizeof(mgmt->u.action.u.chan_switch)))
  2360. break;
  2361. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2362. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2363. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2364. break;
  2365. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2366. bssid = sdata->u.mgd.bssid;
  2367. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2368. bssid = sdata->u.ibss.bssid;
  2369. else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  2370. bssid = mgmt->sa;
  2371. else
  2372. break;
  2373. if (!ether_addr_equal(mgmt->bssid, bssid))
  2374. break;
  2375. goto queue;
  2376. }
  2377. }
  2378. break;
  2379. case WLAN_CATEGORY_SA_QUERY:
  2380. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2381. sizeof(mgmt->u.action.u.sa_query)))
  2382. break;
  2383. switch (mgmt->u.action.u.sa_query.action) {
  2384. case WLAN_ACTION_SA_QUERY_REQUEST:
  2385. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2386. break;
  2387. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2388. goto handled;
  2389. }
  2390. break;
  2391. case WLAN_CATEGORY_SELF_PROTECTED:
  2392. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2393. sizeof(mgmt->u.action.u.self_prot.action_code)))
  2394. break;
  2395. switch (mgmt->u.action.u.self_prot.action_code) {
  2396. case WLAN_SP_MESH_PEERING_OPEN:
  2397. case WLAN_SP_MESH_PEERING_CLOSE:
  2398. case WLAN_SP_MESH_PEERING_CONFIRM:
  2399. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2400. goto invalid;
  2401. if (sdata->u.mesh.user_mpm)
  2402. /* userspace handles this frame */
  2403. break;
  2404. goto queue;
  2405. case WLAN_SP_MGK_INFORM:
  2406. case WLAN_SP_MGK_ACK:
  2407. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2408. goto invalid;
  2409. break;
  2410. }
  2411. break;
  2412. case WLAN_CATEGORY_MESH_ACTION:
  2413. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2414. sizeof(mgmt->u.action.u.mesh_action.action_code)))
  2415. break;
  2416. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2417. break;
  2418. if (mesh_action_is_path_sel(mgmt) &&
  2419. !mesh_path_sel_is_hwmp(sdata))
  2420. break;
  2421. goto queue;
  2422. }
  2423. return RX_CONTINUE;
  2424. invalid:
  2425. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2426. /* will return in the next handlers */
  2427. return RX_CONTINUE;
  2428. handled:
  2429. if (rx->sta)
  2430. rx->sta->rx_packets++;
  2431. dev_kfree_skb(rx->skb);
  2432. return RX_QUEUED;
  2433. queue:
  2434. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2435. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2436. ieee80211_queue_work(&local->hw, &sdata->work);
  2437. if (rx->sta)
  2438. rx->sta->rx_packets++;
  2439. return RX_QUEUED;
  2440. }
  2441. static ieee80211_rx_result debug_noinline
  2442. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2443. {
  2444. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2445. int sig = 0;
  2446. /* skip known-bad action frames and return them in the next handler */
  2447. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2448. return RX_CONTINUE;
  2449. /*
  2450. * Getting here means the kernel doesn't know how to handle
  2451. * it, but maybe userspace does ... include returned frames
  2452. * so userspace can register for those to know whether ones
  2453. * it transmitted were processed or returned.
  2454. */
  2455. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  2456. sig = status->signal;
  2457. if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
  2458. rx->skb->data, rx->skb->len, 0)) {
  2459. if (rx->sta)
  2460. rx->sta->rx_packets++;
  2461. dev_kfree_skb(rx->skb);
  2462. return RX_QUEUED;
  2463. }
  2464. return RX_CONTINUE;
  2465. }
  2466. static ieee80211_rx_result debug_noinline
  2467. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2468. {
  2469. struct ieee80211_local *local = rx->local;
  2470. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2471. struct sk_buff *nskb;
  2472. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2473. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2474. if (!ieee80211_is_action(mgmt->frame_control))
  2475. return RX_CONTINUE;
  2476. /*
  2477. * For AP mode, hostapd is responsible for handling any action
  2478. * frames that we didn't handle, including returning unknown
  2479. * ones. For all other modes we will return them to the sender,
  2480. * setting the 0x80 bit in the action category, as required by
  2481. * 802.11-2012 9.24.4.
  2482. * Newer versions of hostapd shall also use the management frame
  2483. * registration mechanisms, but older ones still use cooked
  2484. * monitor interfaces so push all frames there.
  2485. */
  2486. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2487. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2488. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2489. return RX_DROP_MONITOR;
  2490. if (is_multicast_ether_addr(mgmt->da))
  2491. return RX_DROP_MONITOR;
  2492. /* do not return rejected action frames */
  2493. if (mgmt->u.action.category & 0x80)
  2494. return RX_DROP_UNUSABLE;
  2495. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2496. GFP_ATOMIC);
  2497. if (nskb) {
  2498. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2499. nmgmt->u.action.category |= 0x80;
  2500. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2501. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2502. memset(nskb->cb, 0, sizeof(nskb->cb));
  2503. if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  2504. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
  2505. info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
  2506. IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
  2507. IEEE80211_TX_CTL_NO_CCK_RATE;
  2508. if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
  2509. info->hw_queue =
  2510. local->hw.offchannel_tx_hw_queue;
  2511. }
  2512. __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
  2513. status->band);
  2514. }
  2515. dev_kfree_skb(rx->skb);
  2516. return RX_QUEUED;
  2517. }
  2518. static ieee80211_rx_result debug_noinline
  2519. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2520. {
  2521. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2522. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2523. __le16 stype;
  2524. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2525. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2526. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2527. sdata->vif.type != NL80211_IFTYPE_OCB &&
  2528. sdata->vif.type != NL80211_IFTYPE_STATION)
  2529. return RX_DROP_MONITOR;
  2530. switch (stype) {
  2531. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2532. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2533. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2534. /* process for all: mesh, mlme, ibss */
  2535. break;
  2536. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2537. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2538. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2539. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2540. if (is_multicast_ether_addr(mgmt->da) &&
  2541. !is_broadcast_ether_addr(mgmt->da))
  2542. return RX_DROP_MONITOR;
  2543. /* process only for station */
  2544. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2545. return RX_DROP_MONITOR;
  2546. break;
  2547. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2548. /* process only for ibss and mesh */
  2549. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2550. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2551. return RX_DROP_MONITOR;
  2552. break;
  2553. default:
  2554. return RX_DROP_MONITOR;
  2555. }
  2556. /* queue up frame and kick off work to process it */
  2557. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2558. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2559. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2560. if (rx->sta)
  2561. rx->sta->rx_packets++;
  2562. return RX_QUEUED;
  2563. }
  2564. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2565. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2566. struct ieee80211_rate *rate)
  2567. {
  2568. struct ieee80211_sub_if_data *sdata;
  2569. struct ieee80211_local *local = rx->local;
  2570. struct sk_buff *skb = rx->skb, *skb2;
  2571. struct net_device *prev_dev = NULL;
  2572. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2573. int needed_headroom;
  2574. /*
  2575. * If cooked monitor has been processed already, then
  2576. * don't do it again. If not, set the flag.
  2577. */
  2578. if (rx->flags & IEEE80211_RX_CMNTR)
  2579. goto out_free_skb;
  2580. rx->flags |= IEEE80211_RX_CMNTR;
  2581. /* If there are no cooked monitor interfaces, just free the SKB */
  2582. if (!local->cooked_mntrs)
  2583. goto out_free_skb;
  2584. /* vendor data is long removed here */
  2585. status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
  2586. /* room for the radiotap header based on driver features */
  2587. needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
  2588. if (skb_headroom(skb) < needed_headroom &&
  2589. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2590. goto out_free_skb;
  2591. /* prepend radiotap information */
  2592. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2593. false);
  2594. skb_set_mac_header(skb, 0);
  2595. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2596. skb->pkt_type = PACKET_OTHERHOST;
  2597. skb->protocol = htons(ETH_P_802_2);
  2598. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2599. if (!ieee80211_sdata_running(sdata))
  2600. continue;
  2601. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2602. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2603. continue;
  2604. if (prev_dev) {
  2605. skb2 = skb_clone(skb, GFP_ATOMIC);
  2606. if (skb2) {
  2607. skb2->dev = prev_dev;
  2608. netif_receive_skb(skb2);
  2609. }
  2610. }
  2611. prev_dev = sdata->dev;
  2612. sdata->dev->stats.rx_packets++;
  2613. sdata->dev->stats.rx_bytes += skb->len;
  2614. }
  2615. if (prev_dev) {
  2616. skb->dev = prev_dev;
  2617. netif_receive_skb(skb);
  2618. return;
  2619. }
  2620. out_free_skb:
  2621. dev_kfree_skb(skb);
  2622. }
  2623. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2624. ieee80211_rx_result res)
  2625. {
  2626. switch (res) {
  2627. case RX_DROP_MONITOR:
  2628. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2629. if (rx->sta)
  2630. rx->sta->rx_dropped++;
  2631. /* fall through */
  2632. case RX_CONTINUE: {
  2633. struct ieee80211_rate *rate = NULL;
  2634. struct ieee80211_supported_band *sband;
  2635. struct ieee80211_rx_status *status;
  2636. status = IEEE80211_SKB_RXCB((rx->skb));
  2637. sband = rx->local->hw.wiphy->bands[status->band];
  2638. if (!(status->flag & RX_FLAG_HT) &&
  2639. !(status->flag & RX_FLAG_VHT))
  2640. rate = &sband->bitrates[status->rate_idx];
  2641. ieee80211_rx_cooked_monitor(rx, rate);
  2642. break;
  2643. }
  2644. case RX_DROP_UNUSABLE:
  2645. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2646. if (rx->sta)
  2647. rx->sta->rx_dropped++;
  2648. dev_kfree_skb(rx->skb);
  2649. break;
  2650. case RX_QUEUED:
  2651. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2652. break;
  2653. }
  2654. }
  2655. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
  2656. struct sk_buff_head *frames)
  2657. {
  2658. ieee80211_rx_result res = RX_DROP_MONITOR;
  2659. struct sk_buff *skb;
  2660. #define CALL_RXH(rxh) \
  2661. do { \
  2662. res = rxh(rx); \
  2663. if (res != RX_CONTINUE) \
  2664. goto rxh_next; \
  2665. } while (0);
  2666. spin_lock_bh(&rx->local->rx_path_lock);
  2667. while ((skb = __skb_dequeue(frames))) {
  2668. /*
  2669. * all the other fields are valid across frames
  2670. * that belong to an aMPDU since they are on the
  2671. * same TID from the same station
  2672. */
  2673. rx->skb = skb;
  2674. CALL_RXH(ieee80211_rx_h_check_more_data)
  2675. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
  2676. CALL_RXH(ieee80211_rx_h_sta_process)
  2677. CALL_RXH(ieee80211_rx_h_decrypt)
  2678. CALL_RXH(ieee80211_rx_h_defragment)
  2679. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2680. /* must be after MMIC verify so header is counted in MPDU mic */
  2681. #ifdef CONFIG_MAC80211_MESH
  2682. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2683. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2684. #endif
  2685. CALL_RXH(ieee80211_rx_h_amsdu)
  2686. CALL_RXH(ieee80211_rx_h_data)
  2687. /* special treatment -- needs the queue */
  2688. res = ieee80211_rx_h_ctrl(rx, frames);
  2689. if (res != RX_CONTINUE)
  2690. goto rxh_next;
  2691. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2692. CALL_RXH(ieee80211_rx_h_action)
  2693. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2694. CALL_RXH(ieee80211_rx_h_action_return)
  2695. CALL_RXH(ieee80211_rx_h_mgmt)
  2696. rxh_next:
  2697. ieee80211_rx_handlers_result(rx, res);
  2698. #undef CALL_RXH
  2699. }
  2700. spin_unlock_bh(&rx->local->rx_path_lock);
  2701. }
  2702. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2703. {
  2704. struct sk_buff_head reorder_release;
  2705. ieee80211_rx_result res = RX_DROP_MONITOR;
  2706. __skb_queue_head_init(&reorder_release);
  2707. #define CALL_RXH(rxh) \
  2708. do { \
  2709. res = rxh(rx); \
  2710. if (res != RX_CONTINUE) \
  2711. goto rxh_next; \
  2712. } while (0);
  2713. CALL_RXH(ieee80211_rx_h_check_dup)
  2714. CALL_RXH(ieee80211_rx_h_check)
  2715. ieee80211_rx_reorder_ampdu(rx, &reorder_release);
  2716. ieee80211_rx_handlers(rx, &reorder_release);
  2717. return;
  2718. rxh_next:
  2719. ieee80211_rx_handlers_result(rx, res);
  2720. #undef CALL_RXH
  2721. }
  2722. /*
  2723. * This function makes calls into the RX path, therefore
  2724. * it has to be invoked under RCU read lock.
  2725. */
  2726. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2727. {
  2728. struct sk_buff_head frames;
  2729. struct ieee80211_rx_data rx = {
  2730. .sta = sta,
  2731. .sdata = sta->sdata,
  2732. .local = sta->local,
  2733. /* This is OK -- must be QoS data frame */
  2734. .security_idx = tid,
  2735. .seqno_idx = tid,
  2736. .flags = 0,
  2737. };
  2738. struct tid_ampdu_rx *tid_agg_rx;
  2739. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2740. if (!tid_agg_rx)
  2741. return;
  2742. __skb_queue_head_init(&frames);
  2743. spin_lock(&tid_agg_rx->reorder_lock);
  2744. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  2745. spin_unlock(&tid_agg_rx->reorder_lock);
  2746. ieee80211_rx_handlers(&rx, &frames);
  2747. }
  2748. /* main receive path */
  2749. static bool prepare_for_handlers(struct ieee80211_rx_data *rx,
  2750. struct ieee80211_hdr *hdr)
  2751. {
  2752. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2753. struct sk_buff *skb = rx->skb;
  2754. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2755. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2756. int multicast = is_multicast_ether_addr(hdr->addr1);
  2757. switch (sdata->vif.type) {
  2758. case NL80211_IFTYPE_STATION:
  2759. if (!bssid && !sdata->u.mgd.use_4addr)
  2760. return false;
  2761. if (!multicast &&
  2762. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2763. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2764. sdata->u.mgd.use_4addr)
  2765. return false;
  2766. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2767. }
  2768. break;
  2769. case NL80211_IFTYPE_ADHOC:
  2770. if (!bssid)
  2771. return false;
  2772. if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
  2773. ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
  2774. return false;
  2775. if (ieee80211_is_beacon(hdr->frame_control)) {
  2776. return true;
  2777. } else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2778. return false;
  2779. } else if (!multicast &&
  2780. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2781. if (!(sdata->dev->flags & IFF_PROMISC))
  2782. return false;
  2783. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2784. } else if (!rx->sta) {
  2785. int rate_idx;
  2786. if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
  2787. rate_idx = 0; /* TODO: HT/VHT rates */
  2788. else
  2789. rate_idx = status->rate_idx;
  2790. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  2791. BIT(rate_idx));
  2792. }
  2793. break;
  2794. case NL80211_IFTYPE_OCB:
  2795. if (!bssid)
  2796. return false;
  2797. if (ieee80211_is_beacon(hdr->frame_control)) {
  2798. return false;
  2799. } else if (!is_broadcast_ether_addr(bssid)) {
  2800. ocb_dbg(sdata, "BSSID mismatch in OCB mode!\n");
  2801. return false;
  2802. } else if (!multicast &&
  2803. !ether_addr_equal(sdata->dev->dev_addr,
  2804. hdr->addr1)) {
  2805. /* if we are in promisc mode we also accept
  2806. * packets not destined for us
  2807. */
  2808. if (!(sdata->dev->flags & IFF_PROMISC))
  2809. return false;
  2810. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  2811. } else if (!rx->sta) {
  2812. int rate_idx;
  2813. if (status->flag & RX_FLAG_HT)
  2814. rate_idx = 0; /* TODO: HT rates */
  2815. else
  2816. rate_idx = status->rate_idx;
  2817. ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
  2818. BIT(rate_idx));
  2819. }
  2820. break;
  2821. case NL80211_IFTYPE_MESH_POINT:
  2822. if (!multicast &&
  2823. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2824. if (!(sdata->dev->flags & IFF_PROMISC))
  2825. return false;
  2826. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2827. }
  2828. break;
  2829. case NL80211_IFTYPE_AP_VLAN:
  2830. case NL80211_IFTYPE_AP:
  2831. if (!bssid) {
  2832. if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2833. return false;
  2834. } else if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
  2835. /*
  2836. * Accept public action frames even when the
  2837. * BSSID doesn't match, this is used for P2P
  2838. * and location updates. Note that mac80211
  2839. * itself never looks at these frames.
  2840. */
  2841. if (!multicast &&
  2842. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2843. return false;
  2844. if (ieee80211_is_public_action(hdr, skb->len))
  2845. return true;
  2846. if (!ieee80211_is_beacon(hdr->frame_control))
  2847. return false;
  2848. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2849. } else if (!ieee80211_has_tods(hdr->frame_control)) {
  2850. /* ignore data frames to TDLS-peers */
  2851. if (ieee80211_is_data(hdr->frame_control))
  2852. return false;
  2853. /* ignore action frames to TDLS-peers */
  2854. if (ieee80211_is_action(hdr->frame_control) &&
  2855. !ether_addr_equal(bssid, hdr->addr1))
  2856. return false;
  2857. }
  2858. break;
  2859. case NL80211_IFTYPE_WDS:
  2860. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2861. return false;
  2862. if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
  2863. return false;
  2864. break;
  2865. case NL80211_IFTYPE_P2P_DEVICE:
  2866. if (!ieee80211_is_public_action(hdr, skb->len) &&
  2867. !ieee80211_is_probe_req(hdr->frame_control) &&
  2868. !ieee80211_is_probe_resp(hdr->frame_control) &&
  2869. !ieee80211_is_beacon(hdr->frame_control))
  2870. return false;
  2871. if (!ether_addr_equal(sdata->vif.addr, hdr->addr1) &&
  2872. !multicast)
  2873. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2874. break;
  2875. default:
  2876. /* should never get here */
  2877. WARN_ON_ONCE(1);
  2878. break;
  2879. }
  2880. return true;
  2881. }
  2882. /*
  2883. * This function returns whether or not the SKB
  2884. * was destined for RX processing or not, which,
  2885. * if consume is true, is equivalent to whether
  2886. * or not the skb was consumed.
  2887. */
  2888. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2889. struct sk_buff *skb, bool consume)
  2890. {
  2891. struct ieee80211_local *local = rx->local;
  2892. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2893. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2894. struct ieee80211_hdr *hdr = (void *)skb->data;
  2895. rx->skb = skb;
  2896. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2897. if (!prepare_for_handlers(rx, hdr))
  2898. return false;
  2899. if (!consume) {
  2900. skb = skb_copy(skb, GFP_ATOMIC);
  2901. if (!skb) {
  2902. if (net_ratelimit())
  2903. wiphy_debug(local->hw.wiphy,
  2904. "failed to copy skb for %s\n",
  2905. sdata->name);
  2906. return true;
  2907. }
  2908. rx->skb = skb;
  2909. }
  2910. ieee80211_invoke_rx_handlers(rx);
  2911. return true;
  2912. }
  2913. /*
  2914. * This is the actual Rx frames handler. as it belongs to Rx path it must
  2915. * be called with rcu_read_lock protection.
  2916. */
  2917. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2918. struct sk_buff *skb)
  2919. {
  2920. struct ieee80211_local *local = hw_to_local(hw);
  2921. struct ieee80211_sub_if_data *sdata;
  2922. struct ieee80211_hdr *hdr;
  2923. __le16 fc;
  2924. struct ieee80211_rx_data rx;
  2925. struct ieee80211_sub_if_data *prev;
  2926. struct sta_info *sta, *tmp, *prev_sta;
  2927. int err = 0;
  2928. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2929. memset(&rx, 0, sizeof(rx));
  2930. rx.skb = skb;
  2931. rx.local = local;
  2932. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2933. local->dot11ReceivedFragmentCount++;
  2934. if (ieee80211_is_mgmt(fc)) {
  2935. /* drop frame if too short for header */
  2936. if (skb->len < ieee80211_hdrlen(fc))
  2937. err = -ENOBUFS;
  2938. else
  2939. err = skb_linearize(skb);
  2940. } else {
  2941. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2942. }
  2943. if (err) {
  2944. dev_kfree_skb(skb);
  2945. return;
  2946. }
  2947. hdr = (struct ieee80211_hdr *)skb->data;
  2948. ieee80211_parse_qos(&rx);
  2949. ieee80211_verify_alignment(&rx);
  2950. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
  2951. ieee80211_is_beacon(hdr->frame_control)))
  2952. ieee80211_scan_rx(local, skb);
  2953. if (ieee80211_is_data(fc)) {
  2954. prev_sta = NULL;
  2955. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  2956. if (!prev_sta) {
  2957. prev_sta = sta;
  2958. continue;
  2959. }
  2960. rx.sta = prev_sta;
  2961. rx.sdata = prev_sta->sdata;
  2962. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2963. prev_sta = sta;
  2964. }
  2965. if (prev_sta) {
  2966. rx.sta = prev_sta;
  2967. rx.sdata = prev_sta->sdata;
  2968. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2969. return;
  2970. goto out;
  2971. }
  2972. }
  2973. prev = NULL;
  2974. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2975. if (!ieee80211_sdata_running(sdata))
  2976. continue;
  2977. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2978. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2979. continue;
  2980. /*
  2981. * frame is destined for this interface, but if it's
  2982. * not also for the previous one we handle that after
  2983. * the loop to avoid copying the SKB once too much
  2984. */
  2985. if (!prev) {
  2986. prev = sdata;
  2987. continue;
  2988. }
  2989. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2990. rx.sdata = prev;
  2991. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2992. prev = sdata;
  2993. }
  2994. if (prev) {
  2995. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2996. rx.sdata = prev;
  2997. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2998. return;
  2999. }
  3000. out:
  3001. dev_kfree_skb(skb);
  3002. }
  3003. /*
  3004. * This is the receive path handler. It is called by a low level driver when an
  3005. * 802.11 MPDU is received from the hardware.
  3006. */
  3007. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  3008. {
  3009. struct ieee80211_local *local = hw_to_local(hw);
  3010. struct ieee80211_rate *rate = NULL;
  3011. struct ieee80211_supported_band *sband;
  3012. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3013. WARN_ON_ONCE(softirq_count() == 0);
  3014. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  3015. goto drop;
  3016. sband = local->hw.wiphy->bands[status->band];
  3017. if (WARN_ON(!sband))
  3018. goto drop;
  3019. /*
  3020. * If we're suspending, it is possible although not too likely
  3021. * that we'd be receiving frames after having already partially
  3022. * quiesced the stack. We can't process such frames then since
  3023. * that might, for example, cause stations to be added or other
  3024. * driver callbacks be invoked.
  3025. */
  3026. if (unlikely(local->quiescing || local->suspended))
  3027. goto drop;
  3028. /* We might be during a HW reconfig, prevent Rx for the same reason */
  3029. if (unlikely(local->in_reconfig))
  3030. goto drop;
  3031. /*
  3032. * The same happens when we're not even started,
  3033. * but that's worth a warning.
  3034. */
  3035. if (WARN_ON(!local->started))
  3036. goto drop;
  3037. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  3038. /*
  3039. * Validate the rate, unless a PLCP error means that
  3040. * we probably can't have a valid rate here anyway.
  3041. */
  3042. if (status->flag & RX_FLAG_HT) {
  3043. /*
  3044. * rate_idx is MCS index, which can be [0-76]
  3045. * as documented on:
  3046. *
  3047. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  3048. *
  3049. * Anything else would be some sort of driver or
  3050. * hardware error. The driver should catch hardware
  3051. * errors.
  3052. */
  3053. if (WARN(status->rate_idx > 76,
  3054. "Rate marked as an HT rate but passed "
  3055. "status->rate_idx is not "
  3056. "an MCS index [0-76]: %d (0x%02x)\n",
  3057. status->rate_idx,
  3058. status->rate_idx))
  3059. goto drop;
  3060. } else if (status->flag & RX_FLAG_VHT) {
  3061. if (WARN_ONCE(status->rate_idx > 9 ||
  3062. !status->vht_nss ||
  3063. status->vht_nss > 8,
  3064. "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
  3065. status->rate_idx, status->vht_nss))
  3066. goto drop;
  3067. } else {
  3068. if (WARN_ON(status->rate_idx >= sband->n_bitrates))
  3069. goto drop;
  3070. rate = &sband->bitrates[status->rate_idx];
  3071. }
  3072. }
  3073. status->rx_flags = 0;
  3074. /*
  3075. * key references and virtual interfaces are protected using RCU
  3076. * and this requires that we are in a read-side RCU section during
  3077. * receive processing
  3078. */
  3079. rcu_read_lock();
  3080. /*
  3081. * Frames with failed FCS/PLCP checksum are not returned,
  3082. * all other frames are returned without radiotap header
  3083. * if it was previously present.
  3084. * Also, frames with less than 16 bytes are dropped.
  3085. */
  3086. skb = ieee80211_rx_monitor(local, skb, rate);
  3087. if (!skb) {
  3088. rcu_read_unlock();
  3089. return;
  3090. }
  3091. ieee80211_tpt_led_trig_rx(local,
  3092. ((struct ieee80211_hdr *)skb->data)->frame_control,
  3093. skb->len);
  3094. __ieee80211_rx_handle_packet(hw, skb);
  3095. rcu_read_unlock();
  3096. return;
  3097. drop:
  3098. kfree_skb(skb);
  3099. }
  3100. EXPORT_SYMBOL(ieee80211_rx);
  3101. /* This is a version of the rx handler that can be called from hard irq
  3102. * context. Post the skb on the queue and schedule the tasklet */
  3103. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  3104. {
  3105. struct ieee80211_local *local = hw_to_local(hw);
  3106. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3107. skb->pkt_type = IEEE80211_RX_MSG;
  3108. skb_queue_tail(&local->skb_queue, skb);
  3109. tasklet_schedule(&local->tasklet);
  3110. }
  3111. EXPORT_SYMBOL(ieee80211_rx_irqsafe);