rx.c 45 KB

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