rx.c 70 KB

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