rx.c 97 KB

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