rx.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588
  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. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018 Intel Corporation
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/jiffies.h>
  15. #include <linux/slab.h>
  16. #include <linux/kernel.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/export.h>
  22. #include <linux/bitops.h>
  23. #include <net/mac80211.h>
  24. #include <net/ieee80211_radiotap.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "led.h"
  29. #include "mesh.h"
  30. #include "wep.h"
  31. #include "wpa.h"
  32. #include "tkip.h"
  33. #include "wme.h"
  34. #include "rate.h"
  35. static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
  36. {
  37. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  38. u64_stats_update_begin(&tstats->syncp);
  39. tstats->rx_packets++;
  40. tstats->rx_bytes += len;
  41. u64_stats_update_end(&tstats->syncp);
  42. }
  43. static u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. if (ieee80211_is_data(fc)) {
  48. if (len < 24) /* drop incorrect hdr len (data) */
  49. return NULL;
  50. if (ieee80211_has_a4(fc))
  51. return NULL;
  52. if (ieee80211_has_tods(fc))
  53. return hdr->addr1;
  54. if (ieee80211_has_fromds(fc))
  55. return hdr->addr2;
  56. return hdr->addr3;
  57. }
  58. if (ieee80211_is_mgmt(fc)) {
  59. if (len < 24) /* drop incorrect hdr len (mgmt) */
  60. return NULL;
  61. return hdr->addr3;
  62. }
  63. if (ieee80211_is_ctl(fc)) {
  64. if (ieee80211_is_pspoll(fc))
  65. return hdr->addr1;
  66. if (ieee80211_is_back_req(fc)) {
  67. switch (type) {
  68. case NL80211_IFTYPE_STATION:
  69. return hdr->addr2;
  70. case NL80211_IFTYPE_AP:
  71. case NL80211_IFTYPE_AP_VLAN:
  72. return hdr->addr1;
  73. default:
  74. break; /* fall through to the return */
  75. }
  76. }
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * monitor mode reception
  82. *
  83. * This function cleans up the SKB, i.e. it removes all the stuff
  84. * only useful for monitoring.
  85. */
  86. static void remove_monitor_info(struct sk_buff *skb,
  87. unsigned int present_fcs_len,
  88. unsigned int rtap_space)
  89. {
  90. if (present_fcs_len)
  91. __pskb_trim(skb, skb->len - present_fcs_len);
  92. __pskb_pull(skb, rtap_space);
  93. }
  94. static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
  95. unsigned int rtap_space)
  96. {
  97. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  98. struct ieee80211_hdr *hdr;
  99. hdr = (void *)(skb->data + rtap_space);
  100. if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
  101. RX_FLAG_FAILED_PLCP_CRC |
  102. RX_FLAG_ONLY_MONITOR))
  103. return true;
  104. if (unlikely(skb->len < 16 + present_fcs_len + rtap_space))
  105. return true;
  106. if (ieee80211_is_ctl(hdr->frame_control) &&
  107. !ieee80211_is_pspoll(hdr->frame_control) &&
  108. !ieee80211_is_back_req(hdr->frame_control))
  109. return true;
  110. return false;
  111. }
  112. static int
  113. ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
  114. struct ieee80211_rx_status *status,
  115. struct sk_buff *skb)
  116. {
  117. int len;
  118. /* always present fields */
  119. len = sizeof(struct ieee80211_radiotap_header) + 8;
  120. /* allocate extra bitmaps */
  121. if (status->chains)
  122. len += 4 * hweight8(status->chains);
  123. /* vendor presence bitmap */
  124. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)
  125. len += 4;
  126. if (ieee80211_have_rx_timestamp(status)) {
  127. len = ALIGN(len, 8);
  128. len += 8;
  129. }
  130. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
  131. len += 1;
  132. /* antenna field, if we don't have per-chain info */
  133. if (!status->chains)
  134. len += 1;
  135. /* padding for RX_FLAGS if necessary */
  136. len = ALIGN(len, 2);
  137. if (status->encoding == RX_ENC_HT) /* HT info */
  138. len += 3;
  139. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  140. len = ALIGN(len, 4);
  141. len += 8;
  142. }
  143. if (status->encoding == RX_ENC_VHT) {
  144. len = ALIGN(len, 2);
  145. len += 12;
  146. }
  147. if (local->hw.radiotap_timestamp.units_pos >= 0) {
  148. len = ALIGN(len, 8);
  149. len += 12;
  150. }
  151. if (status->encoding == RX_ENC_HE &&
  152. status->flag & RX_FLAG_RADIOTAP_HE) {
  153. len = ALIGN(len, 2);
  154. len += 12;
  155. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) != 12);
  156. }
  157. if (status->encoding == RX_ENC_HE &&
  158. status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  159. len = ALIGN(len, 2);
  160. len += 12;
  161. BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) != 12);
  162. }
  163. if (status->chains) {
  164. /* antenna and antenna signal fields */
  165. len += 2 * hweight8(status->chains);
  166. }
  167. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  168. struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
  169. /* alignment for fixed 6-byte vendor data header */
  170. len = ALIGN(len, 2);
  171. /* vendor data header */
  172. len += 6;
  173. if (WARN_ON(rtap->align == 0))
  174. rtap->align = 1;
  175. len = ALIGN(len, rtap->align);
  176. len += rtap->len + rtap->pad;
  177. }
  178. return len;
  179. }
  180. static void ieee80211_handle_mu_mimo_mon(struct ieee80211_sub_if_data *sdata,
  181. struct sk_buff *skb,
  182. int rtap_space)
  183. {
  184. struct {
  185. struct ieee80211_hdr_3addr hdr;
  186. u8 category;
  187. u8 action_code;
  188. } __packed __aligned(2) action;
  189. if (!sdata)
  190. return;
  191. BUILD_BUG_ON(sizeof(action) != IEEE80211_MIN_ACTION_SIZE + 1);
  192. if (skb->len < rtap_space + sizeof(action) +
  193. VHT_MUMIMO_GROUPS_DATA_LEN)
  194. return;
  195. if (!is_valid_ether_addr(sdata->u.mntr.mu_follow_addr))
  196. return;
  197. skb_copy_bits(skb, rtap_space, &action, sizeof(action));
  198. if (!ieee80211_is_action(action.hdr.frame_control))
  199. return;
  200. if (action.category != WLAN_CATEGORY_VHT)
  201. return;
  202. if (action.action_code != WLAN_VHT_ACTION_GROUPID_MGMT)
  203. return;
  204. if (!ether_addr_equal(action.hdr.addr1, sdata->u.mntr.mu_follow_addr))
  205. return;
  206. skb = skb_copy(skb, GFP_ATOMIC);
  207. if (!skb)
  208. return;
  209. skb_queue_tail(&sdata->skb_queue, skb);
  210. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  211. }
  212. /*
  213. * ieee80211_add_rx_radiotap_header - add radiotap header
  214. *
  215. * add a radiotap header containing all the fields which the hardware provided.
  216. */
  217. static void
  218. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  219. struct sk_buff *skb,
  220. struct ieee80211_rate *rate,
  221. int rtap_len, bool has_fcs)
  222. {
  223. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  224. struct ieee80211_radiotap_header *rthdr;
  225. unsigned char *pos;
  226. __le32 *it_present;
  227. u32 it_present_val;
  228. u16 rx_flags = 0;
  229. u16 channel_flags = 0;
  230. int mpdulen, chain;
  231. unsigned long chains = status->chains;
  232. struct ieee80211_vendor_radiotap rtap = {};
  233. struct ieee80211_radiotap_he he = {};
  234. struct ieee80211_radiotap_he_mu he_mu = {};
  235. if (status->flag & RX_FLAG_RADIOTAP_HE) {
  236. he = *(struct ieee80211_radiotap_he *)skb->data;
  237. skb_pull(skb, sizeof(he));
  238. WARN_ON_ONCE(status->encoding != RX_ENC_HE);
  239. }
  240. if (status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  241. he_mu = *(struct ieee80211_radiotap_he_mu *)skb->data;
  242. skb_pull(skb, sizeof(he_mu));
  243. }
  244. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  245. rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
  246. /* rtap.len and rtap.pad are undone immediately */
  247. skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
  248. }
  249. mpdulen = skb->len;
  250. if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
  251. mpdulen += FCS_LEN;
  252. rthdr = skb_push(skb, rtap_len);
  253. memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
  254. it_present = &rthdr->it_present;
  255. /* radiotap header, set always present flags */
  256. rthdr->it_len = cpu_to_le16(rtap_len);
  257. it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
  258. BIT(IEEE80211_RADIOTAP_CHANNEL) |
  259. BIT(IEEE80211_RADIOTAP_RX_FLAGS);
  260. if (!status->chains)
  261. it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
  262. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  263. it_present_val |=
  264. BIT(IEEE80211_RADIOTAP_EXT) |
  265. BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
  266. put_unaligned_le32(it_present_val, it_present);
  267. it_present++;
  268. it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
  269. BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  270. }
  271. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  272. it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
  273. BIT(IEEE80211_RADIOTAP_EXT);
  274. put_unaligned_le32(it_present_val, it_present);
  275. it_present++;
  276. it_present_val = rtap.present;
  277. }
  278. put_unaligned_le32(it_present_val, it_present);
  279. pos = (void *)(it_present + 1);
  280. /* the order of the following fields is important */
  281. /* IEEE80211_RADIOTAP_TSFT */
  282. if (ieee80211_have_rx_timestamp(status)) {
  283. /* padding */
  284. while ((pos - (u8 *)rthdr) & 7)
  285. *pos++ = 0;
  286. put_unaligned_le64(
  287. ieee80211_calculate_rx_timestamp(local, status,
  288. mpdulen, 0),
  289. pos);
  290. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  291. pos += 8;
  292. }
  293. /* IEEE80211_RADIOTAP_FLAGS */
  294. if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
  295. *pos |= IEEE80211_RADIOTAP_F_FCS;
  296. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  297. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  298. if (status->enc_flags & RX_ENC_FLAG_SHORTPRE)
  299. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  300. pos++;
  301. /* IEEE80211_RADIOTAP_RATE */
  302. if (!rate || status->encoding != RX_ENC_LEGACY) {
  303. /*
  304. * Without rate information don't add it. If we have,
  305. * MCS information is a separate field in radiotap,
  306. * added below. The byte here is needed as padding
  307. * for the channel though, so initialise it to 0.
  308. */
  309. *pos = 0;
  310. } else {
  311. int shift = 0;
  312. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  313. if (status->bw == RATE_INFO_BW_10)
  314. shift = 1;
  315. else if (status->bw == RATE_INFO_BW_5)
  316. shift = 2;
  317. *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
  318. }
  319. pos++;
  320. /* IEEE80211_RADIOTAP_CHANNEL */
  321. put_unaligned_le16(status->freq, pos);
  322. pos += 2;
  323. if (status->bw == RATE_INFO_BW_10)
  324. channel_flags |= IEEE80211_CHAN_HALF;
  325. else if (status->bw == RATE_INFO_BW_5)
  326. channel_flags |= IEEE80211_CHAN_QUARTER;
  327. if (status->band == NL80211_BAND_5GHZ)
  328. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
  329. else if (status->encoding != RX_ENC_LEGACY)
  330. channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
  331. else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
  332. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
  333. else if (rate)
  334. channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
  335. else
  336. channel_flags |= IEEE80211_CHAN_2GHZ;
  337. put_unaligned_le16(channel_flags, pos);
  338. pos += 2;
  339. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  340. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
  341. !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  342. *pos = status->signal;
  343. rthdr->it_present |=
  344. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  345. pos++;
  346. }
  347. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  348. if (!status->chains) {
  349. /* IEEE80211_RADIOTAP_ANTENNA */
  350. *pos = status->antenna;
  351. pos++;
  352. }
  353. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  354. /* IEEE80211_RADIOTAP_RX_FLAGS */
  355. /* ensure 2 byte alignment for the 2 byte field as required */
  356. if ((pos - (u8 *)rthdr) & 1)
  357. *pos++ = 0;
  358. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  359. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  360. put_unaligned_le16(rx_flags, pos);
  361. pos += 2;
  362. if (status->encoding == RX_ENC_HT) {
  363. unsigned int stbc;
  364. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  365. *pos++ = local->hw.radiotap_mcs_details;
  366. *pos = 0;
  367. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  368. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  369. if (status->bw == RATE_INFO_BW_40)
  370. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  371. if (status->enc_flags & RX_ENC_FLAG_HT_GF)
  372. *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
  373. if (status->enc_flags & RX_ENC_FLAG_LDPC)
  374. *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
  375. stbc = (status->enc_flags & RX_ENC_FLAG_STBC_MASK) >> RX_ENC_FLAG_STBC_SHIFT;
  376. *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
  377. pos++;
  378. *pos++ = status->rate_idx;
  379. }
  380. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  381. u16 flags = 0;
  382. /* ensure 4 byte alignment */
  383. while ((pos - (u8 *)rthdr) & 3)
  384. pos++;
  385. rthdr->it_present |=
  386. cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
  387. put_unaligned_le32(status->ampdu_reference, pos);
  388. pos += 4;
  389. if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
  390. flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
  391. if (status->flag & RX_FLAG_AMPDU_IS_LAST)
  392. flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
  393. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
  394. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
  395. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  396. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
  397. if (status->flag & RX_FLAG_AMPDU_EOF_BIT_KNOWN)
  398. flags |= IEEE80211_RADIOTAP_AMPDU_EOF_KNOWN;
  399. if (status->flag & RX_FLAG_AMPDU_EOF_BIT)
  400. flags |= IEEE80211_RADIOTAP_AMPDU_EOF;
  401. put_unaligned_le16(flags, pos);
  402. pos += 2;
  403. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  404. *pos++ = status->ampdu_delimiter_crc;
  405. else
  406. *pos++ = 0;
  407. *pos++ = 0;
  408. }
  409. if (status->encoding == RX_ENC_VHT) {
  410. u16 known = local->hw.radiotap_vht_details;
  411. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
  412. put_unaligned_le16(known, pos);
  413. pos += 2;
  414. /* flags */
  415. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  416. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
  417. /* in VHT, STBC is binary */
  418. if (status->enc_flags & RX_ENC_FLAG_STBC_MASK)
  419. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
  420. if (status->enc_flags & RX_ENC_FLAG_BF)
  421. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
  422. pos++;
  423. /* bandwidth */
  424. switch (status->bw) {
  425. case RATE_INFO_BW_80:
  426. *pos++ = 4;
  427. break;
  428. case RATE_INFO_BW_160:
  429. *pos++ = 11;
  430. break;
  431. case RATE_INFO_BW_40:
  432. *pos++ = 1;
  433. break;
  434. default:
  435. *pos++ = 0;
  436. }
  437. /* MCS/NSS */
  438. *pos = (status->rate_idx << 4) | status->nss;
  439. pos += 4;
  440. /* coding field */
  441. if (status->enc_flags & RX_ENC_FLAG_LDPC)
  442. *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
  443. pos++;
  444. /* group ID */
  445. pos++;
  446. /* partial_aid */
  447. pos += 2;
  448. }
  449. if (local->hw.radiotap_timestamp.units_pos >= 0) {
  450. u16 accuracy = 0;
  451. u8 flags = IEEE80211_RADIOTAP_TIMESTAMP_FLAG_32BIT;
  452. rthdr->it_present |=
  453. cpu_to_le32(1 << IEEE80211_RADIOTAP_TIMESTAMP);
  454. /* ensure 8 byte alignment */
  455. while ((pos - (u8 *)rthdr) & 7)
  456. pos++;
  457. put_unaligned_le64(status->device_timestamp, pos);
  458. pos += sizeof(u64);
  459. if (local->hw.radiotap_timestamp.accuracy >= 0) {
  460. accuracy = local->hw.radiotap_timestamp.accuracy;
  461. flags |= IEEE80211_RADIOTAP_TIMESTAMP_FLAG_ACCURACY;
  462. }
  463. put_unaligned_le16(accuracy, pos);
  464. pos += sizeof(u16);
  465. *pos++ = local->hw.radiotap_timestamp.units_pos;
  466. *pos++ = flags;
  467. }
  468. if (status->encoding == RX_ENC_HE &&
  469. status->flag & RX_FLAG_RADIOTAP_HE) {
  470. #define HE_PREP(f, val) cpu_to_le16(FIELD_PREP(IEEE80211_RADIOTAP_HE_##f, val))
  471. if (status->enc_flags & RX_ENC_FLAG_STBC_MASK) {
  472. he.data6 |= HE_PREP(DATA6_NSTS,
  473. FIELD_GET(RX_ENC_FLAG_STBC_MASK,
  474. status->enc_flags));
  475. he.data3 |= HE_PREP(DATA3_STBC, 1);
  476. } else {
  477. he.data6 |= HE_PREP(DATA6_NSTS, status->nss);
  478. }
  479. #define CHECK_GI(s) \
  480. BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_GI_##s != \
  481. (int)NL80211_RATE_INFO_HE_GI_##s)
  482. CHECK_GI(0_8);
  483. CHECK_GI(1_6);
  484. CHECK_GI(3_2);
  485. he.data3 |= HE_PREP(DATA3_DATA_MCS, status->rate_idx);
  486. he.data3 |= HE_PREP(DATA3_DATA_DCM, status->he_dcm);
  487. he.data3 |= HE_PREP(DATA3_CODING,
  488. !!(status->enc_flags & RX_ENC_FLAG_LDPC));
  489. he.data5 |= HE_PREP(DATA5_GI, status->he_gi);
  490. switch (status->bw) {
  491. case RATE_INFO_BW_20:
  492. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  493. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_20MHZ);
  494. break;
  495. case RATE_INFO_BW_40:
  496. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  497. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_40MHZ);
  498. break;
  499. case RATE_INFO_BW_80:
  500. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  501. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_80MHZ);
  502. break;
  503. case RATE_INFO_BW_160:
  504. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  505. IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_160MHZ);
  506. break;
  507. case RATE_INFO_BW_HE_RU:
  508. #define CHECK_RU_ALLOC(s) \
  509. BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA5_DATA_BW_RU_ALLOC_##s##T != \
  510. NL80211_RATE_INFO_HE_RU_ALLOC_##s + 4)
  511. CHECK_RU_ALLOC(26);
  512. CHECK_RU_ALLOC(52);
  513. CHECK_RU_ALLOC(106);
  514. CHECK_RU_ALLOC(242);
  515. CHECK_RU_ALLOC(484);
  516. CHECK_RU_ALLOC(996);
  517. CHECK_RU_ALLOC(2x996);
  518. he.data5 |= HE_PREP(DATA5_DATA_BW_RU_ALLOC,
  519. status->he_ru + 4);
  520. break;
  521. default:
  522. WARN_ONCE(1, "Invalid SU BW %d\n", status->bw);
  523. }
  524. /* ensure 2 byte alignment */
  525. while ((pos - (u8 *)rthdr) & 1)
  526. pos++;
  527. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE);
  528. memcpy(pos, &he, sizeof(he));
  529. pos += sizeof(he);
  530. }
  531. if (status->encoding == RX_ENC_HE &&
  532. status->flag & RX_FLAG_RADIOTAP_HE_MU) {
  533. /* ensure 2 byte alignment */
  534. while ((pos - (u8 *)rthdr) & 1)
  535. pos++;
  536. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_HE_MU);
  537. memcpy(pos, &he_mu, sizeof(he_mu));
  538. pos += sizeof(he_mu);
  539. }
  540. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  541. *pos++ = status->chain_signal[chain];
  542. *pos++ = chain;
  543. }
  544. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  545. /* ensure 2 byte alignment for the vendor field as required */
  546. if ((pos - (u8 *)rthdr) & 1)
  547. *pos++ = 0;
  548. *pos++ = rtap.oui[0];
  549. *pos++ = rtap.oui[1];
  550. *pos++ = rtap.oui[2];
  551. *pos++ = rtap.subns;
  552. put_unaligned_le16(rtap.len, pos);
  553. pos += 2;
  554. /* align the actual payload as requested */
  555. while ((pos - (u8 *)rthdr) & (rtap.align - 1))
  556. *pos++ = 0;
  557. /* data (and possible padding) already follows */
  558. }
  559. }
  560. static struct sk_buff *
  561. ieee80211_make_monitor_skb(struct ieee80211_local *local,
  562. struct sk_buff **origskb,
  563. struct ieee80211_rate *rate,
  564. int rtap_space, bool use_origskb)
  565. {
  566. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(*origskb);
  567. int rt_hdrlen, needed_headroom;
  568. struct sk_buff *skb;
  569. /* room for the radiotap header based on driver features */
  570. rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, *origskb);
  571. needed_headroom = rt_hdrlen - rtap_space;
  572. if (use_origskb) {
  573. /* only need to expand headroom if necessary */
  574. skb = *origskb;
  575. *origskb = NULL;
  576. /*
  577. * This shouldn't trigger often because most devices have an
  578. * RX header they pull before we get here, and that should
  579. * be big enough for our radiotap information. We should
  580. * probably export the length to drivers so that we can have
  581. * them allocate enough headroom to start with.
  582. */
  583. if (skb_headroom(skb) < needed_headroom &&
  584. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  585. dev_kfree_skb(skb);
  586. return NULL;
  587. }
  588. } else {
  589. /*
  590. * Need to make a copy and possibly remove radiotap header
  591. * and FCS from the original.
  592. */
  593. skb = skb_copy_expand(*origskb, needed_headroom, 0, GFP_ATOMIC);
  594. if (!skb)
  595. return NULL;
  596. }
  597. /* prepend radiotap information */
  598. ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
  599. skb_reset_mac_header(skb);
  600. skb->ip_summed = CHECKSUM_UNNECESSARY;
  601. skb->pkt_type = PACKET_OTHERHOST;
  602. skb->protocol = htons(ETH_P_802_2);
  603. return skb;
  604. }
  605. /*
  606. * This function copies a received frame to all monitor interfaces and
  607. * returns a cleaned-up SKB that no longer includes the FCS nor the
  608. * radiotap header the driver might have added.
  609. */
  610. static struct sk_buff *
  611. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  612. struct ieee80211_rate *rate)
  613. {
  614. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  615. struct ieee80211_sub_if_data *sdata;
  616. struct sk_buff *monskb = NULL;
  617. int present_fcs_len = 0;
  618. unsigned int rtap_space = 0;
  619. struct ieee80211_sub_if_data *monitor_sdata =
  620. rcu_dereference(local->monitor_sdata);
  621. bool only_monitor = false;
  622. if (status->flag & RX_FLAG_RADIOTAP_HE)
  623. rtap_space += sizeof(struct ieee80211_radiotap_he);
  624. if (status->flag & RX_FLAG_RADIOTAP_HE_MU)
  625. rtap_space += sizeof(struct ieee80211_radiotap_he_mu);
  626. if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
  627. struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
  628. rtap_space += sizeof(*rtap) + rtap->len + rtap->pad;
  629. }
  630. /*
  631. * First, we may need to make a copy of the skb because
  632. * (1) we need to modify it for radiotap (if not present), and
  633. * (2) the other RX handlers will modify the skb we got.
  634. *
  635. * We don't need to, of course, if we aren't going to return
  636. * the SKB because it has a bad FCS/PLCP checksum.
  637. */
  638. if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
  639. if (unlikely(origskb->len <= FCS_LEN)) {
  640. /* driver bug */
  641. WARN_ON(1);
  642. dev_kfree_skb(origskb);
  643. return NULL;
  644. }
  645. present_fcs_len = FCS_LEN;
  646. }
  647. /* ensure hdr->frame_control and vendor radiotap data are in skb head */
  648. if (!pskb_may_pull(origskb, 2 + rtap_space)) {
  649. dev_kfree_skb(origskb);
  650. return NULL;
  651. }
  652. only_monitor = should_drop_frame(origskb, present_fcs_len, rtap_space);
  653. if (!local->monitors || (status->flag & RX_FLAG_SKIP_MONITOR)) {
  654. if (only_monitor) {
  655. dev_kfree_skb(origskb);
  656. return NULL;
  657. }
  658. remove_monitor_info(origskb, present_fcs_len, rtap_space);
  659. return origskb;
  660. }
  661. ieee80211_handle_mu_mimo_mon(monitor_sdata, origskb, rtap_space);
  662. list_for_each_entry_rcu(sdata, &local->mon_list, u.mntr.list) {
  663. bool last_monitor = list_is_last(&sdata->u.mntr.list,
  664. &local->mon_list);
  665. if (!monskb)
  666. monskb = ieee80211_make_monitor_skb(local, &origskb,
  667. rate, rtap_space,
  668. only_monitor &&
  669. last_monitor);
  670. if (monskb) {
  671. struct sk_buff *skb;
  672. if (last_monitor) {
  673. skb = monskb;
  674. monskb = NULL;
  675. } else {
  676. skb = skb_clone(monskb, GFP_ATOMIC);
  677. }
  678. if (skb) {
  679. skb->dev = sdata->dev;
  680. ieee80211_rx_stats(skb->dev, skb->len);
  681. netif_receive_skb(skb);
  682. }
  683. }
  684. if (last_monitor)
  685. break;
  686. }
  687. /* this happens if last_monitor was erroneously false */
  688. dev_kfree_skb(monskb);
  689. /* ditto */
  690. if (!origskb)
  691. return NULL;
  692. remove_monitor_info(origskb, present_fcs_len, rtap_space);
  693. return origskb;
  694. }
  695. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  696. {
  697. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  698. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  699. int tid, seqno_idx, security_idx;
  700. /* does the frame have a qos control field? */
  701. if (ieee80211_is_data_qos(hdr->frame_control)) {
  702. u8 *qc = ieee80211_get_qos_ctl(hdr);
  703. /* frame has qos control */
  704. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  705. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  706. status->rx_flags |= IEEE80211_RX_AMSDU;
  707. seqno_idx = tid;
  708. security_idx = tid;
  709. } else {
  710. /*
  711. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  712. *
  713. * Sequence numbers for management frames, QoS data
  714. * frames with a broadcast/multicast address in the
  715. * Address 1 field, and all non-QoS data frames sent
  716. * by QoS STAs are assigned using an additional single
  717. * modulo-4096 counter, [...]
  718. *
  719. * We also use that counter for non-QoS STAs.
  720. */
  721. seqno_idx = IEEE80211_NUM_TIDS;
  722. security_idx = 0;
  723. if (ieee80211_is_mgmt(hdr->frame_control))
  724. security_idx = IEEE80211_NUM_TIDS;
  725. tid = 0;
  726. }
  727. rx->seqno_idx = seqno_idx;
  728. rx->security_idx = security_idx;
  729. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  730. * For now, set skb->priority to 0 for other cases. */
  731. rx->skb->priority = (tid > 7) ? 0 : tid;
  732. }
  733. /**
  734. * DOC: Packet alignment
  735. *
  736. * Drivers always need to pass packets that are aligned to two-byte boundaries
  737. * to the stack.
  738. *
  739. * Additionally, should, if possible, align the payload data in a way that
  740. * guarantees that the contained IP header is aligned to a four-byte
  741. * boundary. In the case of regular frames, this simply means aligning the
  742. * payload to a four-byte boundary (because either the IP header is directly
  743. * contained, or IV/RFC1042 headers that have a length divisible by four are
  744. * in front of it). If the payload data is not properly aligned and the
  745. * architecture doesn't support efficient unaligned operations, mac80211
  746. * will align the data.
  747. *
  748. * With A-MSDU frames, however, the payload data address must yield two modulo
  749. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  750. * push the IP header further back to a multiple of four again. Thankfully, the
  751. * specs were sane enough this time around to require padding each A-MSDU
  752. * subframe to a length that is a multiple of four.
  753. *
  754. * Padding like Atheros hardware adds which is between the 802.11 header and
  755. * the payload is not supported, the driver is required to move the 802.11
  756. * header to be directly in front of the payload in that case.
  757. */
  758. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  759. {
  760. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  761. WARN_ON_ONCE((unsigned long)rx->skb->data & 1);
  762. #endif
  763. }
  764. /* rx handlers */
  765. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  766. {
  767. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  768. if (is_multicast_ether_addr(hdr->addr1))
  769. return 0;
  770. return ieee80211_is_robust_mgmt_frame(skb);
  771. }
  772. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  773. {
  774. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  775. if (!is_multicast_ether_addr(hdr->addr1))
  776. return 0;
  777. return ieee80211_is_robust_mgmt_frame(skb);
  778. }
  779. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  780. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  781. {
  782. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  783. struct ieee80211_mmie *mmie;
  784. struct ieee80211_mmie_16 *mmie16;
  785. if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
  786. return -1;
  787. if (!ieee80211_is_robust_mgmt_frame(skb))
  788. return -1; /* not a robust management frame */
  789. mmie = (struct ieee80211_mmie *)
  790. (skb->data + skb->len - sizeof(*mmie));
  791. if (mmie->element_id == WLAN_EID_MMIE &&
  792. mmie->length == sizeof(*mmie) - 2)
  793. return le16_to_cpu(mmie->key_id);
  794. mmie16 = (struct ieee80211_mmie_16 *)
  795. (skb->data + skb->len - sizeof(*mmie16));
  796. if (skb->len >= 24 + sizeof(*mmie16) &&
  797. mmie16->element_id == WLAN_EID_MMIE &&
  798. mmie16->length == sizeof(*mmie16) - 2)
  799. return le16_to_cpu(mmie16->key_id);
  800. return -1;
  801. }
  802. static int ieee80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
  803. struct sk_buff *skb)
  804. {
  805. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  806. __le16 fc;
  807. int hdrlen;
  808. u8 keyid;
  809. fc = hdr->frame_control;
  810. hdrlen = ieee80211_hdrlen(fc);
  811. if (skb->len < hdrlen + cs->hdr_len)
  812. return -EINVAL;
  813. skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
  814. keyid &= cs->key_idx_mask;
  815. keyid >>= cs->key_idx_shift;
  816. return keyid;
  817. }
  818. static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  819. {
  820. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  821. char *dev_addr = rx->sdata->vif.addr;
  822. if (ieee80211_is_data(hdr->frame_control)) {
  823. if (is_multicast_ether_addr(hdr->addr1)) {
  824. if (ieee80211_has_tods(hdr->frame_control) ||
  825. !ieee80211_has_fromds(hdr->frame_control))
  826. return RX_DROP_MONITOR;
  827. if (ether_addr_equal(hdr->addr3, dev_addr))
  828. return RX_DROP_MONITOR;
  829. } else {
  830. if (!ieee80211_has_a4(hdr->frame_control))
  831. return RX_DROP_MONITOR;
  832. if (ether_addr_equal(hdr->addr4, dev_addr))
  833. return RX_DROP_MONITOR;
  834. }
  835. }
  836. /* If there is not an established peer link and this is not a peer link
  837. * establisment frame, beacon or probe, drop the frame.
  838. */
  839. if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
  840. struct ieee80211_mgmt *mgmt;
  841. if (!ieee80211_is_mgmt(hdr->frame_control))
  842. return RX_DROP_MONITOR;
  843. if (ieee80211_is_action(hdr->frame_control)) {
  844. u8 category;
  845. /* make sure category field is present */
  846. if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
  847. return RX_DROP_MONITOR;
  848. mgmt = (struct ieee80211_mgmt *)hdr;
  849. category = mgmt->u.action.category;
  850. if (category != WLAN_CATEGORY_MESH_ACTION &&
  851. category != WLAN_CATEGORY_SELF_PROTECTED)
  852. return RX_DROP_MONITOR;
  853. return RX_CONTINUE;
  854. }
  855. if (ieee80211_is_probe_req(hdr->frame_control) ||
  856. ieee80211_is_probe_resp(hdr->frame_control) ||
  857. ieee80211_is_beacon(hdr->frame_control) ||
  858. ieee80211_is_auth(hdr->frame_control))
  859. return RX_CONTINUE;
  860. return RX_DROP_MONITOR;
  861. }
  862. return RX_CONTINUE;
  863. }
  864. static inline bool ieee80211_rx_reorder_ready(struct tid_ampdu_rx *tid_agg_rx,
  865. int index)
  866. {
  867. struct sk_buff_head *frames = &tid_agg_rx->reorder_buf[index];
  868. struct sk_buff *tail = skb_peek_tail(frames);
  869. struct ieee80211_rx_status *status;
  870. if (tid_agg_rx->reorder_buf_filtered & BIT_ULL(index))
  871. return true;
  872. if (!tail)
  873. return false;
  874. status = IEEE80211_SKB_RXCB(tail);
  875. if (status->flag & RX_FLAG_AMSDU_MORE)
  876. return false;
  877. return true;
  878. }
  879. static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
  880. struct tid_ampdu_rx *tid_agg_rx,
  881. int index,
  882. struct sk_buff_head *frames)
  883. {
  884. struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
  885. struct sk_buff *skb;
  886. struct ieee80211_rx_status *status;
  887. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  888. if (skb_queue_empty(skb_list))
  889. goto no_frame;
  890. if (!ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  891. __skb_queue_purge(skb_list);
  892. goto no_frame;
  893. }
  894. /* release frames from the reorder ring buffer */
  895. tid_agg_rx->stored_mpdu_num--;
  896. while ((skb = __skb_dequeue(skb_list))) {
  897. status = IEEE80211_SKB_RXCB(skb);
  898. status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
  899. __skb_queue_tail(frames, skb);
  900. }
  901. no_frame:
  902. tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
  903. tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  904. }
  905. static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
  906. struct tid_ampdu_rx *tid_agg_rx,
  907. u16 head_seq_num,
  908. struct sk_buff_head *frames)
  909. {
  910. int index;
  911. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  912. while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  913. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  914. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  915. frames);
  916. }
  917. }
  918. /*
  919. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  920. * the skb was added to the buffer longer than this time ago, the earlier
  921. * frames that have not yet been received are assumed to be lost and the skb
  922. * can be released for processing. This may also release other skb's from the
  923. * reorder buffer if there are no additional gaps between the frames.
  924. *
  925. * Callers must hold tid_agg_rx->reorder_lock.
  926. */
  927. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  928. static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
  929. struct tid_ampdu_rx *tid_agg_rx,
  930. struct sk_buff_head *frames)
  931. {
  932. int index, i, j;
  933. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  934. /* release the buffer until next missing frame */
  935. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  936. if (!ieee80211_rx_reorder_ready(tid_agg_rx, index) &&
  937. tid_agg_rx->stored_mpdu_num) {
  938. /*
  939. * No buffers ready to be released, but check whether any
  940. * frames in the reorder buffer have timed out.
  941. */
  942. int skipped = 1;
  943. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  944. j = (j + 1) % tid_agg_rx->buf_size) {
  945. if (!ieee80211_rx_reorder_ready(tid_agg_rx, j)) {
  946. skipped++;
  947. continue;
  948. }
  949. if (skipped &&
  950. !time_after(jiffies, tid_agg_rx->reorder_time[j] +
  951. HT_RX_REORDER_BUF_TIMEOUT))
  952. goto set_release_timer;
  953. /* don't leave incomplete A-MSDUs around */
  954. for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
  955. i = (i + 1) % tid_agg_rx->buf_size)
  956. __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
  957. ht_dbg_ratelimited(sdata,
  958. "release an RX reorder frame due to timeout on earlier frames\n");
  959. ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
  960. frames);
  961. /*
  962. * Increment the head seq# also for the skipped slots.
  963. */
  964. tid_agg_rx->head_seq_num =
  965. (tid_agg_rx->head_seq_num +
  966. skipped) & IEEE80211_SN_MASK;
  967. skipped = 0;
  968. }
  969. } else while (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  970. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  971. frames);
  972. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  973. }
  974. if (tid_agg_rx->stored_mpdu_num) {
  975. j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  976. for (; j != (index - 1) % tid_agg_rx->buf_size;
  977. j = (j + 1) % tid_agg_rx->buf_size) {
  978. if (ieee80211_rx_reorder_ready(tid_agg_rx, j))
  979. break;
  980. }
  981. set_release_timer:
  982. if (!tid_agg_rx->removed)
  983. mod_timer(&tid_agg_rx->reorder_timer,
  984. tid_agg_rx->reorder_time[j] + 1 +
  985. HT_RX_REORDER_BUF_TIMEOUT);
  986. } else {
  987. del_timer(&tid_agg_rx->reorder_timer);
  988. }
  989. }
  990. /*
  991. * As this function belongs to the RX path it must be under
  992. * rcu_read_lock protection. It returns false if the frame
  993. * can be processed immediately, true if it was consumed.
  994. */
  995. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
  996. struct tid_ampdu_rx *tid_agg_rx,
  997. struct sk_buff *skb,
  998. struct sk_buff_head *frames)
  999. {
  1000. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1001. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1002. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  1003. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1004. u16 head_seq_num, buf_size;
  1005. int index;
  1006. bool ret = true;
  1007. spin_lock(&tid_agg_rx->reorder_lock);
  1008. /*
  1009. * Offloaded BA sessions have no known starting sequence number so pick
  1010. * one from first Rxed frame for this tid after BA was started.
  1011. */
  1012. if (unlikely(tid_agg_rx->auto_seq)) {
  1013. tid_agg_rx->auto_seq = false;
  1014. tid_agg_rx->ssn = mpdu_seq_num;
  1015. tid_agg_rx->head_seq_num = mpdu_seq_num;
  1016. }
  1017. buf_size = tid_agg_rx->buf_size;
  1018. head_seq_num = tid_agg_rx->head_seq_num;
  1019. /*
  1020. * If the current MPDU's SN is smaller than the SSN, it shouldn't
  1021. * be reordered.
  1022. */
  1023. if (unlikely(!tid_agg_rx->started)) {
  1024. if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
  1025. ret = false;
  1026. goto out;
  1027. }
  1028. tid_agg_rx->started = true;
  1029. }
  1030. /* frame with out of date sequence number */
  1031. if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
  1032. dev_kfree_skb(skb);
  1033. goto out;
  1034. }
  1035. /*
  1036. * If frame the sequence number exceeds our buffering window
  1037. * size release some previous frames to make room for this one.
  1038. */
  1039. if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
  1040. head_seq_num = ieee80211_sn_inc(
  1041. ieee80211_sn_sub(mpdu_seq_num, buf_size));
  1042. /* release stored frames up to new head to stack */
  1043. ieee80211_release_reorder_frames(sdata, tid_agg_rx,
  1044. head_seq_num, frames);
  1045. }
  1046. /* Now the new frame is always in the range of the reordering buffer */
  1047. index = mpdu_seq_num % tid_agg_rx->buf_size;
  1048. /* check if we already stored this frame */
  1049. if (ieee80211_rx_reorder_ready(tid_agg_rx, index)) {
  1050. dev_kfree_skb(skb);
  1051. goto out;
  1052. }
  1053. /*
  1054. * If the current MPDU is in the right order and nothing else
  1055. * is stored we can process it directly, no need to buffer it.
  1056. * If it is first but there's something stored, we may be able
  1057. * to release frames after this one.
  1058. */
  1059. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1060. tid_agg_rx->stored_mpdu_num == 0) {
  1061. if (!(status->flag & RX_FLAG_AMSDU_MORE))
  1062. tid_agg_rx->head_seq_num =
  1063. ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  1064. ret = false;
  1065. goto out;
  1066. }
  1067. /* put the frame in the reordering buffer */
  1068. __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
  1069. if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  1070. tid_agg_rx->reorder_time[index] = jiffies;
  1071. tid_agg_rx->stored_mpdu_num++;
  1072. ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
  1073. }
  1074. out:
  1075. spin_unlock(&tid_agg_rx->reorder_lock);
  1076. return ret;
  1077. }
  1078. /*
  1079. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  1080. * true if the MPDU was buffered, false if it should be processed.
  1081. */
  1082. static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
  1083. struct sk_buff_head *frames)
  1084. {
  1085. struct sk_buff *skb = rx->skb;
  1086. struct ieee80211_local *local = rx->local;
  1087. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1088. struct sta_info *sta = rx->sta;
  1089. struct tid_ampdu_rx *tid_agg_rx;
  1090. u16 sc;
  1091. u8 tid, ack_policy;
  1092. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  1093. is_multicast_ether_addr(hdr->addr1))
  1094. goto dont_reorder;
  1095. /*
  1096. * filter the QoS data rx stream according to
  1097. * STA/TID and check if this STA/TID is on aggregation
  1098. */
  1099. if (!sta)
  1100. goto dont_reorder;
  1101. ack_policy = *ieee80211_get_qos_ctl(hdr) &
  1102. IEEE80211_QOS_CTL_ACK_POLICY_MASK;
  1103. tid = ieee80211_get_tid(hdr);
  1104. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  1105. if (!tid_agg_rx) {
  1106. if (ack_policy == IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  1107. !test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
  1108. !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
  1109. ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
  1110. WLAN_BACK_RECIPIENT,
  1111. WLAN_REASON_QSTA_REQUIRE_SETUP);
  1112. goto dont_reorder;
  1113. }
  1114. /* qos null data frames are excluded */
  1115. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  1116. goto dont_reorder;
  1117. /* not part of a BA session */
  1118. if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  1119. ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
  1120. goto dont_reorder;
  1121. /* new, potentially un-ordered, ampdu frame - process it */
  1122. /* reset session timer */
  1123. if (tid_agg_rx->timeout)
  1124. tid_agg_rx->last_rx = jiffies;
  1125. /* if this mpdu is fragmented - terminate rx aggregation session */
  1126. sc = le16_to_cpu(hdr->seq_ctrl);
  1127. if (sc & IEEE80211_SCTL_FRAG) {
  1128. skb_queue_tail(&rx->sdata->skb_queue, skb);
  1129. ieee80211_queue_work(&local->hw, &rx->sdata->work);
  1130. return;
  1131. }
  1132. /*
  1133. * No locking needed -- we will only ever process one
  1134. * RX packet at a time, and thus own tid_agg_rx. All
  1135. * other code manipulating it needs to (and does) make
  1136. * sure that we cannot get to it any more before doing
  1137. * anything with it.
  1138. */
  1139. if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
  1140. frames))
  1141. return;
  1142. dont_reorder:
  1143. __skb_queue_tail(frames, skb);
  1144. }
  1145. static ieee80211_rx_result debug_noinline
  1146. ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
  1147. {
  1148. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1149. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1150. if (status->flag & RX_FLAG_DUP_VALIDATED)
  1151. return RX_CONTINUE;
  1152. /*
  1153. * Drop duplicate 802.11 retransmissions
  1154. * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
  1155. */
  1156. if (rx->skb->len < 24)
  1157. return RX_CONTINUE;
  1158. if (ieee80211_is_ctl(hdr->frame_control) ||
  1159. ieee80211_is_nullfunc(hdr->frame_control) ||
  1160. ieee80211_is_qos_nullfunc(hdr->frame_control) ||
  1161. is_multicast_ether_addr(hdr->addr1))
  1162. return RX_CONTINUE;
  1163. if (!rx->sta)
  1164. return RX_CONTINUE;
  1165. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  1166. rx->sta->last_seq_ctrl[rx->seqno_idx] == hdr->seq_ctrl)) {
  1167. I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
  1168. rx->sta->rx_stats.num_duplicates++;
  1169. return RX_DROP_UNUSABLE;
  1170. } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  1171. rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
  1172. }
  1173. return RX_CONTINUE;
  1174. }
  1175. static ieee80211_rx_result debug_noinline
  1176. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  1177. {
  1178. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1179. /* Drop disallowed frame classes based on STA auth/assoc state;
  1180. * IEEE 802.11, Chap 5.5.
  1181. *
  1182. * mac80211 filters only based on association state, i.e. it drops
  1183. * Class 3 frames from not associated stations. hostapd sends
  1184. * deauth/disassoc frames when needed. In addition, hostapd is
  1185. * responsible for filtering on both auth and assoc states.
  1186. */
  1187. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1188. return ieee80211_rx_mesh_check(rx);
  1189. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  1190. ieee80211_is_pspoll(hdr->frame_control)) &&
  1191. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1192. rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
  1193. rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
  1194. (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
  1195. /*
  1196. * accept port control frames from the AP even when it's not
  1197. * yet marked ASSOC to prevent a race where we don't set the
  1198. * assoc bit quickly enough before it sends the first frame
  1199. */
  1200. if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1201. ieee80211_is_data_present(hdr->frame_control)) {
  1202. unsigned int hdrlen;
  1203. __be16 ethertype;
  1204. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1205. if (rx->skb->len < hdrlen + 8)
  1206. return RX_DROP_MONITOR;
  1207. skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
  1208. if (ethertype == rx->sdata->control_port_protocol)
  1209. return RX_CONTINUE;
  1210. }
  1211. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  1212. cfg80211_rx_spurious_frame(rx->sdata->dev,
  1213. hdr->addr2,
  1214. GFP_ATOMIC))
  1215. return RX_DROP_UNUSABLE;
  1216. return RX_DROP_MONITOR;
  1217. }
  1218. return RX_CONTINUE;
  1219. }
  1220. static ieee80211_rx_result debug_noinline
  1221. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  1222. {
  1223. struct ieee80211_local *local;
  1224. struct ieee80211_hdr *hdr;
  1225. struct sk_buff *skb;
  1226. local = rx->local;
  1227. skb = rx->skb;
  1228. hdr = (struct ieee80211_hdr *) skb->data;
  1229. if (!local->pspolling)
  1230. return RX_CONTINUE;
  1231. if (!ieee80211_has_fromds(hdr->frame_control))
  1232. /* this is not from AP */
  1233. return RX_CONTINUE;
  1234. if (!ieee80211_is_data(hdr->frame_control))
  1235. return RX_CONTINUE;
  1236. if (!ieee80211_has_moredata(hdr->frame_control)) {
  1237. /* AP has no more frames buffered for us */
  1238. local->pspolling = false;
  1239. return RX_CONTINUE;
  1240. }
  1241. /* more data bit is set, let's request a new frame from the AP */
  1242. ieee80211_send_pspoll(local, rx->sdata);
  1243. return RX_CONTINUE;
  1244. }
  1245. static void sta_ps_start(struct sta_info *sta)
  1246. {
  1247. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1248. struct ieee80211_local *local = sdata->local;
  1249. struct ps_data *ps;
  1250. int tid;
  1251. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1252. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1253. ps = &sdata->bss->ps;
  1254. else
  1255. return;
  1256. atomic_inc(&ps->num_sta_ps);
  1257. set_sta_flag(sta, WLAN_STA_PS_STA);
  1258. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  1259. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  1260. ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
  1261. sta->sta.addr, sta->sta.aid);
  1262. ieee80211_clear_fast_xmit(sta);
  1263. if (!sta->sta.txq[0])
  1264. return;
  1265. for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
  1266. if (txq_has_queue(sta->sta.txq[tid]))
  1267. set_bit(tid, &sta->txq_buffered_tids);
  1268. else
  1269. clear_bit(tid, &sta->txq_buffered_tids);
  1270. }
  1271. }
  1272. static void sta_ps_end(struct sta_info *sta)
  1273. {
  1274. ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
  1275. sta->sta.addr, sta->sta.aid);
  1276. if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  1277. /*
  1278. * Clear the flag only if the other one is still set
  1279. * so that the TX path won't start TX'ing new frames
  1280. * directly ... In the case that the driver flag isn't
  1281. * set ieee80211_sta_ps_deliver_wakeup() will clear it.
  1282. */
  1283. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1284. ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
  1285. sta->sta.addr, sta->sta.aid);
  1286. return;
  1287. }
  1288. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1289. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1290. ieee80211_sta_ps_deliver_wakeup(sta);
  1291. }
  1292. int ieee80211_sta_ps_transition(struct ieee80211_sta *pubsta, bool start)
  1293. {
  1294. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1295. bool in_ps;
  1296. WARN_ON(!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS));
  1297. /* Don't let the same PS state be set twice */
  1298. in_ps = test_sta_flag(sta, WLAN_STA_PS_STA);
  1299. if ((start && in_ps) || (!start && !in_ps))
  1300. return -EINVAL;
  1301. if (start)
  1302. sta_ps_start(sta);
  1303. else
  1304. sta_ps_end(sta);
  1305. return 0;
  1306. }
  1307. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  1308. void ieee80211_sta_pspoll(struct ieee80211_sta *pubsta)
  1309. {
  1310. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1311. if (test_sta_flag(sta, WLAN_STA_SP))
  1312. return;
  1313. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1314. ieee80211_sta_ps_deliver_poll_response(sta);
  1315. else
  1316. set_sta_flag(sta, WLAN_STA_PSPOLL);
  1317. }
  1318. EXPORT_SYMBOL(ieee80211_sta_pspoll);
  1319. void ieee80211_sta_uapsd_trigger(struct ieee80211_sta *pubsta, u8 tid)
  1320. {
  1321. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1322. int ac = ieee80211_ac_from_tid(tid);
  1323. /*
  1324. * If this AC is not trigger-enabled do nothing unless the
  1325. * driver is calling us after it already checked.
  1326. *
  1327. * NB: This could/should check a separate bitmap of trigger-
  1328. * enabled queues, but for now we only implement uAPSD w/o
  1329. * TSPEC changes to the ACs, so they're always the same.
  1330. */
  1331. if (!(sta->sta.uapsd_queues & ieee80211_ac_to_qos_mask[ac]) &&
  1332. tid != IEEE80211_NUM_TIDS)
  1333. return;
  1334. /* if we are in a service period, do nothing */
  1335. if (test_sta_flag(sta, WLAN_STA_SP))
  1336. return;
  1337. if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1338. ieee80211_sta_ps_deliver_uapsd(sta);
  1339. else
  1340. set_sta_flag(sta, WLAN_STA_UAPSD);
  1341. }
  1342. EXPORT_SYMBOL(ieee80211_sta_uapsd_trigger);
  1343. static ieee80211_rx_result debug_noinline
  1344. ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
  1345. {
  1346. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1347. struct ieee80211_hdr *hdr = (void *)rx->skb->data;
  1348. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1349. if (!rx->sta)
  1350. return RX_CONTINUE;
  1351. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1352. sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1353. return RX_CONTINUE;
  1354. /*
  1355. * The device handles station powersave, so don't do anything about
  1356. * uAPSD and PS-Poll frames (the latter shouldn't even come up from
  1357. * it to mac80211 since they're handled.)
  1358. */
  1359. if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
  1360. return RX_CONTINUE;
  1361. /*
  1362. * Don't do anything if the station isn't already asleep. In
  1363. * the uAPSD case, the station will probably be marked asleep,
  1364. * in the PS-Poll case the station must be confused ...
  1365. */
  1366. if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
  1367. return RX_CONTINUE;
  1368. if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
  1369. ieee80211_sta_pspoll(&rx->sta->sta);
  1370. /* Free PS Poll skb here instead of returning RX_DROP that would
  1371. * count as an dropped frame. */
  1372. dev_kfree_skb(rx->skb);
  1373. return RX_QUEUED;
  1374. } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1375. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1376. ieee80211_has_pm(hdr->frame_control) &&
  1377. (ieee80211_is_data_qos(hdr->frame_control) ||
  1378. ieee80211_is_qos_nullfunc(hdr->frame_control))) {
  1379. u8 tid = ieee80211_get_tid(hdr);
  1380. ieee80211_sta_uapsd_trigger(&rx->sta->sta, tid);
  1381. }
  1382. return RX_CONTINUE;
  1383. }
  1384. static ieee80211_rx_result debug_noinline
  1385. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  1386. {
  1387. struct sta_info *sta = rx->sta;
  1388. struct sk_buff *skb = rx->skb;
  1389. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1390. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1391. int i;
  1392. if (!sta)
  1393. return RX_CONTINUE;
  1394. /*
  1395. * Update last_rx only for IBSS packets which are for the current
  1396. * BSSID and for station already AUTHORIZED to avoid keeping the
  1397. * current IBSS network alive in cases where other STAs start
  1398. * using different BSSID. This will also give the station another
  1399. * chance to restart the authentication/authorization in case
  1400. * something went wrong the first time.
  1401. */
  1402. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1403. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1404. NL80211_IFTYPE_ADHOC);
  1405. if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
  1406. test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
  1407. sta->rx_stats.last_rx = jiffies;
  1408. if (ieee80211_is_data(hdr->frame_control) &&
  1409. !is_multicast_ether_addr(hdr->addr1))
  1410. sta->rx_stats.last_rate =
  1411. sta_stats_encode_rate(status);
  1412. }
  1413. } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
  1414. sta->rx_stats.last_rx = jiffies;
  1415. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1416. /*
  1417. * Mesh beacons will update last_rx when if they are found to
  1418. * match the current local configuration when processed.
  1419. */
  1420. sta->rx_stats.last_rx = jiffies;
  1421. if (ieee80211_is_data(hdr->frame_control))
  1422. sta->rx_stats.last_rate = sta_stats_encode_rate(status);
  1423. }
  1424. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1425. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1426. sta->rx_stats.fragments++;
  1427. u64_stats_update_begin(&rx->sta->rx_stats.syncp);
  1428. sta->rx_stats.bytes += rx->skb->len;
  1429. u64_stats_update_end(&rx->sta->rx_stats.syncp);
  1430. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  1431. sta->rx_stats.last_signal = status->signal;
  1432. ewma_signal_add(&sta->rx_stats_avg.signal, -status->signal);
  1433. }
  1434. if (status->chains) {
  1435. sta->rx_stats.chains = status->chains;
  1436. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  1437. int signal = status->chain_signal[i];
  1438. if (!(status->chains & BIT(i)))
  1439. continue;
  1440. sta->rx_stats.chain_signal_last[i] = signal;
  1441. ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
  1442. -signal);
  1443. }
  1444. }
  1445. /*
  1446. * Change STA power saving mode only at the end of a frame
  1447. * exchange sequence, and only for a data or management
  1448. * frame as specified in IEEE 802.11-2016 11.2.3.2
  1449. */
  1450. if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
  1451. !ieee80211_has_morefrags(hdr->frame_control) &&
  1452. !is_multicast_ether_addr(hdr->addr1) &&
  1453. (ieee80211_is_mgmt(hdr->frame_control) ||
  1454. ieee80211_is_data(hdr->frame_control)) &&
  1455. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1456. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1457. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  1458. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1459. if (!ieee80211_has_pm(hdr->frame_control))
  1460. sta_ps_end(sta);
  1461. } else {
  1462. if (ieee80211_has_pm(hdr->frame_control))
  1463. sta_ps_start(sta);
  1464. }
  1465. }
  1466. /* mesh power save support */
  1467. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1468. ieee80211_mps_rx_h_sta_process(sta, hdr);
  1469. /*
  1470. * Drop (qos-)data::nullfunc frames silently, since they
  1471. * are used only to control station power saving mode.
  1472. */
  1473. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1474. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1475. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1476. /*
  1477. * If we receive a 4-addr nullfunc frame from a STA
  1478. * that was not moved to a 4-addr STA vlan yet send
  1479. * the event to userspace and for older hostapd drop
  1480. * the frame to the monitor interface.
  1481. */
  1482. if (ieee80211_has_a4(hdr->frame_control) &&
  1483. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1484. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1485. !rx->sdata->u.vlan.sta))) {
  1486. if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
  1487. cfg80211_rx_unexpected_4addr_frame(
  1488. rx->sdata->dev, sta->sta.addr,
  1489. GFP_ATOMIC);
  1490. return RX_DROP_MONITOR;
  1491. }
  1492. /*
  1493. * Update counter and free packet here to avoid
  1494. * counting this as a dropped packed.
  1495. */
  1496. sta->rx_stats.packets++;
  1497. dev_kfree_skb(rx->skb);
  1498. return RX_QUEUED;
  1499. }
  1500. return RX_CONTINUE;
  1501. } /* ieee80211_rx_h_sta_process */
  1502. static ieee80211_rx_result debug_noinline
  1503. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  1504. {
  1505. struct sk_buff *skb = rx->skb;
  1506. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1507. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1508. int keyidx;
  1509. int hdrlen;
  1510. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  1511. struct ieee80211_key *sta_ptk = NULL;
  1512. int mmie_keyidx = -1;
  1513. __le16 fc;
  1514. const struct ieee80211_cipher_scheme *cs = NULL;
  1515. /*
  1516. * Key selection 101
  1517. *
  1518. * There are four types of keys:
  1519. * - GTK (group keys)
  1520. * - IGTK (group keys for management frames)
  1521. * - PTK (pairwise keys)
  1522. * - STK (station-to-station pairwise keys)
  1523. *
  1524. * When selecting a key, we have to distinguish between multicast
  1525. * (including broadcast) and unicast frames, the latter can only
  1526. * use PTKs and STKs while the former always use GTKs and IGTKs.
  1527. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  1528. * unicast frames can also use key indices like GTKs. Hence, if we
  1529. * don't have a PTK/STK we check the key index for a WEP key.
  1530. *
  1531. * Note that in a regular BSS, multicast frames are sent by the
  1532. * AP only, associated stations unicast the frame to the AP first
  1533. * which then multicasts it on their behalf.
  1534. *
  1535. * There is also a slight problem in IBSS mode: GTKs are negotiated
  1536. * with each station, that is something we don't currently handle.
  1537. * The spec seems to expect that one negotiates the same key with
  1538. * every station but there's no such requirement; VLANs could be
  1539. * possible.
  1540. */
  1541. /* start without a key */
  1542. rx->key = NULL;
  1543. fc = hdr->frame_control;
  1544. if (rx->sta) {
  1545. int keyid = rx->sta->ptk_idx;
  1546. if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
  1547. cs = rx->sta->cipher_scheme;
  1548. keyid = ieee80211_get_cs_keyid(cs, rx->skb);
  1549. if (unlikely(keyid < 0))
  1550. return RX_DROP_UNUSABLE;
  1551. }
  1552. sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
  1553. }
  1554. if (!ieee80211_has_protected(fc))
  1555. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  1556. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  1557. rx->key = sta_ptk;
  1558. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1559. (status->flag & RX_FLAG_IV_STRIPPED))
  1560. return RX_CONTINUE;
  1561. /* Skip decryption if the frame is not protected. */
  1562. if (!ieee80211_has_protected(fc))
  1563. return RX_CONTINUE;
  1564. } else if (mmie_keyidx >= 0) {
  1565. /* Broadcast/multicast robust management frame / BIP */
  1566. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1567. (status->flag & RX_FLAG_IV_STRIPPED))
  1568. return RX_CONTINUE;
  1569. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  1570. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  1571. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  1572. if (rx->sta) {
  1573. if (ieee80211_is_group_privacy_action(skb) &&
  1574. test_sta_flag(rx->sta, WLAN_STA_MFP))
  1575. return RX_DROP_MONITOR;
  1576. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  1577. }
  1578. if (!rx->key)
  1579. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  1580. } else if (!ieee80211_has_protected(fc)) {
  1581. /*
  1582. * The frame was not protected, so skip decryption. However, we
  1583. * need to set rx->key if there is a key that could have been
  1584. * used so that the frame may be dropped if encryption would
  1585. * have been expected.
  1586. */
  1587. struct ieee80211_key *key = NULL;
  1588. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1589. int i;
  1590. if (ieee80211_is_mgmt(fc) &&
  1591. is_multicast_ether_addr(hdr->addr1) &&
  1592. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  1593. rx->key = key;
  1594. else {
  1595. if (rx->sta) {
  1596. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1597. key = rcu_dereference(rx->sta->gtk[i]);
  1598. if (key)
  1599. break;
  1600. }
  1601. }
  1602. if (!key) {
  1603. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1604. key = rcu_dereference(sdata->keys[i]);
  1605. if (key)
  1606. break;
  1607. }
  1608. }
  1609. if (key)
  1610. rx->key = key;
  1611. }
  1612. return RX_CONTINUE;
  1613. } else {
  1614. u8 keyid;
  1615. /*
  1616. * The device doesn't give us the IV so we won't be
  1617. * able to look up the key. That's ok though, we
  1618. * don't need to decrypt the frame, we just won't
  1619. * be able to keep statistics accurate.
  1620. * Except for key threshold notifications, should
  1621. * we somehow allow the driver to tell us which key
  1622. * the hardware used if this flag is set?
  1623. */
  1624. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1625. (status->flag & RX_FLAG_IV_STRIPPED))
  1626. return RX_CONTINUE;
  1627. hdrlen = ieee80211_hdrlen(fc);
  1628. if (cs) {
  1629. keyidx = ieee80211_get_cs_keyid(cs, rx->skb);
  1630. if (unlikely(keyidx < 0))
  1631. return RX_DROP_UNUSABLE;
  1632. } else {
  1633. if (rx->skb->len < 8 + hdrlen)
  1634. return RX_DROP_UNUSABLE; /* TODO: count this? */
  1635. /*
  1636. * no need to call ieee80211_wep_get_keyidx,
  1637. * it verifies a bunch of things we've done already
  1638. */
  1639. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  1640. keyidx = keyid >> 6;
  1641. }
  1642. /* check per-station GTK first, if multicast packet */
  1643. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  1644. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  1645. /* if not found, try default key */
  1646. if (!rx->key) {
  1647. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  1648. /*
  1649. * RSNA-protected unicast frames should always be
  1650. * sent with pairwise or station-to-station keys,
  1651. * but for WEP we allow using a key index as well.
  1652. */
  1653. if (rx->key &&
  1654. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1655. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  1656. !is_multicast_ether_addr(hdr->addr1))
  1657. rx->key = NULL;
  1658. }
  1659. }
  1660. if (rx->key) {
  1661. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  1662. return RX_DROP_MONITOR;
  1663. /* TODO: add threshold stuff again */
  1664. } else {
  1665. return RX_DROP_MONITOR;
  1666. }
  1667. switch (rx->key->conf.cipher) {
  1668. case WLAN_CIPHER_SUITE_WEP40:
  1669. case WLAN_CIPHER_SUITE_WEP104:
  1670. result = ieee80211_crypto_wep_decrypt(rx);
  1671. break;
  1672. case WLAN_CIPHER_SUITE_TKIP:
  1673. result = ieee80211_crypto_tkip_decrypt(rx);
  1674. break;
  1675. case WLAN_CIPHER_SUITE_CCMP:
  1676. result = ieee80211_crypto_ccmp_decrypt(
  1677. rx, IEEE80211_CCMP_MIC_LEN);
  1678. break;
  1679. case WLAN_CIPHER_SUITE_CCMP_256:
  1680. result = ieee80211_crypto_ccmp_decrypt(
  1681. rx, IEEE80211_CCMP_256_MIC_LEN);
  1682. break;
  1683. case WLAN_CIPHER_SUITE_AES_CMAC:
  1684. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  1685. break;
  1686. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1687. result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
  1688. break;
  1689. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1690. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1691. result = ieee80211_crypto_aes_gmac_decrypt(rx);
  1692. break;
  1693. case WLAN_CIPHER_SUITE_GCMP:
  1694. case WLAN_CIPHER_SUITE_GCMP_256:
  1695. result = ieee80211_crypto_gcmp_decrypt(rx);
  1696. break;
  1697. default:
  1698. result = ieee80211_crypto_hw_decrypt(rx);
  1699. }
  1700. /* the hdr variable is invalid after the decrypt handlers */
  1701. /* either the frame has been decrypted or will be dropped */
  1702. status->flag |= RX_FLAG_DECRYPTED;
  1703. return result;
  1704. }
  1705. static inline struct ieee80211_fragment_entry *
  1706. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1707. unsigned int frag, unsigned int seq, int rx_queue,
  1708. struct sk_buff **skb)
  1709. {
  1710. struct ieee80211_fragment_entry *entry;
  1711. entry = &sdata->fragments[sdata->fragment_next++];
  1712. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1713. sdata->fragment_next = 0;
  1714. if (!skb_queue_empty(&entry->skb_list))
  1715. __skb_queue_purge(&entry->skb_list);
  1716. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1717. *skb = NULL;
  1718. entry->first_frag_time = jiffies;
  1719. entry->seq = seq;
  1720. entry->rx_queue = rx_queue;
  1721. entry->last_frag = frag;
  1722. entry->check_sequential_pn = false;
  1723. entry->extra_len = 0;
  1724. return entry;
  1725. }
  1726. static inline struct ieee80211_fragment_entry *
  1727. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1728. unsigned int frag, unsigned int seq,
  1729. int rx_queue, struct ieee80211_hdr *hdr)
  1730. {
  1731. struct ieee80211_fragment_entry *entry;
  1732. int i, idx;
  1733. idx = sdata->fragment_next;
  1734. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1735. struct ieee80211_hdr *f_hdr;
  1736. idx--;
  1737. if (idx < 0)
  1738. idx = IEEE80211_FRAGMENT_MAX - 1;
  1739. entry = &sdata->fragments[idx];
  1740. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1741. entry->rx_queue != rx_queue ||
  1742. entry->last_frag + 1 != frag)
  1743. continue;
  1744. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1745. /*
  1746. * Check ftype and addresses are equal, else check next fragment
  1747. */
  1748. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1749. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1750. !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
  1751. !ether_addr_equal(hdr->addr2, f_hdr->addr2))
  1752. continue;
  1753. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1754. __skb_queue_purge(&entry->skb_list);
  1755. continue;
  1756. }
  1757. return entry;
  1758. }
  1759. return NULL;
  1760. }
  1761. static ieee80211_rx_result debug_noinline
  1762. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1763. {
  1764. struct ieee80211_hdr *hdr;
  1765. u16 sc;
  1766. __le16 fc;
  1767. unsigned int frag, seq;
  1768. struct ieee80211_fragment_entry *entry;
  1769. struct sk_buff *skb;
  1770. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1771. fc = hdr->frame_control;
  1772. if (ieee80211_is_ctl(fc))
  1773. return RX_CONTINUE;
  1774. sc = le16_to_cpu(hdr->seq_ctrl);
  1775. frag = sc & IEEE80211_SCTL_FRAG;
  1776. if (is_multicast_ether_addr(hdr->addr1)) {
  1777. I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
  1778. goto out_no_led;
  1779. }
  1780. if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
  1781. goto out;
  1782. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1783. if (skb_linearize(rx->skb))
  1784. return RX_DROP_UNUSABLE;
  1785. /*
  1786. * skb_linearize() might change the skb->data and
  1787. * previously cached variables (in this case, hdr) need to
  1788. * be refreshed with the new data.
  1789. */
  1790. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1791. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1792. if (frag == 0) {
  1793. /* This is the first fragment of a new frame. */
  1794. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1795. rx->seqno_idx, &(rx->skb));
  1796. if (rx->key &&
  1797. (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
  1798. rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256 ||
  1799. rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP ||
  1800. rx->key->conf.cipher == WLAN_CIPHER_SUITE_GCMP_256) &&
  1801. ieee80211_has_protected(fc)) {
  1802. int queue = rx->security_idx;
  1803. /* Store CCMP/GCMP PN so that we can verify that the
  1804. * next fragment has a sequential PN value.
  1805. */
  1806. entry->check_sequential_pn = true;
  1807. memcpy(entry->last_pn,
  1808. rx->key->u.ccmp.rx_pn[queue],
  1809. IEEE80211_CCMP_PN_LEN);
  1810. BUILD_BUG_ON(offsetof(struct ieee80211_key,
  1811. u.ccmp.rx_pn) !=
  1812. offsetof(struct ieee80211_key,
  1813. u.gcmp.rx_pn));
  1814. BUILD_BUG_ON(sizeof(rx->key->u.ccmp.rx_pn[queue]) !=
  1815. sizeof(rx->key->u.gcmp.rx_pn[queue]));
  1816. BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN !=
  1817. IEEE80211_GCMP_PN_LEN);
  1818. }
  1819. return RX_QUEUED;
  1820. }
  1821. /* This is a fragment for a frame that should already be pending in
  1822. * fragment cache. Add this fragment to the end of the pending entry.
  1823. */
  1824. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1825. rx->seqno_idx, hdr);
  1826. if (!entry) {
  1827. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1828. return RX_DROP_MONITOR;
  1829. }
  1830. /* "The receiver shall discard MSDUs and MMPDUs whose constituent
  1831. * MPDU PN values are not incrementing in steps of 1."
  1832. * see IEEE P802.11-REVmc/D5.0, 12.5.3.4.4, item d (for CCMP)
  1833. * and IEEE P802.11-REVmc/D5.0, 12.5.5.4.4, item d (for GCMP)
  1834. */
  1835. if (entry->check_sequential_pn) {
  1836. int i;
  1837. u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
  1838. int queue;
  1839. if (!rx->key ||
  1840. (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
  1841. rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256 &&
  1842. rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP &&
  1843. rx->key->conf.cipher != WLAN_CIPHER_SUITE_GCMP_256))
  1844. return RX_DROP_UNUSABLE;
  1845. memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
  1846. for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
  1847. pn[i]++;
  1848. if (pn[i])
  1849. break;
  1850. }
  1851. queue = rx->security_idx;
  1852. rpn = rx->key->u.ccmp.rx_pn[queue];
  1853. if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
  1854. return RX_DROP_UNUSABLE;
  1855. memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
  1856. }
  1857. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1858. __skb_queue_tail(&entry->skb_list, rx->skb);
  1859. entry->last_frag = frag;
  1860. entry->extra_len += rx->skb->len;
  1861. if (ieee80211_has_morefrags(fc)) {
  1862. rx->skb = NULL;
  1863. return RX_QUEUED;
  1864. }
  1865. rx->skb = __skb_dequeue(&entry->skb_list);
  1866. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1867. I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
  1868. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1869. GFP_ATOMIC))) {
  1870. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1871. __skb_queue_purge(&entry->skb_list);
  1872. return RX_DROP_UNUSABLE;
  1873. }
  1874. }
  1875. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1876. skb_put_data(rx->skb, skb->data, skb->len);
  1877. dev_kfree_skb(skb);
  1878. }
  1879. out:
  1880. ieee80211_led_rx(rx->local);
  1881. out_no_led:
  1882. if (rx->sta)
  1883. rx->sta->rx_stats.packets++;
  1884. return RX_CONTINUE;
  1885. }
  1886. static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1887. {
  1888. if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1889. return -EACCES;
  1890. return 0;
  1891. }
  1892. static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1893. {
  1894. struct sk_buff *skb = rx->skb;
  1895. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1896. /*
  1897. * Pass through unencrypted frames if the hardware has
  1898. * decrypted them already.
  1899. */
  1900. if (status->flag & RX_FLAG_DECRYPTED)
  1901. return 0;
  1902. /* Drop unencrypted frames if key is set. */
  1903. if (unlikely(!ieee80211_has_protected(fc) &&
  1904. !ieee80211_is_nullfunc(fc) &&
  1905. ieee80211_is_data(fc) && rx->key))
  1906. return -EACCES;
  1907. return 0;
  1908. }
  1909. static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1910. {
  1911. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1912. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1913. __le16 fc = hdr->frame_control;
  1914. /*
  1915. * Pass through unencrypted frames if the hardware has
  1916. * decrypted them already.
  1917. */
  1918. if (status->flag & RX_FLAG_DECRYPTED)
  1919. return 0;
  1920. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1921. if (unlikely(!ieee80211_has_protected(fc) &&
  1922. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1923. rx->key)) {
  1924. if (ieee80211_is_deauth(fc) ||
  1925. ieee80211_is_disassoc(fc))
  1926. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1927. rx->skb->data,
  1928. rx->skb->len);
  1929. return -EACCES;
  1930. }
  1931. /* BIP does not use Protected field, so need to check MMIE */
  1932. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1933. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1934. if (ieee80211_is_deauth(fc) ||
  1935. ieee80211_is_disassoc(fc))
  1936. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1937. rx->skb->data,
  1938. rx->skb->len);
  1939. return -EACCES;
  1940. }
  1941. /*
  1942. * When using MFP, Action frames are not allowed prior to
  1943. * having configured keys.
  1944. */
  1945. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1946. ieee80211_is_robust_mgmt_frame(rx->skb)))
  1947. return -EACCES;
  1948. }
  1949. return 0;
  1950. }
  1951. static int
  1952. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1953. {
  1954. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1955. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1956. bool check_port_control = false;
  1957. struct ethhdr *ehdr;
  1958. int ret;
  1959. *port_control = false;
  1960. if (ieee80211_has_a4(hdr->frame_control) &&
  1961. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1962. return -1;
  1963. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1964. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1965. if (!sdata->u.mgd.use_4addr)
  1966. return -1;
  1967. else
  1968. check_port_control = true;
  1969. }
  1970. if (is_multicast_ether_addr(hdr->addr1) &&
  1971. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1972. return -1;
  1973. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1974. if (ret < 0)
  1975. return ret;
  1976. ehdr = (struct ethhdr *) rx->skb->data;
  1977. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1978. *port_control = true;
  1979. else if (check_port_control)
  1980. return -1;
  1981. return 0;
  1982. }
  1983. /*
  1984. * requires that rx->skb is a frame with ethernet header
  1985. */
  1986. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1987. {
  1988. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1989. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1990. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1991. /*
  1992. * Allow EAPOL frames to us/the PAE group address regardless
  1993. * of whether the frame was encrypted or not.
  1994. */
  1995. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1996. (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
  1997. ether_addr_equal(ehdr->h_dest, pae_group_addr)))
  1998. return true;
  1999. if (ieee80211_802_1x_port_control(rx) ||
  2000. ieee80211_drop_unencrypted(rx, fc))
  2001. return false;
  2002. return true;
  2003. }
  2004. static void ieee80211_deliver_skb_to_local_stack(struct sk_buff *skb,
  2005. struct ieee80211_rx_data *rx)
  2006. {
  2007. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2008. struct net_device *dev = sdata->dev;
  2009. if (unlikely((skb->protocol == sdata->control_port_protocol ||
  2010. skb->protocol == cpu_to_be16(ETH_P_PREAUTH)) &&
  2011. sdata->control_port_over_nl80211)) {
  2012. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2013. bool noencrypt = !(status->flag & RX_FLAG_DECRYPTED);
  2014. cfg80211_rx_control_port(dev, skb, noencrypt);
  2015. dev_kfree_skb(skb);
  2016. } else {
  2017. memset(skb->cb, 0, sizeof(skb->cb));
  2018. /* deliver to local stack */
  2019. if (rx->napi)
  2020. napi_gro_receive(rx->napi, skb);
  2021. else
  2022. netif_receive_skb(skb);
  2023. }
  2024. }
  2025. /*
  2026. * requires that rx->skb is a frame with ethernet header
  2027. */
  2028. static void
  2029. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  2030. {
  2031. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2032. struct net_device *dev = sdata->dev;
  2033. struct sk_buff *skb, *xmit_skb;
  2034. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  2035. struct sta_info *dsta;
  2036. skb = rx->skb;
  2037. xmit_skb = NULL;
  2038. ieee80211_rx_stats(dev, skb->len);
  2039. if (rx->sta) {
  2040. /* The seqno index has the same property as needed
  2041. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  2042. * for non-QoS-data frames. Here we know it's a data
  2043. * frame, so count MSDUs.
  2044. */
  2045. u64_stats_update_begin(&rx->sta->rx_stats.syncp);
  2046. rx->sta->rx_stats.msdu[rx->seqno_idx]++;
  2047. u64_stats_update_end(&rx->sta->rx_stats.syncp);
  2048. }
  2049. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  2050. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  2051. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  2052. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  2053. if (is_multicast_ether_addr(ehdr->h_dest) &&
  2054. ieee80211_vif_get_num_mcast_if(sdata) != 0) {
  2055. /*
  2056. * send multicast frames both to higher layers in
  2057. * local net stack and back to the wireless medium
  2058. */
  2059. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  2060. if (!xmit_skb)
  2061. net_info_ratelimited("%s: failed to clone multicast frame\n",
  2062. dev->name);
  2063. } else if (!is_multicast_ether_addr(ehdr->h_dest)) {
  2064. dsta = sta_info_get(sdata, skb->data);
  2065. if (dsta) {
  2066. /*
  2067. * The destination station is associated to
  2068. * this AP (in this VLAN), so send the frame
  2069. * directly to it and do not pass it to local
  2070. * net stack.
  2071. */
  2072. xmit_skb = skb;
  2073. skb = NULL;
  2074. }
  2075. }
  2076. }
  2077. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2078. if (skb) {
  2079. /* 'align' will only take the values 0 or 2 here since all
  2080. * frames are required to be aligned to 2-byte boundaries
  2081. * when being passed to mac80211; the code here works just
  2082. * as well if that isn't true, but mac80211 assumes it can
  2083. * access fields as 2-byte aligned (e.g. for ether_addr_equal)
  2084. */
  2085. int align;
  2086. align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
  2087. if (align) {
  2088. if (WARN_ON(skb_headroom(skb) < 3)) {
  2089. dev_kfree_skb(skb);
  2090. skb = NULL;
  2091. } else {
  2092. u8 *data = skb->data;
  2093. size_t len = skb_headlen(skb);
  2094. skb->data -= align;
  2095. memmove(skb->data, data, len);
  2096. skb_set_tail_pointer(skb, len);
  2097. }
  2098. }
  2099. }
  2100. #endif
  2101. if (skb) {
  2102. skb->protocol = eth_type_trans(skb, dev);
  2103. ieee80211_deliver_skb_to_local_stack(skb, rx);
  2104. }
  2105. if (xmit_skb) {
  2106. /*
  2107. * Send to wireless media and increase priority by 256 to
  2108. * keep the received priority instead of reclassifying
  2109. * the frame (see cfg80211_classify8021d).
  2110. */
  2111. xmit_skb->priority += 256;
  2112. xmit_skb->protocol = htons(ETH_P_802_3);
  2113. skb_reset_network_header(xmit_skb);
  2114. skb_reset_mac_header(xmit_skb);
  2115. dev_queue_xmit(xmit_skb);
  2116. }
  2117. }
  2118. static ieee80211_rx_result debug_noinline
  2119. __ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx, u8 data_offset)
  2120. {
  2121. struct net_device *dev = rx->sdata->dev;
  2122. struct sk_buff *skb = rx->skb;
  2123. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2124. __le16 fc = hdr->frame_control;
  2125. struct sk_buff_head frame_list;
  2126. struct ethhdr ethhdr;
  2127. const u8 *check_da = ethhdr.h_dest, *check_sa = ethhdr.h_source;
  2128. if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
  2129. check_da = NULL;
  2130. check_sa = NULL;
  2131. } else switch (rx->sdata->vif.type) {
  2132. case NL80211_IFTYPE_AP:
  2133. case NL80211_IFTYPE_AP_VLAN:
  2134. check_da = NULL;
  2135. break;
  2136. case NL80211_IFTYPE_STATION:
  2137. if (!rx->sta ||
  2138. !test_sta_flag(rx->sta, WLAN_STA_TDLS_PEER))
  2139. check_sa = NULL;
  2140. break;
  2141. case NL80211_IFTYPE_MESH_POINT:
  2142. check_sa = NULL;
  2143. break;
  2144. default:
  2145. break;
  2146. }
  2147. skb->dev = dev;
  2148. __skb_queue_head_init(&frame_list);
  2149. if (ieee80211_data_to_8023_exthdr(skb, &ethhdr,
  2150. rx->sdata->vif.addr,
  2151. rx->sdata->vif.type,
  2152. data_offset))
  2153. return RX_DROP_UNUSABLE;
  2154. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  2155. rx->sdata->vif.type,
  2156. rx->local->hw.extra_tx_headroom,
  2157. check_da, check_sa);
  2158. while (!skb_queue_empty(&frame_list)) {
  2159. rx->skb = __skb_dequeue(&frame_list);
  2160. if (!ieee80211_frame_allowed(rx, fc)) {
  2161. dev_kfree_skb(rx->skb);
  2162. continue;
  2163. }
  2164. ieee80211_deliver_skb(rx);
  2165. }
  2166. return RX_QUEUED;
  2167. }
  2168. static ieee80211_rx_result debug_noinline
  2169. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  2170. {
  2171. struct sk_buff *skb = rx->skb;
  2172. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2173. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2174. __le16 fc = hdr->frame_control;
  2175. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  2176. return RX_CONTINUE;
  2177. if (unlikely(!ieee80211_is_data(fc)))
  2178. return RX_CONTINUE;
  2179. if (unlikely(!ieee80211_is_data_present(fc)))
  2180. return RX_DROP_MONITOR;
  2181. if (unlikely(ieee80211_has_a4(hdr->frame_control))) {
  2182. switch (rx->sdata->vif.type) {
  2183. case NL80211_IFTYPE_AP_VLAN:
  2184. if (!rx->sdata->u.vlan.sta)
  2185. return RX_DROP_UNUSABLE;
  2186. break;
  2187. case NL80211_IFTYPE_STATION:
  2188. if (!rx->sdata->u.mgd.use_4addr)
  2189. return RX_DROP_UNUSABLE;
  2190. break;
  2191. default:
  2192. return RX_DROP_UNUSABLE;
  2193. }
  2194. }
  2195. if (is_multicast_ether_addr(hdr->addr1))
  2196. return RX_DROP_UNUSABLE;
  2197. return __ieee80211_rx_h_amsdu(rx, 0);
  2198. }
  2199. #ifdef CONFIG_MAC80211_MESH
  2200. static ieee80211_rx_result
  2201. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  2202. {
  2203. struct ieee80211_hdr *fwd_hdr, *hdr;
  2204. struct ieee80211_tx_info *info;
  2205. struct ieee80211s_hdr *mesh_hdr;
  2206. struct sk_buff *skb = rx->skb, *fwd_skb;
  2207. struct ieee80211_local *local = rx->local;
  2208. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2209. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2210. u16 ac, q, hdrlen;
  2211. int tailroom = 0;
  2212. hdr = (struct ieee80211_hdr *) skb->data;
  2213. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  2214. /* make sure fixed part of mesh header is there, also checks skb len */
  2215. if (!pskb_may_pull(rx->skb, hdrlen + 6))
  2216. return RX_DROP_MONITOR;
  2217. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  2218. /* make sure full mesh header is there, also checks skb len */
  2219. if (!pskb_may_pull(rx->skb,
  2220. hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
  2221. return RX_DROP_MONITOR;
  2222. /* reload pointers */
  2223. hdr = (struct ieee80211_hdr *) skb->data;
  2224. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  2225. if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
  2226. return RX_DROP_MONITOR;
  2227. /* frame is in RMC, don't forward */
  2228. if (ieee80211_is_data(hdr->frame_control) &&
  2229. is_multicast_ether_addr(hdr->addr1) &&
  2230. mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
  2231. return RX_DROP_MONITOR;
  2232. if (!ieee80211_is_data(hdr->frame_control))
  2233. return RX_CONTINUE;
  2234. if (!mesh_hdr->ttl)
  2235. return RX_DROP_MONITOR;
  2236. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  2237. struct mesh_path *mppath;
  2238. char *proxied_addr;
  2239. char *mpp_addr;
  2240. if (is_multicast_ether_addr(hdr->addr1)) {
  2241. mpp_addr = hdr->addr3;
  2242. proxied_addr = mesh_hdr->eaddr1;
  2243. } else if ((mesh_hdr->flags & MESH_FLAGS_AE) ==
  2244. MESH_FLAGS_AE_A5_A6) {
  2245. /* has_a4 already checked in ieee80211_rx_mesh_check */
  2246. mpp_addr = hdr->addr4;
  2247. proxied_addr = mesh_hdr->eaddr2;
  2248. } else {
  2249. return RX_DROP_MONITOR;
  2250. }
  2251. rcu_read_lock();
  2252. mppath = mpp_path_lookup(sdata, proxied_addr);
  2253. if (!mppath) {
  2254. mpp_path_add(sdata, proxied_addr, mpp_addr);
  2255. } else {
  2256. spin_lock_bh(&mppath->state_lock);
  2257. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  2258. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  2259. mppath->exp_time = jiffies;
  2260. spin_unlock_bh(&mppath->state_lock);
  2261. }
  2262. rcu_read_unlock();
  2263. }
  2264. /* Frame has reached destination. Don't forward */
  2265. if (!is_multicast_ether_addr(hdr->addr1) &&
  2266. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  2267. return RX_CONTINUE;
  2268. ac = ieee80211_select_queue_80211(sdata, skb, hdr);
  2269. q = sdata->vif.hw_queue[ac];
  2270. if (ieee80211_queue_stopped(&local->hw, q)) {
  2271. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  2272. return RX_DROP_MONITOR;
  2273. }
  2274. skb_set_queue_mapping(skb, q);
  2275. if (!--mesh_hdr->ttl) {
  2276. if (!is_multicast_ether_addr(hdr->addr1))
  2277. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh,
  2278. dropped_frames_ttl);
  2279. goto out;
  2280. }
  2281. if (!ifmsh->mshcfg.dot11MeshForwarding)
  2282. goto out;
  2283. if (sdata->crypto_tx_tailroom_needed_cnt)
  2284. tailroom = IEEE80211_ENCRYPT_TAILROOM;
  2285. fwd_skb = skb_copy_expand(skb, local->tx_headroom +
  2286. sdata->encrypt_headroom,
  2287. tailroom, GFP_ATOMIC);
  2288. if (!fwd_skb)
  2289. goto out;
  2290. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  2291. fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
  2292. info = IEEE80211_SKB_CB(fwd_skb);
  2293. memset(info, 0, sizeof(*info));
  2294. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  2295. info->control.vif = &rx->sdata->vif;
  2296. info->control.jiffies = jiffies;
  2297. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  2298. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  2299. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  2300. /* update power mode indication when forwarding */
  2301. ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
  2302. } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
  2303. /* mesh power mode flags updated in mesh_nexthop_lookup */
  2304. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  2305. } else {
  2306. /* unable to resolve next hop */
  2307. mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
  2308. fwd_hdr->addr3, 0,
  2309. WLAN_REASON_MESH_PATH_NOFORWARD,
  2310. fwd_hdr->addr2);
  2311. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  2312. kfree_skb(fwd_skb);
  2313. return RX_DROP_MONITOR;
  2314. }
  2315. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  2316. ieee80211_add_pending_skb(local, fwd_skb);
  2317. out:
  2318. if (is_multicast_ether_addr(hdr->addr1))
  2319. return RX_CONTINUE;
  2320. return RX_DROP_MONITOR;
  2321. }
  2322. #endif
  2323. static ieee80211_rx_result debug_noinline
  2324. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  2325. {
  2326. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2327. struct ieee80211_local *local = rx->local;
  2328. struct net_device *dev = sdata->dev;
  2329. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  2330. __le16 fc = hdr->frame_control;
  2331. bool port_control;
  2332. int err;
  2333. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  2334. return RX_CONTINUE;
  2335. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  2336. return RX_DROP_MONITOR;
  2337. /*
  2338. * Send unexpected-4addr-frame event to hostapd. For older versions,
  2339. * also drop the frame to cooked monitor interfaces.
  2340. */
  2341. if (ieee80211_has_a4(hdr->frame_control) &&
  2342. sdata->vif.type == NL80211_IFTYPE_AP) {
  2343. if (rx->sta &&
  2344. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  2345. cfg80211_rx_unexpected_4addr_frame(
  2346. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  2347. return RX_DROP_MONITOR;
  2348. }
  2349. err = __ieee80211_data_to_8023(rx, &port_control);
  2350. if (unlikely(err))
  2351. return RX_DROP_UNUSABLE;
  2352. if (!ieee80211_frame_allowed(rx, fc))
  2353. return RX_DROP_MONITOR;
  2354. /* directly handle TDLS channel switch requests/responses */
  2355. if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
  2356. cpu_to_be16(ETH_P_TDLS))) {
  2357. struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
  2358. if (pskb_may_pull(rx->skb,
  2359. offsetof(struct ieee80211_tdls_data, u)) &&
  2360. tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
  2361. tf->category == WLAN_CATEGORY_TDLS &&
  2362. (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
  2363. tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
  2364. skb_queue_tail(&local->skb_queue_tdls_chsw, rx->skb);
  2365. schedule_work(&local->tdls_chsw_work);
  2366. if (rx->sta)
  2367. rx->sta->rx_stats.packets++;
  2368. return RX_QUEUED;
  2369. }
  2370. }
  2371. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  2372. unlikely(port_control) && sdata->bss) {
  2373. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2374. u.ap);
  2375. dev = sdata->dev;
  2376. rx->sdata = sdata;
  2377. }
  2378. rx->skb->dev = dev;
  2379. if (!ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
  2380. local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  2381. !is_multicast_ether_addr(
  2382. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  2383. (!local->scanning &&
  2384. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)))
  2385. mod_timer(&local->dynamic_ps_timer, jiffies +
  2386. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  2387. ieee80211_deliver_skb(rx);
  2388. return RX_QUEUED;
  2389. }
  2390. static ieee80211_rx_result debug_noinline
  2391. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
  2392. {
  2393. struct sk_buff *skb = rx->skb;
  2394. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  2395. struct tid_ampdu_rx *tid_agg_rx;
  2396. u16 start_seq_num;
  2397. u16 tid;
  2398. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  2399. return RX_CONTINUE;
  2400. if (ieee80211_is_back_req(bar->frame_control)) {
  2401. struct {
  2402. __le16 control, start_seq_num;
  2403. } __packed bar_data;
  2404. struct ieee80211_event event = {
  2405. .type = BAR_RX_EVENT,
  2406. };
  2407. if (!rx->sta)
  2408. return RX_DROP_MONITOR;
  2409. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  2410. &bar_data, sizeof(bar_data)))
  2411. return RX_DROP_MONITOR;
  2412. tid = le16_to_cpu(bar_data.control) >> 12;
  2413. if (!test_bit(tid, rx->sta->ampdu_mlme.agg_session_valid) &&
  2414. !test_and_set_bit(tid, rx->sta->ampdu_mlme.unexpected_agg))
  2415. ieee80211_send_delba(rx->sdata, rx->sta->sta.addr, tid,
  2416. WLAN_BACK_RECIPIENT,
  2417. WLAN_REASON_QSTA_REQUIRE_SETUP);
  2418. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  2419. if (!tid_agg_rx)
  2420. return RX_DROP_MONITOR;
  2421. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  2422. event.u.ba.tid = tid;
  2423. event.u.ba.ssn = start_seq_num;
  2424. event.u.ba.sta = &rx->sta->sta;
  2425. /* reset session timer */
  2426. if (tid_agg_rx->timeout)
  2427. mod_timer(&tid_agg_rx->session_timer,
  2428. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2429. spin_lock(&tid_agg_rx->reorder_lock);
  2430. /* release stored frames up to start of BAR */
  2431. ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
  2432. start_seq_num, frames);
  2433. spin_unlock(&tid_agg_rx->reorder_lock);
  2434. drv_event_callback(rx->local, rx->sdata, &event);
  2435. kfree_skb(skb);
  2436. return RX_QUEUED;
  2437. }
  2438. /*
  2439. * After this point, we only want management frames,
  2440. * so we can drop all remaining control frames to
  2441. * cooked monitor interfaces.
  2442. */
  2443. return RX_DROP_MONITOR;
  2444. }
  2445. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  2446. struct ieee80211_mgmt *mgmt,
  2447. size_t len)
  2448. {
  2449. struct ieee80211_local *local = sdata->local;
  2450. struct sk_buff *skb;
  2451. struct ieee80211_mgmt *resp;
  2452. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  2453. /* Not to own unicast address */
  2454. return;
  2455. }
  2456. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  2457. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  2458. /* Not from the current AP or not associated yet. */
  2459. return;
  2460. }
  2461. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  2462. /* Too short SA Query request frame */
  2463. return;
  2464. }
  2465. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  2466. if (skb == NULL)
  2467. return;
  2468. skb_reserve(skb, local->hw.extra_tx_headroom);
  2469. resp = skb_put_zero(skb, 24);
  2470. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2471. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  2472. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  2473. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2474. IEEE80211_STYPE_ACTION);
  2475. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  2476. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  2477. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  2478. memcpy(resp->u.action.u.sa_query.trans_id,
  2479. mgmt->u.action.u.sa_query.trans_id,
  2480. WLAN_SA_QUERY_TR_ID_LEN);
  2481. ieee80211_tx_skb(sdata, skb);
  2482. }
  2483. static ieee80211_rx_result debug_noinline
  2484. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  2485. {
  2486. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2487. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2488. /*
  2489. * From here on, look only at management frames.
  2490. * Data and control frames are already handled,
  2491. * and unknown (reserved) frames are useless.
  2492. */
  2493. if (rx->skb->len < 24)
  2494. return RX_DROP_MONITOR;
  2495. if (!ieee80211_is_mgmt(mgmt->frame_control))
  2496. return RX_DROP_MONITOR;
  2497. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  2498. ieee80211_is_beacon(mgmt->frame_control) &&
  2499. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  2500. int sig = 0;
  2501. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
  2502. !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
  2503. sig = status->signal;
  2504. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  2505. rx->skb->data, rx->skb->len,
  2506. status->freq, sig);
  2507. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  2508. }
  2509. if (ieee80211_drop_unencrypted_mgmt(rx))
  2510. return RX_DROP_UNUSABLE;
  2511. return RX_CONTINUE;
  2512. }
  2513. static ieee80211_rx_result debug_noinline
  2514. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  2515. {
  2516. struct ieee80211_local *local = rx->local;
  2517. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2518. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2519. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2520. int len = rx->skb->len;
  2521. if (!ieee80211_is_action(mgmt->frame_control))
  2522. return RX_CONTINUE;
  2523. /* drop too small frames */
  2524. if (len < IEEE80211_MIN_ACTION_SIZE)
  2525. return RX_DROP_UNUSABLE;
  2526. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
  2527. mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
  2528. mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  2529. return RX_DROP_UNUSABLE;
  2530. switch (mgmt->u.action.category) {
  2531. case WLAN_CATEGORY_HT:
  2532. /* reject HT action frames from stations not supporting HT */
  2533. if (!rx->sta->sta.ht_cap.ht_supported)
  2534. goto invalid;
  2535. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2536. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2537. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2538. sdata->vif.type != NL80211_IFTYPE_AP &&
  2539. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2540. break;
  2541. /* verify action & smps_control/chanwidth are present */
  2542. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2543. goto invalid;
  2544. switch (mgmt->u.action.u.ht_smps.action) {
  2545. case WLAN_HT_ACTION_SMPS: {
  2546. struct ieee80211_supported_band *sband;
  2547. enum ieee80211_smps_mode smps_mode;
  2548. struct sta_opmode_info sta_opmode = {};
  2549. /* convert to HT capability */
  2550. switch (mgmt->u.action.u.ht_smps.smps_control) {
  2551. case WLAN_HT_SMPS_CONTROL_DISABLED:
  2552. smps_mode = IEEE80211_SMPS_OFF;
  2553. break;
  2554. case WLAN_HT_SMPS_CONTROL_STATIC:
  2555. smps_mode = IEEE80211_SMPS_STATIC;
  2556. break;
  2557. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  2558. smps_mode = IEEE80211_SMPS_DYNAMIC;
  2559. break;
  2560. default:
  2561. goto invalid;
  2562. }
  2563. /* if no change do nothing */
  2564. if (rx->sta->sta.smps_mode == smps_mode)
  2565. goto handled;
  2566. rx->sta->sta.smps_mode = smps_mode;
  2567. sta_opmode.smps_mode =
  2568. ieee80211_smps_mode_to_smps_mode(smps_mode);
  2569. sta_opmode.changed = STA_OPMODE_SMPS_MODE_CHANGED;
  2570. sband = rx->local->hw.wiphy->bands[status->band];
  2571. rate_control_rate_update(local, sband, rx->sta,
  2572. IEEE80211_RC_SMPS_CHANGED);
  2573. cfg80211_sta_opmode_change_notify(sdata->dev,
  2574. rx->sta->addr,
  2575. &sta_opmode,
  2576. GFP_ATOMIC);
  2577. goto handled;
  2578. }
  2579. case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
  2580. struct ieee80211_supported_band *sband;
  2581. u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
  2582. enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
  2583. struct sta_opmode_info sta_opmode = {};
  2584. /* If it doesn't support 40 MHz it can't change ... */
  2585. if (!(rx->sta->sta.ht_cap.cap &
  2586. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  2587. goto handled;
  2588. if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
  2589. max_bw = IEEE80211_STA_RX_BW_20;
  2590. else
  2591. max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
  2592. /* set cur_max_bandwidth and recalc sta bw */
  2593. rx->sta->cur_max_bandwidth = max_bw;
  2594. new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
  2595. if (rx->sta->sta.bandwidth == new_bw)
  2596. goto handled;
  2597. rx->sta->sta.bandwidth = new_bw;
  2598. sband = rx->local->hw.wiphy->bands[status->band];
  2599. sta_opmode.bw =
  2600. ieee80211_sta_rx_bw_to_chan_width(rx->sta);
  2601. sta_opmode.changed = STA_OPMODE_MAX_BW_CHANGED;
  2602. rate_control_rate_update(local, sband, rx->sta,
  2603. IEEE80211_RC_BW_CHANGED);
  2604. cfg80211_sta_opmode_change_notify(sdata->dev,
  2605. rx->sta->addr,
  2606. &sta_opmode,
  2607. GFP_ATOMIC);
  2608. goto handled;
  2609. }
  2610. default:
  2611. goto invalid;
  2612. }
  2613. break;
  2614. case WLAN_CATEGORY_PUBLIC:
  2615. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2616. goto invalid;
  2617. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2618. break;
  2619. if (!rx->sta)
  2620. break;
  2621. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  2622. break;
  2623. if (mgmt->u.action.u.ext_chan_switch.action_code !=
  2624. WLAN_PUB_ACTION_EXT_CHANSW_ANN)
  2625. break;
  2626. if (len < offsetof(struct ieee80211_mgmt,
  2627. u.action.u.ext_chan_switch.variable))
  2628. goto invalid;
  2629. goto queue;
  2630. case WLAN_CATEGORY_VHT:
  2631. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2632. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2633. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2634. sdata->vif.type != NL80211_IFTYPE_AP &&
  2635. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2636. break;
  2637. /* verify action code is present */
  2638. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2639. goto invalid;
  2640. switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
  2641. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  2642. /* verify opmode is present */
  2643. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2644. goto invalid;
  2645. goto queue;
  2646. }
  2647. case WLAN_VHT_ACTION_GROUPID_MGMT: {
  2648. if (len < IEEE80211_MIN_ACTION_SIZE + 25)
  2649. goto invalid;
  2650. goto queue;
  2651. }
  2652. default:
  2653. break;
  2654. }
  2655. break;
  2656. case WLAN_CATEGORY_BACK:
  2657. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2658. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2659. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2660. sdata->vif.type != NL80211_IFTYPE_AP &&
  2661. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2662. break;
  2663. /* verify action_code is present */
  2664. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2665. break;
  2666. switch (mgmt->u.action.u.addba_req.action_code) {
  2667. case WLAN_ACTION_ADDBA_REQ:
  2668. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2669. sizeof(mgmt->u.action.u.addba_req)))
  2670. goto invalid;
  2671. break;
  2672. case WLAN_ACTION_ADDBA_RESP:
  2673. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2674. sizeof(mgmt->u.action.u.addba_resp)))
  2675. goto invalid;
  2676. break;
  2677. case WLAN_ACTION_DELBA:
  2678. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2679. sizeof(mgmt->u.action.u.delba)))
  2680. goto invalid;
  2681. break;
  2682. default:
  2683. goto invalid;
  2684. }
  2685. goto queue;
  2686. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2687. /* verify action_code is present */
  2688. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2689. break;
  2690. switch (mgmt->u.action.u.measurement.action_code) {
  2691. case WLAN_ACTION_SPCT_MSR_REQ:
  2692. if (status->band != NL80211_BAND_5GHZ)
  2693. break;
  2694. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2695. sizeof(mgmt->u.action.u.measurement)))
  2696. break;
  2697. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2698. break;
  2699. ieee80211_process_measurement_req(sdata, mgmt, len);
  2700. goto handled;
  2701. case WLAN_ACTION_SPCT_CHL_SWITCH: {
  2702. u8 *bssid;
  2703. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2704. sizeof(mgmt->u.action.u.chan_switch)))
  2705. break;
  2706. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2707. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2708. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2709. break;
  2710. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2711. bssid = sdata->u.mgd.bssid;
  2712. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2713. bssid = sdata->u.ibss.bssid;
  2714. else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  2715. bssid = mgmt->sa;
  2716. else
  2717. break;
  2718. if (!ether_addr_equal(mgmt->bssid, bssid))
  2719. break;
  2720. goto queue;
  2721. }
  2722. }
  2723. break;
  2724. case WLAN_CATEGORY_SA_QUERY:
  2725. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2726. sizeof(mgmt->u.action.u.sa_query)))
  2727. break;
  2728. switch (mgmt->u.action.u.sa_query.action) {
  2729. case WLAN_ACTION_SA_QUERY_REQUEST:
  2730. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2731. break;
  2732. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2733. goto handled;
  2734. }
  2735. break;
  2736. case WLAN_CATEGORY_SELF_PROTECTED:
  2737. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2738. sizeof(mgmt->u.action.u.self_prot.action_code)))
  2739. break;
  2740. switch (mgmt->u.action.u.self_prot.action_code) {
  2741. case WLAN_SP_MESH_PEERING_OPEN:
  2742. case WLAN_SP_MESH_PEERING_CLOSE:
  2743. case WLAN_SP_MESH_PEERING_CONFIRM:
  2744. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2745. goto invalid;
  2746. if (sdata->u.mesh.user_mpm)
  2747. /* userspace handles this frame */
  2748. break;
  2749. goto queue;
  2750. case WLAN_SP_MGK_INFORM:
  2751. case WLAN_SP_MGK_ACK:
  2752. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2753. goto invalid;
  2754. break;
  2755. }
  2756. break;
  2757. case WLAN_CATEGORY_MESH_ACTION:
  2758. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2759. sizeof(mgmt->u.action.u.mesh_action.action_code)))
  2760. break;
  2761. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2762. break;
  2763. if (mesh_action_is_path_sel(mgmt) &&
  2764. !mesh_path_sel_is_hwmp(sdata))
  2765. break;
  2766. goto queue;
  2767. }
  2768. return RX_CONTINUE;
  2769. invalid:
  2770. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2771. /* will return in the next handlers */
  2772. return RX_CONTINUE;
  2773. handled:
  2774. if (rx->sta)
  2775. rx->sta->rx_stats.packets++;
  2776. dev_kfree_skb(rx->skb);
  2777. return RX_QUEUED;
  2778. queue:
  2779. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2780. ieee80211_queue_work(&local->hw, &sdata->work);
  2781. if (rx->sta)
  2782. rx->sta->rx_stats.packets++;
  2783. return RX_QUEUED;
  2784. }
  2785. static ieee80211_rx_result debug_noinline
  2786. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2787. {
  2788. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2789. int sig = 0;
  2790. /* skip known-bad action frames and return them in the next handler */
  2791. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2792. return RX_CONTINUE;
  2793. /*
  2794. * Getting here means the kernel doesn't know how to handle
  2795. * it, but maybe userspace does ... include returned frames
  2796. * so userspace can register for those to know whether ones
  2797. * it transmitted were processed or returned.
  2798. */
  2799. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM) &&
  2800. !(status->flag & RX_FLAG_NO_SIGNAL_VAL))
  2801. sig = status->signal;
  2802. if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
  2803. rx->skb->data, rx->skb->len, 0)) {
  2804. if (rx->sta)
  2805. rx->sta->rx_stats.packets++;
  2806. dev_kfree_skb(rx->skb);
  2807. return RX_QUEUED;
  2808. }
  2809. return RX_CONTINUE;
  2810. }
  2811. static ieee80211_rx_result debug_noinline
  2812. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2813. {
  2814. struct ieee80211_local *local = rx->local;
  2815. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2816. struct sk_buff *nskb;
  2817. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2818. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2819. if (!ieee80211_is_action(mgmt->frame_control))
  2820. return RX_CONTINUE;
  2821. /*
  2822. * For AP mode, hostapd is responsible for handling any action
  2823. * frames that we didn't handle, including returning unknown
  2824. * ones. For all other modes we will return them to the sender,
  2825. * setting the 0x80 bit in the action category, as required by
  2826. * 802.11-2012 9.24.4.
  2827. * Newer versions of hostapd shall also use the management frame
  2828. * registration mechanisms, but older ones still use cooked
  2829. * monitor interfaces so push all frames there.
  2830. */
  2831. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2832. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2833. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2834. return RX_DROP_MONITOR;
  2835. if (is_multicast_ether_addr(mgmt->da))
  2836. return RX_DROP_MONITOR;
  2837. /* do not return rejected action frames */
  2838. if (mgmt->u.action.category & 0x80)
  2839. return RX_DROP_UNUSABLE;
  2840. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2841. GFP_ATOMIC);
  2842. if (nskb) {
  2843. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2844. nmgmt->u.action.category |= 0x80;
  2845. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2846. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2847. memset(nskb->cb, 0, sizeof(nskb->cb));
  2848. if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  2849. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
  2850. info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
  2851. IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
  2852. IEEE80211_TX_CTL_NO_CCK_RATE;
  2853. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  2854. info->hw_queue =
  2855. local->hw.offchannel_tx_hw_queue;
  2856. }
  2857. __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
  2858. status->band, 0);
  2859. }
  2860. dev_kfree_skb(rx->skb);
  2861. return RX_QUEUED;
  2862. }
  2863. static ieee80211_rx_result debug_noinline
  2864. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2865. {
  2866. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2867. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2868. __le16 stype;
  2869. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2870. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2871. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2872. sdata->vif.type != NL80211_IFTYPE_OCB &&
  2873. sdata->vif.type != NL80211_IFTYPE_STATION)
  2874. return RX_DROP_MONITOR;
  2875. switch (stype) {
  2876. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2877. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2878. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2879. /* process for all: mesh, mlme, ibss */
  2880. break;
  2881. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2882. if (is_multicast_ether_addr(mgmt->da) &&
  2883. !is_broadcast_ether_addr(mgmt->da))
  2884. return RX_DROP_MONITOR;
  2885. /* process only for station/IBSS */
  2886. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2887. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2888. return RX_DROP_MONITOR;
  2889. break;
  2890. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2891. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2892. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2893. if (is_multicast_ether_addr(mgmt->da) &&
  2894. !is_broadcast_ether_addr(mgmt->da))
  2895. return RX_DROP_MONITOR;
  2896. /* process only for station */
  2897. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2898. return RX_DROP_MONITOR;
  2899. break;
  2900. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2901. /* process only for ibss and mesh */
  2902. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2903. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2904. return RX_DROP_MONITOR;
  2905. break;
  2906. default:
  2907. return RX_DROP_MONITOR;
  2908. }
  2909. /* queue up frame and kick off work to process it */
  2910. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2911. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2912. if (rx->sta)
  2913. rx->sta->rx_stats.packets++;
  2914. return RX_QUEUED;
  2915. }
  2916. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2917. struct ieee80211_rate *rate)
  2918. {
  2919. struct ieee80211_sub_if_data *sdata;
  2920. struct ieee80211_local *local = rx->local;
  2921. struct sk_buff *skb = rx->skb, *skb2;
  2922. struct net_device *prev_dev = NULL;
  2923. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2924. int needed_headroom;
  2925. /*
  2926. * If cooked monitor has been processed already, then
  2927. * don't do it again. If not, set the flag.
  2928. */
  2929. if (rx->flags & IEEE80211_RX_CMNTR)
  2930. goto out_free_skb;
  2931. rx->flags |= IEEE80211_RX_CMNTR;
  2932. /* If there are no cooked monitor interfaces, just free the SKB */
  2933. if (!local->cooked_mntrs)
  2934. goto out_free_skb;
  2935. /* vendor data is long removed here */
  2936. status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
  2937. /* room for the radiotap header based on driver features */
  2938. needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
  2939. if (skb_headroom(skb) < needed_headroom &&
  2940. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2941. goto out_free_skb;
  2942. /* prepend radiotap information */
  2943. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2944. false);
  2945. skb_reset_mac_header(skb);
  2946. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2947. skb->pkt_type = PACKET_OTHERHOST;
  2948. skb->protocol = htons(ETH_P_802_2);
  2949. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2950. if (!ieee80211_sdata_running(sdata))
  2951. continue;
  2952. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2953. !(sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES))
  2954. continue;
  2955. if (prev_dev) {
  2956. skb2 = skb_clone(skb, GFP_ATOMIC);
  2957. if (skb2) {
  2958. skb2->dev = prev_dev;
  2959. netif_receive_skb(skb2);
  2960. }
  2961. }
  2962. prev_dev = sdata->dev;
  2963. ieee80211_rx_stats(sdata->dev, skb->len);
  2964. }
  2965. if (prev_dev) {
  2966. skb->dev = prev_dev;
  2967. netif_receive_skb(skb);
  2968. return;
  2969. }
  2970. out_free_skb:
  2971. dev_kfree_skb(skb);
  2972. }
  2973. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2974. ieee80211_rx_result res)
  2975. {
  2976. switch (res) {
  2977. case RX_DROP_MONITOR:
  2978. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2979. if (rx->sta)
  2980. rx->sta->rx_stats.dropped++;
  2981. /* fall through */
  2982. case RX_CONTINUE: {
  2983. struct ieee80211_rate *rate = NULL;
  2984. struct ieee80211_supported_band *sband;
  2985. struct ieee80211_rx_status *status;
  2986. status = IEEE80211_SKB_RXCB((rx->skb));
  2987. sband = rx->local->hw.wiphy->bands[status->band];
  2988. if (status->encoding == RX_ENC_LEGACY)
  2989. rate = &sband->bitrates[status->rate_idx];
  2990. ieee80211_rx_cooked_monitor(rx, rate);
  2991. break;
  2992. }
  2993. case RX_DROP_UNUSABLE:
  2994. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2995. if (rx->sta)
  2996. rx->sta->rx_stats.dropped++;
  2997. dev_kfree_skb(rx->skb);
  2998. break;
  2999. case RX_QUEUED:
  3000. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  3001. break;
  3002. }
  3003. }
  3004. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
  3005. struct sk_buff_head *frames)
  3006. {
  3007. ieee80211_rx_result res = RX_DROP_MONITOR;
  3008. struct sk_buff *skb;
  3009. #define CALL_RXH(rxh) \
  3010. do { \
  3011. res = rxh(rx); \
  3012. if (res != RX_CONTINUE) \
  3013. goto rxh_next; \
  3014. } while (0)
  3015. /* Lock here to avoid hitting all of the data used in the RX
  3016. * path (e.g. key data, station data, ...) concurrently when
  3017. * a frame is released from the reorder buffer due to timeout
  3018. * from the timer, potentially concurrently with RX from the
  3019. * driver.
  3020. */
  3021. spin_lock_bh(&rx->local->rx_path_lock);
  3022. while ((skb = __skb_dequeue(frames))) {
  3023. /*
  3024. * all the other fields are valid across frames
  3025. * that belong to an aMPDU since they are on the
  3026. * same TID from the same station
  3027. */
  3028. rx->skb = skb;
  3029. CALL_RXH(ieee80211_rx_h_check_more_data);
  3030. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll);
  3031. CALL_RXH(ieee80211_rx_h_sta_process);
  3032. CALL_RXH(ieee80211_rx_h_decrypt);
  3033. CALL_RXH(ieee80211_rx_h_defragment);
  3034. CALL_RXH(ieee80211_rx_h_michael_mic_verify);
  3035. /* must be after MMIC verify so header is counted in MPDU mic */
  3036. #ifdef CONFIG_MAC80211_MESH
  3037. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  3038. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  3039. #endif
  3040. CALL_RXH(ieee80211_rx_h_amsdu);
  3041. CALL_RXH(ieee80211_rx_h_data);
  3042. /* special treatment -- needs the queue */
  3043. res = ieee80211_rx_h_ctrl(rx, frames);
  3044. if (res != RX_CONTINUE)
  3045. goto rxh_next;
  3046. CALL_RXH(ieee80211_rx_h_mgmt_check);
  3047. CALL_RXH(ieee80211_rx_h_action);
  3048. CALL_RXH(ieee80211_rx_h_userspace_mgmt);
  3049. CALL_RXH(ieee80211_rx_h_action_return);
  3050. CALL_RXH(ieee80211_rx_h_mgmt);
  3051. rxh_next:
  3052. ieee80211_rx_handlers_result(rx, res);
  3053. #undef CALL_RXH
  3054. }
  3055. spin_unlock_bh(&rx->local->rx_path_lock);
  3056. }
  3057. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  3058. {
  3059. struct sk_buff_head reorder_release;
  3060. ieee80211_rx_result res = RX_DROP_MONITOR;
  3061. __skb_queue_head_init(&reorder_release);
  3062. #define CALL_RXH(rxh) \
  3063. do { \
  3064. res = rxh(rx); \
  3065. if (res != RX_CONTINUE) \
  3066. goto rxh_next; \
  3067. } while (0)
  3068. CALL_RXH(ieee80211_rx_h_check_dup);
  3069. CALL_RXH(ieee80211_rx_h_check);
  3070. ieee80211_rx_reorder_ampdu(rx, &reorder_release);
  3071. ieee80211_rx_handlers(rx, &reorder_release);
  3072. return;
  3073. rxh_next:
  3074. ieee80211_rx_handlers_result(rx, res);
  3075. #undef CALL_RXH
  3076. }
  3077. /*
  3078. * This function makes calls into the RX path, therefore
  3079. * it has to be invoked under RCU read lock.
  3080. */
  3081. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  3082. {
  3083. struct sk_buff_head frames;
  3084. struct ieee80211_rx_data rx = {
  3085. .sta = sta,
  3086. .sdata = sta->sdata,
  3087. .local = sta->local,
  3088. /* This is OK -- must be QoS data frame */
  3089. .security_idx = tid,
  3090. .seqno_idx = tid,
  3091. .napi = NULL, /* must be NULL to not have races */
  3092. };
  3093. struct tid_ampdu_rx *tid_agg_rx;
  3094. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  3095. if (!tid_agg_rx)
  3096. return;
  3097. __skb_queue_head_init(&frames);
  3098. spin_lock(&tid_agg_rx->reorder_lock);
  3099. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  3100. spin_unlock(&tid_agg_rx->reorder_lock);
  3101. if (!skb_queue_empty(&frames)) {
  3102. struct ieee80211_event event = {
  3103. .type = BA_FRAME_TIMEOUT,
  3104. .u.ba.tid = tid,
  3105. .u.ba.sta = &sta->sta,
  3106. };
  3107. drv_event_callback(rx.local, rx.sdata, &event);
  3108. }
  3109. ieee80211_rx_handlers(&rx, &frames);
  3110. }
  3111. void ieee80211_mark_rx_ba_filtered_frames(struct ieee80211_sta *pubsta, u8 tid,
  3112. u16 ssn, u64 filtered,
  3113. u16 received_mpdus)
  3114. {
  3115. struct sta_info *sta;
  3116. struct tid_ampdu_rx *tid_agg_rx;
  3117. struct sk_buff_head frames;
  3118. struct ieee80211_rx_data rx = {
  3119. /* This is OK -- must be QoS data frame */
  3120. .security_idx = tid,
  3121. .seqno_idx = tid,
  3122. };
  3123. int i, diff;
  3124. if (WARN_ON(!pubsta || tid >= IEEE80211_NUM_TIDS))
  3125. return;
  3126. __skb_queue_head_init(&frames);
  3127. sta = container_of(pubsta, struct sta_info, sta);
  3128. rx.sta = sta;
  3129. rx.sdata = sta->sdata;
  3130. rx.local = sta->local;
  3131. rcu_read_lock();
  3132. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  3133. if (!tid_agg_rx)
  3134. goto out;
  3135. spin_lock_bh(&tid_agg_rx->reorder_lock);
  3136. if (received_mpdus >= IEEE80211_SN_MODULO >> 1) {
  3137. int release;
  3138. /* release all frames in the reorder buffer */
  3139. release = (tid_agg_rx->head_seq_num + tid_agg_rx->buf_size) %
  3140. IEEE80211_SN_MODULO;
  3141. ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx,
  3142. release, &frames);
  3143. /* update ssn to match received ssn */
  3144. tid_agg_rx->head_seq_num = ssn;
  3145. } else {
  3146. ieee80211_release_reorder_frames(sta->sdata, tid_agg_rx, ssn,
  3147. &frames);
  3148. }
  3149. /* handle the case that received ssn is behind the mac ssn.
  3150. * it can be tid_agg_rx->buf_size behind and still be valid */
  3151. diff = (tid_agg_rx->head_seq_num - ssn) & IEEE80211_SN_MASK;
  3152. if (diff >= tid_agg_rx->buf_size) {
  3153. tid_agg_rx->reorder_buf_filtered = 0;
  3154. goto release;
  3155. }
  3156. filtered = filtered >> diff;
  3157. ssn += diff;
  3158. /* update bitmap */
  3159. for (i = 0; i < tid_agg_rx->buf_size; i++) {
  3160. int index = (ssn + i) % tid_agg_rx->buf_size;
  3161. tid_agg_rx->reorder_buf_filtered &= ~BIT_ULL(index);
  3162. if (filtered & BIT_ULL(i))
  3163. tid_agg_rx->reorder_buf_filtered |= BIT_ULL(index);
  3164. }
  3165. /* now process also frames that the filter marking released */
  3166. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  3167. release:
  3168. spin_unlock_bh(&tid_agg_rx->reorder_lock);
  3169. ieee80211_rx_handlers(&rx, &frames);
  3170. out:
  3171. rcu_read_unlock();
  3172. }
  3173. EXPORT_SYMBOL(ieee80211_mark_rx_ba_filtered_frames);
  3174. /* main receive path */
  3175. static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
  3176. {
  3177. struct ieee80211_sub_if_data *sdata = rx->sdata;
  3178. struct sk_buff *skb = rx->skb;
  3179. struct ieee80211_hdr *hdr = (void *)skb->data;
  3180. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3181. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  3182. bool multicast = is_multicast_ether_addr(hdr->addr1);
  3183. switch (sdata->vif.type) {
  3184. case NL80211_IFTYPE_STATION:
  3185. if (!bssid && !sdata->u.mgd.use_4addr)
  3186. return false;
  3187. if (ieee80211_is_robust_mgmt_frame(skb) && !rx->sta)
  3188. return false;
  3189. if (multicast)
  3190. return true;
  3191. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3192. case NL80211_IFTYPE_ADHOC:
  3193. if (!bssid)
  3194. return false;
  3195. if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
  3196. ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
  3197. return false;
  3198. if (ieee80211_is_beacon(hdr->frame_control))
  3199. return true;
  3200. if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
  3201. return false;
  3202. if (!multicast &&
  3203. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  3204. return false;
  3205. if (!rx->sta) {
  3206. int rate_idx;
  3207. if (status->encoding != RX_ENC_LEGACY)
  3208. rate_idx = 0; /* TODO: HT/VHT rates */
  3209. else
  3210. rate_idx = status->rate_idx;
  3211. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  3212. BIT(rate_idx));
  3213. }
  3214. return true;
  3215. case NL80211_IFTYPE_OCB:
  3216. if (!bssid)
  3217. return false;
  3218. if (!ieee80211_is_data_present(hdr->frame_control))
  3219. return false;
  3220. if (!is_broadcast_ether_addr(bssid))
  3221. return false;
  3222. if (!multicast &&
  3223. !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
  3224. return false;
  3225. if (!rx->sta) {
  3226. int rate_idx;
  3227. if (status->encoding != RX_ENC_LEGACY)
  3228. rate_idx = 0; /* TODO: HT rates */
  3229. else
  3230. rate_idx = status->rate_idx;
  3231. ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
  3232. BIT(rate_idx));
  3233. }
  3234. return true;
  3235. case NL80211_IFTYPE_MESH_POINT:
  3236. if (ether_addr_equal(sdata->vif.addr, hdr->addr2))
  3237. return false;
  3238. if (multicast)
  3239. return true;
  3240. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3241. case NL80211_IFTYPE_AP_VLAN:
  3242. case NL80211_IFTYPE_AP:
  3243. if (!bssid)
  3244. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  3245. if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
  3246. /*
  3247. * Accept public action frames even when the
  3248. * BSSID doesn't match, this is used for P2P
  3249. * and location updates. Note that mac80211
  3250. * itself never looks at these frames.
  3251. */
  3252. if (!multicast &&
  3253. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  3254. return false;
  3255. if (ieee80211_is_public_action(hdr, skb->len))
  3256. return true;
  3257. return ieee80211_is_beacon(hdr->frame_control);
  3258. }
  3259. if (!ieee80211_has_tods(hdr->frame_control)) {
  3260. /* ignore data frames to TDLS-peers */
  3261. if (ieee80211_is_data(hdr->frame_control))
  3262. return false;
  3263. /* ignore action frames to TDLS-peers */
  3264. if (ieee80211_is_action(hdr->frame_control) &&
  3265. !is_broadcast_ether_addr(bssid) &&
  3266. !ether_addr_equal(bssid, hdr->addr1))
  3267. return false;
  3268. }
  3269. /*
  3270. * 802.11-2016 Table 9-26 says that for data frames, A1 must be
  3271. * the BSSID - we've checked that already but may have accepted
  3272. * the wildcard (ff:ff:ff:ff:ff:ff).
  3273. *
  3274. * It also says:
  3275. * The BSSID of the Data frame is determined as follows:
  3276. * a) If the STA is contained within an AP or is associated
  3277. * with an AP, the BSSID is the address currently in use
  3278. * by the STA contained in the AP.
  3279. *
  3280. * So we should not accept data frames with an address that's
  3281. * multicast.
  3282. *
  3283. * Accepting it also opens a security problem because stations
  3284. * could encrypt it with the GTK and inject traffic that way.
  3285. */
  3286. if (ieee80211_is_data(hdr->frame_control) && multicast)
  3287. return false;
  3288. return true;
  3289. case NL80211_IFTYPE_WDS:
  3290. if (bssid || !ieee80211_is_data(hdr->frame_control))
  3291. return false;
  3292. return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
  3293. case NL80211_IFTYPE_P2P_DEVICE:
  3294. return ieee80211_is_public_action(hdr, skb->len) ||
  3295. ieee80211_is_probe_req(hdr->frame_control) ||
  3296. ieee80211_is_probe_resp(hdr->frame_control) ||
  3297. ieee80211_is_beacon(hdr->frame_control);
  3298. case NL80211_IFTYPE_NAN:
  3299. /* Currently no frames on NAN interface are allowed */
  3300. return false;
  3301. default:
  3302. break;
  3303. }
  3304. WARN_ON_ONCE(1);
  3305. return false;
  3306. }
  3307. void ieee80211_check_fast_rx(struct sta_info *sta)
  3308. {
  3309. struct ieee80211_sub_if_data *sdata = sta->sdata;
  3310. struct ieee80211_local *local = sdata->local;
  3311. struct ieee80211_key *key;
  3312. struct ieee80211_fast_rx fastrx = {
  3313. .dev = sdata->dev,
  3314. .vif_type = sdata->vif.type,
  3315. .control_port_protocol = sdata->control_port_protocol,
  3316. }, *old, *new = NULL;
  3317. bool assign = false;
  3318. /* use sparse to check that we don't return without updating */
  3319. __acquire(check_fast_rx);
  3320. BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != sizeof(rfc1042_header));
  3321. BUILD_BUG_ON(sizeof(fastrx.rfc1042_hdr) != ETH_ALEN);
  3322. ether_addr_copy(fastrx.rfc1042_hdr, rfc1042_header);
  3323. ether_addr_copy(fastrx.vif_addr, sdata->vif.addr);
  3324. fastrx.uses_rss = ieee80211_hw_check(&local->hw, USES_RSS);
  3325. /* fast-rx doesn't do reordering */
  3326. if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
  3327. !ieee80211_hw_check(&local->hw, SUPPORTS_REORDERING_BUFFER))
  3328. goto clear;
  3329. switch (sdata->vif.type) {
  3330. case NL80211_IFTYPE_STATION:
  3331. if (sta->sta.tdls) {
  3332. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
  3333. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  3334. fastrx.expected_ds_bits = 0;
  3335. } else {
  3336. fastrx.sta_notify = sdata->u.mgd.probe_send_count > 0;
  3337. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr1);
  3338. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr3);
  3339. fastrx.expected_ds_bits =
  3340. cpu_to_le16(IEEE80211_FCTL_FROMDS);
  3341. }
  3342. if (sdata->u.mgd.use_4addr && !sta->sta.tdls) {
  3343. fastrx.expected_ds_bits |=
  3344. cpu_to_le16(IEEE80211_FCTL_TODS);
  3345. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
  3346. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  3347. }
  3348. if (!sdata->u.mgd.powersave)
  3349. break;
  3350. /* software powersave is a huge mess, avoid all of it */
  3351. if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
  3352. goto clear;
  3353. if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
  3354. !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
  3355. goto clear;
  3356. break;
  3357. case NL80211_IFTYPE_AP_VLAN:
  3358. case NL80211_IFTYPE_AP:
  3359. /* parallel-rx requires this, at least with calls to
  3360. * ieee80211_sta_ps_transition()
  3361. */
  3362. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  3363. goto clear;
  3364. fastrx.da_offs = offsetof(struct ieee80211_hdr, addr3);
  3365. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr2);
  3366. fastrx.expected_ds_bits = cpu_to_le16(IEEE80211_FCTL_TODS);
  3367. fastrx.internal_forward =
  3368. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  3369. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN ||
  3370. !sdata->u.vlan.sta);
  3371. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  3372. sdata->u.vlan.sta) {
  3373. fastrx.expected_ds_bits |=
  3374. cpu_to_le16(IEEE80211_FCTL_FROMDS);
  3375. fastrx.sa_offs = offsetof(struct ieee80211_hdr, addr4);
  3376. fastrx.internal_forward = 0;
  3377. }
  3378. break;
  3379. default:
  3380. goto clear;
  3381. }
  3382. if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
  3383. goto clear;
  3384. rcu_read_lock();
  3385. key = rcu_dereference(sta->ptk[sta->ptk_idx]);
  3386. if (key) {
  3387. switch (key->conf.cipher) {
  3388. case WLAN_CIPHER_SUITE_TKIP:
  3389. /* we don't want to deal with MMIC in fast-rx */
  3390. goto clear_rcu;
  3391. case WLAN_CIPHER_SUITE_CCMP:
  3392. case WLAN_CIPHER_SUITE_CCMP_256:
  3393. case WLAN_CIPHER_SUITE_GCMP:
  3394. case WLAN_CIPHER_SUITE_GCMP_256:
  3395. break;
  3396. default:
  3397. /* we also don't want to deal with WEP or cipher scheme
  3398. * since those require looking up the key idx in the
  3399. * frame, rather than assuming the PTK is used
  3400. * (we need to revisit this once we implement the real
  3401. * PTK index, which is now valid in the spec, but we
  3402. * haven't implemented that part yet)
  3403. */
  3404. goto clear_rcu;
  3405. }
  3406. fastrx.key = true;
  3407. fastrx.icv_len = key->conf.icv_len;
  3408. }
  3409. assign = true;
  3410. clear_rcu:
  3411. rcu_read_unlock();
  3412. clear:
  3413. __release(check_fast_rx);
  3414. if (assign)
  3415. new = kmemdup(&fastrx, sizeof(fastrx), GFP_KERNEL);
  3416. spin_lock_bh(&sta->lock);
  3417. old = rcu_dereference_protected(sta->fast_rx, true);
  3418. rcu_assign_pointer(sta->fast_rx, new);
  3419. spin_unlock_bh(&sta->lock);
  3420. if (old)
  3421. kfree_rcu(old, rcu_head);
  3422. }
  3423. void ieee80211_clear_fast_rx(struct sta_info *sta)
  3424. {
  3425. struct ieee80211_fast_rx *old;
  3426. spin_lock_bh(&sta->lock);
  3427. old = rcu_dereference_protected(sta->fast_rx, true);
  3428. RCU_INIT_POINTER(sta->fast_rx, NULL);
  3429. spin_unlock_bh(&sta->lock);
  3430. if (old)
  3431. kfree_rcu(old, rcu_head);
  3432. }
  3433. void __ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
  3434. {
  3435. struct ieee80211_local *local = sdata->local;
  3436. struct sta_info *sta;
  3437. lockdep_assert_held(&local->sta_mtx);
  3438. list_for_each_entry(sta, &local->sta_list, list) {
  3439. if (sdata != sta->sdata &&
  3440. (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
  3441. continue;
  3442. ieee80211_check_fast_rx(sta);
  3443. }
  3444. }
  3445. void ieee80211_check_fast_rx_iface(struct ieee80211_sub_if_data *sdata)
  3446. {
  3447. struct ieee80211_local *local = sdata->local;
  3448. mutex_lock(&local->sta_mtx);
  3449. __ieee80211_check_fast_rx_iface(sdata);
  3450. mutex_unlock(&local->sta_mtx);
  3451. }
  3452. static bool ieee80211_invoke_fast_rx(struct ieee80211_rx_data *rx,
  3453. struct ieee80211_fast_rx *fast_rx)
  3454. {
  3455. struct sk_buff *skb = rx->skb;
  3456. struct ieee80211_hdr *hdr = (void *)skb->data;
  3457. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3458. struct sta_info *sta = rx->sta;
  3459. int orig_len = skb->len;
  3460. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  3461. int snap_offs = hdrlen;
  3462. struct {
  3463. u8 snap[sizeof(rfc1042_header)];
  3464. __be16 proto;
  3465. } *payload __aligned(2);
  3466. struct {
  3467. u8 da[ETH_ALEN];
  3468. u8 sa[ETH_ALEN];
  3469. } addrs __aligned(2);
  3470. struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
  3471. if (fast_rx->uses_rss)
  3472. stats = this_cpu_ptr(sta->pcpu_rx_stats);
  3473. /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
  3474. * to a common data structure; drivers can implement that per queue
  3475. * but we don't have that information in mac80211
  3476. */
  3477. if (!(status->flag & RX_FLAG_DUP_VALIDATED))
  3478. return false;
  3479. #define FAST_RX_CRYPT_FLAGS (RX_FLAG_PN_VALIDATED | RX_FLAG_DECRYPTED)
  3480. /* If using encryption, we also need to have:
  3481. * - PN_VALIDATED: similar, but the implementation is tricky
  3482. * - DECRYPTED: necessary for PN_VALIDATED
  3483. */
  3484. if (fast_rx->key &&
  3485. (status->flag & FAST_RX_CRYPT_FLAGS) != FAST_RX_CRYPT_FLAGS)
  3486. return false;
  3487. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  3488. return false;
  3489. if (unlikely(ieee80211_is_frag(hdr)))
  3490. return false;
  3491. /* Since our interface address cannot be multicast, this
  3492. * implicitly also rejects multicast frames without the
  3493. * explicit check.
  3494. *
  3495. * We shouldn't get any *data* frames not addressed to us
  3496. * (AP mode will accept multicast *management* frames), but
  3497. * punting here will make it go through the full checks in
  3498. * ieee80211_accept_frame().
  3499. */
  3500. if (!ether_addr_equal(fast_rx->vif_addr, hdr->addr1))
  3501. return false;
  3502. if ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FROMDS |
  3503. IEEE80211_FCTL_TODS)) !=
  3504. fast_rx->expected_ds_bits)
  3505. return false;
  3506. /* assign the key to drop unencrypted frames (later)
  3507. * and strip the IV/MIC if necessary
  3508. */
  3509. if (fast_rx->key && !(status->flag & RX_FLAG_IV_STRIPPED)) {
  3510. /* GCMP header length is the same */
  3511. snap_offs += IEEE80211_CCMP_HDR_LEN;
  3512. }
  3513. if (!(status->rx_flags & IEEE80211_RX_AMSDU)) {
  3514. if (!pskb_may_pull(skb, snap_offs + sizeof(*payload)))
  3515. goto drop;
  3516. payload = (void *)(skb->data + snap_offs);
  3517. if (!ether_addr_equal(payload->snap, fast_rx->rfc1042_hdr))
  3518. return false;
  3519. /* Don't handle these here since they require special code.
  3520. * Accept AARP and IPX even though they should come with a
  3521. * bridge-tunnel header - but if we get them this way then
  3522. * there's little point in discarding them.
  3523. */
  3524. if (unlikely(payload->proto == cpu_to_be16(ETH_P_TDLS) ||
  3525. payload->proto == fast_rx->control_port_protocol))
  3526. return false;
  3527. }
  3528. /* after this point, don't punt to the slowpath! */
  3529. if (rx->key && !(status->flag & RX_FLAG_MIC_STRIPPED) &&
  3530. pskb_trim(skb, skb->len - fast_rx->icv_len))
  3531. goto drop;
  3532. if (unlikely(fast_rx->sta_notify)) {
  3533. ieee80211_sta_rx_notify(rx->sdata, hdr);
  3534. fast_rx->sta_notify = false;
  3535. }
  3536. /* statistics part of ieee80211_rx_h_sta_process() */
  3537. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  3538. stats->last_signal = status->signal;
  3539. if (!fast_rx->uses_rss)
  3540. ewma_signal_add(&sta->rx_stats_avg.signal,
  3541. -status->signal);
  3542. }
  3543. if (status->chains) {
  3544. int i;
  3545. stats->chains = status->chains;
  3546. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  3547. int signal = status->chain_signal[i];
  3548. if (!(status->chains & BIT(i)))
  3549. continue;
  3550. stats->chain_signal_last[i] = signal;
  3551. if (!fast_rx->uses_rss)
  3552. ewma_signal_add(&sta->rx_stats_avg.chain_signal[i],
  3553. -signal);
  3554. }
  3555. }
  3556. /* end of statistics */
  3557. if (rx->key && !ieee80211_has_protected(hdr->frame_control))
  3558. goto drop;
  3559. if (status->rx_flags & IEEE80211_RX_AMSDU) {
  3560. if (__ieee80211_rx_h_amsdu(rx, snap_offs - hdrlen) !=
  3561. RX_QUEUED)
  3562. goto drop;
  3563. return true;
  3564. }
  3565. stats->last_rx = jiffies;
  3566. stats->last_rate = sta_stats_encode_rate(status);
  3567. stats->fragments++;
  3568. stats->packets++;
  3569. /* do the header conversion - first grab the addresses */
  3570. ether_addr_copy(addrs.da, skb->data + fast_rx->da_offs);
  3571. ether_addr_copy(addrs.sa, skb->data + fast_rx->sa_offs);
  3572. /* remove the SNAP but leave the ethertype */
  3573. skb_pull(skb, snap_offs + sizeof(rfc1042_header));
  3574. /* push the addresses in front */
  3575. memcpy(skb_push(skb, sizeof(addrs)), &addrs, sizeof(addrs));
  3576. skb->dev = fast_rx->dev;
  3577. ieee80211_rx_stats(fast_rx->dev, skb->len);
  3578. /* The seqno index has the same property as needed
  3579. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  3580. * for non-QoS-data frames. Here we know it's a data
  3581. * frame, so count MSDUs.
  3582. */
  3583. u64_stats_update_begin(&stats->syncp);
  3584. stats->msdu[rx->seqno_idx]++;
  3585. stats->bytes += orig_len;
  3586. u64_stats_update_end(&stats->syncp);
  3587. if (fast_rx->internal_forward) {
  3588. struct sk_buff *xmit_skb = NULL;
  3589. bool multicast = is_multicast_ether_addr(skb->data);
  3590. if (multicast) {
  3591. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  3592. } else if (sta_info_get(rx->sdata, skb->data)) {
  3593. xmit_skb = skb;
  3594. skb = NULL;
  3595. }
  3596. if (xmit_skb) {
  3597. /*
  3598. * Send to wireless media and increase priority by 256
  3599. * to keep the received priority instead of
  3600. * reclassifying the frame (see cfg80211_classify8021d).
  3601. */
  3602. xmit_skb->priority += 256;
  3603. xmit_skb->protocol = htons(ETH_P_802_3);
  3604. skb_reset_network_header(xmit_skb);
  3605. skb_reset_mac_header(xmit_skb);
  3606. dev_queue_xmit(xmit_skb);
  3607. }
  3608. if (!skb)
  3609. return true;
  3610. }
  3611. /* deliver to local stack */
  3612. skb->protocol = eth_type_trans(skb, fast_rx->dev);
  3613. memset(skb->cb, 0, sizeof(skb->cb));
  3614. if (rx->napi)
  3615. napi_gro_receive(rx->napi, skb);
  3616. else
  3617. netif_receive_skb(skb);
  3618. return true;
  3619. drop:
  3620. dev_kfree_skb(skb);
  3621. stats->dropped++;
  3622. return true;
  3623. }
  3624. /*
  3625. * This function returns whether or not the SKB
  3626. * was destined for RX processing or not, which,
  3627. * if consume is true, is equivalent to whether
  3628. * or not the skb was consumed.
  3629. */
  3630. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  3631. struct sk_buff *skb, bool consume)
  3632. {
  3633. struct ieee80211_local *local = rx->local;
  3634. struct ieee80211_sub_if_data *sdata = rx->sdata;
  3635. rx->skb = skb;
  3636. /* See if we can do fast-rx; if we have to copy we already lost,
  3637. * so punt in that case. We should never have to deliver a data
  3638. * frame to multiple interfaces anyway.
  3639. *
  3640. * We skip the ieee80211_accept_frame() call and do the necessary
  3641. * checking inside ieee80211_invoke_fast_rx().
  3642. */
  3643. if (consume && rx->sta) {
  3644. struct ieee80211_fast_rx *fast_rx;
  3645. fast_rx = rcu_dereference(rx->sta->fast_rx);
  3646. if (fast_rx && ieee80211_invoke_fast_rx(rx, fast_rx))
  3647. return true;
  3648. }
  3649. if (!ieee80211_accept_frame(rx))
  3650. return false;
  3651. if (!consume) {
  3652. skb = skb_copy(skb, GFP_ATOMIC);
  3653. if (!skb) {
  3654. if (net_ratelimit())
  3655. wiphy_debug(local->hw.wiphy,
  3656. "failed to copy skb for %s\n",
  3657. sdata->name);
  3658. return true;
  3659. }
  3660. rx->skb = skb;
  3661. }
  3662. ieee80211_invoke_rx_handlers(rx);
  3663. return true;
  3664. }
  3665. /*
  3666. * This is the actual Rx frames handler. as it belongs to Rx path it must
  3667. * be called with rcu_read_lock protection.
  3668. */
  3669. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  3670. struct ieee80211_sta *pubsta,
  3671. struct sk_buff *skb,
  3672. struct napi_struct *napi)
  3673. {
  3674. struct ieee80211_local *local = hw_to_local(hw);
  3675. struct ieee80211_sub_if_data *sdata;
  3676. struct ieee80211_hdr *hdr;
  3677. __le16 fc;
  3678. struct ieee80211_rx_data rx;
  3679. struct ieee80211_sub_if_data *prev;
  3680. struct rhlist_head *tmp;
  3681. int err = 0;
  3682. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  3683. memset(&rx, 0, sizeof(rx));
  3684. rx.skb = skb;
  3685. rx.local = local;
  3686. rx.napi = napi;
  3687. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  3688. I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
  3689. if (ieee80211_is_mgmt(fc)) {
  3690. /* drop frame if too short for header */
  3691. if (skb->len < ieee80211_hdrlen(fc))
  3692. err = -ENOBUFS;
  3693. else
  3694. err = skb_linearize(skb);
  3695. } else {
  3696. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  3697. }
  3698. if (err) {
  3699. dev_kfree_skb(skb);
  3700. return;
  3701. }
  3702. hdr = (struct ieee80211_hdr *)skb->data;
  3703. ieee80211_parse_qos(&rx);
  3704. ieee80211_verify_alignment(&rx);
  3705. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
  3706. ieee80211_is_beacon(hdr->frame_control)))
  3707. ieee80211_scan_rx(local, skb);
  3708. if (ieee80211_is_data(fc)) {
  3709. struct sta_info *sta, *prev_sta;
  3710. if (pubsta) {
  3711. rx.sta = container_of(pubsta, struct sta_info, sta);
  3712. rx.sdata = rx.sta->sdata;
  3713. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3714. return;
  3715. goto out;
  3716. }
  3717. prev_sta = NULL;
  3718. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  3719. if (!prev_sta) {
  3720. prev_sta = sta;
  3721. continue;
  3722. }
  3723. rx.sta = prev_sta;
  3724. rx.sdata = prev_sta->sdata;
  3725. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  3726. prev_sta = sta;
  3727. }
  3728. if (prev_sta) {
  3729. rx.sta = prev_sta;
  3730. rx.sdata = prev_sta->sdata;
  3731. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3732. return;
  3733. goto out;
  3734. }
  3735. }
  3736. prev = NULL;
  3737. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  3738. if (!ieee80211_sdata_running(sdata))
  3739. continue;
  3740. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  3741. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  3742. continue;
  3743. /*
  3744. * frame is destined for this interface, but if it's
  3745. * not also for the previous one we handle that after
  3746. * the loop to avoid copying the SKB once too much
  3747. */
  3748. if (!prev) {
  3749. prev = sdata;
  3750. continue;
  3751. }
  3752. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  3753. rx.sdata = prev;
  3754. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  3755. prev = sdata;
  3756. }
  3757. if (prev) {
  3758. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  3759. rx.sdata = prev;
  3760. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  3761. return;
  3762. }
  3763. out:
  3764. dev_kfree_skb(skb);
  3765. }
  3766. /*
  3767. * This is the receive path handler. It is called by a low level driver when an
  3768. * 802.11 MPDU is received from the hardware.
  3769. */
  3770. void ieee80211_rx_napi(struct ieee80211_hw *hw, struct ieee80211_sta *pubsta,
  3771. struct sk_buff *skb, struct napi_struct *napi)
  3772. {
  3773. struct ieee80211_local *local = hw_to_local(hw);
  3774. struct ieee80211_rate *rate = NULL;
  3775. struct ieee80211_supported_band *sband;
  3776. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  3777. WARN_ON_ONCE(softirq_count() == 0);
  3778. if (WARN_ON(status->band >= NUM_NL80211_BANDS))
  3779. goto drop;
  3780. sband = local->hw.wiphy->bands[status->band];
  3781. if (WARN_ON(!sband))
  3782. goto drop;
  3783. /*
  3784. * If we're suspending, it is possible although not too likely
  3785. * that we'd be receiving frames after having already partially
  3786. * quiesced the stack. We can't process such frames then since
  3787. * that might, for example, cause stations to be added or other
  3788. * driver callbacks be invoked.
  3789. */
  3790. if (unlikely(local->quiescing || local->suspended))
  3791. goto drop;
  3792. /* We might be during a HW reconfig, prevent Rx for the same reason */
  3793. if (unlikely(local->in_reconfig))
  3794. goto drop;
  3795. /*
  3796. * The same happens when we're not even started,
  3797. * but that's worth a warning.
  3798. */
  3799. if (WARN_ON(!local->started))
  3800. goto drop;
  3801. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  3802. /*
  3803. * Validate the rate, unless a PLCP error means that
  3804. * we probably can't have a valid rate here anyway.
  3805. */
  3806. switch (status->encoding) {
  3807. case RX_ENC_HT:
  3808. /*
  3809. * rate_idx is MCS index, which can be [0-76]
  3810. * as documented on:
  3811. *
  3812. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  3813. *
  3814. * Anything else would be some sort of driver or
  3815. * hardware error. The driver should catch hardware
  3816. * errors.
  3817. */
  3818. if (WARN(status->rate_idx > 76,
  3819. "Rate marked as an HT rate but passed "
  3820. "status->rate_idx is not "
  3821. "an MCS index [0-76]: %d (0x%02x)\n",
  3822. status->rate_idx,
  3823. status->rate_idx))
  3824. goto drop;
  3825. break;
  3826. case RX_ENC_VHT:
  3827. if (WARN_ONCE(status->rate_idx > 9 ||
  3828. !status->nss ||
  3829. status->nss > 8,
  3830. "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
  3831. status->rate_idx, status->nss))
  3832. goto drop;
  3833. break;
  3834. case RX_ENC_HE:
  3835. if (WARN_ONCE(status->rate_idx > 11 ||
  3836. !status->nss ||
  3837. status->nss > 8,
  3838. "Rate marked as an HE rate but data is invalid: MCS: %d, NSS: %d\n",
  3839. status->rate_idx, status->nss))
  3840. goto drop;
  3841. break;
  3842. default:
  3843. WARN_ON_ONCE(1);
  3844. /* fall through */
  3845. case RX_ENC_LEGACY:
  3846. if (WARN_ON(status->rate_idx >= sband->n_bitrates))
  3847. goto drop;
  3848. rate = &sband->bitrates[status->rate_idx];
  3849. }
  3850. }
  3851. status->rx_flags = 0;
  3852. /*
  3853. * key references and virtual interfaces are protected using RCU
  3854. * and this requires that we are in a read-side RCU section during
  3855. * receive processing
  3856. */
  3857. rcu_read_lock();
  3858. /*
  3859. * Frames with failed FCS/PLCP checksum are not returned,
  3860. * all other frames are returned without radiotap header
  3861. * if it was previously present.
  3862. * Also, frames with less than 16 bytes are dropped.
  3863. */
  3864. skb = ieee80211_rx_monitor(local, skb, rate);
  3865. if (!skb) {
  3866. rcu_read_unlock();
  3867. return;
  3868. }
  3869. ieee80211_tpt_led_trig_rx(local,
  3870. ((struct ieee80211_hdr *)skb->data)->frame_control,
  3871. skb->len);
  3872. __ieee80211_rx_handle_packet(hw, pubsta, skb, napi);
  3873. rcu_read_unlock();
  3874. return;
  3875. drop:
  3876. kfree_skb(skb);
  3877. }
  3878. EXPORT_SYMBOL(ieee80211_rx_napi);
  3879. /* This is a version of the rx handler that can be called from hard irq
  3880. * context. Post the skb on the queue and schedule the tasklet */
  3881. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  3882. {
  3883. struct ieee80211_local *local = hw_to_local(hw);
  3884. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3885. skb->pkt_type = IEEE80211_RX_MSG;
  3886. skb_queue_tail(&local->skb_queue, skb);
  3887. tasklet_schedule(&local->tasklet);
  3888. }
  3889. EXPORT_SYMBOL(ieee80211_rx_irqsafe);