rx.c 87 KB

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