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_PSPOLL);
  519. if (!skb_queue_empty(&sta->ps_tx_buf))
  520. sta_info_clear_tim_bit(sta);
  521. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  522. printk(KERN_DEBUG "%s: STA %s aid %d exits power save mode\n",
  523. dev->name, print_mac(mac, sta->addr), sta->aid);
  524. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  525. /* Send all buffered frames to the station */
  526. while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
  527. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  528. sent++;
  529. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  530. dev_queue_xmit(skb);
  531. }
  532. while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
  533. pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
  534. local->total_ps_buffered--;
  535. sent++;
  536. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  537. printk(KERN_DEBUG "%s: STA %s aid %d send PS frame "
  538. "since STA not sleeping anymore\n", dev->name,
  539. print_mac(mac, sta->addr), sta->aid);
  540. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  541. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  542. dev_queue_xmit(skb);
  543. }
  544. return sent;
  545. }
  546. static ieee80211_rx_result
  547. ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
  548. {
  549. struct sta_info *sta = rx->sta;
  550. struct net_device *dev = rx->dev;
  551. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  552. if (!sta)
  553. return RX_CONTINUE;
  554. /* Update last_rx only for IBSS packets which are for the current
  555. * BSSID to avoid keeping the current IBSS network alive in cases where
  556. * other STAs are using different BSSID. */
  557. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  558. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  559. IEEE80211_IF_TYPE_IBSS);
  560. if (compare_ether_addr(bssid, rx->sdata->u.sta.bssid) == 0)
  561. sta->last_rx = jiffies;
  562. } else
  563. if (!is_multicast_ether_addr(hdr->addr1) ||
  564. rx->sdata->vif.type == IEEE80211_IF_TYPE_STA) {
  565. /* Update last_rx only for unicast frames in order to prevent
  566. * the Probe Request frames (the only broadcast frames from a
  567. * STA in infrastructure mode) from keeping a connection alive.
  568. */
  569. sta->last_rx = jiffies;
  570. }
  571. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  572. return RX_CONTINUE;
  573. sta->rx_fragments++;
  574. sta->rx_bytes += rx->skb->len;
  575. sta->last_rssi = rx->u.rx.status->ssi;
  576. sta->last_signal = rx->u.rx.status->signal;
  577. sta->last_noise = rx->u.rx.status->noise;
  578. if (!(rx->fc & IEEE80211_FCTL_MOREFRAGS)) {
  579. /* Change STA power saving mode only in the end of a frame
  580. * exchange sequence */
  581. if ((sta->flags & WLAN_STA_PS) && !(rx->fc & IEEE80211_FCTL_PM))
  582. rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
  583. else if (!(sta->flags & WLAN_STA_PS) &&
  584. (rx->fc & IEEE80211_FCTL_PM))
  585. ap_sta_ps_start(dev, sta);
  586. }
  587. /* Drop data::nullfunc frames silently, since they are used only to
  588. * control station power saving mode. */
  589. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  590. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_NULLFUNC) {
  591. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  592. /* Update counter and free packet here to avoid counting this
  593. * as a dropped packed. */
  594. sta->rx_packets++;
  595. dev_kfree_skb(rx->skb);
  596. return RX_QUEUED;
  597. }
  598. return RX_CONTINUE;
  599. } /* ieee80211_rx_h_sta_process */
  600. static inline struct ieee80211_fragment_entry *
  601. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  602. unsigned int frag, unsigned int seq, int rx_queue,
  603. struct sk_buff **skb)
  604. {
  605. struct ieee80211_fragment_entry *entry;
  606. int idx;
  607. idx = sdata->fragment_next;
  608. entry = &sdata->fragments[sdata->fragment_next++];
  609. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  610. sdata->fragment_next = 0;
  611. if (!skb_queue_empty(&entry->skb_list)) {
  612. #ifdef CONFIG_MAC80211_DEBUG
  613. struct ieee80211_hdr *hdr =
  614. (struct ieee80211_hdr *) entry->skb_list.next->data;
  615. DECLARE_MAC_BUF(mac);
  616. DECLARE_MAC_BUF(mac2);
  617. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  618. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  619. "addr1=%s addr2=%s\n",
  620. sdata->dev->name, idx,
  621. jiffies - entry->first_frag_time, entry->seq,
  622. entry->last_frag, print_mac(mac, hdr->addr1),
  623. print_mac(mac2, hdr->addr2));
  624. #endif /* CONFIG_MAC80211_DEBUG */
  625. __skb_queue_purge(&entry->skb_list);
  626. }
  627. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  628. *skb = NULL;
  629. entry->first_frag_time = jiffies;
  630. entry->seq = seq;
  631. entry->rx_queue = rx_queue;
  632. entry->last_frag = frag;
  633. entry->ccmp = 0;
  634. entry->extra_len = 0;
  635. return entry;
  636. }
  637. static inline struct ieee80211_fragment_entry *
  638. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  639. u16 fc, unsigned int frag, unsigned int seq,
  640. int rx_queue, struct ieee80211_hdr *hdr)
  641. {
  642. struct ieee80211_fragment_entry *entry;
  643. int i, idx;
  644. idx = sdata->fragment_next;
  645. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  646. struct ieee80211_hdr *f_hdr;
  647. u16 f_fc;
  648. idx--;
  649. if (idx < 0)
  650. idx = IEEE80211_FRAGMENT_MAX - 1;
  651. entry = &sdata->fragments[idx];
  652. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  653. entry->rx_queue != rx_queue ||
  654. entry->last_frag + 1 != frag)
  655. continue;
  656. f_hdr = (struct ieee80211_hdr *) entry->skb_list.next->data;
  657. f_fc = le16_to_cpu(f_hdr->frame_control);
  658. if ((fc & IEEE80211_FCTL_FTYPE) != (f_fc & IEEE80211_FCTL_FTYPE) ||
  659. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  660. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  661. continue;
  662. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  663. __skb_queue_purge(&entry->skb_list);
  664. continue;
  665. }
  666. return entry;
  667. }
  668. return NULL;
  669. }
  670. static ieee80211_rx_result
  671. ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
  672. {
  673. struct ieee80211_hdr *hdr;
  674. u16 sc;
  675. unsigned int frag, seq;
  676. struct ieee80211_fragment_entry *entry;
  677. struct sk_buff *skb;
  678. DECLARE_MAC_BUF(mac);
  679. hdr = (struct ieee80211_hdr *) rx->skb->data;
  680. sc = le16_to_cpu(hdr->seq_ctrl);
  681. frag = sc & IEEE80211_SCTL_FRAG;
  682. if (likely((!(rx->fc & IEEE80211_FCTL_MOREFRAGS) && frag == 0) ||
  683. (rx->skb)->len < 24 ||
  684. is_multicast_ether_addr(hdr->addr1))) {
  685. /* not fragmented */
  686. goto out;
  687. }
  688. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  689. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  690. if (frag == 0) {
  691. /* This is the first fragment of a new frame. */
  692. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  693. rx->u.rx.queue, &(rx->skb));
  694. if (rx->key && rx->key->conf.alg == ALG_CCMP &&
  695. (rx->fc & IEEE80211_FCTL_PROTECTED)) {
  696. /* Store CCMP PN so that we can verify that the next
  697. * fragment has a sequential PN value. */
  698. entry->ccmp = 1;
  699. memcpy(entry->last_pn,
  700. rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
  701. CCMP_PN_LEN);
  702. }
  703. return RX_QUEUED;
  704. }
  705. /* This is a fragment for a frame that should already be pending in
  706. * fragment cache. Add this fragment to the end of the pending entry.
  707. */
  708. entry = ieee80211_reassemble_find(rx->sdata, rx->fc, frag, seq,
  709. rx->u.rx.queue, hdr);
  710. if (!entry) {
  711. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  712. return RX_DROP_MONITOR;
  713. }
  714. /* Verify that MPDUs within one MSDU have sequential PN values.
  715. * (IEEE 802.11i, 8.3.3.4.5) */
  716. if (entry->ccmp) {
  717. int i;
  718. u8 pn[CCMP_PN_LEN], *rpn;
  719. if (!rx->key || rx->key->conf.alg != ALG_CCMP)
  720. return RX_DROP_UNUSABLE;
  721. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  722. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  723. pn[i]++;
  724. if (pn[i])
  725. break;
  726. }
  727. rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
  728. if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
  729. if (net_ratelimit())
  730. printk(KERN_DEBUG "%s: defrag: CCMP PN not "
  731. "sequential A2=%s"
  732. " PN=%02x%02x%02x%02x%02x%02x "
  733. "(expected %02x%02x%02x%02x%02x%02x)\n",
  734. rx->dev->name, print_mac(mac, hdr->addr2),
  735. rpn[0], rpn[1], rpn[2], rpn[3], rpn[4],
  736. rpn[5], pn[0], pn[1], pn[2], pn[3],
  737. pn[4], pn[5]);
  738. return RX_DROP_UNUSABLE;
  739. }
  740. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  741. }
  742. skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
  743. __skb_queue_tail(&entry->skb_list, rx->skb);
  744. entry->last_frag = frag;
  745. entry->extra_len += rx->skb->len;
  746. if (rx->fc & IEEE80211_FCTL_MOREFRAGS) {
  747. rx->skb = NULL;
  748. return RX_QUEUED;
  749. }
  750. rx->skb = __skb_dequeue(&entry->skb_list);
  751. if (skb_tailroom(rx->skb) < entry->extra_len) {
  752. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  753. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  754. GFP_ATOMIC))) {
  755. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  756. __skb_queue_purge(&entry->skb_list);
  757. return RX_DROP_UNUSABLE;
  758. }
  759. }
  760. while ((skb = __skb_dequeue(&entry->skb_list))) {
  761. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  762. dev_kfree_skb(skb);
  763. }
  764. /* Complete frame has been reassembled - process it now */
  765. rx->flags |= IEEE80211_TXRXD_FRAGMENTED;
  766. out:
  767. if (rx->sta)
  768. rx->sta->rx_packets++;
  769. if (is_multicast_ether_addr(hdr->addr1))
  770. rx->local->dot11MulticastReceivedFrameCount++;
  771. else
  772. ieee80211_led_rx(rx->local);
  773. return RX_CONTINUE;
  774. }
  775. static ieee80211_rx_result
  776. ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
  777. {
  778. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  779. struct sk_buff *skb;
  780. int no_pending_pkts;
  781. DECLARE_MAC_BUF(mac);
  782. if (likely(!rx->sta ||
  783. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL ||
  784. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PSPOLL ||
  785. !(rx->flags & IEEE80211_TXRXD_RXRA_MATCH)))
  786. return RX_CONTINUE;
  787. if ((sdata->vif.type != IEEE80211_IF_TYPE_AP) &&
  788. (sdata->vif.type != IEEE80211_IF_TYPE_VLAN))
  789. return RX_DROP_UNUSABLE;
  790. skb = skb_dequeue(&rx->sta->tx_filtered);
  791. if (!skb) {
  792. skb = skb_dequeue(&rx->sta->ps_tx_buf);
  793. if (skb)
  794. rx->local->total_ps_buffered--;
  795. }
  796. no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
  797. skb_queue_empty(&rx->sta->ps_tx_buf);
  798. if (skb) {
  799. struct ieee80211_hdr *hdr =
  800. (struct ieee80211_hdr *) skb->data;
  801. /*
  802. * Tell TX path to send one frame even though the STA may
  803. * still remain is PS mode after this frame exchange.
  804. */
  805. rx->sta->flags |= WLAN_STA_PSPOLL;
  806. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  807. printk(KERN_DEBUG "STA %s aid %d: PS Poll (entries after %d)\n",
  808. print_mac(mac, rx->sta->addr), rx->sta->aid,
  809. skb_queue_len(&rx->sta->ps_tx_buf));
  810. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  811. /* Use MoreData flag to indicate whether there are more
  812. * buffered frames for this STA */
  813. if (no_pending_pkts)
  814. hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  815. else
  816. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  817. dev_queue_xmit(skb);
  818. if (no_pending_pkts)
  819. sta_info_clear_tim_bit(rx->sta);
  820. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  821. } else if (!rx->u.rx.sent_ps_buffered) {
  822. /*
  823. * FIXME: This can be the result of a race condition between
  824. * us expiring a frame and the station polling for it.
  825. * Should we send it a null-func frame indicating we
  826. * have nothing buffered for it?
  827. */
  828. printk(KERN_DEBUG "%s: STA %s sent PS Poll even "
  829. "though there is no buffered frames for it\n",
  830. rx->dev->name, print_mac(mac, rx->sta->addr));
  831. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  832. }
  833. /* Free PS Poll skb here instead of returning RX_DROP that would
  834. * count as an dropped frame. */
  835. dev_kfree_skb(rx->skb);
  836. return RX_QUEUED;
  837. }
  838. static ieee80211_rx_result
  839. ieee80211_rx_h_remove_qos_control(struct ieee80211_txrx_data *rx)
  840. {
  841. u16 fc = rx->fc;
  842. u8 *data = rx->skb->data;
  843. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) data;
  844. if (!WLAN_FC_IS_QOS_DATA(fc))
  845. return RX_CONTINUE;
  846. /* remove the qos control field, update frame type and meta-data */
  847. memmove(data + 2, data, ieee80211_get_hdrlen(fc) - 2);
  848. hdr = (struct ieee80211_hdr *) skb_pull(rx->skb, 2);
  849. /* change frame type to non QOS */
  850. rx->fc = fc &= ~IEEE80211_STYPE_QOS_DATA;
  851. hdr->frame_control = cpu_to_le16(fc);
  852. return RX_CONTINUE;
  853. }
  854. static int
  855. ieee80211_802_1x_port_control(struct ieee80211_txrx_data *rx)
  856. {
  857. if (unlikely(!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED))) {
  858. #ifdef CONFIG_MAC80211_DEBUG
  859. if (net_ratelimit())
  860. printk(KERN_DEBUG "%s: dropped frame "
  861. "(unauthorized port)\n", rx->dev->name);
  862. #endif /* CONFIG_MAC80211_DEBUG */
  863. return -EACCES;
  864. }
  865. return 0;
  866. }
  867. static int
  868. ieee80211_drop_unencrypted(struct ieee80211_txrx_data *rx)
  869. {
  870. /*
  871. * Pass through unencrypted frames if the hardware has
  872. * decrypted them already.
  873. */
  874. if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED)
  875. return 0;
  876. /* Drop unencrypted frames if key is set. */
  877. if (unlikely(!(rx->fc & IEEE80211_FCTL_PROTECTED) &&
  878. (rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
  879. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_NULLFUNC &&
  880. (rx->key || rx->sdata->drop_unencrypted))) {
  881. if (net_ratelimit())
  882. printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
  883. "encryption\n", rx->dev->name);
  884. return -EACCES;
  885. }
  886. return 0;
  887. }
  888. static int
  889. ieee80211_data_to_8023(struct ieee80211_txrx_data *rx)
  890. {
  891. struct net_device *dev = rx->dev;
  892. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  893. u16 fc, hdrlen, ethertype;
  894. u8 *payload;
  895. u8 dst[ETH_ALEN];
  896. u8 src[ETH_ALEN];
  897. struct sk_buff *skb = rx->skb;
  898. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  899. DECLARE_MAC_BUF(mac);
  900. DECLARE_MAC_BUF(mac2);
  901. DECLARE_MAC_BUF(mac3);
  902. DECLARE_MAC_BUF(mac4);
  903. fc = rx->fc;
  904. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  905. return -1;
  906. hdrlen = ieee80211_get_hdrlen(fc);
  907. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  908. * header
  909. * IEEE 802.11 address fields:
  910. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  911. * 0 0 DA SA BSSID n/a
  912. * 0 1 DA BSSID SA n/a
  913. * 1 0 BSSID SA DA n/a
  914. * 1 1 RA TA DA SA
  915. */
  916. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  917. case IEEE80211_FCTL_TODS:
  918. /* BSSID SA DA */
  919. memcpy(dst, hdr->addr3, ETH_ALEN);
  920. memcpy(src, hdr->addr2, ETH_ALEN);
  921. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_AP &&
  922. sdata->vif.type != IEEE80211_IF_TYPE_VLAN)) {
  923. if (net_ratelimit())
  924. printk(KERN_DEBUG "%s: dropped ToDS frame "
  925. "(BSSID=%s SA=%s DA=%s)\n",
  926. dev->name,
  927. print_mac(mac, hdr->addr1),
  928. print_mac(mac2, hdr->addr2),
  929. print_mac(mac3, hdr->addr3));
  930. return -1;
  931. }
  932. break;
  933. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  934. /* RA TA DA SA */
  935. memcpy(dst, hdr->addr3, ETH_ALEN);
  936. memcpy(src, hdr->addr4, ETH_ALEN);
  937. if (unlikely(sdata->vif.type != IEEE80211_IF_TYPE_WDS)) {
  938. if (net_ratelimit())
  939. printk(KERN_DEBUG "%s: dropped FromDS&ToDS "
  940. "frame (RA=%s TA=%s DA=%s SA=%s)\n",
  941. rx->dev->name,
  942. print_mac(mac, hdr->addr1),
  943. print_mac(mac2, hdr->addr2),
  944. print_mac(mac3, hdr->addr3),
  945. print_mac(mac4, hdr->addr4));
  946. return -1;
  947. }
  948. break;
  949. case IEEE80211_FCTL_FROMDS:
  950. /* DA BSSID SA */
  951. memcpy(dst, hdr->addr1, ETH_ALEN);
  952. memcpy(src, hdr->addr3, ETH_ALEN);
  953. if (sdata->vif.type != IEEE80211_IF_TYPE_STA ||
  954. (is_multicast_ether_addr(dst) &&
  955. !compare_ether_addr(src, dev->dev_addr)))
  956. return -1;
  957. break;
  958. case 0:
  959. /* DA SA BSSID */
  960. memcpy(dst, hdr->addr1, ETH_ALEN);
  961. memcpy(src, hdr->addr2, ETH_ALEN);
  962. if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
  963. if (net_ratelimit()) {
  964. printk(KERN_DEBUG "%s: dropped IBSS frame "
  965. "(DA=%s SA=%s BSSID=%s)\n",
  966. dev->name,
  967. print_mac(mac, hdr->addr1),
  968. print_mac(mac2, hdr->addr2),
  969. print_mac(mac3, hdr->addr3));
  970. }
  971. return -1;
  972. }
  973. break;
  974. }
  975. if (unlikely(skb->len - hdrlen < 8)) {
  976. if (net_ratelimit()) {
  977. printk(KERN_DEBUG "%s: RX too short data frame "
  978. "payload\n", dev->name);
  979. }
  980. return -1;
  981. }
  982. payload = skb->data + hdrlen;
  983. ethertype = (payload[6] << 8) | payload[7];
  984. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  985. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  986. compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
  987. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  988. * replace EtherType */
  989. skb_pull(skb, hdrlen + 6);
  990. memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
  991. memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
  992. } else {
  993. struct ethhdr *ehdr;
  994. __be16 len;
  995. skb_pull(skb, hdrlen);
  996. len = htons(skb->len);
  997. ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
  998. memcpy(ehdr->h_dest, dst, ETH_ALEN);
  999. memcpy(ehdr->h_source, src, ETH_ALEN);
  1000. ehdr->h_proto = len;
  1001. }
  1002. return 0;
  1003. }
  1004. /*
  1005. * requires that rx->skb is a frame with ethernet header
  1006. */
  1007. static bool ieee80211_frame_allowed(struct ieee80211_txrx_data *rx)
  1008. {
  1009. static const u8 pae_group_addr[ETH_ALEN]
  1010. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1011. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1012. /*
  1013. * Allow EAPOL frames to us/the PAE group address regardless
  1014. * of whether the frame was encrypted or not.
  1015. */
  1016. if (ehdr->h_proto == htons(ETH_P_PAE) &&
  1017. (compare_ether_addr(ehdr->h_dest, rx->dev->dev_addr) == 0 ||
  1018. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1019. return true;
  1020. if (ieee80211_802_1x_port_control(rx) ||
  1021. ieee80211_drop_unencrypted(rx))
  1022. return false;
  1023. return true;
  1024. }
  1025. /*
  1026. * requires that rx->skb is a frame with ethernet header
  1027. */
  1028. static void
  1029. ieee80211_deliver_skb(struct ieee80211_txrx_data *rx)
  1030. {
  1031. struct net_device *dev = rx->dev;
  1032. struct ieee80211_local *local = rx->local;
  1033. struct sk_buff *skb, *xmit_skb;
  1034. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1035. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1036. struct sta_info *dsta;
  1037. skb = rx->skb;
  1038. xmit_skb = NULL;
  1039. if (local->bridge_packets && (sdata->vif.type == IEEE80211_IF_TYPE_AP ||
  1040. sdata->vif.type == IEEE80211_IF_TYPE_VLAN) &&
  1041. (rx->flags & IEEE80211_TXRXD_RXRA_MATCH)) {
  1042. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1043. /*
  1044. * send multicast frames both to higher layers in
  1045. * local net stack and back to the wireless medium
  1046. */
  1047. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1048. if (!xmit_skb && net_ratelimit())
  1049. printk(KERN_DEBUG "%s: failed to clone "
  1050. "multicast frame\n", dev->name);
  1051. } else {
  1052. dsta = sta_info_get(local, skb->data);
  1053. if (dsta && dsta->dev == dev) {
  1054. /*
  1055. * The destination station is associated to
  1056. * this AP (in this VLAN), so send the frame
  1057. * directly to it and do not pass it to local
  1058. * net stack.
  1059. */
  1060. xmit_skb = skb;
  1061. skb = NULL;
  1062. }
  1063. if (dsta)
  1064. sta_info_put(dsta);
  1065. }
  1066. }
  1067. if (skb) {
  1068. /* deliver to local stack */
  1069. skb->protocol = eth_type_trans(skb, dev);
  1070. memset(skb->cb, 0, sizeof(skb->cb));
  1071. netif_rx(skb);
  1072. }
  1073. if (xmit_skb) {
  1074. /* send to wireless media */
  1075. xmit_skb->protocol = htons(ETH_P_802_3);
  1076. skb_reset_network_header(xmit_skb);
  1077. skb_reset_mac_header(xmit_skb);
  1078. dev_queue_xmit(xmit_skb);
  1079. }
  1080. }
  1081. static ieee80211_rx_result
  1082. ieee80211_rx_h_amsdu(struct ieee80211_txrx_data *rx)
  1083. {
  1084. struct net_device *dev = rx->dev;
  1085. struct ieee80211_local *local = rx->local;
  1086. u16 fc, ethertype;
  1087. u8 *payload;
  1088. struct sk_buff *skb = rx->skb, *frame = NULL;
  1089. const struct ethhdr *eth;
  1090. int remaining, err;
  1091. u8 dst[ETH_ALEN];
  1092. u8 src[ETH_ALEN];
  1093. DECLARE_MAC_BUF(mac);
  1094. fc = rx->fc;
  1095. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1096. return RX_CONTINUE;
  1097. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1098. return RX_DROP_MONITOR;
  1099. if (!(rx->flags & IEEE80211_TXRXD_RX_AMSDU))
  1100. return RX_CONTINUE;
  1101. err = ieee80211_data_to_8023(rx);
  1102. if (unlikely(err))
  1103. return RX_DROP_UNUSABLE;
  1104. skb->dev = dev;
  1105. dev->stats.rx_packets++;
  1106. dev->stats.rx_bytes += skb->len;
  1107. /* skip the wrapping header */
  1108. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  1109. if (!eth)
  1110. return RX_DROP_UNUSABLE;
  1111. while (skb != frame) {
  1112. u8 padding;
  1113. __be16 len = eth->h_proto;
  1114. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  1115. remaining = skb->len;
  1116. memcpy(dst, eth->h_dest, ETH_ALEN);
  1117. memcpy(src, eth->h_source, ETH_ALEN);
  1118. padding = ((4 - subframe_len) & 0x3);
  1119. /* the last MSDU has no padding */
  1120. if (subframe_len > remaining) {
  1121. printk(KERN_DEBUG "%s: wrong buffer size", dev->name);
  1122. return RX_DROP_UNUSABLE;
  1123. }
  1124. skb_pull(skb, sizeof(struct ethhdr));
  1125. /* if last subframe reuse skb */
  1126. if (remaining <= subframe_len + padding)
  1127. frame = skb;
  1128. else {
  1129. frame = dev_alloc_skb(local->hw.extra_tx_headroom +
  1130. subframe_len);
  1131. if (frame == NULL)
  1132. return RX_DROP_UNUSABLE;
  1133. skb_reserve(frame, local->hw.extra_tx_headroom +
  1134. sizeof(struct ethhdr));
  1135. memcpy(skb_put(frame, ntohs(len)), skb->data,
  1136. ntohs(len));
  1137. eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
  1138. padding);
  1139. if (!eth) {
  1140. printk(KERN_DEBUG "%s: wrong buffer size ",
  1141. dev->name);
  1142. dev_kfree_skb(frame);
  1143. return RX_DROP_UNUSABLE;
  1144. }
  1145. }
  1146. skb_reset_network_header(frame);
  1147. frame->dev = dev;
  1148. frame->priority = skb->priority;
  1149. rx->skb = frame;
  1150. payload = frame->data;
  1151. ethertype = (payload[6] << 8) | payload[7];
  1152. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  1153. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1154. compare_ether_addr(payload,
  1155. bridge_tunnel_header) == 0)) {
  1156. /* remove RFC1042 or Bridge-Tunnel
  1157. * encapsulation and replace EtherType */
  1158. skb_pull(frame, 6);
  1159. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1160. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1161. } else {
  1162. memcpy(skb_push(frame, sizeof(__be16)),
  1163. &len, sizeof(__be16));
  1164. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1165. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1166. }
  1167. if (!ieee80211_frame_allowed(rx)) {
  1168. if (skb == frame) /* last frame */
  1169. return RX_DROP_UNUSABLE;
  1170. dev_kfree_skb(frame);
  1171. continue;
  1172. }
  1173. ieee80211_deliver_skb(rx);
  1174. }
  1175. return RX_QUEUED;
  1176. }
  1177. static ieee80211_rx_result
  1178. ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
  1179. {
  1180. struct net_device *dev = rx->dev;
  1181. u16 fc;
  1182. int err;
  1183. fc = rx->fc;
  1184. if (unlikely((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA))
  1185. return RX_CONTINUE;
  1186. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  1187. return RX_DROP_MONITOR;
  1188. err = ieee80211_data_to_8023(rx);
  1189. if (unlikely(err))
  1190. return RX_DROP_UNUSABLE;
  1191. if (!ieee80211_frame_allowed(rx))
  1192. return RX_DROP_MONITOR;
  1193. rx->skb->dev = dev;
  1194. dev->stats.rx_packets++;
  1195. dev->stats.rx_bytes += rx->skb->len;
  1196. ieee80211_deliver_skb(rx);
  1197. return RX_QUEUED;
  1198. }
  1199. static ieee80211_rx_result
  1200. ieee80211_rx_h_ctrl(struct ieee80211_txrx_data *rx)
  1201. {
  1202. struct ieee80211_local *local = rx->local;
  1203. struct ieee80211_hw *hw = &local->hw;
  1204. struct sk_buff *skb = rx->skb;
  1205. struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
  1206. struct tid_ampdu_rx *tid_agg_rx;
  1207. u16 start_seq_num;
  1208. u16 tid;
  1209. if (likely((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_CTL))
  1210. return RX_CONTINUE;
  1211. if ((rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ) {
  1212. if (!rx->sta)
  1213. return RX_CONTINUE;
  1214. tid = le16_to_cpu(bar->control) >> 12;
  1215. tid_agg_rx = &(rx->sta->ampdu_mlme.tid_rx[tid]);
  1216. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1217. return RX_CONTINUE;
  1218. start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
  1219. /* reset session timer */
  1220. if (tid_agg_rx->timeout) {
  1221. unsigned long expires =
  1222. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1223. mod_timer(&tid_agg_rx->session_timer, expires);
  1224. }
  1225. /* manage reordering buffer according to requested */
  1226. /* sequence number */
  1227. rcu_read_lock();
  1228. ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, NULL,
  1229. start_seq_num, 1);
  1230. rcu_read_unlock();
  1231. return RX_DROP_UNUSABLE;
  1232. }
  1233. return RX_CONTINUE;
  1234. }
  1235. static ieee80211_rx_result
  1236. ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
  1237. {
  1238. struct ieee80211_sub_if_data *sdata;
  1239. if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
  1240. return RX_DROP_MONITOR;
  1241. sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
  1242. if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  1243. sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
  1244. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  1245. ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
  1246. else
  1247. return RX_DROP_MONITOR;
  1248. return RX_QUEUED;
  1249. }
  1250. static void ieee80211_rx_michael_mic_report(struct net_device *dev,
  1251. struct ieee80211_hdr *hdr,
  1252. struct ieee80211_txrx_data *rx)
  1253. {
  1254. int keyidx, hdrlen;
  1255. DECLARE_MAC_BUF(mac);
  1256. DECLARE_MAC_BUF(mac2);
  1257. hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
  1258. if (rx->skb->len >= hdrlen + 4)
  1259. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  1260. else
  1261. keyidx = -1;
  1262. if (net_ratelimit())
  1263. printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
  1264. "failure from %s to %s keyidx=%d\n",
  1265. dev->name, print_mac(mac, hdr->addr2),
  1266. print_mac(mac2, hdr->addr1), keyidx);
  1267. if (!rx->sta) {
  1268. /*
  1269. * Some hardware seem to generate incorrect Michael MIC
  1270. * reports; ignore them to avoid triggering countermeasures.
  1271. */
  1272. if (net_ratelimit())
  1273. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1274. "error for unknown address %s\n",
  1275. dev->name, print_mac(mac, hdr->addr2));
  1276. goto ignore;
  1277. }
  1278. if (!(rx->fc & IEEE80211_FCTL_PROTECTED)) {
  1279. if (net_ratelimit())
  1280. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1281. "error for a frame with no PROTECTED flag (src "
  1282. "%s)\n", dev->name, print_mac(mac, hdr->addr2));
  1283. goto ignore;
  1284. }
  1285. if (rx->sdata->vif.type == IEEE80211_IF_TYPE_AP && keyidx) {
  1286. /*
  1287. * APs with pairwise keys should never receive Michael MIC
  1288. * errors for non-zero keyidx because these are reserved for
  1289. * group keys and only the AP is sending real multicast
  1290. * frames in the BSS.
  1291. */
  1292. if (net_ratelimit())
  1293. printk(KERN_DEBUG "%s: ignored Michael MIC error for "
  1294. "a frame with non-zero keyidx (%d)"
  1295. " (src %s)\n", dev->name, keyidx,
  1296. print_mac(mac, hdr->addr2));
  1297. goto ignore;
  1298. }
  1299. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA &&
  1300. ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
  1301. (rx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_AUTH)) {
  1302. if (net_ratelimit())
  1303. printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
  1304. "error for a frame that cannot be encrypted "
  1305. "(fc=0x%04x) (src %s)\n",
  1306. dev->name, rx->fc, print_mac(mac, hdr->addr2));
  1307. goto ignore;
  1308. }
  1309. mac80211_ev_michael_mic_failure(rx->dev, keyidx, hdr);
  1310. ignore:
  1311. dev_kfree_skb(rx->skb);
  1312. rx->skb = NULL;
  1313. }
  1314. static void ieee80211_rx_cooked_monitor(struct ieee80211_txrx_data *rx)
  1315. {
  1316. struct ieee80211_sub_if_data *sdata;
  1317. struct ieee80211_local *local = rx->local;
  1318. struct ieee80211_rtap_hdr {
  1319. struct ieee80211_radiotap_header hdr;
  1320. u8 flags;
  1321. u8 rate;
  1322. __le16 chan_freq;
  1323. __le16 chan_flags;
  1324. } __attribute__ ((packed)) *rthdr;
  1325. struct sk_buff *skb = rx->skb, *skb2;
  1326. struct net_device *prev_dev = NULL;
  1327. struct ieee80211_rx_status *status = rx->u.rx.status;
  1328. if (rx->flags & IEEE80211_TXRXD_RX_CMNTR_REPORTED)
  1329. goto out_free_skb;
  1330. if (skb_headroom(skb) < sizeof(*rthdr) &&
  1331. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  1332. goto out_free_skb;
  1333. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  1334. memset(rthdr, 0, sizeof(*rthdr));
  1335. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1336. rthdr->hdr.it_present =
  1337. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  1338. (1 << IEEE80211_RADIOTAP_RATE) |
  1339. (1 << IEEE80211_RADIOTAP_CHANNEL));
  1340. rthdr->rate = rx->u.rx.rate->bitrate / 5;
  1341. rthdr->chan_freq = cpu_to_le16(status->freq);
  1342. if (status->band == IEEE80211_BAND_5GHZ)
  1343. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  1344. IEEE80211_CHAN_5GHZ);
  1345. else
  1346. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  1347. IEEE80211_CHAN_2GHZ);
  1348. skb_set_mac_header(skb, 0);
  1349. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1350. skb->pkt_type = PACKET_OTHERHOST;
  1351. skb->protocol = htons(ETH_P_802_2);
  1352. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1353. if (!netif_running(sdata->dev))
  1354. continue;
  1355. if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR ||
  1356. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  1357. continue;
  1358. if (prev_dev) {
  1359. skb2 = skb_clone(skb, GFP_ATOMIC);
  1360. if (skb2) {
  1361. skb2->dev = prev_dev;
  1362. netif_rx(skb2);
  1363. }
  1364. }
  1365. prev_dev = sdata->dev;
  1366. sdata->dev->stats.rx_packets++;
  1367. sdata->dev->stats.rx_bytes += skb->len;
  1368. }
  1369. if (prev_dev) {
  1370. skb->dev = prev_dev;
  1371. netif_rx(skb);
  1372. skb = NULL;
  1373. } else
  1374. goto out_free_skb;
  1375. rx->flags |= IEEE80211_TXRXD_RX_CMNTR_REPORTED;
  1376. return;
  1377. out_free_skb:
  1378. dev_kfree_skb(skb);
  1379. }
  1380. typedef ieee80211_rx_result (*ieee80211_rx_handler)(struct ieee80211_txrx_data *);
  1381. static ieee80211_rx_handler ieee80211_rx_handlers[] =
  1382. {
  1383. ieee80211_rx_h_if_stats,
  1384. ieee80211_rx_h_passive_scan,
  1385. ieee80211_rx_h_check,
  1386. ieee80211_rx_h_decrypt,
  1387. ieee80211_rx_h_sta_process,
  1388. ieee80211_rx_h_defragment,
  1389. ieee80211_rx_h_ps_poll,
  1390. ieee80211_rx_h_michael_mic_verify,
  1391. /* this must be after decryption - so header is counted in MPDU mic
  1392. * must be before pae and data, so QOS_DATA format frames
  1393. * are not passed to user space by these functions
  1394. */
  1395. ieee80211_rx_h_remove_qos_control,
  1396. ieee80211_rx_h_amsdu,
  1397. ieee80211_rx_h_data,
  1398. ieee80211_rx_h_ctrl,
  1399. ieee80211_rx_h_mgmt,
  1400. NULL
  1401. };
  1402. static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
  1403. struct ieee80211_txrx_data *rx,
  1404. struct sk_buff *skb)
  1405. {
  1406. ieee80211_rx_handler *handler;
  1407. ieee80211_rx_result res = RX_DROP_MONITOR;
  1408. rx->skb = skb;
  1409. rx->sdata = sdata;
  1410. rx->dev = sdata->dev;
  1411. for (handler = ieee80211_rx_handlers; *handler != NULL; handler++) {
  1412. res = (*handler)(rx);
  1413. switch (res) {
  1414. case RX_CONTINUE:
  1415. continue;
  1416. case RX_DROP_UNUSABLE:
  1417. case RX_DROP_MONITOR:
  1418. I802_DEBUG_INC(sdata->local->rx_handlers_drop);
  1419. if (rx->sta)
  1420. rx->sta->rx_dropped++;
  1421. break;
  1422. case RX_QUEUED:
  1423. I802_DEBUG_INC(sdata->local->rx_handlers_queued);
  1424. break;
  1425. }
  1426. break;
  1427. }
  1428. switch (res) {
  1429. case RX_CONTINUE:
  1430. case RX_DROP_MONITOR:
  1431. ieee80211_rx_cooked_monitor(rx);
  1432. break;
  1433. case RX_DROP_UNUSABLE:
  1434. dev_kfree_skb(rx->skb);
  1435. break;
  1436. }
  1437. }
  1438. /* main receive path */
  1439. static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
  1440. u8 *bssid, struct ieee80211_txrx_data *rx,
  1441. struct ieee80211_hdr *hdr)
  1442. {
  1443. int multicast = is_multicast_ether_addr(hdr->addr1);
  1444. switch (sdata->vif.type) {
  1445. case IEEE80211_IF_TYPE_STA:
  1446. if (!bssid)
  1447. return 0;
  1448. if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1449. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1450. return 0;
  1451. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1452. } else if (!multicast &&
  1453. compare_ether_addr(sdata->dev->dev_addr,
  1454. hdr->addr1) != 0) {
  1455. if (!(sdata->dev->flags & IFF_PROMISC))
  1456. return 0;
  1457. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1458. }
  1459. break;
  1460. case IEEE80211_IF_TYPE_IBSS:
  1461. if (!bssid)
  1462. return 0;
  1463. if ((rx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
  1464. (rx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
  1465. return 1;
  1466. else if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid)) {
  1467. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1468. return 0;
  1469. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1470. } else if (!multicast &&
  1471. compare_ether_addr(sdata->dev->dev_addr,
  1472. hdr->addr1) != 0) {
  1473. if (!(sdata->dev->flags & IFF_PROMISC))
  1474. return 0;
  1475. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1476. } else if (!rx->sta)
  1477. rx->sta = ieee80211_ibss_add_sta(sdata->dev, rx->skb,
  1478. bssid, hdr->addr2);
  1479. break;
  1480. case IEEE80211_IF_TYPE_VLAN:
  1481. case IEEE80211_IF_TYPE_AP:
  1482. if (!bssid) {
  1483. if (compare_ether_addr(sdata->dev->dev_addr,
  1484. hdr->addr1))
  1485. return 0;
  1486. } else if (!ieee80211_bssid_match(bssid,
  1487. sdata->dev->dev_addr)) {
  1488. if (!(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1489. return 0;
  1490. rx->flags &= ~IEEE80211_TXRXD_RXRA_MATCH;
  1491. }
  1492. if (sdata->dev == sdata->local->mdev &&
  1493. !(rx->flags & IEEE80211_TXRXD_RXIN_SCAN))
  1494. /* do not receive anything via
  1495. * master device when not scanning */
  1496. return 0;
  1497. break;
  1498. case IEEE80211_IF_TYPE_WDS:
  1499. if (bssid ||
  1500. (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  1501. return 0;
  1502. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  1503. return 0;
  1504. break;
  1505. case IEEE80211_IF_TYPE_MNTR:
  1506. /* take everything */
  1507. break;
  1508. case IEEE80211_IF_TYPE_INVALID:
  1509. /* should never get here */
  1510. WARN_ON(1);
  1511. break;
  1512. }
  1513. return 1;
  1514. }
  1515. /*
  1516. * This is the actual Rx frames handler. as it blongs to Rx path it must
  1517. * be called with rcu_read_lock protection.
  1518. */
  1519. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  1520. struct sk_buff *skb,
  1521. struct ieee80211_rx_status *status,
  1522. u32 load,
  1523. struct ieee80211_rate *rate)
  1524. {
  1525. struct ieee80211_local *local = hw_to_local(hw);
  1526. struct ieee80211_sub_if_data *sdata;
  1527. struct ieee80211_hdr *hdr;
  1528. struct ieee80211_txrx_data rx;
  1529. u16 type;
  1530. int prepares;
  1531. struct ieee80211_sub_if_data *prev = NULL;
  1532. struct sk_buff *skb_new;
  1533. u8 *bssid;
  1534. hdr = (struct ieee80211_hdr *) skb->data;
  1535. memset(&rx, 0, sizeof(rx));
  1536. rx.skb = skb;
  1537. rx.local = local;
  1538. rx.u.rx.status = status;
  1539. rx.u.rx.load = load;
  1540. rx.u.rx.rate = rate;
  1541. rx.fc = le16_to_cpu(hdr->frame_control);
  1542. type = rx.fc & IEEE80211_FCTL_FTYPE;
  1543. if (type == IEEE80211_FTYPE_DATA || type == IEEE80211_FTYPE_MGMT)
  1544. local->dot11ReceivedFragmentCount++;
  1545. rx.sta = sta_info_get(local, hdr->addr2);
  1546. if (rx.sta) {
  1547. rx.dev = rx.sta->dev;
  1548. rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
  1549. }
  1550. if ((status->flag & RX_FLAG_MMIC_ERROR)) {
  1551. ieee80211_rx_michael_mic_report(local->mdev, hdr, &rx);
  1552. goto end;
  1553. }
  1554. if (unlikely(local->sta_sw_scanning || local->sta_hw_scanning))
  1555. rx.flags |= IEEE80211_TXRXD_RXIN_SCAN;
  1556. ieee80211_parse_qos(&rx);
  1557. ieee80211_verify_ip_alignment(&rx);
  1558. skb = rx.skb;
  1559. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1560. if (!netif_running(sdata->dev))
  1561. continue;
  1562. if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR)
  1563. continue;
  1564. bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  1565. rx.flags |= IEEE80211_TXRXD_RXRA_MATCH;
  1566. prepares = prepare_for_handlers(sdata, bssid, &rx, hdr);
  1567. if (!prepares)
  1568. continue;
  1569. /*
  1570. * frame is destined for this interface, but if it's not
  1571. * also for the previous one we handle that after the
  1572. * loop to avoid copying the SKB once too much
  1573. */
  1574. if (!prev) {
  1575. prev = sdata;
  1576. continue;
  1577. }
  1578. /*
  1579. * frame was destined for the previous interface
  1580. * so invoke RX handlers for it
  1581. */
  1582. skb_new = skb_copy(skb, GFP_ATOMIC);
  1583. if (!skb_new) {
  1584. if (net_ratelimit())
  1585. printk(KERN_DEBUG "%s: failed to copy "
  1586. "multicast frame for %s",
  1587. wiphy_name(local->hw.wiphy),
  1588. prev->dev->name);
  1589. continue;
  1590. }
  1591. rx.fc = le16_to_cpu(hdr->frame_control);
  1592. ieee80211_invoke_rx_handlers(prev, &rx, skb_new);
  1593. prev = sdata;
  1594. }
  1595. if (prev) {
  1596. rx.fc = le16_to_cpu(hdr->frame_control);
  1597. ieee80211_invoke_rx_handlers(prev, &rx, skb);
  1598. } else
  1599. dev_kfree_skb(skb);
  1600. end:
  1601. if (rx.sta)
  1602. sta_info_put(rx.sta);
  1603. }
  1604. #define SEQ_MODULO 0x1000
  1605. #define SEQ_MASK 0xfff
  1606. static inline int seq_less(u16 sq1, u16 sq2)
  1607. {
  1608. return (((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1));
  1609. }
  1610. static inline u16 seq_inc(u16 sq)
  1611. {
  1612. return ((sq + 1) & SEQ_MASK);
  1613. }
  1614. static inline u16 seq_sub(u16 sq1, u16 sq2)
  1615. {
  1616. return ((sq1 - sq2) & SEQ_MASK);
  1617. }
  1618. /*
  1619. * As it function blongs to Rx path it must be called with
  1620. * the proper rcu_read_lock protection for its flow.
  1621. */
  1622. u8 ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  1623. struct tid_ampdu_rx *tid_agg_rx,
  1624. struct sk_buff *skb, u16 mpdu_seq_num,
  1625. int bar_req)
  1626. {
  1627. struct ieee80211_local *local = hw_to_local(hw);
  1628. struct ieee80211_rx_status status;
  1629. u16 head_seq_num, buf_size;
  1630. int index;
  1631. u32 pkt_load;
  1632. struct ieee80211_supported_band *sband;
  1633. struct ieee80211_rate *rate;
  1634. buf_size = tid_agg_rx->buf_size;
  1635. head_seq_num = tid_agg_rx->head_seq_num;
  1636. /* frame with out of date sequence number */
  1637. if (seq_less(mpdu_seq_num, head_seq_num)) {
  1638. dev_kfree_skb(skb);
  1639. return 1;
  1640. }
  1641. /* if frame sequence number exceeds our buffering window size or
  1642. * block Ack Request arrived - release stored frames */
  1643. if ((!seq_less(mpdu_seq_num, head_seq_num + buf_size)) || (bar_req)) {
  1644. /* new head to the ordering buffer */
  1645. if (bar_req)
  1646. head_seq_num = mpdu_seq_num;
  1647. else
  1648. head_seq_num =
  1649. seq_inc(seq_sub(mpdu_seq_num, buf_size));
  1650. /* release stored frames up to new head to stack */
  1651. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  1652. index = seq_sub(tid_agg_rx->head_seq_num,
  1653. tid_agg_rx->ssn)
  1654. % tid_agg_rx->buf_size;
  1655. if (tid_agg_rx->reorder_buf[index]) {
  1656. /* release the reordered frames to stack */
  1657. memcpy(&status,
  1658. tid_agg_rx->reorder_buf[index]->cb,
  1659. sizeof(status));
  1660. sband = local->hw.wiphy->bands[status.band];
  1661. rate = &sband->bitrates[status.rate_idx];
  1662. pkt_load = ieee80211_rx_load_stats(local,
  1663. tid_agg_rx->reorder_buf[index],
  1664. &status, rate);
  1665. __ieee80211_rx_handle_packet(hw,
  1666. tid_agg_rx->reorder_buf[index],
  1667. &status, pkt_load, rate);
  1668. tid_agg_rx->stored_mpdu_num--;
  1669. tid_agg_rx->reorder_buf[index] = NULL;
  1670. }
  1671. tid_agg_rx->head_seq_num =
  1672. seq_inc(tid_agg_rx->head_seq_num);
  1673. }
  1674. if (bar_req)
  1675. return 1;
  1676. }
  1677. /* now the new frame is always in the range of the reordering */
  1678. /* buffer window */
  1679. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn)
  1680. % tid_agg_rx->buf_size;
  1681. /* check if we already stored this frame */
  1682. if (tid_agg_rx->reorder_buf[index]) {
  1683. dev_kfree_skb(skb);
  1684. return 1;
  1685. }
  1686. /* if arrived mpdu is in the right order and nothing else stored */
  1687. /* release it immediately */
  1688. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1689. tid_agg_rx->stored_mpdu_num == 0) {
  1690. tid_agg_rx->head_seq_num =
  1691. seq_inc(tid_agg_rx->head_seq_num);
  1692. return 0;
  1693. }
  1694. /* put the frame in the reordering buffer */
  1695. tid_agg_rx->reorder_buf[index] = skb;
  1696. tid_agg_rx->stored_mpdu_num++;
  1697. /* release the buffer until next missing frame */
  1698. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn)
  1699. % tid_agg_rx->buf_size;
  1700. while (tid_agg_rx->reorder_buf[index]) {
  1701. /* release the reordered frame back to stack */
  1702. memcpy(&status, tid_agg_rx->reorder_buf[index]->cb,
  1703. sizeof(status));
  1704. sband = local->hw.wiphy->bands[status.band];
  1705. rate = &sband->bitrates[status.rate_idx];
  1706. pkt_load = ieee80211_rx_load_stats(local,
  1707. tid_agg_rx->reorder_buf[index],
  1708. &status, rate);
  1709. __ieee80211_rx_handle_packet(hw, tid_agg_rx->reorder_buf[index],
  1710. &status, pkt_load, rate);
  1711. tid_agg_rx->stored_mpdu_num--;
  1712. tid_agg_rx->reorder_buf[index] = NULL;
  1713. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  1714. index = seq_sub(tid_agg_rx->head_seq_num,
  1715. tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  1716. }
  1717. return 1;
  1718. }
  1719. static u8 ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
  1720. struct sk_buff *skb)
  1721. {
  1722. struct ieee80211_hw *hw = &local->hw;
  1723. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1724. struct sta_info *sta;
  1725. struct tid_ampdu_rx *tid_agg_rx;
  1726. u16 fc, sc;
  1727. u16 mpdu_seq_num;
  1728. u8 ret = 0, *qc;
  1729. int tid;
  1730. sta = sta_info_get(local, hdr->addr2);
  1731. if (!sta)
  1732. return ret;
  1733. fc = le16_to_cpu(hdr->frame_control);
  1734. /* filter the QoS data rx stream according to
  1735. * STA/TID and check if this STA/TID is on aggregation */
  1736. if (!WLAN_FC_IS_QOS_DATA(fc))
  1737. goto end_reorder;
  1738. qc = skb->data + ieee80211_get_hdrlen(fc) - QOS_CONTROL_LEN;
  1739. tid = qc[0] & QOS_CONTROL_TID_MASK;
  1740. tid_agg_rx = &(sta->ampdu_mlme.tid_rx[tid]);
  1741. if (tid_agg_rx->state != HT_AGG_STATE_OPERATIONAL)
  1742. goto end_reorder;
  1743. /* null data frames are excluded */
  1744. if (unlikely(fc & IEEE80211_STYPE_NULLFUNC))
  1745. goto end_reorder;
  1746. /* new un-ordered ampdu frame - process it */
  1747. /* reset session timer */
  1748. if (tid_agg_rx->timeout) {
  1749. unsigned long expires =
  1750. jiffies + (tid_agg_rx->timeout / 1000) * HZ;
  1751. mod_timer(&tid_agg_rx->session_timer, expires);
  1752. }
  1753. /* if this mpdu is fragmented - terminate rx aggregation session */
  1754. sc = le16_to_cpu(hdr->seq_ctrl);
  1755. if (sc & IEEE80211_SCTL_FRAG) {
  1756. ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr,
  1757. tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
  1758. ret = 1;
  1759. goto end_reorder;
  1760. }
  1761. /* according to mpdu sequence number deal with reordering buffer */
  1762. mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1763. ret = ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb,
  1764. mpdu_seq_num, 0);
  1765. end_reorder:
  1766. if (sta)
  1767. sta_info_put(sta);
  1768. return ret;
  1769. }
  1770. /*
  1771. * This is the receive path handler. It is called by a low level driver when an
  1772. * 802.11 MPDU is received from the hardware.
  1773. */
  1774. void __ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
  1775. struct ieee80211_rx_status *status)
  1776. {
  1777. struct ieee80211_local *local = hw_to_local(hw);
  1778. u32 pkt_load;
  1779. struct ieee80211_rate *rate = NULL;
  1780. struct ieee80211_supported_band *sband;
  1781. if (status->band < 0 ||
  1782. status->band > IEEE80211_NUM_BANDS) {
  1783. WARN_ON(1);
  1784. return;
  1785. }
  1786. sband = local->hw.wiphy->bands[status->band];
  1787. if (!sband ||
  1788. status->rate_idx < 0 ||
  1789. status->rate_idx >= sband->n_bitrates) {
  1790. WARN_ON(1);
  1791. return;
  1792. }
  1793. rate = &sband->bitrates[status->rate_idx];
  1794. /*
  1795. * key references and virtual interfaces are protected using RCU
  1796. * and this requires that we are in a read-side RCU section during
  1797. * receive processing
  1798. */
  1799. rcu_read_lock();
  1800. /*
  1801. * Frames with failed FCS/PLCP checksum are not returned,
  1802. * all other frames are returned without radiotap header
  1803. * if it was previously present.
  1804. * Also, frames with less than 16 bytes are dropped.
  1805. */
  1806. skb = ieee80211_rx_monitor(local, skb, status, rate);
  1807. if (!skb) {
  1808. rcu_read_unlock();
  1809. return;
  1810. }
  1811. pkt_load = ieee80211_rx_load_stats(local, skb, status, rate);
  1812. local->channel_use_raw += pkt_load;
  1813. if (!ieee80211_rx_reorder_ampdu(local, skb))
  1814. __ieee80211_rx_handle_packet(hw, skb, status, pkt_load, rate);
  1815. rcu_read_unlock();
  1816. }
  1817. EXPORT_SYMBOL(__ieee80211_rx);
  1818. /* This is a version of the rx handler that can be called from hard irq
  1819. * context. Post the skb on the queue and schedule the tasklet */
  1820. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb,
  1821. struct ieee80211_rx_status *status)
  1822. {
  1823. struct ieee80211_local *local = hw_to_local(hw);
  1824. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  1825. skb->dev = local->mdev;
  1826. /* copy status into skb->cb for use by tasklet */
  1827. memcpy(skb->cb, status, sizeof(*status));
  1828. skb->pkt_type = IEEE80211_RX_MSG;
  1829. skb_queue_tail(&local->skb_queue, skb);
  1830. tasklet_schedule(&local->tasklet);
  1831. }
  1832. EXPORT_SYMBOL(ieee80211_rx_irqsafe);