rx.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  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. rx->key->tx_rx_count++;
  866. /* TODO: add threshold stuff again */
  867. } else {
  868. return RX_DROP_MONITOR;
  869. }
  870. if (skb_linearize(rx->skb))
  871. return RX_DROP_UNUSABLE;
  872. /* the hdr variable is invalid now! */
  873. switch (rx->key->conf.cipher) {
  874. case WLAN_CIPHER_SUITE_WEP40:
  875. case WLAN_CIPHER_SUITE_WEP104:
  876. /* Check for weak IVs if possible */
  877. if (rx->sta && ieee80211_is_data(fc) &&
  878. (!(status->flag & RX_FLAG_IV_STRIPPED) ||
  879. !(status->flag & RX_FLAG_DECRYPTED)) &&
  880. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  881. rx->sta->wep_weak_iv_count++;
  882. result = ieee80211_crypto_wep_decrypt(rx);
  883. break;
  884. case WLAN_CIPHER_SUITE_TKIP:
  885. result = ieee80211_crypto_tkip_decrypt(rx);
  886. break;
  887. case WLAN_CIPHER_SUITE_CCMP:
  888. result = ieee80211_crypto_ccmp_decrypt(rx);
  889. break;
  890. case WLAN_CIPHER_SUITE_AES_CMAC:
  891. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  892. break;
  893. default:
  894. /*
  895. * We can reach here only with HW-only algorithms
  896. * but why didn't it decrypt the frame?!
  897. */
  898. return RX_DROP_UNUSABLE;
  899. }
  900. /* either the frame has been decrypted or will be dropped */
  901. status->flag |= RX_FLAG_DECRYPTED;
  902. return result;
  903. }
  904. static ieee80211_rx_result debug_noinline
  905. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  906. {
  907. struct ieee80211_local *local;
  908. struct ieee80211_hdr *hdr;
  909. struct sk_buff *skb;
  910. local = rx->local;
  911. skb = rx->skb;
  912. hdr = (struct ieee80211_hdr *) skb->data;
  913. if (!local->pspolling)
  914. return RX_CONTINUE;
  915. if (!ieee80211_has_fromds(hdr->frame_control))
  916. /* this is not from AP */
  917. return RX_CONTINUE;
  918. if (!ieee80211_is_data(hdr->frame_control))
  919. return RX_CONTINUE;
  920. if (!ieee80211_has_moredata(hdr->frame_control)) {
  921. /* AP has no more frames buffered for us */
  922. local->pspolling = false;
  923. return RX_CONTINUE;
  924. }
  925. /* more data bit is set, let's request a new frame from the AP */
  926. ieee80211_send_pspoll(local, rx->sdata);
  927. return RX_CONTINUE;
  928. }
  929. static void ap_sta_ps_start(struct sta_info *sta)
  930. {
  931. struct ieee80211_sub_if_data *sdata = sta->sdata;
  932. struct ieee80211_local *local = sdata->local;
  933. atomic_inc(&sdata->bss->num_sta_ps);
  934. set_sta_flags(sta, WLAN_STA_PS_STA);
  935. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  936. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  937. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  938. printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
  939. sdata->name, sta->sta.addr, sta->sta.aid);
  940. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  941. }
  942. static void ap_sta_ps_end(struct sta_info *sta)
  943. {
  944. struct ieee80211_sub_if_data *sdata = sta->sdata;
  945. atomic_dec(&sdata->bss->num_sta_ps);
  946. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  947. printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
  948. sdata->name, sta->sta.addr, sta->sta.aid);
  949. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  950. if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
  951. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  952. printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
  953. sdata->name, sta->sta.addr, sta->sta.aid);
  954. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  955. return;
  956. }
  957. ieee80211_sta_ps_deliver_wakeup(sta);
  958. }
  959. int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
  960. {
  961. struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
  962. bool in_ps;
  963. WARN_ON(!(sta_inf->local->hw.flags & IEEE80211_HW_AP_LINK_PS));
  964. /* Don't let the same PS state be set twice */
  965. in_ps = test_sta_flags(sta_inf, WLAN_STA_PS_STA);
  966. if ((start && in_ps) || (!start && !in_ps))
  967. return -EINVAL;
  968. if (start)
  969. ap_sta_ps_start(sta_inf);
  970. else
  971. ap_sta_ps_end(sta_inf);
  972. return 0;
  973. }
  974. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  975. static ieee80211_rx_result debug_noinline
  976. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  977. {
  978. struct sta_info *sta = rx->sta;
  979. struct sk_buff *skb = rx->skb;
  980. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  981. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  982. if (!sta)
  983. return RX_CONTINUE;
  984. /*
  985. * Update last_rx only for IBSS packets which are for the current
  986. * BSSID to avoid keeping the current IBSS network alive in cases
  987. * where other STAs start using different BSSID.
  988. */
  989. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  990. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  991. NL80211_IFTYPE_ADHOC);
  992. if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0) {
  993. sta->last_rx = jiffies;
  994. if (ieee80211_is_data(hdr->frame_control)) {
  995. sta->last_rx_rate_idx = status->rate_idx;
  996. sta->last_rx_rate_flag = status->flag;
  997. }
  998. }
  999. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1000. /*
  1001. * Mesh beacons will update last_rx when if they are found to
  1002. * match the current local configuration when processed.
  1003. */
  1004. sta->last_rx = jiffies;
  1005. if (ieee80211_is_data(hdr->frame_control)) {
  1006. sta->last_rx_rate_idx = status->rate_idx;
  1007. sta->last_rx_rate_flag = status->flag;
  1008. }
  1009. }
  1010. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1011. return RX_CONTINUE;
  1012. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1013. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1014. sta->rx_fragments++;
  1015. sta->rx_bytes += rx->skb->len;
  1016. sta->last_signal = status->signal;
  1017. ewma_add(&sta->avg_signal, -status->signal);
  1018. /*
  1019. * Change STA power saving mode only at the end of a frame
  1020. * exchange sequence.
  1021. */
  1022. if (!(sta->local->hw.flags & IEEE80211_HW_AP_LINK_PS) &&
  1023. !ieee80211_has_morefrags(hdr->frame_control) &&
  1024. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1025. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1026. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  1027. if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
  1028. /*
  1029. * Ignore doze->wake transitions that are
  1030. * indicated by non-data frames, the standard
  1031. * is unclear here, but for example going to
  1032. * PS mode and then scanning would cause a
  1033. * doze->wake transition for the probe request,
  1034. * and that is clearly undesirable.
  1035. */
  1036. if (ieee80211_is_data(hdr->frame_control) &&
  1037. !ieee80211_has_pm(hdr->frame_control))
  1038. ap_sta_ps_end(sta);
  1039. } else {
  1040. if (ieee80211_has_pm(hdr->frame_control))
  1041. ap_sta_ps_start(sta);
  1042. }
  1043. }
  1044. /*
  1045. * Drop (qos-)data::nullfunc frames silently, since they
  1046. * are used only to control station power saving mode.
  1047. */
  1048. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1049. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1050. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1051. /*
  1052. * If we receive a 4-addr nullfunc frame from a STA
  1053. * that was not moved to a 4-addr STA vlan yet, drop
  1054. * the frame to the monitor interface, to make sure
  1055. * that hostapd sees it
  1056. */
  1057. if (ieee80211_has_a4(hdr->frame_control) &&
  1058. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1059. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1060. !rx->sdata->u.vlan.sta)))
  1061. return RX_DROP_MONITOR;
  1062. /*
  1063. * Update counter and free packet here to avoid
  1064. * counting this as a dropped packed.
  1065. */
  1066. sta->rx_packets++;
  1067. dev_kfree_skb(rx->skb);
  1068. return RX_QUEUED;
  1069. }
  1070. return RX_CONTINUE;
  1071. } /* ieee80211_rx_h_sta_process */
  1072. static inline struct ieee80211_fragment_entry *
  1073. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1074. unsigned int frag, unsigned int seq, int rx_queue,
  1075. struct sk_buff **skb)
  1076. {
  1077. struct ieee80211_fragment_entry *entry;
  1078. int idx;
  1079. idx = sdata->fragment_next;
  1080. entry = &sdata->fragments[sdata->fragment_next++];
  1081. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1082. sdata->fragment_next = 0;
  1083. if (!skb_queue_empty(&entry->skb_list)) {
  1084. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1085. struct ieee80211_hdr *hdr =
  1086. (struct ieee80211_hdr *) entry->skb_list.next->data;
  1087. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  1088. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  1089. "addr1=%pM addr2=%pM\n",
  1090. sdata->name, idx,
  1091. jiffies - entry->first_frag_time, entry->seq,
  1092. entry->last_frag, hdr->addr1, hdr->addr2);
  1093. #endif
  1094. __skb_queue_purge(&entry->skb_list);
  1095. }
  1096. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1097. *skb = NULL;
  1098. entry->first_frag_time = jiffies;
  1099. entry->seq = seq;
  1100. entry->rx_queue = rx_queue;
  1101. entry->last_frag = frag;
  1102. entry->ccmp = 0;
  1103. entry->extra_len = 0;
  1104. return entry;
  1105. }
  1106. static inline struct ieee80211_fragment_entry *
  1107. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1108. unsigned int frag, unsigned int seq,
  1109. int rx_queue, struct ieee80211_hdr *hdr)
  1110. {
  1111. struct ieee80211_fragment_entry *entry;
  1112. int i, idx;
  1113. idx = sdata->fragment_next;
  1114. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1115. struct ieee80211_hdr *f_hdr;
  1116. idx--;
  1117. if (idx < 0)
  1118. idx = IEEE80211_FRAGMENT_MAX - 1;
  1119. entry = &sdata->fragments[idx];
  1120. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1121. entry->rx_queue != rx_queue ||
  1122. entry->last_frag + 1 != frag)
  1123. continue;
  1124. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1125. /*
  1126. * Check ftype and addresses are equal, else check next fragment
  1127. */
  1128. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1129. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1130. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  1131. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  1132. continue;
  1133. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1134. __skb_queue_purge(&entry->skb_list);
  1135. continue;
  1136. }
  1137. return entry;
  1138. }
  1139. return NULL;
  1140. }
  1141. static ieee80211_rx_result debug_noinline
  1142. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1143. {
  1144. struct ieee80211_hdr *hdr;
  1145. u16 sc;
  1146. __le16 fc;
  1147. unsigned int frag, seq;
  1148. struct ieee80211_fragment_entry *entry;
  1149. struct sk_buff *skb;
  1150. struct ieee80211_rx_status *status;
  1151. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1152. fc = hdr->frame_control;
  1153. sc = le16_to_cpu(hdr->seq_ctrl);
  1154. frag = sc & IEEE80211_SCTL_FRAG;
  1155. if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
  1156. (rx->skb)->len < 24 ||
  1157. is_multicast_ether_addr(hdr->addr1))) {
  1158. /* not fragmented */
  1159. goto out;
  1160. }
  1161. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1162. if (skb_linearize(rx->skb))
  1163. return RX_DROP_UNUSABLE;
  1164. /*
  1165. * skb_linearize() might change the skb->data and
  1166. * previously cached variables (in this case, hdr) need to
  1167. * be refreshed with the new data.
  1168. */
  1169. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1170. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1171. if (frag == 0) {
  1172. /* This is the first fragment of a new frame. */
  1173. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1174. rx->seqno_idx, &(rx->skb));
  1175. if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
  1176. ieee80211_has_protected(fc)) {
  1177. int queue = rx->security_idx;
  1178. /* Store CCMP PN so that we can verify that the next
  1179. * fragment has a sequential PN value. */
  1180. entry->ccmp = 1;
  1181. memcpy(entry->last_pn,
  1182. rx->key->u.ccmp.rx_pn[queue],
  1183. CCMP_PN_LEN);
  1184. }
  1185. return RX_QUEUED;
  1186. }
  1187. /* This is a fragment for a frame that should already be pending in
  1188. * fragment cache. Add this fragment to the end of the pending entry.
  1189. */
  1190. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1191. rx->seqno_idx, hdr);
  1192. if (!entry) {
  1193. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1194. return RX_DROP_MONITOR;
  1195. }
  1196. /* Verify that MPDUs within one MSDU have sequential PN values.
  1197. * (IEEE 802.11i, 8.3.3.4.5) */
  1198. if (entry->ccmp) {
  1199. int i;
  1200. u8 pn[CCMP_PN_LEN], *rpn;
  1201. int queue;
  1202. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
  1203. return RX_DROP_UNUSABLE;
  1204. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  1205. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  1206. pn[i]++;
  1207. if (pn[i])
  1208. break;
  1209. }
  1210. queue = rx->security_idx;
  1211. rpn = rx->key->u.ccmp.rx_pn[queue];
  1212. if (memcmp(pn, rpn, CCMP_PN_LEN))
  1213. return RX_DROP_UNUSABLE;
  1214. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  1215. }
  1216. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1217. __skb_queue_tail(&entry->skb_list, rx->skb);
  1218. entry->last_frag = frag;
  1219. entry->extra_len += rx->skb->len;
  1220. if (ieee80211_has_morefrags(fc)) {
  1221. rx->skb = NULL;
  1222. return RX_QUEUED;
  1223. }
  1224. rx->skb = __skb_dequeue(&entry->skb_list);
  1225. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1226. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  1227. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1228. GFP_ATOMIC))) {
  1229. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1230. __skb_queue_purge(&entry->skb_list);
  1231. return RX_DROP_UNUSABLE;
  1232. }
  1233. }
  1234. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1235. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1236. dev_kfree_skb(skb);
  1237. }
  1238. /* Complete frame has been reassembled - process it now */
  1239. status = IEEE80211_SKB_RXCB(rx->skb);
  1240. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1241. out:
  1242. if (rx->sta)
  1243. rx->sta->rx_packets++;
  1244. if (is_multicast_ether_addr(hdr->addr1))
  1245. rx->local->dot11MulticastReceivedFrameCount++;
  1246. else
  1247. ieee80211_led_rx(rx->local);
  1248. return RX_CONTINUE;
  1249. }
  1250. static ieee80211_rx_result debug_noinline
  1251. ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
  1252. {
  1253. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1254. __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
  1255. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1256. if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
  1257. !(status->rx_flags & IEEE80211_RX_RA_MATCH)))
  1258. return RX_CONTINUE;
  1259. if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
  1260. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
  1261. return RX_DROP_UNUSABLE;
  1262. if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
  1263. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  1264. else
  1265. set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
  1266. /* Free PS Poll skb here instead of returning RX_DROP that would
  1267. * count as an dropped frame. */
  1268. dev_kfree_skb(rx->skb);
  1269. return RX_QUEUED;
  1270. }
  1271. static ieee80211_rx_result debug_noinline
  1272. ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
  1273. {
  1274. u8 *data = rx->skb->data;
  1275. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  1276. if (!ieee80211_is_data_qos(hdr->frame_control))
  1277. return RX_CONTINUE;
  1278. /* remove the qos control field, update frame type and meta-data */
  1279. memmove(data + IEEE80211_QOS_CTL_LEN, data,
  1280. ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
  1281. hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
  1282. /* change frame type to non QOS */
  1283. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1284. return RX_CONTINUE;
  1285. }
  1286. static int
  1287. ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1288. {
  1289. if (unlikely(!rx->sta ||
  1290. !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
  1291. return -EACCES;
  1292. return 0;
  1293. }
  1294. static int
  1295. ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1296. {
  1297. struct sk_buff *skb = rx->skb;
  1298. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1299. /*
  1300. * Pass through unencrypted frames if the hardware has
  1301. * decrypted them already.
  1302. */
  1303. if (status->flag & RX_FLAG_DECRYPTED)
  1304. return 0;
  1305. /* Drop unencrypted frames if key is set. */
  1306. if (unlikely(!ieee80211_has_protected(fc) &&
  1307. !ieee80211_is_nullfunc(fc) &&
  1308. ieee80211_is_data(fc) &&
  1309. (rx->key || rx->sdata->drop_unencrypted)))
  1310. return -EACCES;
  1311. return 0;
  1312. }
  1313. static int
  1314. ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1315. {
  1316. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1317. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1318. __le16 fc = hdr->frame_control;
  1319. /*
  1320. * Pass through unencrypted frames if the hardware has
  1321. * decrypted them already.
  1322. */
  1323. if (status->flag & RX_FLAG_DECRYPTED)
  1324. return 0;
  1325. if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
  1326. if (unlikely(!ieee80211_has_protected(fc) &&
  1327. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1328. rx->key)) {
  1329. if (ieee80211_is_deauth(fc))
  1330. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1331. rx->skb->data,
  1332. rx->skb->len);
  1333. else if (ieee80211_is_disassoc(fc))
  1334. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1335. rx->skb->data,
  1336. rx->skb->len);
  1337. return -EACCES;
  1338. }
  1339. /* BIP does not use Protected field, so need to check MMIE */
  1340. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1341. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1342. if (ieee80211_is_deauth(fc))
  1343. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1344. rx->skb->data,
  1345. rx->skb->len);
  1346. else if (ieee80211_is_disassoc(fc))
  1347. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1348. rx->skb->data,
  1349. rx->skb->len);
  1350. return -EACCES;
  1351. }
  1352. /*
  1353. * When using MFP, Action frames are not allowed prior to
  1354. * having configured keys.
  1355. */
  1356. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1357. ieee80211_is_robust_mgmt_frame(
  1358. (struct ieee80211_hdr *) rx->skb->data)))
  1359. return -EACCES;
  1360. }
  1361. return 0;
  1362. }
  1363. static int
  1364. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1365. {
  1366. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1367. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1368. bool check_port_control = false;
  1369. struct ethhdr *ehdr;
  1370. int ret;
  1371. *port_control = false;
  1372. if (ieee80211_has_a4(hdr->frame_control) &&
  1373. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1374. return -1;
  1375. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1376. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1377. if (!sdata->u.mgd.use_4addr)
  1378. return -1;
  1379. else
  1380. check_port_control = true;
  1381. }
  1382. if (is_multicast_ether_addr(hdr->addr1) &&
  1383. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1384. return -1;
  1385. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1386. if (ret < 0)
  1387. return ret;
  1388. ehdr = (struct ethhdr *) rx->skb->data;
  1389. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1390. *port_control = true;
  1391. else if (check_port_control)
  1392. return -1;
  1393. return 0;
  1394. }
  1395. /*
  1396. * requires that rx->skb is a frame with ethernet header
  1397. */
  1398. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1399. {
  1400. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1401. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1402. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1403. /*
  1404. * Allow EAPOL frames to us/the PAE group address regardless
  1405. * of whether the frame was encrypted or not.
  1406. */
  1407. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1408. (compare_ether_addr(ehdr->h_dest, rx->sdata->vif.addr) == 0 ||
  1409. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1410. return true;
  1411. if (ieee80211_802_1x_port_control(rx) ||
  1412. ieee80211_drop_unencrypted(rx, fc))
  1413. return false;
  1414. return true;
  1415. }
  1416. /*
  1417. * requires that rx->skb is a frame with ethernet header
  1418. */
  1419. static void
  1420. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1421. {
  1422. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1423. struct net_device *dev = sdata->dev;
  1424. struct sk_buff *skb, *xmit_skb;
  1425. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1426. struct sta_info *dsta;
  1427. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1428. skb = rx->skb;
  1429. xmit_skb = NULL;
  1430. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1431. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1432. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1433. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1434. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1435. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1436. /*
  1437. * send multicast frames both to higher layers in
  1438. * local net stack and back to the wireless medium
  1439. */
  1440. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1441. if (!xmit_skb && net_ratelimit())
  1442. printk(KERN_DEBUG "%s: failed to clone "
  1443. "multicast frame\n", dev->name);
  1444. } else {
  1445. dsta = sta_info_get(sdata, skb->data);
  1446. if (dsta) {
  1447. /*
  1448. * The destination station is associated to
  1449. * this AP (in this VLAN), so send the frame
  1450. * directly to it and do not pass it to local
  1451. * net stack.
  1452. */
  1453. xmit_skb = skb;
  1454. skb = NULL;
  1455. }
  1456. }
  1457. }
  1458. if (skb) {
  1459. int align __maybe_unused;
  1460. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1461. /*
  1462. * 'align' will only take the values 0 or 2 here
  1463. * since all frames are required to be aligned
  1464. * to 2-byte boundaries when being passed to
  1465. * mac80211. That also explains the __skb_push()
  1466. * below.
  1467. */
  1468. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1469. if (align) {
  1470. if (WARN_ON(skb_headroom(skb) < 3)) {
  1471. dev_kfree_skb(skb);
  1472. skb = NULL;
  1473. } else {
  1474. u8 *data = skb->data;
  1475. size_t len = skb_headlen(skb);
  1476. skb->data -= align;
  1477. memmove(skb->data, data, len);
  1478. skb_set_tail_pointer(skb, len);
  1479. }
  1480. }
  1481. #endif
  1482. if (skb) {
  1483. /* deliver to local stack */
  1484. skb->protocol = eth_type_trans(skb, dev);
  1485. memset(skb->cb, 0, sizeof(skb->cb));
  1486. netif_receive_skb(skb);
  1487. }
  1488. }
  1489. if (xmit_skb) {
  1490. /* send to wireless media */
  1491. xmit_skb->protocol = htons(ETH_P_802_3);
  1492. skb_reset_network_header(xmit_skb);
  1493. skb_reset_mac_header(xmit_skb);
  1494. dev_queue_xmit(xmit_skb);
  1495. }
  1496. }
  1497. static ieee80211_rx_result debug_noinline
  1498. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1499. {
  1500. struct net_device *dev = rx->sdata->dev;
  1501. struct sk_buff *skb = rx->skb;
  1502. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1503. __le16 fc = hdr->frame_control;
  1504. struct sk_buff_head frame_list;
  1505. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1506. if (unlikely(!ieee80211_is_data(fc)))
  1507. return RX_CONTINUE;
  1508. if (unlikely(!ieee80211_is_data_present(fc)))
  1509. return RX_DROP_MONITOR;
  1510. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1511. return RX_CONTINUE;
  1512. if (ieee80211_has_a4(hdr->frame_control) &&
  1513. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1514. !rx->sdata->u.vlan.sta)
  1515. return RX_DROP_UNUSABLE;
  1516. if (is_multicast_ether_addr(hdr->addr1) &&
  1517. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1518. rx->sdata->u.vlan.sta) ||
  1519. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1520. rx->sdata->u.mgd.use_4addr)))
  1521. return RX_DROP_UNUSABLE;
  1522. skb->dev = dev;
  1523. __skb_queue_head_init(&frame_list);
  1524. if (skb_linearize(skb))
  1525. return RX_DROP_UNUSABLE;
  1526. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1527. rx->sdata->vif.type,
  1528. rx->local->hw.extra_tx_headroom, true);
  1529. while (!skb_queue_empty(&frame_list)) {
  1530. rx->skb = __skb_dequeue(&frame_list);
  1531. if (!ieee80211_frame_allowed(rx, fc)) {
  1532. dev_kfree_skb(rx->skb);
  1533. continue;
  1534. }
  1535. dev->stats.rx_packets++;
  1536. dev->stats.rx_bytes += rx->skb->len;
  1537. ieee80211_deliver_skb(rx);
  1538. }
  1539. return RX_QUEUED;
  1540. }
  1541. #ifdef CONFIG_MAC80211_MESH
  1542. static ieee80211_rx_result
  1543. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1544. {
  1545. struct ieee80211_hdr *hdr;
  1546. struct ieee80211s_hdr *mesh_hdr;
  1547. unsigned int hdrlen;
  1548. struct sk_buff *skb = rx->skb, *fwd_skb;
  1549. struct ieee80211_local *local = rx->local;
  1550. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1551. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1552. hdr = (struct ieee80211_hdr *) skb->data;
  1553. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1554. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1555. if (!ieee80211_is_data(hdr->frame_control))
  1556. return RX_CONTINUE;
  1557. if (!mesh_hdr->ttl)
  1558. /* illegal frame */
  1559. return RX_DROP_MONITOR;
  1560. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1561. struct mesh_path *mppath;
  1562. char *proxied_addr;
  1563. char *mpp_addr;
  1564. if (is_multicast_ether_addr(hdr->addr1)) {
  1565. mpp_addr = hdr->addr3;
  1566. proxied_addr = mesh_hdr->eaddr1;
  1567. } else {
  1568. mpp_addr = hdr->addr4;
  1569. proxied_addr = mesh_hdr->eaddr2;
  1570. }
  1571. rcu_read_lock();
  1572. mppath = mpp_path_lookup(proxied_addr, sdata);
  1573. if (!mppath) {
  1574. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1575. } else {
  1576. spin_lock_bh(&mppath->state_lock);
  1577. if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
  1578. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1579. spin_unlock_bh(&mppath->state_lock);
  1580. }
  1581. rcu_read_unlock();
  1582. }
  1583. /* Frame has reached destination. Don't forward */
  1584. if (!is_multicast_ether_addr(hdr->addr1) &&
  1585. compare_ether_addr(sdata->vif.addr, hdr->addr3) == 0)
  1586. return RX_CONTINUE;
  1587. mesh_hdr->ttl--;
  1588. if (status->rx_flags & IEEE80211_RX_RA_MATCH) {
  1589. if (!mesh_hdr->ttl)
  1590. IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
  1591. dropped_frames_ttl);
  1592. else {
  1593. struct ieee80211_hdr *fwd_hdr;
  1594. struct ieee80211_tx_info *info;
  1595. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1596. if (!fwd_skb && net_ratelimit())
  1597. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1598. sdata->name);
  1599. if (!fwd_skb)
  1600. goto out;
  1601. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1602. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1603. info = IEEE80211_SKB_CB(fwd_skb);
  1604. memset(info, 0, sizeof(*info));
  1605. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1606. info->control.vif = &rx->sdata->vif;
  1607. skb_set_queue_mapping(skb,
  1608. ieee80211_select_queue(rx->sdata, fwd_skb));
  1609. ieee80211_set_qos_hdr(local, skb);
  1610. if (is_multicast_ether_addr(fwd_hdr->addr1))
  1611. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1612. fwded_mcast);
  1613. else {
  1614. int err;
  1615. /*
  1616. * Save TA to addr1 to send TA a path error if a
  1617. * suitable next hop is not found
  1618. */
  1619. memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
  1620. ETH_ALEN);
  1621. err = mesh_nexthop_lookup(fwd_skb, sdata);
  1622. /* Failed to immediately resolve next hop:
  1623. * fwded frame was dropped or will be added
  1624. * later to the pending skb queue. */
  1625. if (err)
  1626. return RX_DROP_MONITOR;
  1627. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1628. fwded_unicast);
  1629. }
  1630. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1631. fwded_frames);
  1632. ieee80211_add_pending_skb(local, fwd_skb);
  1633. }
  1634. }
  1635. out:
  1636. if (is_multicast_ether_addr(hdr->addr1) ||
  1637. sdata->dev->flags & IFF_PROMISC)
  1638. return RX_CONTINUE;
  1639. else
  1640. return RX_DROP_MONITOR;
  1641. }
  1642. #endif
  1643. static ieee80211_rx_result debug_noinline
  1644. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1645. {
  1646. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1647. struct ieee80211_local *local = rx->local;
  1648. struct net_device *dev = sdata->dev;
  1649. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1650. __le16 fc = hdr->frame_control;
  1651. bool port_control;
  1652. int err;
  1653. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1654. return RX_CONTINUE;
  1655. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1656. return RX_DROP_MONITOR;
  1657. /*
  1658. * Allow the cooked monitor interface of an AP to see 4-addr frames so
  1659. * that a 4-addr station can be detected and moved into a separate VLAN
  1660. */
  1661. if (ieee80211_has_a4(hdr->frame_control) &&
  1662. sdata->vif.type == NL80211_IFTYPE_AP)
  1663. return RX_DROP_MONITOR;
  1664. err = __ieee80211_data_to_8023(rx, &port_control);
  1665. if (unlikely(err))
  1666. return RX_DROP_UNUSABLE;
  1667. if (!ieee80211_frame_allowed(rx, fc))
  1668. return RX_DROP_MONITOR;
  1669. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1670. unlikely(port_control) && sdata->bss) {
  1671. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1672. u.ap);
  1673. dev = sdata->dev;
  1674. rx->sdata = sdata;
  1675. }
  1676. rx->skb->dev = dev;
  1677. dev->stats.rx_packets++;
  1678. dev->stats.rx_bytes += rx->skb->len;
  1679. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  1680. !is_multicast_ether_addr(
  1681. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  1682. (!local->scanning &&
  1683. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  1684. mod_timer(&local->dynamic_ps_timer, jiffies +
  1685. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1686. }
  1687. ieee80211_deliver_skb(rx);
  1688. return RX_QUEUED;
  1689. }
  1690. static ieee80211_rx_result debug_noinline
  1691. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1692. {
  1693. struct ieee80211_local *local = rx->local;
  1694. struct ieee80211_hw *hw = &local->hw;
  1695. struct sk_buff *skb = rx->skb;
  1696. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1697. struct tid_ampdu_rx *tid_agg_rx;
  1698. u16 start_seq_num;
  1699. u16 tid;
  1700. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1701. return RX_CONTINUE;
  1702. if (ieee80211_is_back_req(bar->frame_control)) {
  1703. struct {
  1704. __le16 control, start_seq_num;
  1705. } __packed bar_data;
  1706. if (!rx->sta)
  1707. return RX_DROP_MONITOR;
  1708. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  1709. &bar_data, sizeof(bar_data)))
  1710. return RX_DROP_MONITOR;
  1711. tid = le16_to_cpu(bar_data.control) >> 12;
  1712. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  1713. if (!tid_agg_rx)
  1714. return RX_DROP_MONITOR;
  1715. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  1716. /* reset session timer */
  1717. if (tid_agg_rx->timeout)
  1718. mod_timer(&tid_agg_rx->session_timer,
  1719. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1720. spin_lock(&tid_agg_rx->reorder_lock);
  1721. /* release stored frames up to start of BAR */
  1722. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1723. spin_unlock(&tid_agg_rx->reorder_lock);
  1724. kfree_skb(skb);
  1725. return RX_QUEUED;
  1726. }
  1727. /*
  1728. * After this point, we only want management frames,
  1729. * so we can drop all remaining control frames to
  1730. * cooked monitor interfaces.
  1731. */
  1732. return RX_DROP_MONITOR;
  1733. }
  1734. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1735. struct ieee80211_mgmt *mgmt,
  1736. size_t len)
  1737. {
  1738. struct ieee80211_local *local = sdata->local;
  1739. struct sk_buff *skb;
  1740. struct ieee80211_mgmt *resp;
  1741. if (compare_ether_addr(mgmt->da, sdata->vif.addr) != 0) {
  1742. /* Not to own unicast address */
  1743. return;
  1744. }
  1745. if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
  1746. compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
  1747. /* Not from the current AP or not associated yet. */
  1748. return;
  1749. }
  1750. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1751. /* Too short SA Query request frame */
  1752. return;
  1753. }
  1754. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1755. if (skb == NULL)
  1756. return;
  1757. skb_reserve(skb, local->hw.extra_tx_headroom);
  1758. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1759. memset(resp, 0, 24);
  1760. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1761. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  1762. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1763. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1764. IEEE80211_STYPE_ACTION);
  1765. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1766. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1767. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1768. memcpy(resp->u.action.u.sa_query.trans_id,
  1769. mgmt->u.action.u.sa_query.trans_id,
  1770. WLAN_SA_QUERY_TR_ID_LEN);
  1771. ieee80211_tx_skb(sdata, skb);
  1772. }
  1773. static ieee80211_rx_result debug_noinline
  1774. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  1775. {
  1776. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1777. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1778. /*
  1779. * From here on, look only at management frames.
  1780. * Data and control frames are already handled,
  1781. * and unknown (reserved) frames are useless.
  1782. */
  1783. if (rx->skb->len < 24)
  1784. return RX_DROP_MONITOR;
  1785. if (!ieee80211_is_mgmt(mgmt->frame_control))
  1786. return RX_DROP_MONITOR;
  1787. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1788. return RX_DROP_MONITOR;
  1789. if (ieee80211_drop_unencrypted_mgmt(rx))
  1790. return RX_DROP_UNUSABLE;
  1791. return RX_CONTINUE;
  1792. }
  1793. static ieee80211_rx_result debug_noinline
  1794. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1795. {
  1796. struct ieee80211_local *local = rx->local;
  1797. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1798. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1799. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1800. int len = rx->skb->len;
  1801. if (!ieee80211_is_action(mgmt->frame_control))
  1802. return RX_CONTINUE;
  1803. /* drop too small frames */
  1804. if (len < IEEE80211_MIN_ACTION_SIZE)
  1805. return RX_DROP_UNUSABLE;
  1806. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
  1807. return RX_DROP_UNUSABLE;
  1808. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1809. return RX_DROP_UNUSABLE;
  1810. switch (mgmt->u.action.category) {
  1811. case WLAN_CATEGORY_BACK:
  1812. /*
  1813. * The aggregation code is not prepared to handle
  1814. * anything but STA/AP due to the BSSID handling;
  1815. * IBSS could work in the code but isn't supported
  1816. * by drivers or the standard.
  1817. */
  1818. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1819. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1820. sdata->vif.type != NL80211_IFTYPE_AP)
  1821. break;
  1822. /* verify action_code is present */
  1823. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1824. break;
  1825. switch (mgmt->u.action.u.addba_req.action_code) {
  1826. case WLAN_ACTION_ADDBA_REQ:
  1827. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1828. sizeof(mgmt->u.action.u.addba_req)))
  1829. goto invalid;
  1830. break;
  1831. case WLAN_ACTION_ADDBA_RESP:
  1832. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1833. sizeof(mgmt->u.action.u.addba_resp)))
  1834. goto invalid;
  1835. break;
  1836. case WLAN_ACTION_DELBA:
  1837. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1838. sizeof(mgmt->u.action.u.delba)))
  1839. goto invalid;
  1840. break;
  1841. default:
  1842. goto invalid;
  1843. }
  1844. goto queue;
  1845. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1846. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1847. break;
  1848. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1849. break;
  1850. /* verify action_code is present */
  1851. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1852. break;
  1853. switch (mgmt->u.action.u.measurement.action_code) {
  1854. case WLAN_ACTION_SPCT_MSR_REQ:
  1855. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1856. sizeof(mgmt->u.action.u.measurement)))
  1857. break;
  1858. ieee80211_process_measurement_req(sdata, mgmt, len);
  1859. goto handled;
  1860. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1861. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1862. sizeof(mgmt->u.action.u.chan_switch)))
  1863. break;
  1864. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1865. break;
  1866. if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
  1867. break;
  1868. goto queue;
  1869. }
  1870. break;
  1871. case WLAN_CATEGORY_SA_QUERY:
  1872. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1873. sizeof(mgmt->u.action.u.sa_query)))
  1874. break;
  1875. switch (mgmt->u.action.u.sa_query.action) {
  1876. case WLAN_ACTION_SA_QUERY_REQUEST:
  1877. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1878. break;
  1879. ieee80211_process_sa_query_req(sdata, mgmt, len);
  1880. goto handled;
  1881. }
  1882. break;
  1883. case WLAN_CATEGORY_MESH_ACTION:
  1884. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1885. break;
  1886. goto queue;
  1887. case WLAN_CATEGORY_MESH_PATH_SEL:
  1888. if (!mesh_path_sel_is_hwmp(sdata))
  1889. break;
  1890. goto queue;
  1891. }
  1892. return RX_CONTINUE;
  1893. invalid:
  1894. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  1895. /* will return in the next handlers */
  1896. return RX_CONTINUE;
  1897. handled:
  1898. if (rx->sta)
  1899. rx->sta->rx_packets++;
  1900. dev_kfree_skb(rx->skb);
  1901. return RX_QUEUED;
  1902. queue:
  1903. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  1904. skb_queue_tail(&sdata->skb_queue, rx->skb);
  1905. ieee80211_queue_work(&local->hw, &sdata->work);
  1906. if (rx->sta)
  1907. rx->sta->rx_packets++;
  1908. return RX_QUEUED;
  1909. }
  1910. static ieee80211_rx_result debug_noinline
  1911. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  1912. {
  1913. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1914. /* skip known-bad action frames and return them in the next handler */
  1915. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  1916. return RX_CONTINUE;
  1917. /*
  1918. * Getting here means the kernel doesn't know how to handle
  1919. * it, but maybe userspace does ... include returned frames
  1920. * so userspace can register for those to know whether ones
  1921. * it transmitted were processed or returned.
  1922. */
  1923. if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq,
  1924. rx->skb->data, rx->skb->len,
  1925. GFP_ATOMIC)) {
  1926. if (rx->sta)
  1927. rx->sta->rx_packets++;
  1928. dev_kfree_skb(rx->skb);
  1929. return RX_QUEUED;
  1930. }
  1931. return RX_CONTINUE;
  1932. }
  1933. static ieee80211_rx_result debug_noinline
  1934. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  1935. {
  1936. struct ieee80211_local *local = rx->local;
  1937. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1938. struct sk_buff *nskb;
  1939. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1940. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1941. if (!ieee80211_is_action(mgmt->frame_control))
  1942. return RX_CONTINUE;
  1943. /*
  1944. * For AP mode, hostapd is responsible for handling any action
  1945. * frames that we didn't handle, including returning unknown
  1946. * ones. For all other modes we will return them to the sender,
  1947. * setting the 0x80 bit in the action category, as required by
  1948. * 802.11-2007 7.3.1.11.
  1949. * Newer versions of hostapd shall also use the management frame
  1950. * registration mechanisms, but older ones still use cooked
  1951. * monitor interfaces so push all frames there.
  1952. */
  1953. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  1954. (sdata->vif.type == NL80211_IFTYPE_AP ||
  1955. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  1956. return RX_DROP_MONITOR;
  1957. /* do not return rejected action frames */
  1958. if (mgmt->u.action.category & 0x80)
  1959. return RX_DROP_UNUSABLE;
  1960. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  1961. GFP_ATOMIC);
  1962. if (nskb) {
  1963. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  1964. nmgmt->u.action.category |= 0x80;
  1965. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  1966. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  1967. memset(nskb->cb, 0, sizeof(nskb->cb));
  1968. ieee80211_tx_skb(rx->sdata, nskb);
  1969. }
  1970. dev_kfree_skb(rx->skb);
  1971. return RX_QUEUED;
  1972. }
  1973. static ieee80211_rx_result debug_noinline
  1974. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  1975. {
  1976. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1977. ieee80211_rx_result rxs;
  1978. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  1979. __le16 stype;
  1980. rxs = ieee80211_work_rx_mgmt(rx->sdata, rx->skb);
  1981. if (rxs != RX_CONTINUE)
  1982. return rxs;
  1983. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  1984. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  1985. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  1986. sdata->vif.type != NL80211_IFTYPE_STATION)
  1987. return RX_DROP_MONITOR;
  1988. switch (stype) {
  1989. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1990. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1991. /* process for all: mesh, mlme, ibss */
  1992. break;
  1993. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1994. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1995. if (is_multicast_ether_addr(mgmt->da) &&
  1996. !is_broadcast_ether_addr(mgmt->da))
  1997. return RX_DROP_MONITOR;
  1998. /* process only for station */
  1999. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2000. return RX_DROP_MONITOR;
  2001. break;
  2002. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2003. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2004. /* process only for ibss */
  2005. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2006. return RX_DROP_MONITOR;
  2007. break;
  2008. default:
  2009. return RX_DROP_MONITOR;
  2010. }
  2011. /* queue up frame and kick off work to process it */
  2012. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2013. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2014. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2015. if (rx->sta)
  2016. rx->sta->rx_packets++;
  2017. return RX_QUEUED;
  2018. }
  2019. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2020. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2021. struct ieee80211_rate *rate)
  2022. {
  2023. struct ieee80211_sub_if_data *sdata;
  2024. struct ieee80211_local *local = rx->local;
  2025. struct ieee80211_rtap_hdr {
  2026. struct ieee80211_radiotap_header hdr;
  2027. u8 flags;
  2028. u8 rate_or_pad;
  2029. __le16 chan_freq;
  2030. __le16 chan_flags;
  2031. } __packed *rthdr;
  2032. struct sk_buff *skb = rx->skb, *skb2;
  2033. struct net_device *prev_dev = NULL;
  2034. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2035. /*
  2036. * If cooked monitor has been processed already, then
  2037. * don't do it again. If not, set the flag.
  2038. */
  2039. if (rx->flags & IEEE80211_RX_CMNTR)
  2040. goto out_free_skb;
  2041. rx->flags |= IEEE80211_RX_CMNTR;
  2042. if (skb_headroom(skb) < sizeof(*rthdr) &&
  2043. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  2044. goto out_free_skb;
  2045. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  2046. memset(rthdr, 0, sizeof(*rthdr));
  2047. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  2048. rthdr->hdr.it_present =
  2049. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  2050. (1 << IEEE80211_RADIOTAP_CHANNEL));
  2051. if (rate) {
  2052. rthdr->rate_or_pad = rate->bitrate / 5;
  2053. rthdr->hdr.it_present |=
  2054. cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  2055. }
  2056. rthdr->chan_freq = cpu_to_le16(status->freq);
  2057. if (status->band == IEEE80211_BAND_5GHZ)
  2058. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  2059. IEEE80211_CHAN_5GHZ);
  2060. else
  2061. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  2062. IEEE80211_CHAN_2GHZ);
  2063. skb_set_mac_header(skb, 0);
  2064. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2065. skb->pkt_type = PACKET_OTHERHOST;
  2066. skb->protocol = htons(ETH_P_802_2);
  2067. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2068. if (!ieee80211_sdata_running(sdata))
  2069. continue;
  2070. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2071. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2072. continue;
  2073. if (prev_dev) {
  2074. skb2 = skb_clone(skb, GFP_ATOMIC);
  2075. if (skb2) {
  2076. skb2->dev = prev_dev;
  2077. netif_receive_skb(skb2);
  2078. }
  2079. }
  2080. prev_dev = sdata->dev;
  2081. sdata->dev->stats.rx_packets++;
  2082. sdata->dev->stats.rx_bytes += skb->len;
  2083. }
  2084. if (prev_dev) {
  2085. skb->dev = prev_dev;
  2086. netif_receive_skb(skb);
  2087. return;
  2088. }
  2089. out_free_skb:
  2090. dev_kfree_skb(skb);
  2091. }
  2092. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2093. ieee80211_rx_result res)
  2094. {
  2095. switch (res) {
  2096. case RX_DROP_MONITOR:
  2097. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2098. if (rx->sta)
  2099. rx->sta->rx_dropped++;
  2100. /* fall through */
  2101. case RX_CONTINUE: {
  2102. struct ieee80211_rate *rate = NULL;
  2103. struct ieee80211_supported_band *sband;
  2104. struct ieee80211_rx_status *status;
  2105. status = IEEE80211_SKB_RXCB((rx->skb));
  2106. sband = rx->local->hw.wiphy->bands[status->band];
  2107. if (!(status->flag & RX_FLAG_HT))
  2108. rate = &sband->bitrates[status->rate_idx];
  2109. ieee80211_rx_cooked_monitor(rx, rate);
  2110. break;
  2111. }
  2112. case RX_DROP_UNUSABLE:
  2113. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2114. if (rx->sta)
  2115. rx->sta->rx_dropped++;
  2116. dev_kfree_skb(rx->skb);
  2117. break;
  2118. case RX_QUEUED:
  2119. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2120. break;
  2121. }
  2122. }
  2123. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
  2124. {
  2125. ieee80211_rx_result res = RX_DROP_MONITOR;
  2126. struct sk_buff *skb;
  2127. #define CALL_RXH(rxh) \
  2128. do { \
  2129. res = rxh(rx); \
  2130. if (res != RX_CONTINUE) \
  2131. goto rxh_next; \
  2132. } while (0);
  2133. spin_lock(&rx->local->rx_skb_queue.lock);
  2134. if (rx->local->running_rx_handler)
  2135. goto unlock;
  2136. rx->local->running_rx_handler = true;
  2137. while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
  2138. spin_unlock(&rx->local->rx_skb_queue.lock);
  2139. /*
  2140. * all the other fields are valid across frames
  2141. * that belong to an aMPDU since they are on the
  2142. * same TID from the same station
  2143. */
  2144. rx->skb = skb;
  2145. CALL_RXH(ieee80211_rx_h_decrypt)
  2146. CALL_RXH(ieee80211_rx_h_check_more_data)
  2147. CALL_RXH(ieee80211_rx_h_sta_process)
  2148. CALL_RXH(ieee80211_rx_h_defragment)
  2149. CALL_RXH(ieee80211_rx_h_ps_poll)
  2150. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2151. /* must be after MMIC verify so header is counted in MPDU mic */
  2152. CALL_RXH(ieee80211_rx_h_remove_qos_control)
  2153. CALL_RXH(ieee80211_rx_h_amsdu)
  2154. #ifdef CONFIG_MAC80211_MESH
  2155. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2156. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2157. #endif
  2158. CALL_RXH(ieee80211_rx_h_data)
  2159. CALL_RXH(ieee80211_rx_h_ctrl);
  2160. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2161. CALL_RXH(ieee80211_rx_h_action)
  2162. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2163. CALL_RXH(ieee80211_rx_h_action_return)
  2164. CALL_RXH(ieee80211_rx_h_mgmt)
  2165. rxh_next:
  2166. ieee80211_rx_handlers_result(rx, res);
  2167. spin_lock(&rx->local->rx_skb_queue.lock);
  2168. #undef CALL_RXH
  2169. }
  2170. rx->local->running_rx_handler = false;
  2171. unlock:
  2172. spin_unlock(&rx->local->rx_skb_queue.lock);
  2173. }
  2174. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2175. {
  2176. ieee80211_rx_result res = RX_DROP_MONITOR;
  2177. #define CALL_RXH(rxh) \
  2178. do { \
  2179. res = rxh(rx); \
  2180. if (res != RX_CONTINUE) \
  2181. goto rxh_next; \
  2182. } while (0);
  2183. CALL_RXH(ieee80211_rx_h_passive_scan)
  2184. CALL_RXH(ieee80211_rx_h_check)
  2185. ieee80211_rx_reorder_ampdu(rx);
  2186. ieee80211_rx_handlers(rx);
  2187. return;
  2188. rxh_next:
  2189. ieee80211_rx_handlers_result(rx, res);
  2190. #undef CALL_RXH
  2191. }
  2192. /*
  2193. * This function makes calls into the RX path, therefore
  2194. * it has to be invoked under RCU read lock.
  2195. */
  2196. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2197. {
  2198. struct ieee80211_rx_data rx = {
  2199. .sta = sta,
  2200. .sdata = sta->sdata,
  2201. .local = sta->local,
  2202. /* This is OK -- must be QoS data frame */
  2203. .security_idx = tid,
  2204. .seqno_idx = tid,
  2205. .flags = 0,
  2206. };
  2207. struct tid_ampdu_rx *tid_agg_rx;
  2208. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2209. if (!tid_agg_rx)
  2210. return;
  2211. spin_lock(&tid_agg_rx->reorder_lock);
  2212. ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
  2213. spin_unlock(&tid_agg_rx->reorder_lock);
  2214. ieee80211_rx_handlers(&rx);
  2215. }
  2216. /* main receive path */
  2217. static int prepare_for_handlers(struct ieee80211_rx_data *rx,
  2218. struct ieee80211_hdr *hdr)
  2219. {
  2220. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2221. struct sk_buff *skb = rx->skb;
  2222. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2223. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2224. int multicast = is_multicast_ether_addr(hdr->addr1);
  2225. switch (sdata->vif.type) {
  2226. case NL80211_IFTYPE_STATION:
  2227. if (!bssid && !sdata->u.mgd.use_4addr)
  2228. return 0;
  2229. if (!multicast &&
  2230. compare_ether_addr(sdata->vif.addr, hdr->addr1) != 0) {
  2231. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2232. sdata->u.mgd.use_4addr)
  2233. return 0;
  2234. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2235. }
  2236. break;
  2237. case NL80211_IFTYPE_ADHOC:
  2238. if (!bssid)
  2239. return 0;
  2240. if (ieee80211_is_beacon(hdr->frame_control)) {
  2241. return 1;
  2242. }
  2243. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2244. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
  2245. return 0;
  2246. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2247. } else if (!multicast &&
  2248. compare_ether_addr(sdata->vif.addr,
  2249. hdr->addr1) != 0) {
  2250. if (!(sdata->dev->flags & IFF_PROMISC))
  2251. return 0;
  2252. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2253. } else if (!rx->sta) {
  2254. int rate_idx;
  2255. if (status->flag & RX_FLAG_HT)
  2256. rate_idx = 0; /* TODO: HT rates */
  2257. else
  2258. rate_idx = status->rate_idx;
  2259. rx->sta = ieee80211_ibss_add_sta(sdata, bssid,
  2260. hdr->addr2, BIT(rate_idx), GFP_ATOMIC);
  2261. }
  2262. break;
  2263. case NL80211_IFTYPE_MESH_POINT:
  2264. if (!multicast &&
  2265. compare_ether_addr(sdata->vif.addr,
  2266. hdr->addr1) != 0) {
  2267. if (!(sdata->dev->flags & IFF_PROMISC))
  2268. return 0;
  2269. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2270. }
  2271. break;
  2272. case NL80211_IFTYPE_AP_VLAN:
  2273. case NL80211_IFTYPE_AP:
  2274. if (!bssid) {
  2275. if (compare_ether_addr(sdata->vif.addr,
  2276. hdr->addr1))
  2277. return 0;
  2278. } else if (!ieee80211_bssid_match(bssid,
  2279. sdata->vif.addr)) {
  2280. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2281. !ieee80211_is_beacon(hdr->frame_control))
  2282. return 0;
  2283. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2284. }
  2285. break;
  2286. case NL80211_IFTYPE_WDS:
  2287. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2288. return 0;
  2289. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  2290. return 0;
  2291. break;
  2292. default:
  2293. /* should never get here */
  2294. WARN_ON(1);
  2295. break;
  2296. }
  2297. return 1;
  2298. }
  2299. /*
  2300. * This function returns whether or not the SKB
  2301. * was destined for RX processing or not, which,
  2302. * if consume is true, is equivalent to whether
  2303. * or not the skb was consumed.
  2304. */
  2305. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2306. struct sk_buff *skb, bool consume)
  2307. {
  2308. struct ieee80211_local *local = rx->local;
  2309. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2310. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2311. struct ieee80211_hdr *hdr = (void *)skb->data;
  2312. int prepares;
  2313. rx->skb = skb;
  2314. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2315. prepares = prepare_for_handlers(rx, hdr);
  2316. if (!prepares)
  2317. return false;
  2318. if (!consume) {
  2319. skb = skb_copy(skb, GFP_ATOMIC);
  2320. if (!skb) {
  2321. if (net_ratelimit())
  2322. wiphy_debug(local->hw.wiphy,
  2323. "failed to copy skb for %s\n",
  2324. sdata->name);
  2325. return true;
  2326. }
  2327. rx->skb = skb;
  2328. }
  2329. ieee80211_invoke_rx_handlers(rx);
  2330. return true;
  2331. }
  2332. /*
  2333. * This is the actual Rx frames handler. as it blongs to Rx path it must
  2334. * be called with rcu_read_lock protection.
  2335. */
  2336. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2337. struct sk_buff *skb)
  2338. {
  2339. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2340. struct ieee80211_local *local = hw_to_local(hw);
  2341. struct ieee80211_sub_if_data *sdata;
  2342. struct ieee80211_hdr *hdr;
  2343. __le16 fc;
  2344. struct ieee80211_rx_data rx;
  2345. struct ieee80211_sub_if_data *prev;
  2346. struct sta_info *sta, *tmp, *prev_sta;
  2347. int err = 0;
  2348. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2349. memset(&rx, 0, sizeof(rx));
  2350. rx.skb = skb;
  2351. rx.local = local;
  2352. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2353. local->dot11ReceivedFragmentCount++;
  2354. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  2355. test_bit(SCAN_SW_SCANNING, &local->scanning)))
  2356. status->rx_flags |= IEEE80211_RX_IN_SCAN;
  2357. if (ieee80211_is_mgmt(fc))
  2358. err = skb_linearize(skb);
  2359. else
  2360. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2361. if (err) {
  2362. dev_kfree_skb(skb);
  2363. return;
  2364. }
  2365. hdr = (struct ieee80211_hdr *)skb->data;
  2366. ieee80211_parse_qos(&rx);
  2367. ieee80211_verify_alignment(&rx);
  2368. if (ieee80211_is_data(fc)) {
  2369. prev_sta = NULL;
  2370. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  2371. if (!prev_sta) {
  2372. prev_sta = sta;
  2373. continue;
  2374. }
  2375. rx.sta = prev_sta;
  2376. rx.sdata = prev_sta->sdata;
  2377. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2378. prev_sta = sta;
  2379. }
  2380. if (prev_sta) {
  2381. rx.sta = prev_sta;
  2382. rx.sdata = prev_sta->sdata;
  2383. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2384. return;
  2385. goto out;
  2386. }
  2387. }
  2388. prev = NULL;
  2389. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2390. if (!ieee80211_sdata_running(sdata))
  2391. continue;
  2392. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2393. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2394. continue;
  2395. /*
  2396. * frame is destined for this interface, but if it's
  2397. * not also for the previous one we handle that after
  2398. * the loop to avoid copying the SKB once too much
  2399. */
  2400. if (!prev) {
  2401. prev = sdata;
  2402. continue;
  2403. }
  2404. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2405. rx.sdata = prev;
  2406. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2407. prev = sdata;
  2408. }
  2409. if (prev) {
  2410. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2411. rx.sdata = prev;
  2412. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2413. return;
  2414. }
  2415. out:
  2416. dev_kfree_skb(skb);
  2417. }
  2418. /*
  2419. * This is the receive path handler. It is called by a low level driver when an
  2420. * 802.11 MPDU is received from the hardware.
  2421. */
  2422. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2423. {
  2424. struct ieee80211_local *local = hw_to_local(hw);
  2425. struct ieee80211_rate *rate = NULL;
  2426. struct ieee80211_supported_band *sband;
  2427. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2428. WARN_ON_ONCE(softirq_count() == 0);
  2429. if (WARN_ON(status->band < 0 ||
  2430. status->band >= IEEE80211_NUM_BANDS))
  2431. goto drop;
  2432. sband = local->hw.wiphy->bands[status->band];
  2433. if (WARN_ON(!sband))
  2434. goto drop;
  2435. /*
  2436. * If we're suspending, it is possible although not too likely
  2437. * that we'd be receiving frames after having already partially
  2438. * quiesced the stack. We can't process such frames then since
  2439. * that might, for example, cause stations to be added or other
  2440. * driver callbacks be invoked.
  2441. */
  2442. if (unlikely(local->quiescing || local->suspended))
  2443. goto drop;
  2444. /*
  2445. * The same happens when we're not even started,
  2446. * but that's worth a warning.
  2447. */
  2448. if (WARN_ON(!local->started))
  2449. goto drop;
  2450. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  2451. /*
  2452. * Validate the rate, unless a PLCP error means that
  2453. * we probably can't have a valid rate here anyway.
  2454. */
  2455. if (status->flag & RX_FLAG_HT) {
  2456. /*
  2457. * rate_idx is MCS index, which can be [0-76]
  2458. * as documented on:
  2459. *
  2460. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2461. *
  2462. * Anything else would be some sort of driver or
  2463. * hardware error. The driver should catch hardware
  2464. * errors.
  2465. */
  2466. if (WARN((status->rate_idx < 0 ||
  2467. status->rate_idx > 76),
  2468. "Rate marked as an HT rate but passed "
  2469. "status->rate_idx is not "
  2470. "an MCS index [0-76]: %d (0x%02x)\n",
  2471. status->rate_idx,
  2472. status->rate_idx))
  2473. goto drop;
  2474. } else {
  2475. if (WARN_ON(status->rate_idx < 0 ||
  2476. status->rate_idx >= sband->n_bitrates))
  2477. goto drop;
  2478. rate = &sband->bitrates[status->rate_idx];
  2479. }
  2480. }
  2481. status->rx_flags = 0;
  2482. /*
  2483. * key references and virtual interfaces are protected using RCU
  2484. * and this requires that we are in a read-side RCU section during
  2485. * receive processing
  2486. */
  2487. rcu_read_lock();
  2488. /*
  2489. * Frames with failed FCS/PLCP checksum are not returned,
  2490. * all other frames are returned without radiotap header
  2491. * if it was previously present.
  2492. * Also, frames with less than 16 bytes are dropped.
  2493. */
  2494. skb = ieee80211_rx_monitor(local, skb, rate);
  2495. if (!skb) {
  2496. rcu_read_unlock();
  2497. return;
  2498. }
  2499. ieee80211_tpt_led_trig_rx(local,
  2500. ((struct ieee80211_hdr *)skb->data)->frame_control,
  2501. skb->len);
  2502. __ieee80211_rx_handle_packet(hw, skb);
  2503. rcu_read_unlock();
  2504. return;
  2505. drop:
  2506. kfree_skb(skb);
  2507. }
  2508. EXPORT_SYMBOL(ieee80211_rx);
  2509. /* This is a version of the rx handler that can be called from hard irq
  2510. * context. Post the skb on the queue and schedule the tasklet */
  2511. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2512. {
  2513. struct ieee80211_local *local = hw_to_local(hw);
  2514. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2515. skb->pkt_type = IEEE80211_RX_MSG;
  2516. skb_queue_tail(&local->skb_queue, skb);
  2517. tasklet_schedule(&local->tasklet);
  2518. }
  2519. EXPORT_SYMBOL(ieee80211_rx_irqsafe);