rx.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. struct ieee80211_key *stakey = NULL;
  366. /*
  367. * Key selection 101
  368. *
  369. * There are three types of keys:
  370. * - GTK (group keys)
  371. * - PTK (pairwise keys)
  372. * - STK (station-to-station pairwise keys)
  373. *
  374. * When selecting a key, we have to distinguish between multicast
  375. * (including broadcast) and unicast frames, the latter can only
  376. * use PTKs and STKs while the former always use GTKs. Unless, of
  377. * course, actual WEP keys ("pre-RSNA") are used, then unicast
  378. * frames can also use key indizes like GTKs. Hence, if we don't
  379. * have a PTK/STK we check the key index for a WEP key.
  380. *
  381. * Note that in a regular BSS, multicast frames are sent by the
  382. * AP only, associated stations unicast the frame to the AP first
  383. * which then multicasts it on their behalf.
  384. *
  385. * There is also a slight problem in IBSS mode: GTKs are negotiated
  386. * with each station, that is something we don't currently handle.
  387. * The spec seems to expect that one negotiates the same key with
  388. * every station but there's no such requirement; VLANs could be
  389. * possible.
  390. */
  391. if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
  392. return TXRX_CONTINUE;
  393. /*
  394. * No point in finding a key and decrypting if the frame is neither
  395. * addressed to us nor a multicast frame.
  396. */
  397. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  398. return TXRX_CONTINUE;
  399. if (rx->sta)
  400. stakey = rcu_dereference(rx->sta->key);
  401. if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
  402. rx->key = stakey;
  403. } else {
  404. /*
  405. * The device doesn't give us the IV so we won't be
  406. * able to look up the key. That's ok though, we
  407. * don't need to decrypt the frame, we just won't
  408. * be able to keep statistics accurate.
  409. * Except for key threshold notifications, should
  410. * we somehow allow the driver to tell us which key
  411. * the hardware used if this flag is set?
  412. */
  413. if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
  414. (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
  415. return TXRX_CONTINUE;
  416. hdrlen = ieee80211_get_hdrlen(rx->fc);
  417. if (rx->skb->len < 8 + hdrlen)
  418. return TXRX_DROP; /* TODO: count this? */
  419. /*
  420. * no need to call ieee80211_wep_get_keyidx,
  421. * it verifies a bunch of things we've done already
  422. */
  423. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  424. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  425. /*
  426. * RSNA-protected unicast frames should always be sent with
  427. * pairwise or station-to-station keys, but for WEP we allow
  428. * using a key index as well.
  429. */
  430. if (rx->key && rx->key->conf.alg != ALG_WEP &&
  431. !is_multicast_ether_addr(hdr->addr1))
  432. rx->key = NULL;
  433. }
  434. if (rx->key) {
  435. rx->key->tx_rx_count++;
  436. /* TODO: add threshold stuff again */
  437. } else {
  438. if (net_ratelimit())
  439. printk(KERN_DEBUG "%s: RX protected frame,"
  440. " but have no key\n", rx->dev->name);
  441. return TXRX_DROP;
  442. }
  443. /* Check for weak IVs if possible */
  444. if (rx->sta && rx->key->conf.alg == ALG_WEP &&
  445. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  446. (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
  447. !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
  448. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  449. rx->sta->wep_weak_iv_count++;
  450. switch (rx->key->conf.alg) {
  451. case ALG_WEP:
  452. return ieee80211_crypto_wep_decrypt(rx);
  453. case ALG_TKIP:
  454. return ieee80211_crypto_tkip_decrypt(rx);
  455. case ALG_CCMP:
  456. return ieee80211_crypto_ccmp_decrypt(rx);
  457. case ALG_NONE:
  458. WARN_ON(1);
  459. return TXRX_CONTINUE;
  460. }
  461. /* not reached */
  462. WARN_ON(1);
  463. return TXRX_DROP;
  464. }
  465. static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
  466. {
  467. struct ieee80211_sub_if_data *sdata;
  468. DECLARE_MAC_BUF(mac);
  469. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  470. if (sdata->bss)
  471. atomic_inc(&sdata->bss->num_sta_ps);
  472. sta->flags |= WLAN_STA_PS;
  473. sta->pspoll = 0;
  474. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  475. printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
  476. dev->name, print_mac(mac, sta->addr), sta->aid);
  477. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  478. }
  479. static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
  480. {
  481. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  482. struct sk_buff *skb;
  483. int sent = 0;
  484. struct ieee80211_sub_if_data *sdata;
  485. struct ieee80211_tx_packet_data *pkt_data;
  486. DECLARE_MAC_BUF(mac);
  487. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  488. if (sdata->bss)
  489. atomic_dec(&sdata->bss->num_sta_ps);
  490. sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
  491. sta->pspoll = 0;
  492. if (!skb_queue_empty(&sta->ps_tx_buf)) {
  493. if (local->ops->set_tim)
  494. local->ops->set_tim(local_to_hw(local), sta->aid, 0);
  495. if (sdata->bss)
  496. bss_tim_clear(local, sdata->bss, sta->aid);
  497. }
  498. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  499. printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
  500. dev->name, print_mac(mac, sta->addr), sta->aid);
  501. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  502. /* Send all buffered frames to the station */
  503. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  504. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  505. sent++;
  506. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  507. dev_queue_xmit(skb);
  508. }
  509. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  510. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  511. local->total_ps_buffered--;
  512. sent++;
  513. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  514. printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
  515. "since STA not sleeping anymore\n", dev->name,
  516. print_mac(mac, sta->addr), sta->aid);
  517. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  518. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  519. dev_queue_xmit(skb);
  520. }
  521. return sent;
  522. }
  523. static ieee80211_txrx_result
  524. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  525. {
  526. struct sta_info *sta = rx->sta;
  527. struct net_device *dev = rx->dev;
  528. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  529. if (!sta)
  530. return TXRX_CONTINUE;
  531. /* Update last_rx only for IBSS packets which are for the current
  532. * BSSID to avoid keeping the current IBSS network alive in cases where
  533. * other STAs are using different BSSID. */
  534. if (rx->sdata->type == IEEE80211_IF_TYPE_IBSS) {
  535. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
  536. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  537. sta->last_rx = jiffies;
  538. } else
  539. if (!is_multicast_ether_addr(hdr->addr1) ||
  540. rx->sdata->type == IEEE80211_IF_TYPE_STA) {
  541. /* Update last_rx only for unicast frames in order to prevent
  542. * the Probe Request frames (the only broadcast frames from a
  543. * STA in infrastructure mode) from keeping a connection alive.
  544. */
  545. sta->last_rx = jiffies;
  546. }
  547. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  548. return TXRX_CONTINUE;
  549. sta->rx_fragments++;
  550. sta->rx_bytes += rx->skb->len;
  551. sta->last_rssi = rx->u.rx.status->ssi;
  552. sta->last_signal = rx->u.rx.status->signal;
  553. sta->last_noise = rx->u.rx.status->noise;
  554. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  555. /* Change STA power saving mode only in the end of a frame
  556. * exchange sequence */
  557. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  558. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  559. else if (!(sta->flags & WLAN_STA_PS) &&
  560. (rx->fc & IEEE80211_FCTL_PM))
  561. ap_sta_ps_start(dev, sta);
  562. }
  563. /* Drop data::nullfunc frames silently, since they are used only to
  564. * control station power saving mode. */
  565. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  566. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  567. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  568. /* Update counter and free packet here to avoid counting this
  569. * as a dropped packed. */
  570. sta->rx_packets++;
  571. dev_kfree_skb(rx->skb);
  572. return TXRX_QUEUED;
  573. }
  574. return TXRX_CONTINUE;
  575. } /* ieee80211_rx_h_sta_process */
  576. static inline struct ieee80211_fragment_entry *
  577. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  578. unsigned int frag, unsigned int seq, int rx_queue,
  579. struct sk_buff **skb)
  580. {
  581. struct ieee80211_fragment_entry *entry;
  582. int idx;
  583. idx = sdata->fragment_next;
  584. entry = &sdata->fragments[sdata->fragment_next++];
  585. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  586. sdata->fragment_next = 0;
  587. if (!skb_queue_empty(&entry->skb_list)) {
  588. #ifdef CONFIG_MAC80211_DEBUG
  589. struct ieee80211_hdr *hdr =
  590. (struct ieee80211_hdr *) entry->skb_list.next->data;
  591. DECLARE_MAC_BUF(mac);
  592. DECLARE_MAC_BUF(mac2);
  593. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  594. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  595. "addr1=%s addr2=%s\n",
  596. sdata->dev->name, idx,
  597. jiffies - entry->first_frag_time, entry->seq,
  598. entry->last_frag, print_mac(mac, hdr->addr1),
  599. print_mac(mac2, hdr->addr2));
  600. #endif /* CONFIG_MAC80211_DEBUG */
  601. __skb_queue_purge(&entry->skb_list);
  602. }
  603. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  604. *skb = NULL;
  605. entry->first_frag_time = jiffies;
  606. entry->seq = seq;
  607. entry->rx_queue = rx_queue;
  608. entry->last_frag = frag;
  609. entry->ccmp = 0;
  610. entry->extra_len = 0;
  611. return entry;
  612. }
  613. static inline struct ieee80211_fragment_entry *
  614. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  615. u16 fc, unsigned int frag, unsigned int seq,
  616. int rx_queue, struct ieee80211_hdr *hdr)
  617. {
  618. struct ieee80211_fragment_entry *entry;
  619. int i, idx;
  620. idx = sdata->fragment_next;
  621. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  622. struct ieee80211_hdr *f_hdr;
  623. u16 f_fc;
  624. idx--;
  625. if (idx < 0)
  626. idx = IEEE80211_FRAGMENT_MAX - 1;
  627. entry = &sdata->fragments[idx];
  628. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  629. entry->rx_queue != rx_queue ||
  630. entry->last_frag + 1 != frag)
  631. continue;
  632. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  633. f_fc = le16_to_cpu(f_hdr->frame_control);
  634. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  635. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  636. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  637. continue;
  638. if (entry->first_frag_time + 2 * HZ < jiffies) {
  639. __skb_queue_purge(&entry->skb_list);
  640. continue;
  641. }
  642. return entry;
  643. }
  644. return NULL;
  645. }
  646. static ieee80211_txrx_result
  647. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  648. {
  649. struct ieee80211_hdr *hdr;
  650. u16 sc;
  651. unsigned int frag, seq;
  652. struct ieee80211_fragment_entry *entry;
  653. struct sk_buff *skb;
  654. DECLARE_MAC_BUF(mac);
  655. hdr = (struct ieee80211_hdr *) rx->skb->data;
  656. sc = le16_to_cpu(hdr->seq_ctrl);
  657. frag = sc & IEEE80211_SCTL_FRAG;
  658. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  659. (rx->skb)->len < 24 ||
  660. is_multicast_ether_addr(hdr->addr1))) {
  661. /* not fragmented */
  662. goto out;
  663. }
  664. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  665. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  666. if (frag == 0) {
  667. /* This is the first fragment of a new frame. */
  668. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  669. rx->u.rx.queue, &(rx->skb));
  670. if (rx->key && rx->key->conf.alg == ALG_CCMP &&
  671. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  672. /* Store CCMP PN so that we can verify that the next
  673. * fragment has a sequential PN value. */
  674. entry->ccmp = 1;
  675. memcpy(entry->last_pn,
  676. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  677. CCMP_PN_LEN);
  678. }
  679. return TXRX_QUEUED;
  680. }
  681. /* This is a fragment for a frame that should already be pending in
  682. * fragment cache. Add this fragment to the end of the pending entry.
  683. */
  684. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  685. rx->u.rx.queue, hdr);
  686. if (!entry) {
  687. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  688. return TXRX_DROP;
  689. }
  690. /* Verify that MPDUs within one MSDU have sequential PN values.
  691. * (IEEE 802.11i, 8.3.3.4.5) */
  692. if (entry->ccmp) {
  693. int i;
  694. u8 pn[CCMP_PN_LEN], *rpn;
  695. if (!rx->key || rx->key->conf.alg != ALG_CCMP)
  696. return TXRX_DROP;
  697. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  698. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  699. pn[i]++;
  700. if (pn[i])
  701. break;
  702. }
  703. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  704. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  705. if (net_ratelimit())
  706. printk(KERN_DEBUG "%s: defrag: CCMP PN not "
  707. "sequential A2=%s"
  708. " PN=%02x%02x%02x%02x%02x%02x "
  709. "(expected %02x%02x%02x%02x%02x%02x)\n",
  710. rx->dev->name, print_mac(mac, hdr->addr2),
  711. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
  712. rpn[5], pn[0], pn[1], pn[2], pn[3],
  713. pn[4], pn[5]);
  714. return TXRX_DROP;
  715. }
  716. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  717. }
  718. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  719. __skb_queue_tail(&entry->skb_list, rx->skb);
  720. entry->last_frag = frag;
  721. entry->extra_len += rx->skb->len;
  722. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  723. rx->skb = NULL;
  724. return TXRX_QUEUED;
  725. }
  726. rx->skb = __skb_dequeue(&entry->skb_list);
  727. if (skb_tailroom(rx->skb) < entry->extra_len) {
  728. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  729. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  730. GFP_ATOMIC))) {
  731. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  732. __skb_queue_purge(&entry->skb_list);
  733. return TXRX_DROP;
  734. }
  735. }
  736. while ((skb = __skb_dequeue(&entry->skb_list))) {
  737. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  738. dev_kfree_skb(skb);
  739. }
  740. /* Complete frame has been reassembled - process it now */
  741. rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  742. out:
  743. if (rx->sta)
  744. rx->sta->rx_packets++;
  745. if (is_multicast_ether_addr(hdr->addr1))
  746. rx->local->dot11MulticastReceivedFrameCount++;
  747. else
  748. ieee80211_led_rx(rx->local);
  749. return TXRX_CONTINUE;
  750. }
  751. static ieee80211_txrx_result
  752. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  753. {
  754. struct sk_buff *skb;
  755. int no_pending_pkts;
  756. DECLARE_MAC_BUF(mac);
  757. if (likely(!rx->sta ||
  758. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  759. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  760. !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
  761. return TXRX_CONTINUE;
  762. skb = skb_dequeue(&rx->sta->tx_filtered);
  763. if (!skb) {
  764. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  765. if (skb)
  766. rx->local->total_ps_buffered--;
  767. }
  768. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  769. skb_queue_empty(&rx->sta->ps_tx_buf);
  770. if (skb) {
  771. struct ieee80211_hdr *hdr =
  772. (struct ieee80211_hdr *) skb->data;
  773. /* tell TX path to send one frame even though the STA may
  774. * still remain is PS mode after this frame exchange */
  775. rx->sta->pspoll = 1;
  776. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  777. printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
  778. print_mac(mac, rx->sta->addr), rx->sta->aid,
  779. skb_queue_len(&rx->sta->ps_tx_buf));
  780. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  781. /* Use MoreData flag to indicate whether there are more
  782. * buffered frames for this STA */
  783. if (no_pending_pkts) {
  784. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  785. rx->sta->flags &= ~WLAN_STA_TIM;
  786. } else
  787. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  788. dev_queue_xmit(skb);
  789. if (no_pending_pkts) {
  790. if (rx->local->ops->set_tim)
  791. rx->local->ops->set_tim(local_to_hw(rx->local),
  792. rx->sta->aid, 0);
  793. if (rx->sdata->bss)
  794. bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
  795. }
  796. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  797. } else if (!rx->u.rx.sent_ps_buffered) {
  798. printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
  799. "though there is no buffered frames for it\n",
  800. rx->dev->name, print_mac(mac, rx->sta->addr));
  801. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  802. }
  803. /* Free PS Poll skb here instead of returning TXRX_DROP that would
  804. * count as an dropped frame. */
  805. dev_kfree_skb(rx->skb);
  806. return TXRX_QUEUED;
  807. }
  808. static ieee80211_txrx_result
  809. ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
  810. {
  811. u16 fc = rx->fc;
  812. u8 *data = rx->skb->data;
  813. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
  814. if (!WLAN_FC_IS_QOS_DATA(fc))
  815. return TXRX_CONTINUE;
  816. /* remove the qos control field, update frame type and meta-data */
  817. memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
  818. hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
  819. /* change frame type to non QOS */
  820. rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
  821. hdr->frame_control = cpu_to_le16(fc);
  822. return TXRX_CONTINUE;
  823. }
  824. static ieee80211_txrx_result
  825. ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
  826. {
  827. if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
  828. rx->sdata->type != IEEE80211_IF_TYPE_STA &&
  829. (rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  830. return TXRX_CONTINUE;
  831. if (unlikely(rx->sdata->ieee802_1x &&
  832. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  833. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  834. (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
  835. !ieee80211_is_eapol(rx->skb))) {
  836. #ifdef CONFIG_MAC80211_DEBUG
  837. struct ieee80211_hdr *hdr =
  838. (struct ieee80211_hdr *) rx->skb->data;
  839. DECLARE_MAC_BUF(mac);
  840. printk(KERN_DEBUG "%s: dropped frame from %s"
  841. " (unauthorized port)\n", rx->dev->name,
  842. print_mac(mac, hdr->addr2));
  843. #endif /* CONFIG_MAC80211_DEBUG */
  844. return TXRX_DROP;
  845. }
  846. return TXRX_CONTINUE;
  847. }
  848. static ieee80211_txrx_result
  849. ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
  850. {
  851. /*
  852. * Pass through unencrypted frames if the hardware has
  853. * decrypted them already.
  854. */
  855. if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
  856. return TXRX_CONTINUE;
  857. /* Drop unencrypted frames if key is set. */
  858. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  859. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  860. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  861. (rx->key || rx->sdata->drop_unencrypted) &&
  862. (rx->sdata->eapol == 0 ||
  863. !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. !rx->local->user_space_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);