rx.c 82 KB

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