rx.c 70 KB

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