rx.c 69 KB

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