rx.c 86 KB

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