rx.c 86 KB

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