rx.c 84 KB

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