rx.c 73 KB

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