rx.c 63 KB

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