rx.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. /*
  1268. * key references and virtual interfaces are protected using RCU
  1269. * and this requires that we are in a read-side RCU section during
  1270. * receive processing
  1271. */
  1272. rcu_read_lock();
  1273. /*
  1274. * Frames with failed FCS/PLCP checksum are not returned,
  1275. * all other frames are returned without radiotap header
  1276. * if it was previously present.
  1277. * Also, frames with less than 16 bytes are dropped.
  1278. */
  1279. skb = ieee80211_rx_monitor(local, skb, status);
  1280. if (!skb) {
  1281. rcu_read_unlock();
  1282. return;
  1283. }
  1284. hdr = (struct ieee80211_hdr *) skb->data;
  1285. memset(&rx, 0, sizeof(rx));
  1286. rx.skb = skb;
  1287. rx.local = local;
  1288. rx.u.rx.status = status;
  1289. rx.fc = le16_to_cpu(hdr->frame_control);
  1290. type = rx.fc & IEEE80211_FCTL_FTYPE;
  1291. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  1292. local->dot11ReceivedFragmentCount++;
  1293. sta = rx.sta = sta_info_get(local, hdr->addr2);
  1294. if (sta) {
  1295. rx.dev = rx.sta->dev;
  1296. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  1297. }
  1298. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  1299. ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
  1300. goto end;
  1301. }
  1302. if (unlikely(local->sta_scanning))
  1303. rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
  1304. if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
  1305. sta) != TXRX_CONTINUE)
  1306. goto end;
  1307. skb = rx.skb;
  1308. if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
  1309. !atomic_read(&local->iff_promiscs) &&
  1310. !is_multicast_ether_addr(hdr->addr1)) {
  1311. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1312. ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
  1313. rx.sta);
  1314. sta_info_put(sta);
  1315. rcu_read_unlock();
  1316. return;
  1317. }
  1318. bssid = ieee80211_get_bssid(hdr, skb->len);
  1319. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1320. if (!netif_running(sdata->dev))
  1321. continue;
  1322. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  1323. continue;
  1324. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1325. prepres = prepare_for_handlers(sdata, bssid, &rx, hdr);
  1326. /* prepare_for_handlers can change sta */
  1327. sta = rx.sta;
  1328. if (!prepres)
  1329. continue;
  1330. /*
  1331. * frame is destined for this interface, but if it's not
  1332. * also for the previous one we handle that after the
  1333. * loop to avoid copying the SKB once too much
  1334. */
  1335. if (!prev) {
  1336. prev = sdata;
  1337. continue;
  1338. }
  1339. /*
  1340. * frame was destined for the previous interface
  1341. * so invoke RX handlers for it
  1342. */
  1343. skb_new = skb_copy(skb, GFP_ATOMIC);
  1344. if (!skb_new) {
  1345. if (net_ratelimit())
  1346. printk(KERN_DEBUG "%s: failed to copy "
  1347. "multicast frame for %s",
  1348. wiphy_name(local->hw.wiphy),
  1349. prev->dev->name);
  1350. continue;
  1351. }
  1352. rx.skb = skb_new;
  1353. rx.dev = prev->dev;
  1354. rx.sdata = prev;
  1355. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  1356. &rx, sta);
  1357. prev = sdata;
  1358. }
  1359. if (prev) {
  1360. rx.skb = skb;
  1361. rx.dev = prev->dev;
  1362. rx.sdata = prev;
  1363. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  1364. &rx, sta);
  1365. } else
  1366. dev_kfree_skb(skb);
  1367. end:
  1368. rcu_read_unlock();
  1369. if (sta)
  1370. sta_info_put(sta);
  1371. }
  1372. EXPORT_SYMBOL(__ieee80211_rx);
  1373. /* This is a version of the rx handler that can be called from hard irq
  1374. * context. Post the skb on the queue and schedule the tasklet */
  1375. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  1376. struct ieee80211_rx_status *status)
  1377. {
  1378. struct ieee80211_local *local = hw_to_local(hw);
  1379. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  1380. skb->dev = local->mdev;
  1381. /* copy status into skb->cb for use by tasklet */
  1382. memcpy(skb->cb, status, sizeof(*status));
  1383. skb->pkt_type = IEEE80211_RX_MSG;
  1384. skb_queue_tail(&local->skb_queue, skb);
  1385. tasklet_schedule(&local->tasklet);
  1386. }
  1387. EXPORT_SYMBOL(ieee80211_rx_irqsafe);