rx.c 70 KB

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