rx.c 87 KB

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