rx.c 81 KB

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