rx.c 86 KB

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