rx.c 73 KB

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