rx.c 44 KB

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