rx.c 59 KB

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