rx.c 49 KB

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