rx.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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. u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  25. struct tid_ampdu_rx *tid_agg_rx,
  26. struct sk_buff *skb, u16 mpdu_seq_num,
  27. int bar_req);
  28. /*
  29. * monitor mode reception
  30. *
  31. * This function cleans up the SKB, i.e. it removes all the stuff
  32. * only useful for monitoring.
  33. */
  34. static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
  35. struct sk_buff *skb,
  36. int rtap_len)
  37. {
  38. skb_pull(skb, rtap_len);
  39. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
  40. if (likely(skb->len > FCS_LEN))
  41. skb_trim(skb, skb->len - FCS_LEN);
  42. else {
  43. /* driver bug */
  44. WARN_ON(1);
  45. dev_kfree_skb(skb);
  46. skb = NULL;
  47. }
  48. }
  49. return skb;
  50. }
  51. static inline int should_drop_frame(struct ieee80211_rx_status *status,
  52. struct sk_buff *skb,
  53. int present_fcs_len,
  54. int radiotap_len)
  55. {
  56. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  57. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  58. return 1;
  59. if (unlikely(skb->len < 16 + present_fcs_len + radiotap_len))
  60. return 1;
  61. if (((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_FTYPE)) ==
  62. cpu_to_le16(IEEE80211_FTYPE_CTL)) &&
  63. ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
  64. cpu_to_le16(IEEE80211_STYPE_PSPOLL)) &&
  65. ((hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE)) !=
  66. cpu_to_le16(IEEE80211_STYPE_BACK_REQ)))
  67. return 1;
  68. return 0;
  69. }
  70. /*
  71. * This function copies a received frame to all monitor interfaces and
  72. * returns a cleaned-up SKB that no longer includes the FCS nor the
  73. * radiotap header the driver might have added.
  74. */
  75. static struct sk_buff *
  76. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  77. struct ieee80211_rx_status *status)
  78. {
  79. struct ieee80211_sub_if_data *sdata;
  80. struct ieee80211_rate *rate;
  81. int needed_headroom = 0;
  82. struct ieee80211_radiotap_header *rthdr;
  83. __le64 *rttsft = NULL;
  84. struct ieee80211_rtap_fixed_data {
  85. u8 flags;
  86. u8 rate;
  87. __le16 chan_freq;
  88. __le16 chan_flags;
  89. u8 antsignal;
  90. u8 padding_for_rxflags;
  91. __le16 rx_flags;
  92. } __attribute__ ((packed)) *rtfixed;
  93. struct sk_buff *skb, *skb2;
  94. struct net_device *prev_dev = NULL;
  95. int present_fcs_len = 0;
  96. int rtap_len = 0;
  97. /*
  98. * First, we may need to make a copy of the skb because
  99. * (1) we need to modify it for radiotap (if not present), and
  100. * (2) the other RX handlers will modify the skb we got.
  101. *
  102. * We don't need to, of course, if we aren't going to return
  103. * the SKB because it has a bad FCS/PLCP checksum.
  104. */
  105. if (status->flag & RX_FLAG_RADIOTAP)
  106. rtap_len = ieee80211_get_radiotap_len(origskb->data);
  107. else
  108. /* room for radiotap header, always present fields and TSFT */
  109. needed_headroom = sizeof(*rthdr) + sizeof(*rtfixed) + 8;
  110. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  111. present_fcs_len = FCS_LEN;
  112. if (!local->monitors) {
  113. if (should_drop_frame(status, origskb, present_fcs_len,
  114. rtap_len)) {
  115. dev_kfree_skb(origskb);
  116. return NULL;
  117. }
  118. return remove_monitor_info(local, origskb, rtap_len);
  119. }
  120. if (should_drop_frame(status, origskb, present_fcs_len, rtap_len)) {
  121. /* only need to expand headroom if necessary */
  122. skb = origskb;
  123. origskb = NULL;
  124. /*
  125. * This shouldn't trigger often because most devices have an
  126. * RX header they pull before we get here, and that should
  127. * be big enough for our radiotap information. We should
  128. * probably export the length to drivers so that we can have
  129. * them allocate enough headroom to start with.
  130. */
  131. if (skb_headroom(skb) < needed_headroom &&
  132. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  133. dev_kfree_skb(skb);
  134. return NULL;
  135. }
  136. } else {
  137. /*
  138. * Need to make a copy and possibly remove radiotap header
  139. * and FCS from the original.
  140. */
  141. skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
  142. origskb = remove_monitor_info(local, origskb, rtap_len);
  143. if (!skb)
  144. return origskb;
  145. }
  146. /* if necessary, prepend radiotap information */
  147. if (!(status->flag & RX_FLAG_RADIOTAP)) {
  148. rtfixed = (void *) skb_push(skb, sizeof(*rtfixed));
  149. rtap_len = sizeof(*rthdr) + sizeof(*rtfixed);
  150. if (status->flag & RX_FLAG_TSFT) {
  151. rttsft = (void *) skb_push(skb, sizeof(*rttsft));
  152. rtap_len += 8;
  153. }
  154. rthdr = (void *) skb_push(skb, sizeof(*rthdr));
  155. memset(rthdr, 0, sizeof(*rthdr));
  156. memset(rtfixed, 0, sizeof(*rtfixed));
  157. rthdr->it_present =
  158. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  159. (1 << IEEE80211_RADIOTAP_RATE) |
  160. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  161. (1 << IEEE80211_RADIOTAP_DB_ANTSIGNAL) |
  162. (1 << IEEE80211_RADIOTAP_RX_FLAGS));
  163. rtfixed->flags = 0;
  164. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  165. rtfixed->flags |= IEEE80211_RADIOTAP_F_FCS;
  166. if (rttsft) {
  167. *rttsft = cpu_to_le64(status->mactime);
  168. rthdr->it_present |=
  169. cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  170. }
  171. /* FIXME: when radiotap gets a 'bad PLCP' flag use it here */
  172. rtfixed->rx_flags = 0;
  173. if (status->flag &
  174. (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  175. rtfixed->rx_flags |=
  176. cpu_to_le16(IEEE80211_RADIOTAP_F_RX_BADFCS);
  177. rate = ieee80211_get_rate(local, status->phymode,
  178. status->rate);
  179. if (rate)
  180. rtfixed->rate = rate->rate / 5;
  181. rtfixed->chan_freq = cpu_to_le16(status->freq);
  182. if (status->phymode == MODE_IEEE80211A)
  183. rtfixed->chan_flags =
  184. cpu_to_le16(IEEE80211_CHAN_OFDM |
  185. IEEE80211_CHAN_5GHZ);
  186. else
  187. rtfixed->chan_flags =
  188. cpu_to_le16(IEEE80211_CHAN_DYN |
  189. IEEE80211_CHAN_2GHZ);
  190. rtfixed->antsignal = status->ssi;
  191. rthdr->it_len = cpu_to_le16(rtap_len);
  192. }
  193. skb_reset_mac_header(skb);
  194. skb->ip_summed = CHECKSUM_UNNECESSARY;
  195. skb->pkt_type = PACKET_OTHERHOST;
  196. skb->protocol = htons(ETH_P_802_2);
  197. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  198. if (!netif_running(sdata->dev))
  199. continue;
  200. if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR)
  201. continue;
  202. if (prev_dev) {
  203. skb2 = skb_clone(skb, GFP_ATOMIC);
  204. if (skb2) {
  205. skb2->dev = prev_dev;
  206. netif_rx(skb2);
  207. }
  208. }
  209. prev_dev = sdata->dev;
  210. sdata->dev->stats.rx_packets++;
  211. sdata->dev->stats.rx_bytes += skb->len;
  212. }
  213. if (prev_dev) {
  214. skb->dev = prev_dev;
  215. netif_rx(skb);
  216. } else
  217. dev_kfree_skb(skb);
  218. return origskb;
  219. }
  220. /* pre-rx handlers
  221. *
  222. * these don't have dev/sdata fields in the rx data
  223. * The sta value should also not be used because it may
  224. * be NULL even though a STA (in IBSS mode) will be added.
  225. */
  226. static ieee80211_txrx_result
  227. ieee80211_rx_h_parse_qos(struct ieee80211_txrx_data *rx)
  228. {
  229. u8 *data = rx->skb->data;
  230. int tid;
  231. /* does the frame have a qos control field? */
  232. if (WLAN_FC_IS_QOS_DATA(rx->fc)) {
  233. u8 *qc = data + ieee80211_get_hdrlen(rx->fc) - QOS_CONTROL_LEN;
  234. /* frame has qos control */
  235. tid = qc[0] & QOS_CONTROL_TID_MASK;
  236. if (qc[0] & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  237. rx->flags |= IEEE80211_TXRXD_RX_AMSDU;
  238. else
  239. rx->flags &= ~IEEE80211_TXRXD_RX_AMSDU;
  240. } else {
  241. if (unlikely((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)) {
  242. /* Separate TID for management frames */
  243. tid = NUM_RX_DATA_QUEUES - 1;
  244. } else {
  245. /* no qos control present */
  246. tid = 0; /* 802.1d - Best Effort */
  247. }
  248. }
  249. I802_DEBUG_INC(rx->local->wme_rx_queue[tid]);
  250. /* only a debug counter, sta might not be assigned properly yet */
  251. if (rx->sta)
  252. I802_DEBUG_INC(rx->sta->wme_rx_queue[tid]);
  253. rx->u.rx.queue = tid;
  254. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  255. * For now, set skb->priority to 0 for other cases. */
  256. rx->skb->priority = (tid > 7) ? 0 : tid;
  257. return TXRX_CONTINUE;
  258. }
  259. static u32 ieee80211_rx_load_stats(struct ieee80211_local *local,
  260. struct sk_buff *skb,
  261. struct ieee80211_rx_status *status)
  262. {
  263. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  264. u32 load = 0, hdrtime;
  265. struct ieee80211_rate *rate;
  266. struct ieee80211_hw_mode *mode = local->hw.conf.mode;
  267. int i;
  268. /* Estimate total channel use caused by this frame */
  269. if (unlikely(mode->num_rates < 0))
  270. return TXRX_CONTINUE;
  271. rate = &mode->rates[0];
  272. for (i = 0; i < mode->num_rates; i++) {
  273. if (mode->rates[i].val == status->rate) {
  274. rate = &mode->rates[i];
  275. break;
  276. }
  277. }
  278. /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
  279. * 1 usec = 1/8 * (1080 / 10) = 13.5 */
  280. if (mode->mode == MODE_IEEE80211A ||
  281. (mode->mode == MODE_IEEE80211G &&
  282. rate->flags & IEEE80211_RATE_ERP))
  283. hdrtime = CHAN_UTIL_HDR_SHORT;
  284. else
  285. hdrtime = CHAN_UTIL_HDR_LONG;
  286. load = hdrtime;
  287. if (!is_multicast_ether_addr(hdr->addr1))
  288. load += hdrtime;
  289. load += skb->len * rate->rate_inv;
  290. /* Divide channel_use by 8 to avoid wrapping around the counter */
  291. load >>= CHAN_UTIL_SHIFT;
  292. return load;
  293. }
  294. static ieee80211_txrx_result
  295. ieee80211_rx_h_verify_ip_alignment(struct ieee80211_txrx_data *rx)
  296. {
  297. int hdrlen;
  298. /*
  299. * Drivers are required to align the payload data in a way that
  300. * guarantees that the contained IP header is aligned to a four-
  301. * byte boundary. In the case of regular frames, this simply means
  302. * aligning the payload to a four-byte boundary (because either
  303. * the IP header is directly contained, or IV/RFC1042 headers that
  304. * have a length divisible by four are in front of it.
  305. *
  306. * With A-MSDU frames, however, the payload data address must
  307. * yield two modulo four because there are 14-byte 802.3 headers
  308. * within the A-MSDU frames that push the IP header further back
  309. * to a multiple of four again. Thankfully, the specs were sane
  310. * enough this time around to require padding each A-MSDU subframe
  311. * to a length that is a multiple of four.
  312. *
  313. * Padding like atheros hardware adds which is inbetween the 802.11
  314. * header and the payload is not supported, the driver is required
  315. * to move the 802.11 header further back in that case.
  316. */
  317. hdrlen = ieee80211_get_hdrlen(rx->fc);
  318. if (rx->flags & IEEE80211_TXRXD_RX_AMSDU)
  319. hdrlen += ETH_HLEN;
  320. WARN_ON_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3);
  321. return TXRX_CONTINUE;
  322. }
  323. ieee80211_rx_handler ieee80211_rx_pre_handlers[] =
  324. {
  325. ieee80211_rx_h_parse_qos,
  326. ieee80211_rx_h_verify_ip_alignment,
  327. NULL
  328. };
  329. /* rx handlers */
  330. static ieee80211_txrx_result
  331. ieee80211_rx_h_if_stats(struct ieee80211_txrx_data *rx)
  332. {
  333. if (rx->sta)
  334. rx->sta->channel_use_raw += rx->u.rx.load;
  335. rx->sdata->channel_use_raw += rx->u.rx.load;
  336. return TXRX_CONTINUE;
  337. }
  338. static ieee80211_txrx_result
  339. ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
  340. {
  341. struct ieee80211_local *local = rx->local;
  342. struct sk_buff *skb = rx->skb;
  343. if (unlikely(local->sta_hw_scanning))
  344. return ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
  345. if (unlikely(local->sta_sw_scanning)) {
  346. /* drop all the other packets during a software scan anyway */
  347. if (ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status)
  348. != TXRX_QUEUED)
  349. dev_kfree_skb(skb);
  350. return TXRX_QUEUED;
  351. }
  352. if (unlikely(rx->flags & IEEE80211_TXRXD_RXIN_SCAN)) {
  353. /* scanning finished during invoking of handlers */
  354. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  355. return TXRX_DROP;
  356. }
  357. return TXRX_CONTINUE;
  358. }
  359. static ieee80211_txrx_result
  360. ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
  361. {
  362. struct ieee80211_hdr *hdr;
  363. hdr = (struct ieee80211_hdr *) rx->skb->data;
  364. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  365. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  366. if (unlikely(rx->fc & IEEE80211_FCTL_RETRY &&
  367. rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
  368. hdr->seq_ctrl)) {
  369. if (rx->flags & IEEE80211_TXRXD_RXRA_MATCH) {
  370. rx->local->dot11FrameDuplicateCount++;
  371. rx->sta->num_duplicates++;
  372. }
  373. return TXRX_DROP;
  374. } else
  375. rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
  376. }
  377. if (unlikely(rx->skb->len < 16)) {
  378. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  379. return TXRX_DROP;
  380. }
  381. /* Drop disallowed frame classes based on STA auth/assoc state;
  382. * IEEE 802.11, Chap 5.5.
  383. *
  384. * 80211.o does filtering only based on association state, i.e., it
  385. * drops Class 3 frames from not associated stations. hostapd sends
  386. * deauth/disassoc frames when needed. In addition, hostapd is
  387. * responsible for filtering on both auth and assoc states.
  388. */
  389. if (unlikely(((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA ||
  390. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL &&
  391. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)) &&
  392. rx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
  393. (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
  394. if ((!(rx->fc & IEEE80211_FCTL_FROMDS) &&
  395. !(rx->fc & IEEE80211_FCTL_TODS) &&
  396. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
  397. || !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
  398. /* Drop IBSS frames and frames for other hosts
  399. * silently. */
  400. return TXRX_DROP;
  401. }
  402. return TXRX_DROP;
  403. }
  404. return TXRX_CONTINUE;
  405. }
  406. static ieee80211_txrx_result
  407. ieee80211_rx_h_decrypt(struct ieee80211_txrx_data *rx)
  408. {
  409. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  410. int keyidx;
  411. int hdrlen;
  412. ieee80211_txrx_result result = TXRX_DROP;
  413. struct ieee80211_key *stakey = NULL;
  414. /*
  415. * Key selection 101
  416. *
  417. * There are three types of keys:
  418. * - GTK (group keys)
  419. * - PTK (pairwise keys)
  420. * - STK (station-to-station pairwise keys)
  421. *
  422. * When selecting a key, we have to distinguish between multicast
  423. * (including broadcast) and unicast frames, the latter can only
  424. * use PTKs and STKs while the former always use GTKs. Unless, of
  425. * course, actual WEP keys ("pre-RSNA") are used, then unicast
  426. * frames can also use key indizes like GTKs. Hence, if we don't
  427. * have a PTK/STK we check the key index for a WEP key.
  428. *
  429. * Note that in a regular BSS, multicast frames are sent by the
  430. * AP only, associated stations unicast the frame to the AP first
  431. * which then multicasts it on their behalf.
  432. *
  433. * There is also a slight problem in IBSS mode: GTKs are negotiated
  434. * with each station, that is something we don't currently handle.
  435. * The spec seems to expect that one negotiates the same key with
  436. * every station but there's no such requirement; VLANs could be
  437. * possible.
  438. */
  439. if (!(rx->fc & IEEE80211_FCTL_PROTECTED))
  440. return TXRX_CONTINUE;
  441. /*
  442. * No point in finding a key and decrypting if the frame is neither
  443. * addressed to us nor a multicast frame.
  444. */
  445. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  446. return TXRX_CONTINUE;
  447. if (rx->sta)
  448. stakey = rcu_dereference(rx->sta->key);
  449. if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
  450. rx->key = stakey;
  451. } else {
  452. /*
  453. * The device doesn't give us the IV so we won't be
  454. * able to look up the key. That's ok though, we
  455. * don't need to decrypt the frame, we just won't
  456. * be able to keep statistics accurate.
  457. * Except for key threshold notifications, should
  458. * we somehow allow the driver to tell us which key
  459. * the hardware used if this flag is set?
  460. */
  461. if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
  462. (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
  463. return TXRX_CONTINUE;
  464. hdrlen = ieee80211_get_hdrlen(rx->fc);
  465. if (rx->skb->len < 8 + hdrlen)
  466. return TXRX_DROP; /* TODO: count this? */
  467. /*
  468. * no need to call ieee80211_wep_get_keyidx,
  469. * it verifies a bunch of things we've done already
  470. */
  471. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  472. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  473. /*
  474. * RSNA-protected unicast frames should always be sent with
  475. * pairwise or station-to-station keys, but for WEP we allow
  476. * using a key index as well.
  477. */
  478. if (rx->key && rx->key->conf.alg != ALG_WEP &&
  479. !is_multicast_ether_addr(hdr->addr1))
  480. rx->key = NULL;
  481. }
  482. if (rx->key) {
  483. rx->key->tx_rx_count++;
  484. /* TODO: add threshold stuff again */
  485. } else {
  486. #ifdef CONFIG_MAC80211_DEBUG
  487. if (net_ratelimit())
  488. printk(KERN_DEBUG "%s: RX protected frame,"
  489. " but have no key\n", rx->dev->name);
  490. #endif /* CONFIG_MAC80211_DEBUG */
  491. return TXRX_DROP;
  492. }
  493. /* Check for weak IVs if possible */
  494. if (rx->sta && rx->key->conf.alg == ALG_WEP &&
  495. ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  496. (!(rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) ||
  497. !(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) &&
  498. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  499. rx->sta->wep_weak_iv_count++;
  500. switch (rx->key->conf.alg) {
  501. case ALG_WEP:
  502. result = ieee80211_crypto_wep_decrypt(rx);
  503. break;
  504. case ALG_TKIP:
  505. result = ieee80211_crypto_tkip_decrypt(rx);
  506. break;
  507. case ALG_CCMP:
  508. result = ieee80211_crypto_ccmp_decrypt(rx);
  509. break;
  510. }
  511. /* either the frame has been decrypted or will be dropped */
  512. rx->u.rx.status->flag |= RX_FLAG_DECRYPTED;
  513. return result;
  514. }
  515. static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
  516. {
  517. struct ieee80211_sub_if_data *sdata;
  518. DECLARE_MAC_BUF(mac);
  519. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  520. if (sdata->bss)
  521. atomic_inc(&sdata->bss->num_sta_ps);
  522. sta->flags |= WLAN_STA_PS;
  523. sta->pspoll = 0;
  524. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  525. printk(KERN_DEBUG "%s: STA %s aid %d enters power save mode\n",
  526. dev->name, print_mac(mac, sta->addr), sta->aid);
  527. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  528. }
  529. static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
  530. {
  531. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  532. struct sk_buff *skb;
  533. int sent = 0;
  534. struct ieee80211_sub_if_data *sdata;
  535. struct ieee80211_tx_packet_data *pkt_data;
  536. DECLARE_MAC_BUF(mac);
  537. sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
  538. if (sdata->bss)
  539. atomic_dec(&sdata->bss->num_sta_ps);
  540. sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
  541. sta->pspoll = 0;
  542. if (!skb_queue_empty(&sta->ps_tx_buf)) {
  543. if (local->ops->set_tim)
  544. local->ops->set_tim(local_to_hw(local), sta->aid, 0);
  545. if (sdata->bss)
  546. bss_tim_clear(local, sdata->bss, sta->aid);
  547. }
  548. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  549. printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
  550. dev->name, print_mac(mac, sta->addr), sta->aid);
  551. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  552. /* Send all buffered frames to the station */
  553. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  554. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  555. sent++;
  556. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  557. dev_queue_xmit(skb);
  558. }
  559. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  560. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  561. local->total_ps_buffered--;
  562. sent++;
  563. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  564. printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
  565. "since STA not sleeping anymore\n", dev->name,
  566. print_mac(mac, sta->addr), sta->aid);
  567. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  568. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  569. dev_queue_xmit(skb);
  570. }
  571. return sent;
  572. }
  573. static ieee80211_txrx_result
  574. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  575. {
  576. struct sta_info *sta = rx->sta;
  577. struct net_device *dev = rx->dev;
  578. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  579. if (!sta)
  580. return TXRX_CONTINUE;
  581. /* Update last_rx only for IBSS packets which are for the current
  582. * BSSID to avoid keeping the current IBSS network alive in cases where
  583. * other STAs are using different BSSID. */
  584. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  585. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  586. IEEE80211_IF_TYPE_IBSS);
  587. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  588. sta->last_rx = jiffies;
  589. } else
  590. if (!is_multicast_ether_addr(hdr->addr1) ||
  591. rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  592. /* Update last_rx only for unicast frames in order to prevent
  593. * the Probe Request frames (the only broadcast frames from a
  594. * STA in infrastructure mode) from keeping a connection alive.
  595. */
  596. sta->last_rx = jiffies;
  597. }
  598. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  599. return TXRX_CONTINUE;
  600. sta->rx_fragments++;
  601. sta->rx_bytes += rx->skb->len;
  602. sta->last_rssi = rx->u.rx.status->ssi;
  603. sta->last_signal = rx->u.rx.status->signal;
  604. sta->last_noise = rx->u.rx.status->noise;
  605. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  606. /* Change STA power saving mode only in the end of a frame
  607. * exchange sequence */
  608. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  609. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  610. else if (!(sta->flags & WLAN_STA_PS) &&
  611. (rx->fc & IEEE80211_FCTL_PM))
  612. ap_sta_ps_start(dev, sta);
  613. }
  614. /* Drop data::nullfunc frames silently, since they are used only to
  615. * control station power saving mode. */
  616. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  617. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  618. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  619. /* Update counter and free packet here to avoid counting this
  620. * as a dropped packed. */
  621. sta->rx_packets++;
  622. dev_kfree_skb(rx->skb);
  623. return TXRX_QUEUED;
  624. }
  625. return TXRX_CONTINUE;
  626. } /* ieee80211_rx_h_sta_process */
  627. static inline struct ieee80211_fragment_entry *
  628. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  629. unsigned int frag, unsigned int seq, int rx_queue,
  630. struct sk_buff **skb)
  631. {
  632. struct ieee80211_fragment_entry *entry;
  633. int idx;
  634. idx = sdata->fragment_next;
  635. entry = &sdata->fragments[sdata->fragment_next++];
  636. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  637. sdata->fragment_next = 0;
  638. if (!skb_queue_empty(&entry->skb_list)) {
  639. #ifdef CONFIG_MAC80211_DEBUG
  640. struct ieee80211_hdr *hdr =
  641. (struct ieee80211_hdr *) entry->skb_list.next->data;
  642. DECLARE_MAC_BUF(mac);
  643. DECLARE_MAC_BUF(mac2);
  644. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  645. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  646. "addr1=%s addr2=%s\n",
  647. sdata->dev->name, idx,
  648. jiffies - entry->first_frag_time, entry->seq,
  649. entry->last_frag, print_mac(mac, hdr->addr1),
  650. print_mac(mac2, hdr->addr2));
  651. #endif /* CONFIG_MAC80211_DEBUG */
  652. __skb_queue_purge(&entry->skb_list);
  653. }
  654. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  655. *skb = NULL;
  656. entry->first_frag_time = jiffies;
  657. entry->seq = seq;
  658. entry->rx_queue = rx_queue;
  659. entry->last_frag = frag;
  660. entry->ccmp = 0;
  661. entry->extra_len = 0;
  662. return entry;
  663. }
  664. static inline struct ieee80211_fragment_entry *
  665. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  666. u16 fc, unsigned int frag, unsigned int seq,
  667. int rx_queue, struct ieee80211_hdr *hdr)
  668. {
  669. struct ieee80211_fragment_entry *entry;
  670. int i, idx;
  671. idx = sdata->fragment_next;
  672. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  673. struct ieee80211_hdr *f_hdr;
  674. u16 f_fc;
  675. idx--;
  676. if (idx < 0)
  677. idx = IEEE80211_FRAGMENT_MAX - 1;
  678. entry = &sdata->fragments[idx];
  679. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  680. entry->rx_queue != rx_queue ||
  681. entry->last_frag + 1 != frag)
  682. continue;
  683. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  684. f_fc = le16_to_cpu(f_hdr->frame_control);
  685. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  686. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  687. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  688. continue;
  689. if (entry->first_frag_time + 2 * HZ < jiffies) {
  690. __skb_queue_purge(&entry->skb_list);
  691. continue;
  692. }
  693. return entry;
  694. }
  695. return NULL;
  696. }
  697. static ieee80211_txrx_result
  698. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  699. {
  700. struct ieee80211_hdr *hdr;
  701. u16 sc;
  702. unsigned int frag, seq;
  703. struct ieee80211_fragment_entry *entry;
  704. struct sk_buff *skb;
  705. DECLARE_MAC_BUF(mac);
  706. hdr = (struct ieee80211_hdr *) rx->skb->data;
  707. sc = le16_to_cpu(hdr->seq_ctrl);
  708. frag = sc & IEEE80211_SCTL_FRAG;
  709. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  710. (rx->skb)->len < 24 ||
  711. is_multicast_ether_addr(hdr->addr1))) {
  712. /* not fragmented */
  713. goto out;
  714. }
  715. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  716. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  717. if (frag == 0) {
  718. /* This is the first fragment of a new frame. */
  719. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  720. rx->u.rx.queue, &(rx->skb));
  721. if (rx->key && rx->key->conf.alg == ALG_CCMP &&
  722. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  723. /* Store CCMP PN so that we can verify that the next
  724. * fragment has a sequential PN value. */
  725. entry->ccmp = 1;
  726. memcpy(entry->last_pn,
  727. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  728. CCMP_PN_LEN);
  729. }
  730. return TXRX_QUEUED;
  731. }
  732. /* This is a fragment for a frame that should already be pending in
  733. * fragment cache. Add this fragment to the end of the pending entry.
  734. */
  735. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  736. rx->u.rx.queue, hdr);
  737. if (!entry) {
  738. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  739. return TXRX_DROP;
  740. }
  741. /* Verify that MPDUs within one MSDU have sequential PN values.
  742. * (IEEE 802.11i, 8.3.3.4.5) */
  743. if (entry->ccmp) {
  744. int i;
  745. u8 pn[CCMP_PN_LEN], *rpn;
  746. if (!rx->key || rx->key->conf.alg != ALG_CCMP)
  747. return TXRX_DROP;
  748. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  749. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  750. pn[i]++;
  751. if (pn[i])
  752. break;
  753. }
  754. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  755. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  756. if (net_ratelimit())
  757. printk(KERN_DEBUG "%s: defrag: CCMP PN not "
  758. "sequential A2=%s"
  759. " PN=%02x%02x%02x%02x%02x%02x "
  760. "(expected %02x%02x%02x%02x%02x%02x)\n",
  761. rx->dev->name, print_mac(mac, hdr->addr2),
  762. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
  763. rpn[5], pn[0], pn[1], pn[2], pn[3],
  764. pn[4], pn[5]);
  765. return TXRX_DROP;
  766. }
  767. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  768. }
  769. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  770. __skb_queue_tail(&entry->skb_list, rx->skb);
  771. entry->last_frag = frag;
  772. entry->extra_len += rx->skb->len;
  773. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  774. rx->skb = NULL;
  775. return TXRX_QUEUED;
  776. }
  777. rx->skb = __skb_dequeue(&entry->skb_list);
  778. if (skb_tailroom(rx->skb) < entry->extra_len) {
  779. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  780. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  781. GFP_ATOMIC))) {
  782. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  783. __skb_queue_purge(&entry->skb_list);
  784. return TXRX_DROP;
  785. }
  786. }
  787. while ((skb = __skb_dequeue(&entry->skb_list))) {
  788. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  789. dev_kfree_skb(skb);
  790. }
  791. /* Complete frame has been reassembled - process it now */
  792. rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  793. out:
  794. if (rx->sta)
  795. rx->sta->rx_packets++;
  796. if (is_multicast_ether_addr(hdr->addr1))
  797. rx->local->dot11MulticastReceivedFrameCount++;
  798. else
  799. ieee80211_led_rx(rx->local);
  800. return TXRX_CONTINUE;
  801. }
  802. static ieee80211_txrx_result
  803. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  804. {
  805. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  806. struct sk_buff *skb;
  807. int no_pending_pkts;
  808. DECLARE_MAC_BUF(mac);
  809. if (likely(!rx->sta ||
  810. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  811. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  812. !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
  813. return TXRX_CONTINUE;
  814. if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
  815. (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
  816. return TXRX_DROP;
  817. skb = skb_dequeue(&rx->sta->tx_filtered);
  818. if (!skb) {
  819. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  820. if (skb)
  821. rx->local->total_ps_buffered--;
  822. }
  823. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  824. skb_queue_empty(&rx->sta->ps_tx_buf);
  825. if (skb) {
  826. struct ieee80211_hdr *hdr =
  827. (struct ieee80211_hdr *) skb->data;
  828. /* tell TX path to send one frame even though the STA may
  829. * still remain is PS mode after this frame exchange */
  830. rx->sta->pspoll = 1;
  831. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  832. printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
  833. print_mac(mac, rx->sta->addr), rx->sta->aid,
  834. skb_queue_len(&rx->sta->ps_tx_buf));
  835. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  836. /* Use MoreData flag to indicate whether there are more
  837. * buffered frames for this STA */
  838. if (no_pending_pkts) {
  839. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  840. rx->sta->flags &= ~WLAN_STA_TIM;
  841. } else
  842. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  843. dev_queue_xmit(skb);
  844. if (no_pending_pkts) {
  845. if (rx->local->ops->set_tim)
  846. rx->local->ops->set_tim(local_to_hw(rx->local),
  847. rx->sta->aid, 0);
  848. if (rx->sdata->bss)
  849. bss_tim_clear(rx->local, rx->sdata->bss, rx->sta->aid);
  850. }
  851. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  852. } else if (!rx->u.rx.sent_ps_buffered) {
  853. printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
  854. "though there is no buffered frames for it\n",
  855. rx->dev->name, print_mac(mac, rx->sta->addr));
  856. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  857. }
  858. /* Free PS Poll skb here instead of returning TXRX_DROP that would
  859. * count as an dropped frame. */
  860. dev_kfree_skb(rx->skb);
  861. return TXRX_QUEUED;
  862. }
  863. static ieee80211_txrx_result
  864. ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
  865. {
  866. u16 fc = rx->fc;
  867. u8 *data = rx->skb->data;
  868. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
  869. if (!WLAN_FC_IS_QOS_DATA(fc))
  870. return TXRX_CONTINUE;
  871. /* remove the qos control field, update frame type and meta-data */
  872. memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
  873. hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
  874. /* change frame type to non QOS */
  875. rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
  876. hdr->frame_control = cpu_to_le16(fc);
  877. return TXRX_CONTINUE;
  878. }
  879. static int
  880. ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
  881. {
  882. if (unlikely(rx->sdata->ieee802_1x_pac &&
  883. (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)))) {
  884. #ifdef CONFIG_MAC80211_DEBUG
  885. printk(KERN_DEBUG "%s: dropped frame "
  886. "(unauthorized port)\n", rx->dev->name);
  887. #endif /* CONFIG_MAC80211_DEBUG */
  888. return -EACCES;
  889. }
  890. return 0;
  891. }
  892. static int
  893. ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
  894. {
  895. /*
  896. * Pass through unencrypted frames if the hardware has
  897. * decrypted them already.
  898. */
  899. if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
  900. return 0;
  901. /* Drop unencrypted frames if key is set. */
  902. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  903. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  904. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  905. (rx->key || rx->sdata->drop_unencrypted))) {
  906. if (net_ratelimit())
  907. printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
  908. "encryption\n", rx->dev->name);
  909. return -EACCES;
  910. }
  911. return 0;
  912. }
  913. static int
  914. ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
  915. {
  916. struct net_device *dev = rx->dev;
  917. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  918. u16 fc, hdrlen, ethertype;
  919. u8 *payload;
  920. u8 dst[ETH_ALEN];
  921. u8 src[ETH_ALEN];
  922. struct sk_buff *skb = rx->skb;
  923. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  924. DECLARE_MAC_BUF(mac);
  925. DECLARE_MAC_BUF(mac2);
  926. DECLARE_MAC_BUF(mac3);
  927. DECLARE_MAC_BUF(mac4);
  928. fc = rx->fc;
  929. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  930. return -1;
  931. hdrlen = ieee80211_get_hdrlen(fc);
  932. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  933. * header
  934. * IEEE 802.11 address fields:
  935. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  936. * 0 0 DA SA BSSID n/a
  937. * 0 1 DA BSSID SA n/a
  938. * 1 0 BSSID SA DA n/a
  939. * 1 1 RA TA DA SA
  940. */
  941. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  942. case IEEE80211_FCTL_TODS:
  943. /* BSSID SA DA */
  944. memcpy(dst, hdr->addr3, ETH_ALEN);
  945. memcpy(src, hdr->addr2, ETH_ALEN);
  946. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
  947. sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
  948. if (net_ratelimit())
  949. printk(KERN_DEBUG "%s: dropped ToDS frame "
  950. "(BSSID=%s SA=%s DA=%s)\n",
  951. dev->name,
  952. print_mac(mac, hdr->addr1),
  953. print_mac(mac2, hdr->addr2),
  954. print_mac(mac3, hdr->addr3));
  955. return -1;
  956. }
  957. break;
  958. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  959. /* RA TA DA SA */
  960. memcpy(dst, hdr->addr3, ETH_ALEN);
  961. memcpy(src, hdr->addr4, ETH_ALEN);
  962. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS)) {
  963. if (net_ratelimit())
  964. printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
  965. "frame (RA=%s TA=%s DA=%s SA=%s)\n",
  966. rx->dev->name,
  967. print_mac(mac, hdr->addr1),
  968. print_mac(mac2, hdr->addr2),
  969. print_mac(mac3, hdr->addr3),
  970. print_mac(mac4, hdr->addr4));
  971. return -1;
  972. }
  973. break;
  974. case IEEE80211_FCTL_FROMDS:
  975. /* DA BSSID SA */
  976. memcpy(dst, hdr->addr1, ETH_ALEN);
  977. memcpy(src, hdr->addr3, ETH_ALEN);
  978. if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
  979. (is_multicast_ether_addr(dst) &&
  980. !compare_ether_addr(src, dev->dev_addr)))
  981. return -1;
  982. break;
  983. case 0:
  984. /* DA SA BSSID */
  985. memcpy(dst, hdr->addr1, ETH_ALEN);
  986. memcpy(src, hdr->addr2, ETH_ALEN);
  987. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
  988. if (net_ratelimit()) {
  989. printk(KERN_DEBUG "%s: dropped IBSS frame "
  990. "(DA=%s SA=%s BSSID=%s)\n",
  991. dev->name,
  992. print_mac(mac, hdr->addr1),
  993. print_mac(mac2, hdr->addr2),
  994. print_mac(mac3, hdr->addr3));
  995. }
  996. return -1;
  997. }
  998. break;
  999. }
  1000. if (unlikely(skb->len - hdrlen < 8)) {
  1001. if (net_ratelimit()) {
  1002. printk(KERN_DEBUG "%s: RX too short data frame "
  1003. "payload\n", dev->name);
  1004. }
  1005. return -1;
  1006. }
  1007. payload = skb->data + hdrlen;
  1008. ethertype = (payload[6] << 8) | payload[7];
  1009. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  1010. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1011. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  1012. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  1013. * replace EtherType */
  1014. skb_pull(skb, hdrlen + 6);
  1015. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  1016. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  1017. } else {
  1018. struct ethhdr *ehdr;
  1019. __be16 len;
  1020. skb_pull(skb, hdrlen);
  1021. len = htons(skb->len);
  1022. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  1023. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  1024. memcpy(ehdr->h_source, src, ETH_ALEN);
  1025. ehdr->h_proto = len;
  1026. }
  1027. return 0;
  1028. }
  1029. /*
  1030. * requires that rx->skb is a frame with ethernet header
  1031. */
  1032. static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
  1033. {
  1034. static const u8 pae_group_addr[ETH_ALEN]
  1035. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1036. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1037. /*
  1038. * Allow EAPOL frames to us/the PAE group address regardless
  1039. * of whether the frame was encrypted or not.
  1040. */
  1041. if (ehdr->h_proto == htons(ETH_P_PAE) &&
  1042. (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
  1043. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1044. return true;
  1045. if (ieee80211_802_1x_port_control(rx) ||
  1046. ieee80211_drop_unencrypted(rx))
  1047. return false;
  1048. return true;
  1049. }
  1050. /*
  1051. * requires that rx->skb is a frame with ethernet header
  1052. */
  1053. static void
  1054. ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
  1055. {
  1056. struct net_device *dev = rx->dev;
  1057. struct ieee80211_local *local = rx->local;
  1058. struct sk_buff *skb, *xmit_skb;
  1059. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1060. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1061. struct sta_info *dsta;
  1062. skb = rx->skb;
  1063. xmit_skb = NULL;
  1064. if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
  1065. sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
  1066. (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
  1067. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1068. /*
  1069. * send multicast frames both to higher layers in
  1070. * local net stack and back to the wireless medium
  1071. */
  1072. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1073. if (!xmit_skb && net_ratelimit())
  1074. printk(KERN_DEBUG "%s: failed to clone "
  1075. "multicast frame\n", dev->name);
  1076. } else {
  1077. dsta = sta_info_get(local, skb->data);
  1078. if (dsta && dsta->dev == dev) {
  1079. /*
  1080. * The destination station is associated to
  1081. * this AP (in this VLAN), so send the frame
  1082. * directly to it and do not pass it to local
  1083. * net stack.
  1084. */
  1085. xmit_skb = skb;
  1086. skb = NULL;
  1087. }
  1088. if (dsta)
  1089. sta_info_put(dsta);
  1090. }
  1091. }
  1092. if (skb) {
  1093. /* deliver to local stack */
  1094. skb->protocol = eth_type_trans(skb, dev);
  1095. memset(skb->cb, 0, sizeof(skb->cb));
  1096. netif_rx(skb);
  1097. }
  1098. if (xmit_skb) {
  1099. /* send to wireless media */
  1100. xmit_skb->protocol = htons(ETH_P_802_3);
  1101. skb_reset_network_header(xmit_skb);
  1102. skb_reset_mac_header(xmit_skb);
  1103. dev_queue_xmit(xmit_skb);
  1104. }
  1105. }
  1106. static ieee80211_txrx_result
  1107. ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
  1108. {
  1109. struct net_device *dev = rx->dev;
  1110. struct ieee80211_local *local = rx->local;
  1111. u16 fc, ethertype;
  1112. u8 *payload;
  1113. struct sk_buff *skb = rx->skb, *frame = NULL;
  1114. const struct ethhdr *eth;
  1115. int remaining, err;
  1116. u8 dst[ETH_ALEN];
  1117. u8 src[ETH_ALEN];
  1118. DECLARE_MAC_BUF(mac);
  1119. fc = rx->fc;
  1120. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1121. return TXRX_CONTINUE;
  1122. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1123. return TXRX_DROP;
  1124. if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
  1125. return TXRX_CONTINUE;
  1126. err = ieee80211_data_to_8023(rx);
  1127. if (unlikely(err))
  1128. return TXRX_DROP;
  1129. skb->dev = dev;
  1130. dev->stats.rx_packets++;
  1131. dev->stats.rx_bytes += skb->len;
  1132. /* skip the wrapping header */
  1133. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  1134. if (!eth)
  1135. return TXRX_DROP;
  1136. while (skb != frame) {
  1137. u8 padding;
  1138. __be16 len = eth->h_proto;
  1139. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  1140. remaining = skb->len;
  1141. memcpy(dst, eth->h_dest, ETH_ALEN);
  1142. memcpy(src, eth->h_source, ETH_ALEN);
  1143. padding = ((4 - subframe_len) & 0x3);
  1144. /* the last MSDU has no padding */
  1145. if (subframe_len > remaining) {
  1146. printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
  1147. return TXRX_DROP;
  1148. }
  1149. skb_pull(skb, sizeof(struct ethhdr));
  1150. /* if last subframe reuse skb */
  1151. if (remaining <= subframe_len + padding)
  1152. frame = skb;
  1153. else {
  1154. frame = dev_alloc_skb(local->hw.extra_tx_headroom +
  1155. subframe_len);
  1156. if (frame == NULL)
  1157. return TXRX_DROP;
  1158. skb_reserve(frame, local->hw.extra_tx_headroom +
  1159. sizeof(struct ethhdr));
  1160. memcpy(skb_put(frame, ntohs(len)), skb->data,
  1161. ntohs(len));
  1162. eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
  1163. padding);
  1164. if (!eth) {
  1165. printk(KERN_DEBUG "%s: wrong buffer size ",
  1166. dev->name);
  1167. dev_kfree_skb(frame);
  1168. return TXRX_DROP;
  1169. }
  1170. }
  1171. skb_reset_network_header(frame);
  1172. frame->dev = dev;
  1173. frame->priority = skb->priority;
  1174. rx->skb = frame;
  1175. payload = frame->data;
  1176. ethertype = (payload[6] << 8) | payload[7];
  1177. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  1178. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1179. compare_ether_addr(payload,
  1180. bridge_tunnel_header) == 0)) {
  1181. /* remove RFC1042 or Bridge-Tunnel
  1182. * encapsulation and replace EtherType */
  1183. skb_pull(frame, 6);
  1184. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1185. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1186. } else {
  1187. memcpy(skb_push(frame, sizeof(__be16)),
  1188. &len, sizeof(__be16));
  1189. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1190. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1191. }
  1192. if (!ieee80211_frame_allowed(rx)) {
  1193. if (skb == frame) /* last frame */
  1194. return TXRX_DROP;
  1195. dev_kfree_skb(frame);
  1196. continue;
  1197. }
  1198. ieee80211_deliver_skb(rx);
  1199. }
  1200. return TXRX_QUEUED;
  1201. }
  1202. static ieee80211_txrx_result
  1203. ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
  1204. {
  1205. struct net_device *dev = rx->dev;
  1206. u16 fc;
  1207. int err;
  1208. fc = rx->fc;
  1209. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1210. return TXRX_CONTINUE;
  1211. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1212. return TXRX_DROP;
  1213. err = ieee80211_data_to_8023(rx);
  1214. if (unlikely(err))
  1215. return TXRX_DROP;
  1216. if (!ieee80211_frame_allowed(rx))
  1217. return TXRX_DROP;
  1218. rx->skb->dev = dev;
  1219. dev->stats.rx_packets++;
  1220. dev->stats.rx_bytes += rx->skb->len;
  1221. ieee80211_deliver_skb(rx);
  1222. return TXRX_QUEUED;
  1223. }
  1224. static ieee80211_txrx_result
  1225. ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
  1226. {
  1227. struct ieee80211_local *local = rx->local;
  1228. struct ieee80211_hw *hw = &local->hw;
  1229. struct sk_buff *skb = rx->skb;
  1230. struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
  1231. struct tid_ampdu_rx *tid_agg_rx;
  1232. u16 start_seq_num;
  1233. u16 tid;
  1234. if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
  1235. return TXRX_CONTINUE;
  1236. if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
  1237. if (!rx->sta)
  1238. return TXRX_CONTINUE;
  1239. tid = le16_to_cpu(bar->control) >> 12;
  1240. tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
  1241. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1242. return TXRX_CONTINUE;
  1243. start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
  1244. /* reset session timer */
  1245. if (tid_agg_rx->timeout) {
  1246. unsigned long expires =
  1247. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1248. mod_timer(&tid_agg_rx->session_timer, expires);
  1249. }
  1250. /* manage reordering buffer according to requested */
  1251. /* sequence number */
  1252. rcu_read_lock();
  1253. ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
  1254. start_seq_num, 1);
  1255. rcu_read_unlock();
  1256. return TXRX_DROP;
  1257. }
  1258. return TXRX_CONTINUE;
  1259. }
  1260. static ieee80211_txrx_result
  1261. ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
  1262. {
  1263. struct ieee80211_sub_if_data *sdata;
  1264. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  1265. return TXRX_DROP;
  1266. sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  1267. if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  1268. sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
  1269. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  1270. ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
  1271. else
  1272. return TXRX_DROP;
  1273. return TXRX_QUEUED;
  1274. }
  1275. static inline ieee80211_txrx_result __ieee80211_invoke_rx_handlers(
  1276. struct ieee80211_local *local,
  1277. ieee80211_rx_handler *handlers,
  1278. struct ieee80211_txrx_data *rx,
  1279. struct sta_info *sta)
  1280. {
  1281. ieee80211_rx_handler *handler;
  1282. ieee80211_txrx_result res = TXRX_DROP;
  1283. for (handler = handlers; *handler != NULL; handler++) {
  1284. res = (*handler)(rx);
  1285. switch (res) {
  1286. case TXRX_CONTINUE:
  1287. continue;
  1288. case TXRX_DROP:
  1289. I802_DEBUG_INC(local->rx_handlers_drop);
  1290. if (sta)
  1291. sta->rx_dropped++;
  1292. break;
  1293. case TXRX_QUEUED:
  1294. I802_DEBUG_INC(local->rx_handlers_queued);
  1295. break;
  1296. }
  1297. break;
  1298. }
  1299. if (res == TXRX_DROP)
  1300. dev_kfree_skb(rx->skb);
  1301. return res;
  1302. }
  1303. static inline void ieee80211_invoke_rx_handlers(struct ieee80211_local *local,
  1304. ieee80211_rx_handler *handlers,
  1305. struct ieee80211_txrx_data *rx,
  1306. struct sta_info *sta)
  1307. {
  1308. if (__ieee80211_invoke_rx_handlers(local, handlers, rx, sta) ==
  1309. TXRX_CONTINUE)
  1310. dev_kfree_skb(rx->skb);
  1311. }
  1312. static void ieee80211_rx_michael_mic_report(struct net_device *dev,
  1313. struct ieee80211_hdr *hdr,
  1314. struct sta_info *sta,
  1315. struct ieee80211_txrx_data *rx)
  1316. {
  1317. int keyidx, hdrlen;
  1318. DECLARE_MAC_BUF(mac);
  1319. DECLARE_MAC_BUF(mac2);
  1320. hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
  1321. if (rx->skb->len >= hdrlen + 4)
  1322. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  1323. else
  1324. keyidx = -1;
  1325. if (net_ratelimit())
  1326. printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
  1327. "failure from %s to %s keyidx=%d\n",
  1328. dev->name, print_mac(mac, hdr->addr2),
  1329. print_mac(mac2, hdr->addr1), keyidx);
  1330. if (!sta) {
  1331. /*
  1332. * Some hardware seem to generate incorrect Michael MIC
  1333. * reports; ignore them to avoid triggering countermeasures.
  1334. */
  1335. if (net_ratelimit())
  1336. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1337. "error for unknown address %s\n",
  1338. dev->name, print_mac(mac, hdr->addr2));
  1339. goto ignore;
  1340. }
  1341. if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
  1342. if (net_ratelimit())
  1343. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1344. "error for a frame with no PROTECTED flag (src "
  1345. "%s)\n", dev->name, print_mac(mac, hdr->addr2));
  1346. goto ignore;
  1347. }
  1348. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
  1349. /*
  1350. * APs with pairwise keys should never receive Michael MIC
  1351. * errors for non-zero keyidx because these are reserved for
  1352. * group keys and only the AP is sending real multicast
  1353. * frames in the BSS.
  1354. */
  1355. if (net_ratelimit())
  1356. printk(KERN_DEBUG "%s: ignored Michael MIC error for "
  1357. "a frame with non-zero keyidx (%d)"
  1358. " (src %s)\n", dev->name, keyidx,
  1359. print_mac(mac, hdr->addr2));
  1360. goto ignore;
  1361. }
  1362. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  1363. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  1364. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
  1365. if (net_ratelimit())
  1366. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1367. "error for a frame that cannot be encrypted "
  1368. "(fc=0x%04x) (src %s)\n",
  1369. dev->name, rx->fc, print_mac(mac, hdr->addr2));
  1370. goto ignore;
  1371. }
  1372. mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
  1373. ignore:
  1374. dev_kfree_skb(rx->skb);
  1375. rx->skb = NULL;
  1376. }
  1377. ieee80211_rx_handler ieee80211_rx_handlers[] =
  1378. {
  1379. ieee80211_rx_h_if_stats,
  1380. ieee80211_rx_h_passive_scan,
  1381. ieee80211_rx_h_check,
  1382. ieee80211_rx_h_decrypt,
  1383. ieee80211_rx_h_sta_process,
  1384. ieee80211_rx_h_defragment,
  1385. ieee80211_rx_h_ps_poll,
  1386. ieee80211_rx_h_michael_mic_verify,
  1387. /* this must be after decryption - so header is counted in MPDU mic
  1388. * must be before pae and data, so QOS_DATA format frames
  1389. * are not passed to user space by these functions
  1390. */
  1391. ieee80211_rx_h_remove_qos_control,
  1392. ieee80211_rx_h_amsdu,
  1393. ieee80211_rx_h_data,
  1394. ieee80211_rx_h_ctrl,
  1395. ieee80211_rx_h_mgmt,
  1396. NULL
  1397. };
  1398. /* main receive path */
  1399. static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
  1400. u8 *bssid, struct ieee80211_txrx_data *rx,
  1401. struct ieee80211_hdr *hdr)
  1402. {
  1403. int multicast = is_multicast_ether_addr(hdr->addr1);
  1404. switch (sdata->vif.type) {
  1405. case IEEE80211_IF_TYPE_STA:
  1406. if (!bssid)
  1407. return 0;
  1408. if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1409. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1410. return 0;
  1411. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1412. } else if (!multicast &&
  1413. compare_ether_addr(sdata->dev->dev_addr,
  1414. hdr->addr1) != 0) {
  1415. if (!(sdata->dev->flags & IFF_PROMISC))
  1416. return 0;
  1417. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1418. }
  1419. break;
  1420. case IEEE80211_IF_TYPE_IBSS:
  1421. if (!bssid)
  1422. return 0;
  1423. if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1424. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1425. return 0;
  1426. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1427. } else if (!multicast &&
  1428. compare_ether_addr(sdata->dev->dev_addr,
  1429. hdr->addr1) != 0) {
  1430. if (!(sdata->dev->flags & IFF_PROMISC))
  1431. return 0;
  1432. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1433. } else if (!rx->sta)
  1434. rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
  1435. bssid, hdr->addr2);
  1436. break;
  1437. case IEEE80211_IF_TYPE_VLAN:
  1438. case IEEE80211_IF_TYPE_AP:
  1439. if (!bssid) {
  1440. if (compare_ether_addr(sdata->dev->dev_addr,
  1441. hdr->addr1))
  1442. return 0;
  1443. } else if (!ieee80211_bssid_match(bssid,
  1444. sdata->dev->dev_addr)) {
  1445. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1446. return 0;
  1447. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1448. }
  1449. if (sdata->dev == sdata->local->mdev &&
  1450. !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1451. /* do not receive anything via
  1452. * master device when not scanning */
  1453. return 0;
  1454. break;
  1455. case IEEE80211_IF_TYPE_WDS:
  1456. if (bssid ||
  1457. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  1458. return 0;
  1459. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  1460. return 0;
  1461. break;
  1462. case IEEE80211_IF_TYPE_MNTR:
  1463. /* take everything */
  1464. break;
  1465. case IEEE80211_IF_TYPE_INVALID:
  1466. /* should never get here */
  1467. WARN_ON(1);
  1468. break;
  1469. }
  1470. return 1;
  1471. }
  1472. /*
  1473. * This is the actual Rx frames handler. as it blongs to Rx path it must
  1474. * be called with rcu_read_lock protection.
  1475. */
  1476. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  1477. struct sk_buff *skb,
  1478. struct ieee80211_rx_status *status,
  1479. u32 load)
  1480. {
  1481. struct ieee80211_local *local = hw_to_local(hw);
  1482. struct ieee80211_sub_if_data *sdata;
  1483. struct sta_info *sta;
  1484. struct ieee80211_hdr *hdr;
  1485. struct ieee80211_txrx_data rx;
  1486. u16 type;
  1487. int prepares;
  1488. struct ieee80211_sub_if_data *prev = NULL;
  1489. struct sk_buff *skb_new;
  1490. u8 *bssid;
  1491. hdr = (struct ieee80211_hdr *) skb->data;
  1492. memset(&rx, 0, sizeof(rx));
  1493. rx.skb = skb;
  1494. rx.local = local;
  1495. rx.u.rx.status = status;
  1496. rx.u.rx.load = load;
  1497. rx.fc = le16_to_cpu(hdr->frame_control);
  1498. type = rx.fc & IEEE80211_FCTL_FTYPE;
  1499. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  1500. local->dot11ReceivedFragmentCount++;
  1501. sta = rx.sta = sta_info_get(local, hdr->addr2);
  1502. if (sta) {
  1503. rx.dev = rx.sta->dev;
  1504. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  1505. }
  1506. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  1507. ieee80211_rx_michael_mic_report(local->mdev, hdr, sta, &rx);
  1508. goto end;
  1509. }
  1510. if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
  1511. rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
  1512. if (__ieee80211_invoke_rx_handlers(local, local->rx_pre_handlers, &rx,
  1513. sta) != TXRX_CONTINUE)
  1514. goto end;
  1515. skb = rx.skb;
  1516. if (sta && !(sta->flags & (WLAN_STA_WDS | WLAN_STA_ASSOC_AP)) &&
  1517. !atomic_read(&local->iff_promiscs) &&
  1518. !is_multicast_ether_addr(hdr->addr1)) {
  1519. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1520. ieee80211_invoke_rx_handlers(local, local->rx_handlers, &rx,
  1521. rx.sta);
  1522. sta_info_put(sta);
  1523. return;
  1524. }
  1525. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1526. if (!netif_running(sdata->dev))
  1527. continue;
  1528. if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
  1529. continue;
  1530. bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  1531. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1532. prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
  1533. /* prepare_for_handlers can change sta */
  1534. sta = rx.sta;
  1535. if (!prepares)
  1536. continue;
  1537. /*
  1538. * frame is destined for this interface, but if it's not
  1539. * also for the previous one we handle that after the
  1540. * loop to avoid copying the SKB once too much
  1541. */
  1542. if (!prev) {
  1543. prev = sdata;
  1544. continue;
  1545. }
  1546. /*
  1547. * frame was destined for the previous interface
  1548. * so invoke RX handlers for it
  1549. */
  1550. skb_new = skb_copy(skb, GFP_ATOMIC);
  1551. if (!skb_new) {
  1552. if (net_ratelimit())
  1553. printk(KERN_DEBUG "%s: failed to copy "
  1554. "multicast frame for %s",
  1555. wiphy_name(local->hw.wiphy),
  1556. prev->dev->name);
  1557. continue;
  1558. }
  1559. rx.fc = le16_to_cpu(hdr->frame_control);
  1560. rx.skb = skb_new;
  1561. rx.dev = prev->dev;
  1562. rx.sdata = prev;
  1563. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  1564. &rx, sta);
  1565. prev = sdata;
  1566. }
  1567. if (prev) {
  1568. rx.fc = le16_to_cpu(hdr->frame_control);
  1569. rx.skb = skb;
  1570. rx.dev = prev->dev;
  1571. rx.sdata = prev;
  1572. ieee80211_invoke_rx_handlers(local, local->rx_handlers,
  1573. &rx, sta);
  1574. } else
  1575. dev_kfree_skb(skb);
  1576. end:
  1577. if (sta)
  1578. sta_info_put(sta);
  1579. }
  1580. #define SEQ_MODULO 0x1000
  1581. #define SEQ_MASK 0xfff
  1582. static inline int seq_less(u16 sq1, u16 sq2)
  1583. {
  1584. return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
  1585. }
  1586. static inline u16 seq_inc(u16 sq)
  1587. {
  1588. return ((sq + 1) & SEQ_MASK);
  1589. }
  1590. static inline u16 seq_sub(u16 sq1, u16 sq2)
  1591. {
  1592. return ((sq1 - sq2) & SEQ_MASK);
  1593. }
  1594. /*
  1595. * As it function blongs to Rx path it must be called with
  1596. * the proper rcu_read_lock protection for its flow.
  1597. */
  1598. u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  1599. struct tid_ampdu_rx *tid_agg_rx,
  1600. struct sk_buff *skb, u16 mpdu_seq_num,
  1601. int bar_req)
  1602. {
  1603. struct ieee80211_local *local = hw_to_local(hw);
  1604. struct ieee80211_rx_status status;
  1605. u16 head_seq_num, buf_size;
  1606. int index;
  1607. u32 pkt_load;
  1608. buf_size = tid_agg_rx->buf_size;
  1609. head_seq_num = tid_agg_rx->head_seq_num;
  1610. /* frame with out of date sequence number */
  1611. if (seq_less(mpdu_seq_num, head_seq_num)) {
  1612. dev_kfree_skb(skb);
  1613. return 1;
  1614. }
  1615. /* if frame sequence number exceeds our buffering window size or
  1616. * block Ack Request arrived - release stored frames */
  1617. if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
  1618. /* new head to the ordering buffer */
  1619. if (bar_req)
  1620. head_seq_num = mpdu_seq_num;
  1621. else
  1622. head_seq_num =
  1623. seq_inc(seq_sub(mpdu_seq_num, buf_size));
  1624. /* release stored frames up to new head to stack */
  1625. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  1626. index = seq_sub(tid_agg_rx->head_seq_num,
  1627. tid_agg_rx->ssn)
  1628. % tid_agg_rx->buf_size;
  1629. if (tid_agg_rx->reorder_buf[index]) {
  1630. /* release the reordered frames to stack */
  1631. memcpy(&status,
  1632. tid_agg_rx->reorder_buf[index]->cb,
  1633. sizeof(status));
  1634. pkt_load = ieee80211_rx_load_stats(local,
  1635. tid_agg_rx->reorder_buf[index],
  1636. &status);
  1637. __ieee80211_rx_handle_packet(hw,
  1638. tid_agg_rx->reorder_buf[index],
  1639. &status, pkt_load);
  1640. tid_agg_rx->stored_mpdu_num--;
  1641. tid_agg_rx->reorder_buf[index] = NULL;
  1642. }
  1643. tid_agg_rx->head_seq_num =
  1644. seq_inc(tid_agg_rx->head_seq_num);
  1645. }
  1646. if (bar_req)
  1647. return 1;
  1648. }
  1649. /* now the new frame is always in the range of the reordering */
  1650. /* buffer window */
  1651. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
  1652. % tid_agg_rx->buf_size;
  1653. /* check if we already stored this frame */
  1654. if (tid_agg_rx->reorder_buf[index]) {
  1655. dev_kfree_skb(skb);
  1656. return 1;
  1657. }
  1658. /* if arrived mpdu is in the right order and nothing else stored */
  1659. /* release it immediately */
  1660. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1661. tid_agg_rx->stored_mpdu_num == 0) {
  1662. tid_agg_rx->head_seq_num =
  1663. seq_inc(tid_agg_rx->head_seq_num);
  1664. return 0;
  1665. }
  1666. /* put the frame in the reordering buffer */
  1667. tid_agg_rx->reorder_buf[index] = skb;
  1668. tid_agg_rx->stored_mpdu_num++;
  1669. /* release the buffer until next missing frame */
  1670. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
  1671. % tid_agg_rx->buf_size;
  1672. while (tid_agg_rx->reorder_buf[index]) {
  1673. /* release the reordered frame back to stack */
  1674. memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
  1675. sizeof(status));
  1676. pkt_load = ieee80211_rx_load_stats(local,
  1677. tid_agg_rx->reorder_buf[index],
  1678. &status);
  1679. __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
  1680. &status, pkt_load);
  1681. tid_agg_rx->stored_mpdu_num--;
  1682. tid_agg_rx->reorder_buf[index] = NULL;
  1683. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  1684. index = seq_sub(tid_agg_rx->head_seq_num,
  1685. tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  1686. }
  1687. return 1;
  1688. }
  1689. static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
  1690. struct sk_buff *skb)
  1691. {
  1692. struct ieee80211_hw *hw = &local->hw;
  1693. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1694. struct sta_info *sta;
  1695. struct tid_ampdu_rx *tid_agg_rx;
  1696. u16 fc, sc;
  1697. u16 mpdu_seq_num;
  1698. u8 ret = 0, *qc;
  1699. int tid;
  1700. sta = sta_info_get(local, hdr->addr2);
  1701. if (!sta)
  1702. return ret;
  1703. fc = le16_to_cpu(hdr->frame_control);
  1704. /* filter the QoS data rx stream according to
  1705. * STA/TID and check if this STA/TID is on aggregation */
  1706. if (!WLAN_FC_IS_QOS_DATA(fc))
  1707. goto end_reorder;
  1708. qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
  1709. tid = qc[0] & QOS_CONTROL_TID_MASK;
  1710. tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
  1711. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1712. goto end_reorder;
  1713. /* null data frames are excluded */
  1714. if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
  1715. goto end_reorder;
  1716. /* new un-ordered ampdu frame - process it */
  1717. /* reset session timer */
  1718. if (tid_agg_rx->timeout) {
  1719. unsigned long expires =
  1720. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1721. mod_timer(&tid_agg_rx->session_timer, expires);
  1722. }
  1723. /* if this mpdu is fragmented - terminate rx aggregation session */
  1724. sc = le16_to_cpu(hdr->seq_ctrl);
  1725. if (sc & IEEE80211_SCTL_FRAG) {
  1726. ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
  1727. tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
  1728. ret = 1;
  1729. goto end_reorder;
  1730. }
  1731. /* according to mpdu sequence number deal with reordering buffer */
  1732. mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1733. ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
  1734. mpdu_seq_num, 0);
  1735. end_reorder:
  1736. if (sta)
  1737. sta_info_put(sta);
  1738. return ret;
  1739. }
  1740. /*
  1741. * This is the receive path handler. It is called by a low level driver when an
  1742. * 802.11 MPDU is received from the hardware.
  1743. */
  1744. void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
  1745. struct ieee80211_rx_status *status)
  1746. {
  1747. struct ieee80211_local *local = hw_to_local(hw);
  1748. u32 pkt_load;
  1749. /*
  1750. * key references and virtual interfaces are protected using RCU
  1751. * and this requires that we are in a read-side RCU section during
  1752. * receive processing
  1753. */
  1754. rcu_read_lock();
  1755. /*
  1756. * Frames with failed FCS/PLCP checksum are not returned,
  1757. * all other frames are returned without radiotap header
  1758. * if it was previously present.
  1759. * Also, frames with less than 16 bytes are dropped.
  1760. */
  1761. skb = ieee80211_rx_monitor(local, skb, status);
  1762. if (!skb) {
  1763. rcu_read_unlock();
  1764. return;
  1765. }
  1766. pkt_load = ieee80211_rx_load_stats(local, skb, status);
  1767. local->channel_use_raw += pkt_load;
  1768. if (!ieee80211_rx_reorder_ampdu(local, skb))
  1769. __ieee80211_rx_handle_packet(hw, skb, status, pkt_load);
  1770. rcu_read_unlock();
  1771. }
  1772. EXPORT_SYMBOL(__ieee80211_rx);
  1773. /* This is a version of the rx handler that can be called from hard irq
  1774. * context. Post the skb on the queue and schedule the tasklet */
  1775. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  1776. struct ieee80211_rx_status *status)
  1777. {
  1778. struct ieee80211_local *local = hw_to_local(hw);
  1779. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  1780. skb->dev = local->mdev;
  1781. /* copy status into skb->cb for use by tasklet */
  1782. memcpy(skb->cb, status, sizeof(*status));
  1783. skb->pkt_type = IEEE80211_RX_MSG;
  1784. skb_queue_tail(&local->skb_queue, skb);
  1785. tasklet_schedule(&local->tasklet);
  1786. }
  1787. EXPORT_SYMBOL(ieee80211_rx_irqsafe);