rx.c 80 KB

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