rx.c 84 KB

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