rx.c 89 KB

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