rx.c 86 KB

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