rx.c 45 KB

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