rx.c 95 KB

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