rx.c 101 KB

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