rx.c 81 KB

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