rx.c 101 KB

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