rx.c 84 KB

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