rx.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  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/kernel.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/rcupdate.h>
  16. #include <net/mac80211.h>
  17. #include <net/ieee80211_radiotap.h>
  18. #include "ieee80211_i.h"
  19. #include "ieee80211_led.h"
  20. #include "wep.h"
  21. #include "wpa.h"
  22. #include "tkip.h"
  23. #include "wme.h"
  24. u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  25. struct tid_ampdu_rx *tid_agg_rx,
  26. struct sk_buff *skb, u16 mpdu_seq_num,
  27. int bar_req);
  28. /*
  29. * monitor mode reception
  30. *
  31. * This function cleans up the SKB, i.e. it removes all the stuff
  32. * only useful for monitoring.
  33. */
  34. static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
  35. struct sk_buff *skb,
  36. int rtap_len)
  37. {
  38. skb_pull(skb, rtap_len);
  39. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
  40. if (likely(skb->len > FCS_LEN))
  41. skb_trim(skb, skb->len - FCS_LEN);
  42. else {
  43. /* driver bug */
  44. WARN_ON(1);
  45. dev_kfree_skb(skb);
  46. skb = NULL;
  47. }
  48. }
  49. return skb;
  50. }
  51. static inline int should_drop_frame(struct ieee80211_rx_status *status,
  52. struct sk_buff *skb,
  53. int present_fcs_len,
  54. int radiotap_len)
  55. {
  56. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  57. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  58. return 1;
  59. if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
  60. return 1;
  61. if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
  62. cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
  63. ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
  64. cpu_to_le16(IEEE80211_STYPE_PSPOLL)) &&
  65. ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
  66. cpu_to_le16(IEEE80211_STYPE_BACK_REQ)))
  67. return 1;
  68. return 0;
  69. }
  70. /*
  71. * This function copies a received frame to all monitor interfaces and
  72. * returns a cleaned-up SKB that no longer includes the FCS nor the
  73. * radiotap header the driver might have added.
  74. */
  75. static struct sk_buff *
  76. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  77. struct ieee80211_rx_status *status,
  78. struct ieee80211_rate *rate)
  79. {
  80. struct ieee80211_sub_if_data *sdata;
  81. int needed_headroom = 0;
  82. struct ieee80211_radiotap_header *rthdr;
  83. __le64 *rttsft = NULL;
  84. struct ieee80211_rtap_fixed_data {
  85. u8 flags;
  86. u8 rate;
  87. __le16 chan_freq;
  88. __le16 chan_flags;
  89. u8 antsignal;
  90. u8 padding_for_rxflags;
  91. __le16 rx_flags;
  92. } __attribute__ ((packed)) *rtfixed;
  93. struct sk_buff *skb, *skb2;
  94. struct net_device *prev_dev = NULL;
  95. int present_fcs_len = 0;
  96. int rtap_len = 0;
  97. /*
  98. * First, we may need to make a copy of the skb because
  99. * (1) we need to modify it for radiotap (if not present), and
  100. * (2) the other RX handlers will modify the skb we got.
  101. *
  102. * We don't need to, of course, if we aren't going to return
  103. * the SKB because it has a bad FCS/PLCP checksum.
  104. */
  105. if (status->flag & RX_FLAG_RADIOTAP)
  106. rtap_len = ieee80211_get_radiotap_len(origskb->data);
  107. else
  108. /* room for radiotap header, always present fields and TSFT */
  109. needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
  110. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  111. present_fcs_len = FCS_LEN;
  112. if (!local->monitors) {
  113. if (should_drop_frame(status, origskb, present_fcs_len,
  114. rtap_len)) {
  115. dev_kfree_skb(origskb);
  116. return NULL;
  117. }
  118. return remove_monitor_info(local, origskb, rtap_len);
  119. }
  120. if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
  121. /* only need to expand headroom if necessary */
  122. skb = origskb;
  123. origskb = NULL;
  124. /*
  125. * This shouldn't trigger often because most devices have an
  126. * RX header they pull before we get here, and that should
  127. * be big enough for our radiotap information. We should
  128. * probably export the length to drivers so that we can have
  129. * them allocate enough headroom to start with.
  130. */
  131. if (skb_headroom(skb) < needed_headroom &&
  132. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  133. dev_kfree_skb(skb);
  134. return NULL;
  135. }
  136. } else {
  137. /*
  138. * Need to make a copy and possibly remove radiotap header
  139. * and FCS from the original.
  140. */
  141. skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
  142. origskb = remove_monitor_info(local, origskb, rtap_len);
  143. if (!skb)
  144. return origskb;
  145. }
  146. /* if necessary, prepend radiotap information */
  147. if (!(status->flag & RX_FLAG_RADIOTAP)) {
  148. rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
  149. rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
  150. if (status->flag & RX_FLAG_TSFT) {
  151. rttsft = (void *) skb_push(skb, sizeof(*rttsft));
  152. rtap_len += 8;
  153. }
  154. rthdr = (void *) skb_push(skb, sizeof(*rthdr));
  155. memset(rthdr, 0, sizeof(*rthdr));
  156. memset(rtfixed, 0, sizeof(*rtfixed));
  157. rthdr->it_present =
  158. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  159. (1 << IEEE80211_RADIOTAP_RATE) |
  160. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  161. (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
  162. (1 << IEEE80211_RADIOTAP_RX_FLAGS));
  163. rtfixed->flags = 0;
  164. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  165. rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
  166. if (rttsft) {
  167. *rttsft = cpu_to_le64(status->mactime);
  168. rthdr->it_present |=
  169. cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  170. }
  171. /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
  172. rtfixed->rx_flags = 0;
  173. if (status->flag &
  174. (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  175. rtfixed->rx_flags |=
  176. cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
  177. rtfixed->rate = rate->bitrate / 5;
  178. rtfixed->chan_freq = cpu_to_le16(status->freq);
  179. if (status->band == IEEE80211_BAND_5GHZ)
  180. rtfixed->chan_flags =
  181. cpu_to_le16(IEEE80211_CHAN_OFDM |
  182. IEEE80211_CHAN_5GHZ);
  183. else
  184. rtfixed->chan_flags =
  185. cpu_to_le16(IEEE80211_CHAN_DYN |
  186. IEEE80211_CHAN_2GHZ);
  187. rtfixed->antsignal = status->ssi;
  188. rthdr->it_len = cpu_to_le16(rtap_len);
  189. }
  190. skb_reset_mac_header(skb);
  191. skb->ip_summed = CHECKSUM_UNNECESSARY;
  192. skb->pkt_type = PACKET_OTHERHOST;
  193. skb->protocol = htons(ETH_P_802_2);
  194. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  195. if (!netif_running(sdata->dev))
  196. continue;
  197. if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR)
  198. continue;
  199. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
  200. continue;
  201. if (prev_dev) {
  202. skb2 = skb_clone(skb, GFP_ATOMIC);
  203. if (skb2) {
  204. skb2->dev = prev_dev;
  205. netif_rx(skb2);
  206. }
  207. }
  208. prev_dev = sdata->dev;
  209. sdata->dev->stats.rx_packets++;
  210. sdata->dev->stats.rx_bytes += skb->len;
  211. }
  212. if (prev_dev) {
  213. skb->dev = prev_dev;
  214. netif_rx(skb);
  215. } else
  216. dev_kfree_skb(skb);
  217. return origskb;
  218. }
  219. static void ieee80211_parse_qos(struct ieee80211_txrx_data *rx)
  220. {
  221. u8 *data = rx->skb->data;
  222. int tid;
  223. /* does the frame have a qos control field? */
  224. if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
  225. u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
  226. /* frame has qos control */
  227. tid = qc[0] & QOS_CONTROL_TID_MASK;
  228. if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  229. rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
  230. else
  231. rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
  232. } else {
  233. if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
  234. /* Separate TID for management frames */
  235. tid = NUM_RX_DATA_QUEUES - 1;
  236. } else {
  237. /* no qos control present */
  238. tid = 0; /* 802.1d - Best Effort */
  239. }
  240. }
  241. I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
  242. /* only a debug counter, sta might not be assigned properly yet */
  243. if (rx->sta)
  244. I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
  245. rx->u.rx.queue = tid;
  246. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  247. * For now, set skb->priority to 0 for other cases. */
  248. rx->skb->priority = (tid > 7) ? 0 : tid;
  249. }
  250. static void ieee80211_verify_ip_alignment(struct ieee80211_txrx_data *rx)
  251. {
  252. #ifdef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
  253. int hdrlen;
  254. if (!WLAN_FC_DATA_PRESENT(rx->fc))
  255. return;
  256. /*
  257. * Drivers are required to align the payload data in a way that
  258. * guarantees that the contained IP header is aligned to a four-
  259. * byte boundary. In the case of regular frames, this simply means
  260. * aligning the payload to a four-byte boundary (because either
  261. * the IP header is directly contained, or IV/RFC1042 headers that
  262. * have a length divisible by four are in front of it.
  263. *
  264. * With A-MSDU frames, however, the payload data address must
  265. * yield two modulo four because there are 14-byte 802.3 headers
  266. * within the A-MSDU frames that push the IP header further back
  267. * to a multiple of four again. Thankfully, the specs were sane
  268. * enough this time around to require padding each A-MSDU subframe
  269. * to a length that is a multiple of four.
  270. *
  271. * Padding like atheros hardware adds which is inbetween the 802.11
  272. * header and the payload is not supported, the driver is required
  273. * to move the 802.11 header further back in that case.
  274. */
  275. hdrlen = ieee80211_get_hdrlen(rx->fc);
  276. if (rx->flags & IEEE80211_TXRXD_RX_AMSDU)
  277. hdrlen += ETH_HLEN;
  278. WARN_ON_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3);
  279. #endif
  280. }
  281. static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
  282. struct sk_buff *skb,
  283. struct ieee80211_rx_status *status,
  284. struct ieee80211_rate *rate)
  285. {
  286. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  287. u32 load = 0, hdrtime;
  288. /* Estimate total channel use caused by this frame */
  289. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  290. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  291. if (status->band == IEEE80211_BAND_5GHZ ||
  292. (status->band == IEEE80211_BAND_5GHZ &&
  293. rate->flags & IEEE80211_RATE_ERP_G))
  294. hdrtime = CHAN_UTIL_HDR_SHORT;
  295. else
  296. hdrtime = CHAN_UTIL_HDR_LONG;
  297. load = hdrtime;
  298. if (!is_multicast_ether_addr(hdr->addr1))
  299. load += hdrtime;
  300. /* TODO: optimise again */
  301. load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
  302. /* Divide channel_use by 8 to avoid wrapping around the counter */
  303. load >>= CHAN_UTIL_SHIFT;
  304. return load;
  305. }
  306. /* rx handlers */
  307. static ieee80211_rx_result
  308. ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
  309. {
  310. if (rx->sta)
  311. rx->sta->channel_use_raw += rx->u.rx.load;
  312. rx->sdata->channel_use_raw += rx->u.rx.load;
  313. return RX_CONTINUE;
  314. }
  315. static ieee80211_rx_result
  316. ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
  317. {
  318. struct ieee80211_local *local = rx->local;
  319. struct sk_buff *skb = rx->skb;
  320. if (unlikely(local->sta_hw_scanning))
  321. return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
  322. if (unlikely(local->sta_sw_scanning)) {
  323. /* drop all the other packets during a software scan anyway */
  324. if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
  325. != RX_QUEUED)
  326. dev_kfree_skb(skb);
  327. return RX_QUEUED;
  328. }
  329. if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
  330. /* scanning finished during invoking of handlers */
  331. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  332. return RX_DROP_UNUSABLE;
  333. }
  334. return RX_CONTINUE;
  335. }
  336. static ieee80211_rx_result
  337. ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
  338. {
  339. struct ieee80211_hdr *hdr;
  340. hdr = (struct ieee80211_hdr *) rx->skb->data;
  341. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  342. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  343. if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
  344. rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
  345. hdr->seq_ctrl)) {
  346. if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
  347. rx->local->dot11FrameDuplicateCount++;
  348. rx->sta->num_duplicates++;
  349. }
  350. return RX_DROP_MONITOR;
  351. } else
  352. rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
  353. }
  354. if (unlikely(rx->skb->len < 16)) {
  355. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  356. return RX_DROP_MONITOR;
  357. }
  358. /* Drop disallowed frame classes based on STA auth/assoc state;
  359. * IEEE 802.11, Chap 5.5.
  360. *
  361. * 80211.o does filtering only based on association state, i.e., it
  362. * drops Class 3 frames from not associated stations. hostapd sends
  363. * deauth/disassoc frames when needed. In addition, hostapd is
  364. * responsible for filtering on both auth and assoc states.
  365. */
  366. if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
  367. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
  368. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
  369. rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  370. (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
  371. if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
  372. !(rx->fc & IEEE80211_FCTL_TODS) &&
  373. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
  374. || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
  375. /* Drop IBSS frames and frames for other hosts
  376. * silently. */
  377. return RX_DROP_MONITOR;
  378. }
  379. return RX_DROP_MONITOR;
  380. }
  381. return RX_CONTINUE;
  382. }
  383. static ieee80211_rx_result
  384. ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
  385. {
  386. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  387. int keyidx;
  388. int hdrlen;
  389. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  390. struct ieee80211_key *stakey = NULL;
  391. /*
  392. * Key selection 101
  393. *
  394. * There are three types of keys:
  395. * - GTK (group keys)
  396. * - PTK (pairwise keys)
  397. * - STK (station-to-station pairwise keys)
  398. *
  399. * When selecting a key, we have to distinguish between multicast
  400. * (including broadcast) and unicast frames, the latter can only
  401. * use PTKs and STKs while the former always use GTKs. Unless, of
  402. * course, actual WEP keys ("pre-RSNA") are used, then unicast
  403. * frames can also use key indizes like GTKs. Hence, if we don't
  404. * have a PTK/STK we check the key index for a WEP key.
  405. *
  406. * Note that in a regular BSS, multicast frames are sent by the
  407. * AP only, associated stations unicast the frame to the AP first
  408. * which then multicasts it on their behalf.
  409. *
  410. * There is also a slight problem in IBSS mode: GTKs are negotiated
  411. * with each station, that is something we don't currently handle.
  412. * The spec seems to expect that one negotiates the same key with
  413. * every station but there's no such requirement; VLANs could be
  414. * possible.
  415. */
  416. if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
  417. return RX_CONTINUE;
  418. /*
  419. * No point in finding a key and decrypting if the frame is neither
  420. * addressed to us nor a multicast frame.
  421. */
  422. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  423. return RX_CONTINUE;
  424. if (rx->sta)
  425. stakey = rcu_dereference(rx->sta->key);
  426. if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
  427. rx->key = stakey;
  428. } else {
  429. /*
  430. * The device doesn't give us the IV so we won't be
  431. * able to look up the key. That's ok though, we
  432. * don't need to decrypt the frame, we just won't
  433. * be able to keep statistics accurate.
  434. * Except for key threshold notifications, should
  435. * we somehow allow the driver to tell us which key
  436. * the hardware used if this flag is set?
  437. */
  438. if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
  439. (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
  440. return RX_CONTINUE;
  441. hdrlen = ieee80211_get_hdrlen(rx->fc);
  442. if (rx->skb->len < 8 + hdrlen)
  443. return RX_DROP_UNUSABLE; /* TODO: count this? */
  444. /*
  445. * no need to call ieee80211_wep_get_keyidx,
  446. * it verifies a bunch of things we've done already
  447. */
  448. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  449. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  450. /*
  451. * RSNA-protected unicast frames should always be sent with
  452. * pairwise or station-to-station keys, but for WEP we allow
  453. * using a key index as well.
  454. */
  455. if (rx->key && rx->key->conf.alg != ALG_WEP &&
  456. !is_multicast_ether_addr(hdr->addr1))
  457. rx->key = NULL;
  458. }
  459. if (rx->key) {
  460. rx->key->tx_rx_count++;
  461. /* TODO: add threshold stuff again */
  462. } else {
  463. #ifdef CONFIG_MAC80211_DEBUG
  464. if (net_ratelimit())
  465. printk(KERN_DEBUG "%s: RX protected frame,"
  466. " but have no key\n", rx->dev->name);
  467. #endif /* CONFIG_MAC80211_DEBUG */
  468. return RX_DROP_MONITOR;
  469. }
  470. /* Check for weak IVs if possible */
  471. if (rx->sta && rx->key->conf.alg == ALG_WEP &&
  472. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  473. (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
  474. !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
  475. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  476. rx->sta->wep_weak_iv_count++;
  477. switch (rx->key->conf.alg) {
  478. case ALG_WEP:
  479. result = ieee80211_crypto_wep_decrypt(rx);
  480. break;
  481. case ALG_TKIP:
  482. result = ieee80211_crypto_tkip_decrypt(rx);
  483. break;
  484. case ALG_CCMP:
  485. result = ieee80211_crypto_ccmp_decrypt(rx);
  486. break;
  487. }
  488. /* either the frame has been decrypted or will be dropped */
  489. rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
  490. return result;
  491. }
  492. static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
  493. {
  494. struct ieee80211_sub_if_data *sdata;
  495. DECLARE_MAC_BUF(mac);
  496. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  497. if (sdata->bss)
  498. atomic_inc(&sdata->bss->num_sta_ps);
  499. sta->flags |= WLAN_STA_PS;
  500. sta->pspoll = 0;
  501. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  502. printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
  503. dev->name, print_mac(mac, sta->addr), sta->aid);
  504. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  505. }
  506. static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
  507. {
  508. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  509. struct sk_buff *skb;
  510. int sent = 0;
  511. struct ieee80211_sub_if_data *sdata;
  512. struct ieee80211_tx_packet_data *pkt_data;
  513. DECLARE_MAC_BUF(mac);
  514. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  515. if (sdata->bss)
  516. atomic_dec(&sdata->bss->num_sta_ps);
  517. sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
  518. sta->pspoll = 0;
  519. if (!skb_queue_empty(&sta->ps_tx_buf)) {
  520. if (local->ops->set_tim)
  521. local->ops->set_tim(local_to_hw(local), sta->aid, 0);
  522. if (sdata->bss)
  523. bss_tim_clear(local, sdata->bss, sta->aid);
  524. }
  525. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  526. printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
  527. dev->name, print_mac(mac, sta->addr), sta->aid);
  528. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  529. /* Send all buffered frames to the station */
  530. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  531. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  532. sent++;
  533. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  534. dev_queue_xmit(skb);
  535. }
  536. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  537. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  538. local->total_ps_buffered--;
  539. sent++;
  540. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  541. printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
  542. "since STA not sleeping anymore\n", dev->name,
  543. print_mac(mac, sta->addr), sta->aid);
  544. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  545. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  546. dev_queue_xmit(skb);
  547. }
  548. return sent;
  549. }
  550. static ieee80211_rx_result
  551. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  552. {
  553. struct sta_info *sta = rx->sta;
  554. struct net_device *dev = rx->dev;
  555. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  556. if (!sta)
  557. return RX_CONTINUE;
  558. /* Update last_rx only for IBSS packets which are for the current
  559. * BSSID to avoid keeping the current IBSS network alive in cases where
  560. * other STAs are using different BSSID. */
  561. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  562. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  563. IEEE80211_IF_TYPE_IBSS);
  564. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  565. sta->last_rx = jiffies;
  566. } else
  567. if (!is_multicast_ether_addr(hdr->addr1) ||
  568. rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  569. /* Update last_rx only for unicast frames in order to prevent
  570. * the Probe Request frames (the only broadcast frames from a
  571. * STA in infrastructure mode) from keeping a connection alive.
  572. */
  573. sta->last_rx = jiffies;
  574. }
  575. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  576. return RX_CONTINUE;
  577. sta->rx_fragments++;
  578. sta->rx_bytes += rx->skb->len;
  579. sta->last_rssi = rx->u.rx.status->ssi;
  580. sta->last_signal = rx->u.rx.status->signal;
  581. sta->last_noise = rx->u.rx.status->noise;
  582. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  583. /* Change STA power saving mode only in the end of a frame
  584. * exchange sequence */
  585. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  586. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  587. else if (!(sta->flags & WLAN_STA_PS) &&
  588. (rx->fc & IEEE80211_FCTL_PM))
  589. ap_sta_ps_start(dev, sta);
  590. }
  591. /* Drop data::nullfunc frames silently, since they are used only to
  592. * control station power saving mode. */
  593. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  594. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  595. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  596. /* Update counter and free packet here to avoid counting this
  597. * as a dropped packed. */
  598. sta->rx_packets++;
  599. dev_kfree_skb(rx->skb);
  600. return RX_QUEUED;
  601. }
  602. return RX_CONTINUE;
  603. } /* ieee80211_rx_h_sta_process */
  604. static inline struct ieee80211_fragment_entry *
  605. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  606. unsigned int frag, unsigned int seq, int rx_queue,
  607. struct sk_buff **skb)
  608. {
  609. struct ieee80211_fragment_entry *entry;
  610. int idx;
  611. idx = sdata->fragment_next;
  612. entry = &sdata->fragments[sdata->fragment_next++];
  613. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  614. sdata->fragment_next = 0;
  615. if (!skb_queue_empty(&entry->skb_list)) {
  616. #ifdef CONFIG_MAC80211_DEBUG
  617. struct ieee80211_hdr *hdr =
  618. (struct ieee80211_hdr *) entry->skb_list.next->data;
  619. DECLARE_MAC_BUF(mac);
  620. DECLARE_MAC_BUF(mac2);
  621. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  622. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  623. "addr1=%s addr2=%s\n",
  624. sdata->dev->name, idx,
  625. jiffies - entry->first_frag_time, entry->seq,
  626. entry->last_frag, print_mac(mac, hdr->addr1),
  627. print_mac(mac2, hdr->addr2));
  628. #endif /* CONFIG_MAC80211_DEBUG */
  629. __skb_queue_purge(&entry->skb_list);
  630. }
  631. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  632. *skb = NULL;
  633. entry->first_frag_time = jiffies;
  634. entry->seq = seq;
  635. entry->rx_queue = rx_queue;
  636. entry->last_frag = frag;
  637. entry->ccmp = 0;
  638. entry->extra_len = 0;
  639. return entry;
  640. }
  641. static inline struct ieee80211_fragment_entry *
  642. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  643. u16 fc, unsigned int frag, unsigned int seq,
  644. int rx_queue, struct ieee80211_hdr *hdr)
  645. {
  646. struct ieee80211_fragment_entry *entry;
  647. int i, idx;
  648. idx = sdata->fragment_next;
  649. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  650. struct ieee80211_hdr *f_hdr;
  651. u16 f_fc;
  652. idx--;
  653. if (idx < 0)
  654. idx = IEEE80211_FRAGMENT_MAX - 1;
  655. entry = &sdata->fragments[idx];
  656. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  657. entry->rx_queue != rx_queue ||
  658. entry->last_frag + 1 != frag)
  659. continue;
  660. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  661. f_fc = le16_to_cpu(f_hdr->frame_control);
  662. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  663. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  664. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  665. continue;
  666. if (entry->first_frag_time + 2 * HZ < jiffies) {
  667. __skb_queue_purge(&entry->skb_list);
  668. continue;
  669. }
  670. return entry;
  671. }
  672. return NULL;
  673. }
  674. static ieee80211_rx_result
  675. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  676. {
  677. struct ieee80211_hdr *hdr;
  678. u16 sc;
  679. unsigned int frag, seq;
  680. struct ieee80211_fragment_entry *entry;
  681. struct sk_buff *skb;
  682. DECLARE_MAC_BUF(mac);
  683. hdr = (struct ieee80211_hdr *) rx->skb->data;
  684. sc = le16_to_cpu(hdr->seq_ctrl);
  685. frag = sc & IEEE80211_SCTL_FRAG;
  686. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  687. (rx->skb)->len < 24 ||
  688. is_multicast_ether_addr(hdr->addr1))) {
  689. /* not fragmented */
  690. goto out;
  691. }
  692. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  693. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  694. if (frag == 0) {
  695. /* This is the first fragment of a new frame. */
  696. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  697. rx->u.rx.queue, &(rx->skb));
  698. if (rx->key && rx->key->conf.alg == ALG_CCMP &&
  699. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  700. /* Store CCMP PN so that we can verify that the next
  701. * fragment has a sequential PN value. */
  702. entry->ccmp = 1;
  703. memcpy(entry->last_pn,
  704. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  705. CCMP_PN_LEN);
  706. }
  707. return RX_QUEUED;
  708. }
  709. /* This is a fragment for a frame that should already be pending in
  710. * fragment cache. Add this fragment to the end of the pending entry.
  711. */
  712. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  713. rx->u.rx.queue, hdr);
  714. if (!entry) {
  715. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  716. return RX_DROP_MONITOR;
  717. }
  718. /* Verify that MPDUs within one MSDU have sequential PN values.
  719. * (IEEE 802.11i, 8.3.3.4.5) */
  720. if (entry->ccmp) {
  721. int i;
  722. u8 pn[CCMP_PN_LEN], *rpn;
  723. if (!rx->key || rx->key->conf.alg != ALG_CCMP)
  724. return RX_DROP_UNUSABLE;
  725. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  726. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  727. pn[i]++;
  728. if (pn[i])
  729. break;
  730. }
  731. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  732. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  733. if (net_ratelimit())
  734. printk(KERN_DEBUG "%s: defrag: CCMP PN not "
  735. "sequential A2=%s"
  736. " PN=%02x%02x%02x%02x%02x%02x "
  737. "(expected %02x%02x%02x%02x%02x%02x)\n",
  738. rx->dev->name, print_mac(mac, hdr->addr2),
  739. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
  740. rpn[5], pn[0], pn[1], pn[2], pn[3],
  741. pn[4], pn[5]);
  742. return RX_DROP_UNUSABLE;
  743. }
  744. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  745. }
  746. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  747. __skb_queue_tail(&entry->skb_list, rx->skb);
  748. entry->last_frag = frag;
  749. entry->extra_len += rx->skb->len;
  750. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  751. rx->skb = NULL;
  752. return RX_QUEUED;
  753. }
  754. rx->skb = __skb_dequeue(&entry->skb_list);
  755. if (skb_tailroom(rx->skb) < entry->extra_len) {
  756. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  757. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  758. GFP_ATOMIC))) {
  759. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  760. __skb_queue_purge(&entry->skb_list);
  761. return RX_DROP_UNUSABLE;
  762. }
  763. }
  764. while ((skb = __skb_dequeue(&entry->skb_list))) {
  765. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  766. dev_kfree_skb(skb);
  767. }
  768. /* Complete frame has been reassembled - process it now */
  769. rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  770. out:
  771. if (rx->sta)
  772. rx->sta->rx_packets++;
  773. if (is_multicast_ether_addr(hdr->addr1))
  774. rx->local->dot11MulticastReceivedFrameCount++;
  775. else
  776. ieee80211_led_rx(rx->local);
  777. return RX_CONTINUE;
  778. }
  779. static ieee80211_rx_result
  780. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  781. {
  782. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  783. struct sk_buff *skb;
  784. int no_pending_pkts;
  785. DECLARE_MAC_BUF(mac);
  786. if (likely(!rx->sta ||
  787. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  788. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  789. !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
  790. return RX_CONTINUE;
  791. if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
  792. (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
  793. return RX_DROP_UNUSABLE;
  794. skb = skb_dequeue(&rx->sta->tx_filtered);
  795. if (!skb) {
  796. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  797. if (skb)
  798. rx->local->total_ps_buffered--;
  799. }
  800. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  801. skb_queue_empty(&rx->sta->ps_tx_buf);
  802. if (skb) {
  803. struct ieee80211_hdr *hdr =
  804. (struct ieee80211_hdr *) skb->data;
  805. /* tell TX path to send one frame even though the STA may
  806. * still remain is PS mode after this frame exchange */
  807. rx->sta->pspoll = 1;
  808. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  809. printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
  810. print_mac(mac, rx->sta->addr), rx->sta->aid,
  811. skb_queue_len(&rx->sta->ps_tx_buf));
  812. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  813. /* Use MoreData flag to indicate whether there are more
  814. * buffered frames for this STA */
  815. if (no_pending_pkts) {
  816. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  817. rx->sta->flags &= ~WLAN_STA_TIM;
  818. } else
  819. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  820. dev_queue_xmit(skb);
  821. if (no_pending_pkts) {
  822. if (rx->local->ops->set_tim)
  823. rx->local->ops->set_tim(local_to_hw(rx->local),
  824. rx->sta->aid, 0);
  825. if (rx->sdata->bss)
  826. bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
  827. }
  828. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  829. } else if (!rx->u.rx.sent_ps_buffered) {
  830. printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
  831. "though there is no buffered frames for it\n",
  832. rx->dev->name, print_mac(mac, rx->sta->addr));
  833. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  834. }
  835. /* Free PS Poll skb here instead of returning RX_DROP that would
  836. * count as an dropped frame. */
  837. dev_kfree_skb(rx->skb);
  838. return RX_QUEUED;
  839. }
  840. static ieee80211_rx_result
  841. ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
  842. {
  843. u16 fc = rx->fc;
  844. u8 *data = rx->skb->data;
  845. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
  846. if (!WLAN_FC_IS_QOS_DATA(fc))
  847. return RX_CONTINUE;
  848. /* remove the qos control field, update frame type and meta-data */
  849. memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
  850. hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
  851. /* change frame type to non QOS */
  852. rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
  853. hdr->frame_control = cpu_to_le16(fc);
  854. return RX_CONTINUE;
  855. }
  856. static int
  857. ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
  858. {
  859. if (unlikely(!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED))) {
  860. #ifdef CONFIG_MAC80211_DEBUG
  861. if (net_ratelimit())
  862. printk(KERN_DEBUG "%s: dropped frame "
  863. "(unauthorized port)\n", rx->dev->name);
  864. #endif /* CONFIG_MAC80211_DEBUG */
  865. return -EACCES;
  866. }
  867. return 0;
  868. }
  869. static int
  870. ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
  871. {
  872. /*
  873. * Pass through unencrypted frames if the hardware has
  874. * decrypted them already.
  875. */
  876. if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
  877. return 0;
  878. /* Drop unencrypted frames if key is set. */
  879. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  880. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  881. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  882. (rx->key || rx->sdata->drop_unencrypted))) {
  883. if (net_ratelimit())
  884. printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
  885. "encryption\n", rx->dev->name);
  886. return -EACCES;
  887. }
  888. return 0;
  889. }
  890. static int
  891. ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
  892. {
  893. struct net_device *dev = rx->dev;
  894. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  895. u16 fc, hdrlen, ethertype;
  896. u8 *payload;
  897. u8 dst[ETH_ALEN];
  898. u8 src[ETH_ALEN];
  899. struct sk_buff *skb = rx->skb;
  900. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  901. DECLARE_MAC_BUF(mac);
  902. DECLARE_MAC_BUF(mac2);
  903. DECLARE_MAC_BUF(mac3);
  904. DECLARE_MAC_BUF(mac4);
  905. fc = rx->fc;
  906. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  907. return -1;
  908. hdrlen = ieee80211_get_hdrlen(fc);
  909. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  910. * header
  911. * IEEE 802.11 address fields:
  912. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  913. * 0 0 DA SA BSSID n/a
  914. * 0 1 DA BSSID SA n/a
  915. * 1 0 BSSID SA DA n/a
  916. * 1 1 RA TA DA SA
  917. */
  918. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  919. case IEEE80211_FCTL_TODS:
  920. /* BSSID SA DA */
  921. memcpy(dst, hdr->addr3, ETH_ALEN);
  922. memcpy(src, hdr->addr2, ETH_ALEN);
  923. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
  924. sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
  925. if (net_ratelimit())
  926. printk(KERN_DEBUG "%s: dropped ToDS frame "
  927. "(BSSID=%s SA=%s DA=%s)\n",
  928. dev->name,
  929. print_mac(mac, hdr->addr1),
  930. print_mac(mac2, hdr->addr2),
  931. print_mac(mac3, hdr->addr3));
  932. return -1;
  933. }
  934. break;
  935. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  936. /* RA TA DA SA */
  937. memcpy(dst, hdr->addr3, ETH_ALEN);
  938. memcpy(src, hdr->addr4, ETH_ALEN);
  939. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS)) {
  940. if (net_ratelimit())
  941. printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
  942. "frame (RA=%s TA=%s DA=%s SA=%s)\n",
  943. rx->dev->name,
  944. print_mac(mac, hdr->addr1),
  945. print_mac(mac2, hdr->addr2),
  946. print_mac(mac3, hdr->addr3),
  947. print_mac(mac4, hdr->addr4));
  948. return -1;
  949. }
  950. break;
  951. case IEEE80211_FCTL_FROMDS:
  952. /* DA BSSID SA */
  953. memcpy(dst, hdr->addr1, ETH_ALEN);
  954. memcpy(src, hdr->addr3, ETH_ALEN);
  955. if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
  956. (is_multicast_ether_addr(dst) &&
  957. !compare_ether_addr(src, dev->dev_addr)))
  958. return -1;
  959. break;
  960. case 0:
  961. /* DA SA BSSID */
  962. memcpy(dst, hdr->addr1, ETH_ALEN);
  963. memcpy(src, hdr->addr2, ETH_ALEN);
  964. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
  965. if (net_ratelimit()) {
  966. printk(KERN_DEBUG "%s: dropped IBSS frame "
  967. "(DA=%s SA=%s BSSID=%s)\n",
  968. dev->name,
  969. print_mac(mac, hdr->addr1),
  970. print_mac(mac2, hdr->addr2),
  971. print_mac(mac3, hdr->addr3));
  972. }
  973. return -1;
  974. }
  975. break;
  976. }
  977. if (unlikely(skb->len - hdrlen < 8)) {
  978. if (net_ratelimit()) {
  979. printk(KERN_DEBUG "%s: RX too short data frame "
  980. "payload\n", dev->name);
  981. }
  982. return -1;
  983. }
  984. payload = skb->data + hdrlen;
  985. ethertype = (payload[6] << 8) | payload[7];
  986. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  987. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  988. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  989. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  990. * replace EtherType */
  991. skb_pull(skb, hdrlen + 6);
  992. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  993. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  994. } else {
  995. struct ethhdr *ehdr;
  996. __be16 len;
  997. skb_pull(skb, hdrlen);
  998. len = htons(skb->len);
  999. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  1000. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  1001. memcpy(ehdr->h_source, src, ETH_ALEN);
  1002. ehdr->h_proto = len;
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * requires that rx->skb is a frame with ethernet header
  1008. */
  1009. static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
  1010. {
  1011. static const u8 pae_group_addr[ETH_ALEN]
  1012. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1013. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1014. /*
  1015. * Allow EAPOL frames to us/the PAE group address regardless
  1016. * of whether the frame was encrypted or not.
  1017. */
  1018. if (ehdr->h_proto == htons(ETH_P_PAE) &&
  1019. (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
  1020. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1021. return true;
  1022. if (ieee80211_802_1x_port_control(rx) ||
  1023. ieee80211_drop_unencrypted(rx))
  1024. return false;
  1025. return true;
  1026. }
  1027. /*
  1028. * requires that rx->skb is a frame with ethernet header
  1029. */
  1030. static void
  1031. ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
  1032. {
  1033. struct net_device *dev = rx->dev;
  1034. struct ieee80211_local *local = rx->local;
  1035. struct sk_buff *skb, *xmit_skb;
  1036. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1037. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1038. struct sta_info *dsta;
  1039. skb = rx->skb;
  1040. xmit_skb = NULL;
  1041. if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
  1042. sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
  1043. (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
  1044. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1045. /*
  1046. * send multicast frames both to higher layers in
  1047. * local net stack and back to the wireless medium
  1048. */
  1049. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1050. if (!xmit_skb && net_ratelimit())
  1051. printk(KERN_DEBUG "%s: failed to clone "
  1052. "multicast frame\n", dev->name);
  1053. } else {
  1054. dsta = sta_info_get(local, skb->data);
  1055. if (dsta && dsta->dev == dev) {
  1056. /*
  1057. * The destination station is associated to
  1058. * this AP (in this VLAN), so send the frame
  1059. * directly to it and do not pass it to local
  1060. * net stack.
  1061. */
  1062. xmit_skb = skb;
  1063. skb = NULL;
  1064. }
  1065. if (dsta)
  1066. sta_info_put(dsta);
  1067. }
  1068. }
  1069. if (skb) {
  1070. /* deliver to local stack */
  1071. skb->protocol = eth_type_trans(skb, dev);
  1072. memset(skb->cb, 0, sizeof(skb->cb));
  1073. netif_rx(skb);
  1074. }
  1075. if (xmit_skb) {
  1076. /* send to wireless media */
  1077. xmit_skb->protocol = htons(ETH_P_802_3);
  1078. skb_reset_network_header(xmit_skb);
  1079. skb_reset_mac_header(xmit_skb);
  1080. dev_queue_xmit(xmit_skb);
  1081. }
  1082. }
  1083. static ieee80211_rx_result
  1084. ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
  1085. {
  1086. struct net_device *dev = rx->dev;
  1087. struct ieee80211_local *local = rx->local;
  1088. u16 fc, ethertype;
  1089. u8 *payload;
  1090. struct sk_buff *skb = rx->skb, *frame = NULL;
  1091. const struct ethhdr *eth;
  1092. int remaining, err;
  1093. u8 dst[ETH_ALEN];
  1094. u8 src[ETH_ALEN];
  1095. DECLARE_MAC_BUF(mac);
  1096. fc = rx->fc;
  1097. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1098. return RX_CONTINUE;
  1099. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1100. return RX_DROP_MONITOR;
  1101. if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
  1102. return RX_CONTINUE;
  1103. err = ieee80211_data_to_8023(rx);
  1104. if (unlikely(err))
  1105. return RX_DROP_UNUSABLE;
  1106. skb->dev = dev;
  1107. dev->stats.rx_packets++;
  1108. dev->stats.rx_bytes += skb->len;
  1109. /* skip the wrapping header */
  1110. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  1111. if (!eth)
  1112. return RX_DROP_UNUSABLE;
  1113. while (skb != frame) {
  1114. u8 padding;
  1115. __be16 len = eth->h_proto;
  1116. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  1117. remaining = skb->len;
  1118. memcpy(dst, eth->h_dest, ETH_ALEN);
  1119. memcpy(src, eth->h_source, ETH_ALEN);
  1120. padding = ((4 - subframe_len) & 0x3);
  1121. /* the last MSDU has no padding */
  1122. if (subframe_len > remaining) {
  1123. printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
  1124. return RX_DROP_UNUSABLE;
  1125. }
  1126. skb_pull(skb, sizeof(struct ethhdr));
  1127. /* if last subframe reuse skb */
  1128. if (remaining <= subframe_len + padding)
  1129. frame = skb;
  1130. else {
  1131. frame = dev_alloc_skb(local->hw.extra_tx_headroom +
  1132. subframe_len);
  1133. if (frame == NULL)
  1134. return RX_DROP_UNUSABLE;
  1135. skb_reserve(frame, local->hw.extra_tx_headroom +
  1136. sizeof(struct ethhdr));
  1137. memcpy(skb_put(frame, ntohs(len)), skb->data,
  1138. ntohs(len));
  1139. eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
  1140. padding);
  1141. if (!eth) {
  1142. printk(KERN_DEBUG "%s: wrong buffer size ",
  1143. dev->name);
  1144. dev_kfree_skb(frame);
  1145. return RX_DROP_UNUSABLE;
  1146. }
  1147. }
  1148. skb_reset_network_header(frame);
  1149. frame->dev = dev;
  1150. frame->priority = skb->priority;
  1151. rx->skb = frame;
  1152. payload = frame->data;
  1153. ethertype = (payload[6] << 8) | payload[7];
  1154. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  1155. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1156. compare_ether_addr(payload,
  1157. bridge_tunnel_header) == 0)) {
  1158. /* remove RFC1042 or Bridge-Tunnel
  1159. * encapsulation and replace EtherType */
  1160. skb_pull(frame, 6);
  1161. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1162. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1163. } else {
  1164. memcpy(skb_push(frame, sizeof(__be16)),
  1165. &len, sizeof(__be16));
  1166. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1167. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1168. }
  1169. if (!ieee80211_frame_allowed(rx)) {
  1170. if (skb == frame) /* last frame */
  1171. return RX_DROP_UNUSABLE;
  1172. dev_kfree_skb(frame);
  1173. continue;
  1174. }
  1175. ieee80211_deliver_skb(rx);
  1176. }
  1177. return RX_QUEUED;
  1178. }
  1179. static ieee80211_rx_result
  1180. ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
  1181. {
  1182. struct net_device *dev = rx->dev;
  1183. u16 fc;
  1184. int err;
  1185. fc = rx->fc;
  1186. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1187. return RX_CONTINUE;
  1188. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1189. return RX_DROP_MONITOR;
  1190. err = ieee80211_data_to_8023(rx);
  1191. if (unlikely(err))
  1192. return RX_DROP_UNUSABLE;
  1193. if (!ieee80211_frame_allowed(rx))
  1194. return RX_DROP_MONITOR;
  1195. rx->skb->dev = dev;
  1196. dev->stats.rx_packets++;
  1197. dev->stats.rx_bytes += rx->skb->len;
  1198. ieee80211_deliver_skb(rx);
  1199. return RX_QUEUED;
  1200. }
  1201. static ieee80211_rx_result
  1202. ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
  1203. {
  1204. struct ieee80211_local *local = rx->local;
  1205. struct ieee80211_hw *hw = &local->hw;
  1206. struct sk_buff *skb = rx->skb;
  1207. struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
  1208. struct tid_ampdu_rx *tid_agg_rx;
  1209. u16 start_seq_num;
  1210. u16 tid;
  1211. if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
  1212. return RX_CONTINUE;
  1213. if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
  1214. if (!rx->sta)
  1215. return RX_CONTINUE;
  1216. tid = le16_to_cpu(bar->control) >> 12;
  1217. tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
  1218. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1219. return RX_CONTINUE;
  1220. start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
  1221. /* reset session timer */
  1222. if (tid_agg_rx->timeout) {
  1223. unsigned long expires =
  1224. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1225. mod_timer(&tid_agg_rx->session_timer, expires);
  1226. }
  1227. /* manage reordering buffer according to requested */
  1228. /* sequence number */
  1229. rcu_read_lock();
  1230. ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
  1231. start_seq_num, 1);
  1232. rcu_read_unlock();
  1233. return RX_DROP_UNUSABLE;
  1234. }
  1235. return RX_CONTINUE;
  1236. }
  1237. static ieee80211_rx_result
  1238. ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
  1239. {
  1240. struct ieee80211_sub_if_data *sdata;
  1241. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  1242. return RX_DROP_MONITOR;
  1243. sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  1244. if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  1245. sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
  1246. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  1247. ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
  1248. else
  1249. return RX_DROP_MONITOR;
  1250. return RX_QUEUED;
  1251. }
  1252. static void ieee80211_rx_michael_mic_report(struct net_device *dev,
  1253. struct ieee80211_hdr *hdr,
  1254. struct ieee80211_txrx_data *rx)
  1255. {
  1256. int keyidx, hdrlen;
  1257. DECLARE_MAC_BUF(mac);
  1258. DECLARE_MAC_BUF(mac2);
  1259. hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
  1260. if (rx->skb->len >= hdrlen + 4)
  1261. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  1262. else
  1263. keyidx = -1;
  1264. if (net_ratelimit())
  1265. printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
  1266. "failure from %s to %s keyidx=%d\n",
  1267. dev->name, print_mac(mac, hdr->addr2),
  1268. print_mac(mac2, hdr->addr1), keyidx);
  1269. if (!rx->sta) {
  1270. /*
  1271. * Some hardware seem to generate incorrect Michael MIC
  1272. * reports; ignore them to avoid triggering countermeasures.
  1273. */
  1274. if (net_ratelimit())
  1275. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1276. "error for unknown address %s\n",
  1277. dev->name, print_mac(mac, hdr->addr2));
  1278. goto ignore;
  1279. }
  1280. if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
  1281. if (net_ratelimit())
  1282. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1283. "error for a frame with no PROTECTED flag (src "
  1284. "%s)\n", dev->name, print_mac(mac, hdr->addr2));
  1285. goto ignore;
  1286. }
  1287. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
  1288. /*
  1289. * APs with pairwise keys should never receive Michael MIC
  1290. * errors for non-zero keyidx because these are reserved for
  1291. * group keys and only the AP is sending real multicast
  1292. * frames in the BSS.
  1293. */
  1294. if (net_ratelimit())
  1295. printk(KERN_DEBUG "%s: ignored Michael MIC error for "
  1296. "a frame with non-zero keyidx (%d)"
  1297. " (src %s)\n", dev->name, keyidx,
  1298. print_mac(mac, hdr->addr2));
  1299. goto ignore;
  1300. }
  1301. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  1302. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  1303. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
  1304. if (net_ratelimit())
  1305. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1306. "error for a frame that cannot be encrypted "
  1307. "(fc=0x%04x) (src %s)\n",
  1308. dev->name, rx->fc, print_mac(mac, hdr->addr2));
  1309. goto ignore;
  1310. }
  1311. mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
  1312. ignore:
  1313. dev_kfree_skb(rx->skb);
  1314. rx->skb = NULL;
  1315. }
  1316. static void ieee80211_rx_cooked_monitor(struct ieee80211_txrx_data *rx)
  1317. {
  1318. struct ieee80211_sub_if_data *sdata;
  1319. struct ieee80211_local *local = rx->local;
  1320. struct ieee80211_rtap_hdr {
  1321. struct ieee80211_radiotap_header hdr;
  1322. u8 flags;
  1323. u8 rate;
  1324. __le16 chan_freq;
  1325. __le16 chan_flags;
  1326. } __attribute__ ((packed)) *rthdr;
  1327. struct sk_buff *skb = rx->skb, *skb2;
  1328. struct net_device *prev_dev = NULL;
  1329. struct ieee80211_rx_status *status = rx->u.rx.status;
  1330. if (rx->flags & IEEE80211_TXRXD_RX_CMNTR_REPORTED)
  1331. goto out_free_skb;
  1332. if (skb_headroom(skb) < sizeof(*rthdr) &&
  1333. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  1334. goto out_free_skb;
  1335. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  1336. memset(rthdr, 0, sizeof(*rthdr));
  1337. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1338. rthdr->hdr.it_present =
  1339. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  1340. (1 << IEEE80211_RADIOTAP_RATE) |
  1341. (1 << IEEE80211_RADIOTAP_CHANNEL));
  1342. rthdr->rate = rx->u.rx.rate->bitrate / 5;
  1343. rthdr->chan_freq = cpu_to_le16(status->freq);
  1344. if (status->band == IEEE80211_BAND_5GHZ)
  1345. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  1346. IEEE80211_CHAN_5GHZ);
  1347. else
  1348. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  1349. IEEE80211_CHAN_2GHZ);
  1350. skb_set_mac_header(skb, 0);
  1351. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1352. skb->pkt_type = PACKET_OTHERHOST;
  1353. skb->protocol = htons(ETH_P_802_2);
  1354. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1355. if (!netif_running(sdata->dev))
  1356. continue;
  1357. if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR ||
  1358. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  1359. continue;
  1360. if (prev_dev) {
  1361. skb2 = skb_clone(skb, GFP_ATOMIC);
  1362. if (skb2) {
  1363. skb2->dev = prev_dev;
  1364. netif_rx(skb2);
  1365. }
  1366. }
  1367. prev_dev = sdata->dev;
  1368. sdata->dev->stats.rx_packets++;
  1369. sdata->dev->stats.rx_bytes += skb->len;
  1370. }
  1371. if (prev_dev) {
  1372. skb->dev = prev_dev;
  1373. netif_rx(skb);
  1374. skb = NULL;
  1375. } else
  1376. goto out_free_skb;
  1377. rx->flags |= IEEE80211_TXRXD_RX_CMNTR_REPORTED;
  1378. return;
  1379. out_free_skb:
  1380. dev_kfree_skb(skb);
  1381. }
  1382. typedef ieee80211_rx_result (*ieee80211_rx_handler)(struct ieee80211_txrx_data *);
  1383. static ieee80211_rx_handler ieee80211_rx_handlers[] =
  1384. {
  1385. ieee80211_rx_h_if_stats,
  1386. ieee80211_rx_h_passive_scan,
  1387. ieee80211_rx_h_check,
  1388. ieee80211_rx_h_decrypt,
  1389. ieee80211_rx_h_sta_process,
  1390. ieee80211_rx_h_defragment,
  1391. ieee80211_rx_h_ps_poll,
  1392. ieee80211_rx_h_michael_mic_verify,
  1393. /* this must be after decryption - so header is counted in MPDU mic
  1394. * must be before pae and data, so QOS_DATA format frames
  1395. * are not passed to user space by these functions
  1396. */
  1397. ieee80211_rx_h_remove_qos_control,
  1398. ieee80211_rx_h_amsdu,
  1399. ieee80211_rx_h_data,
  1400. ieee80211_rx_h_ctrl,
  1401. ieee80211_rx_h_mgmt,
  1402. NULL
  1403. };
  1404. static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
  1405. struct ieee80211_txrx_data *rx,
  1406. struct sk_buff *skb)
  1407. {
  1408. ieee80211_rx_handler *handler;
  1409. ieee80211_rx_result res = RX_DROP_MONITOR;
  1410. rx->skb = skb;
  1411. rx->sdata = sdata;
  1412. rx->dev = sdata->dev;
  1413. for (handler = ieee80211_rx_handlers; *handler != NULL; handler++) {
  1414. res = (*handler)(rx);
  1415. switch (res) {
  1416. case RX_CONTINUE:
  1417. continue;
  1418. case RX_DROP_UNUSABLE:
  1419. case RX_DROP_MONITOR:
  1420. I802_DEBUG_INC(sdata->local->rx_handlers_drop);
  1421. if (rx->sta)
  1422. rx->sta->rx_dropped++;
  1423. break;
  1424. case RX_QUEUED:
  1425. I802_DEBUG_INC(sdata->local->rx_handlers_queued);
  1426. break;
  1427. }
  1428. break;
  1429. }
  1430. switch (res) {
  1431. case RX_CONTINUE:
  1432. case RX_DROP_MONITOR:
  1433. ieee80211_rx_cooked_monitor(rx);
  1434. break;
  1435. case RX_DROP_UNUSABLE:
  1436. dev_kfree_skb(rx->skb);
  1437. break;
  1438. }
  1439. }
  1440. /* main receive path */
  1441. static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
  1442. u8 *bssid, struct ieee80211_txrx_data *rx,
  1443. struct ieee80211_hdr *hdr)
  1444. {
  1445. int multicast = is_multicast_ether_addr(hdr->addr1);
  1446. switch (sdata->vif.type) {
  1447. case IEEE80211_IF_TYPE_STA:
  1448. if (!bssid)
  1449. return 0;
  1450. if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1451. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1452. return 0;
  1453. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1454. } else if (!multicast &&
  1455. compare_ether_addr(sdata->dev->dev_addr,
  1456. hdr->addr1) != 0) {
  1457. if (!(sdata->dev->flags & IFF_PROMISC))
  1458. return 0;
  1459. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1460. }
  1461. break;
  1462. case IEEE80211_IF_TYPE_IBSS:
  1463. if (!bssid)
  1464. return 0;
  1465. if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1466. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1467. return 0;
  1468. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1469. } else if (!multicast &&
  1470. compare_ether_addr(sdata->dev->dev_addr,
  1471. hdr->addr1) != 0) {
  1472. if (!(sdata->dev->flags & IFF_PROMISC))
  1473. return 0;
  1474. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1475. } else if (!rx->sta)
  1476. rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
  1477. bssid, hdr->addr2);
  1478. break;
  1479. case IEEE80211_IF_TYPE_VLAN:
  1480. case IEEE80211_IF_TYPE_AP:
  1481. if (!bssid) {
  1482. if (compare_ether_addr(sdata->dev->dev_addr,
  1483. hdr->addr1))
  1484. return 0;
  1485. } else if (!ieee80211_bssid_match(bssid,
  1486. sdata->dev->dev_addr)) {
  1487. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1488. return 0;
  1489. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1490. }
  1491. if (sdata->dev == sdata->local->mdev &&
  1492. !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1493. /* do not receive anything via
  1494. * master device when not scanning */
  1495. return 0;
  1496. break;
  1497. case IEEE80211_IF_TYPE_WDS:
  1498. if (bssid ||
  1499. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  1500. return 0;
  1501. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  1502. return 0;
  1503. break;
  1504. case IEEE80211_IF_TYPE_MNTR:
  1505. /* take everything */
  1506. break;
  1507. case IEEE80211_IF_TYPE_INVALID:
  1508. /* should never get here */
  1509. WARN_ON(1);
  1510. break;
  1511. }
  1512. return 1;
  1513. }
  1514. /*
  1515. * This is the actual Rx frames handler. as it blongs to Rx path it must
  1516. * be called with rcu_read_lock protection.
  1517. */
  1518. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  1519. struct sk_buff *skb,
  1520. struct ieee80211_rx_status *status,
  1521. u32 load,
  1522. struct ieee80211_rate *rate)
  1523. {
  1524. struct ieee80211_local *local = hw_to_local(hw);
  1525. struct ieee80211_sub_if_data *sdata;
  1526. struct ieee80211_hdr *hdr;
  1527. struct ieee80211_txrx_data rx;
  1528. u16 type;
  1529. int prepares;
  1530. struct ieee80211_sub_if_data *prev = NULL;
  1531. struct sk_buff *skb_new;
  1532. u8 *bssid;
  1533. hdr = (struct ieee80211_hdr *) skb->data;
  1534. memset(&rx, 0, sizeof(rx));
  1535. rx.skb = skb;
  1536. rx.local = local;
  1537. rx.u.rx.status = status;
  1538. rx.u.rx.load = load;
  1539. rx.u.rx.rate = rate;
  1540. rx.fc = le16_to_cpu(hdr->frame_control);
  1541. type = rx.fc & IEEE80211_FCTL_FTYPE;
  1542. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  1543. local->dot11ReceivedFragmentCount++;
  1544. rx.sta = sta_info_get(local, hdr->addr2);
  1545. if (rx.sta) {
  1546. rx.dev = rx.sta->dev;
  1547. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  1548. }
  1549. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  1550. ieee80211_rx_michael_mic_report(local->mdev, hdr, &rx);
  1551. goto end;
  1552. }
  1553. if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
  1554. rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
  1555. ieee80211_parse_qos(&rx);
  1556. ieee80211_verify_ip_alignment(&rx);
  1557. skb = rx.skb;
  1558. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1559. if (!netif_running(sdata->dev))
  1560. continue;
  1561. if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
  1562. continue;
  1563. bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  1564. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1565. prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
  1566. if (!prepares)
  1567. continue;
  1568. /*
  1569. * frame is destined for this interface, but if it's not
  1570. * also for the previous one we handle that after the
  1571. * loop to avoid copying the SKB once too much
  1572. */
  1573. if (!prev) {
  1574. prev = sdata;
  1575. continue;
  1576. }
  1577. /*
  1578. * frame was destined for the previous interface
  1579. * so invoke RX handlers for it
  1580. */
  1581. skb_new = skb_copy(skb, GFP_ATOMIC);
  1582. if (!skb_new) {
  1583. if (net_ratelimit())
  1584. printk(KERN_DEBUG "%s: failed to copy "
  1585. "multicast frame for %s",
  1586. wiphy_name(local->hw.wiphy),
  1587. prev->dev->name);
  1588. continue;
  1589. }
  1590. rx.fc = le16_to_cpu(hdr->frame_control);
  1591. ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
  1592. prev = sdata;
  1593. }
  1594. if (prev) {
  1595. rx.fc = le16_to_cpu(hdr->frame_control);
  1596. ieee80211_invoke_rx_handlers(prev, &rx, skb);
  1597. } else
  1598. dev_kfree_skb(skb);
  1599. end:
  1600. if (rx.sta)
  1601. sta_info_put(rx.sta);
  1602. }
  1603. #define SEQ_MODULO 0x1000
  1604. #define SEQ_MASK 0xfff
  1605. static inline int seq_less(u16 sq1, u16 sq2)
  1606. {
  1607. return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
  1608. }
  1609. static inline u16 seq_inc(u16 sq)
  1610. {
  1611. return ((sq + 1) & SEQ_MASK);
  1612. }
  1613. static inline u16 seq_sub(u16 sq1, u16 sq2)
  1614. {
  1615. return ((sq1 - sq2) & SEQ_MASK);
  1616. }
  1617. /*
  1618. * As it function blongs to Rx path it must be called with
  1619. * the proper rcu_read_lock protection for its flow.
  1620. */
  1621. u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  1622. struct tid_ampdu_rx *tid_agg_rx,
  1623. struct sk_buff *skb, u16 mpdu_seq_num,
  1624. int bar_req)
  1625. {
  1626. struct ieee80211_local *local = hw_to_local(hw);
  1627. struct ieee80211_rx_status status;
  1628. u16 head_seq_num, buf_size;
  1629. int index;
  1630. u32 pkt_load;
  1631. struct ieee80211_supported_band *sband;
  1632. struct ieee80211_rate *rate;
  1633. buf_size = tid_agg_rx->buf_size;
  1634. head_seq_num = tid_agg_rx->head_seq_num;
  1635. /* frame with out of date sequence number */
  1636. if (seq_less(mpdu_seq_num, head_seq_num)) {
  1637. dev_kfree_skb(skb);
  1638. return 1;
  1639. }
  1640. /* if frame sequence number exceeds our buffering window size or
  1641. * block Ack Request arrived - release stored frames */
  1642. if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
  1643. /* new head to the ordering buffer */
  1644. if (bar_req)
  1645. head_seq_num = mpdu_seq_num;
  1646. else
  1647. head_seq_num =
  1648. seq_inc(seq_sub(mpdu_seq_num, buf_size));
  1649. /* release stored frames up to new head to stack */
  1650. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  1651. index = seq_sub(tid_agg_rx->head_seq_num,
  1652. tid_agg_rx->ssn)
  1653. % tid_agg_rx->buf_size;
  1654. if (tid_agg_rx->reorder_buf[index]) {
  1655. /* release the reordered frames to stack */
  1656. memcpy(&status,
  1657. tid_agg_rx->reorder_buf[index]->cb,
  1658. sizeof(status));
  1659. sband = local->hw.wiphy->bands[status.band];
  1660. rate = &sband->bitrates[status.rate_idx];
  1661. pkt_load = ieee80211_rx_load_stats(local,
  1662. tid_agg_rx->reorder_buf[index],
  1663. &status, rate);
  1664. __ieee80211_rx_handle_packet(hw,
  1665. tid_agg_rx->reorder_buf[index],
  1666. &status, pkt_load, rate);
  1667. tid_agg_rx->stored_mpdu_num--;
  1668. tid_agg_rx->reorder_buf[index] = NULL;
  1669. }
  1670. tid_agg_rx->head_seq_num =
  1671. seq_inc(tid_agg_rx->head_seq_num);
  1672. }
  1673. if (bar_req)
  1674. return 1;
  1675. }
  1676. /* now the new frame is always in the range of the reordering */
  1677. /* buffer window */
  1678. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
  1679. % tid_agg_rx->buf_size;
  1680. /* check if we already stored this frame */
  1681. if (tid_agg_rx->reorder_buf[index]) {
  1682. dev_kfree_skb(skb);
  1683. return 1;
  1684. }
  1685. /* if arrived mpdu is in the right order and nothing else stored */
  1686. /* release it immediately */
  1687. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1688. tid_agg_rx->stored_mpdu_num == 0) {
  1689. tid_agg_rx->head_seq_num =
  1690. seq_inc(tid_agg_rx->head_seq_num);
  1691. return 0;
  1692. }
  1693. /* put the frame in the reordering buffer */
  1694. tid_agg_rx->reorder_buf[index] = skb;
  1695. tid_agg_rx->stored_mpdu_num++;
  1696. /* release the buffer until next missing frame */
  1697. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
  1698. % tid_agg_rx->buf_size;
  1699. while (tid_agg_rx->reorder_buf[index]) {
  1700. /* release the reordered frame back to stack */
  1701. memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
  1702. sizeof(status));
  1703. sband = local->hw.wiphy->bands[status.band];
  1704. rate = &sband->bitrates[status.rate_idx];
  1705. pkt_load = ieee80211_rx_load_stats(local,
  1706. tid_agg_rx->reorder_buf[index],
  1707. &status, rate);
  1708. __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
  1709. &status, pkt_load, rate);
  1710. tid_agg_rx->stored_mpdu_num--;
  1711. tid_agg_rx->reorder_buf[index] = NULL;
  1712. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  1713. index = seq_sub(tid_agg_rx->head_seq_num,
  1714. tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  1715. }
  1716. return 1;
  1717. }
  1718. static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
  1719. struct sk_buff *skb)
  1720. {
  1721. struct ieee80211_hw *hw = &local->hw;
  1722. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1723. struct sta_info *sta;
  1724. struct tid_ampdu_rx *tid_agg_rx;
  1725. u16 fc, sc;
  1726. u16 mpdu_seq_num;
  1727. u8 ret = 0, *qc;
  1728. int tid;
  1729. sta = sta_info_get(local, hdr->addr2);
  1730. if (!sta)
  1731. return ret;
  1732. fc = le16_to_cpu(hdr->frame_control);
  1733. /* filter the QoS data rx stream according to
  1734. * STA/TID and check if this STA/TID is on aggregation */
  1735. if (!WLAN_FC_IS_QOS_DATA(fc))
  1736. goto end_reorder;
  1737. qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
  1738. tid = qc[0] & QOS_CONTROL_TID_MASK;
  1739. tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
  1740. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1741. goto end_reorder;
  1742. /* null data frames are excluded */
  1743. if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
  1744. goto end_reorder;
  1745. /* new un-ordered ampdu frame - process it */
  1746. /* reset session timer */
  1747. if (tid_agg_rx->timeout) {
  1748. unsigned long expires =
  1749. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1750. mod_timer(&tid_agg_rx->session_timer, expires);
  1751. }
  1752. /* if this mpdu is fragmented - terminate rx aggregation session */
  1753. sc = le16_to_cpu(hdr->seq_ctrl);
  1754. if (sc & IEEE80211_SCTL_FRAG) {
  1755. ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
  1756. tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
  1757. ret = 1;
  1758. goto end_reorder;
  1759. }
  1760. /* according to mpdu sequence number deal with reordering buffer */
  1761. mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1762. ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
  1763. mpdu_seq_num, 0);
  1764. end_reorder:
  1765. if (sta)
  1766. sta_info_put(sta);
  1767. return ret;
  1768. }
  1769. /*
  1770. * This is the receive path handler. It is called by a low level driver when an
  1771. * 802.11 MPDU is received from the hardware.
  1772. */
  1773. void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
  1774. struct ieee80211_rx_status *status)
  1775. {
  1776. struct ieee80211_local *local = hw_to_local(hw);
  1777. u32 pkt_load;
  1778. struct ieee80211_rate *rate = NULL;
  1779. struct ieee80211_supported_band *sband;
  1780. if (status->band < 0 ||
  1781. status->band > IEEE80211_NUM_BANDS) {
  1782. WARN_ON(1);
  1783. return;
  1784. }
  1785. sband = local->hw.wiphy->bands[status->band];
  1786. if (!sband ||
  1787. status->rate_idx < 0 ||
  1788. status->rate_idx >= sband->n_bitrates) {
  1789. WARN_ON(1);
  1790. return;
  1791. }
  1792. rate = &sband->bitrates[status->rate_idx];
  1793. /*
  1794. * key references and virtual interfaces are protected using RCU
  1795. * and this requires that we are in a read-side RCU section during
  1796. * receive processing
  1797. */
  1798. rcu_read_lock();
  1799. /*
  1800. * Frames with failed FCS/PLCP checksum are not returned,
  1801. * all other frames are returned without radiotap header
  1802. * if it was previously present.
  1803. * Also, frames with less than 16 bytes are dropped.
  1804. */
  1805. skb = ieee80211_rx_monitor(local, skb, status, rate);
  1806. if (!skb) {
  1807. rcu_read_unlock();
  1808. return;
  1809. }
  1810. pkt_load = ieee80211_rx_load_stats(local, skb, status, rate);
  1811. local->channel_use_raw += pkt_load;
  1812. if (!ieee80211_rx_reorder_ampdu(local, skb))
  1813. __ieee80211_rx_handle_packet(hw, skb, status, pkt_load, rate);
  1814. rcu_read_unlock();
  1815. }
  1816. EXPORT_SYMBOL(__ieee80211_rx);
  1817. /* This is a version of the rx handler that can be called from hard irq
  1818. * context. Post the skb on the queue and schedule the tasklet */
  1819. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  1820. struct ieee80211_rx_status *status)
  1821. {
  1822. struct ieee80211_local *local = hw_to_local(hw);
  1823. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  1824. skb->dev = local->mdev;
  1825. /* copy status into skb->cb for use by tasklet */
  1826. memcpy(skb->cb, status, sizeof(*status));
  1827. skb->pkt_type = IEEE80211_RX_MSG;
  1828. skb_queue_tail(&local->skb_queue, skb);
  1829. tasklet_schedule(&local->tasklet);
  1830. }
  1831. EXPORT_SYMBOL(ieee80211_rx_irqsafe);