rx.c 84 KB

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