rx.c 102 KB

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