rx.c 57 KB

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