rx.c 81 KB

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