rx.c 71 KB

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