rx.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  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 "driver-ops.h"
  21. #include "led.h"
  22. #include "mesh.h"
  23. #include "wep.h"
  24. #include "wpa.h"
  25. #include "tkip.h"
  26. #include "wme.h"
  27. static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
  28. struct tid_ampdu_rx *tid_agg_rx,
  29. u16 head_seq_num);
  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. {
  39. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) {
  40. if (likely(skb->len > FCS_LEN))
  41. skb_trim(skb, skb->len - FCS_LEN);
  42. else {
  43. /* driver bug */
  44. WARN_ON(1);
  45. dev_kfree_skb(skb);
  46. skb = NULL;
  47. }
  48. }
  49. return skb;
  50. }
  51. static inline int should_drop_frame(struct sk_buff *skb,
  52. int present_fcs_len)
  53. {
  54. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  55. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  56. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  57. return 1;
  58. if (unlikely(skb->len < 16 + present_fcs_len))
  59. return 1;
  60. if (ieee80211_is_ctl(hdr->frame_control) &&
  61. !ieee80211_is_pspoll(hdr->frame_control) &&
  62. !ieee80211_is_back_req(hdr->frame_control))
  63. return 1;
  64. return 0;
  65. }
  66. static int
  67. ieee80211_rx_radiotap_len(struct ieee80211_local *local,
  68. struct ieee80211_rx_status *status)
  69. {
  70. int len;
  71. /* always present fields */
  72. len = sizeof(struct ieee80211_radiotap_header) + 9;
  73. if (status->flag & RX_FLAG_TSFT)
  74. len += 8;
  75. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  76. len += 1;
  77. if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
  78. len += 1;
  79. if (len & 1) /* padding for RX_FLAGS if necessary */
  80. len++;
  81. return len;
  82. }
  83. /*
  84. * ieee80211_add_rx_radiotap_header - add radiotap header
  85. *
  86. * add a radiotap header containing all the fields which the hardware provided.
  87. */
  88. static void
  89. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  90. struct sk_buff *skb,
  91. struct ieee80211_rate *rate,
  92. int rtap_len)
  93. {
  94. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  95. struct ieee80211_radiotap_header *rthdr;
  96. unsigned char *pos;
  97. u16 rx_flags = 0;
  98. rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
  99. memset(rthdr, 0, rtap_len);
  100. /* radiotap header, set always present flags */
  101. rthdr->it_present =
  102. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  103. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  104. (1 << IEEE80211_RADIOTAP_ANTENNA) |
  105. (1 << IEEE80211_RADIOTAP_RX_FLAGS));
  106. rthdr->it_len = cpu_to_le16(rtap_len);
  107. pos = (unsigned char *)(rthdr+1);
  108. /* the order of the following fields is important */
  109. /* IEEE80211_RADIOTAP_TSFT */
  110. if (status->flag & RX_FLAG_TSFT) {
  111. put_unaligned_le64(status->mactime, pos);
  112. rthdr->it_present |=
  113. cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  114. pos += 8;
  115. }
  116. /* IEEE80211_RADIOTAP_FLAGS */
  117. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  118. *pos |= IEEE80211_RADIOTAP_F_FCS;
  119. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  120. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  121. if (status->flag & RX_FLAG_SHORTPRE)
  122. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  123. pos++;
  124. /* IEEE80211_RADIOTAP_RATE */
  125. if (status->flag & RX_FLAG_HT) {
  126. /*
  127. * TODO: add following information into radiotap header once
  128. * suitable fields are defined for it:
  129. * - MCS index (status->rate_idx)
  130. * - HT40 (status->flag & RX_FLAG_40MHZ)
  131. * - short-GI (status->flag & RX_FLAG_SHORT_GI)
  132. */
  133. *pos = 0;
  134. } else {
  135. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  136. *pos = rate->bitrate / 5;
  137. }
  138. pos++;
  139. /* IEEE80211_RADIOTAP_CHANNEL */
  140. put_unaligned_le16(status->freq, pos);
  141. pos += 2;
  142. if (status->band == IEEE80211_BAND_5GHZ)
  143. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
  144. pos);
  145. else if (status->flag & RX_FLAG_HT)
  146. put_unaligned_le16(IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ,
  147. pos);
  148. else if (rate->flags & IEEE80211_RATE_ERP_G)
  149. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
  150. pos);
  151. else
  152. put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
  153. pos);
  154. pos += 2;
  155. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  156. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  157. *pos = status->signal;
  158. rthdr->it_present |=
  159. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  160. pos++;
  161. }
  162. /* IEEE80211_RADIOTAP_DBM_ANTNOISE */
  163. if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
  164. *pos = status->noise;
  165. rthdr->it_present |=
  166. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTNOISE);
  167. pos++;
  168. }
  169. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  170. /* IEEE80211_RADIOTAP_ANTENNA */
  171. *pos = status->antenna;
  172. pos++;
  173. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  174. /* IEEE80211_RADIOTAP_RX_FLAGS */
  175. /* ensure 2 byte alignment for the 2 byte field as required */
  176. if ((pos - (u8 *)rthdr) & 1)
  177. pos++;
  178. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  179. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  180. put_unaligned_le16(rx_flags, pos);
  181. pos += 2;
  182. }
  183. /*
  184. * This function copies a received frame to all monitor interfaces and
  185. * returns a cleaned-up SKB that no longer includes the FCS nor the
  186. * radiotap header the driver might have added.
  187. */
  188. static struct sk_buff *
  189. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  190. struct ieee80211_rate *rate)
  191. {
  192. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  193. struct ieee80211_sub_if_data *sdata;
  194. int needed_headroom = 0;
  195. struct sk_buff *skb, *skb2;
  196. struct net_device *prev_dev = NULL;
  197. int present_fcs_len = 0;
  198. /*
  199. * First, we may need to make a copy of the skb because
  200. * (1) we need to modify it for radiotap (if not present), and
  201. * (2) the other RX handlers will modify the skb we got.
  202. *
  203. * We don't need to, of course, if we aren't going to return
  204. * the SKB because it has a bad FCS/PLCP checksum.
  205. */
  206. /* room for the radiotap header based on driver features */
  207. needed_headroom = ieee80211_rx_radiotap_len(local, status);
  208. if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
  209. present_fcs_len = FCS_LEN;
  210. if (!local->monitors) {
  211. if (should_drop_frame(origskb, present_fcs_len)) {
  212. dev_kfree_skb(origskb);
  213. return NULL;
  214. }
  215. return remove_monitor_info(local, origskb);
  216. }
  217. if (should_drop_frame(origskb, present_fcs_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);
  240. if (!skb)
  241. return origskb;
  242. }
  243. /* prepend radiotap information */
  244. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom);
  245. skb_reset_mac_header(skb);
  246. skb->ip_summed = CHECKSUM_UNNECESSARY;
  247. skb->pkt_type = PACKET_OTHERHOST;
  248. skb->protocol = htons(ETH_P_802_2);
  249. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  250. if (!netif_running(sdata->dev))
  251. continue;
  252. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  253. continue;
  254. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
  255. continue;
  256. if (prev_dev) {
  257. skb2 = skb_clone(skb, GFP_ATOMIC);
  258. if (skb2) {
  259. skb2->dev = prev_dev;
  260. netif_rx(skb2);
  261. }
  262. }
  263. prev_dev = sdata->dev;
  264. sdata->dev->stats.rx_packets++;
  265. sdata->dev->stats.rx_bytes += skb->len;
  266. }
  267. if (prev_dev) {
  268. skb->dev = prev_dev;
  269. netif_rx(skb);
  270. } else
  271. dev_kfree_skb(skb);
  272. return origskb;
  273. }
  274. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  275. {
  276. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  277. int tid;
  278. /* does the frame have a qos control field? */
  279. if (ieee80211_is_data_qos(hdr->frame_control)) {
  280. u8 *qc = ieee80211_get_qos_ctl(hdr);
  281. /* frame has qos control */
  282. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  283. if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  284. rx->flags |= IEEE80211_RX_AMSDU;
  285. else
  286. rx->flags &= ~IEEE80211_RX_AMSDU;
  287. } else {
  288. /*
  289. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  290. *
  291. * Sequence numbers for management frames, QoS data
  292. * frames with a broadcast/multicast address in the
  293. * Address 1 field, and all non-QoS data frames sent
  294. * by QoS STAs are assigned using an additional single
  295. * modulo-4096 counter, [...]
  296. *
  297. * We also use that counter for non-QoS STAs.
  298. */
  299. tid = NUM_RX_DATA_QUEUES - 1;
  300. }
  301. rx->queue = tid;
  302. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  303. * For now, set skb->priority to 0 for other cases. */
  304. rx->skb->priority = (tid > 7) ? 0 : tid;
  305. }
  306. /**
  307. * DOC: Packet alignment
  308. *
  309. * Drivers always need to pass packets that are aligned to two-byte boundaries
  310. * to the stack.
  311. *
  312. * Additionally, should, if possible, align the payload data in a way that
  313. * guarantees that the contained IP header is aligned to a four-byte
  314. * boundary. In the case of regular frames, this simply means aligning the
  315. * payload to a four-byte boundary (because either the IP header is directly
  316. * contained, or IV/RFC1042 headers that have a length divisible by four are
  317. * in front of it).
  318. *
  319. * With A-MSDU frames, however, the payload data address must yield two modulo
  320. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  321. * push the IP header further back to a multiple of four again. Thankfully, the
  322. * specs were sane enough this time around to require padding each A-MSDU
  323. * subframe to a length that is a multiple of four.
  324. *
  325. * Padding like Atheros hardware adds which is inbetween the 802.11 header and
  326. * the payload is not supported, the driver is required to move the 802.11
  327. * header to be directly in front of the payload in that case.
  328. */
  329. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  330. {
  331. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  332. int hdrlen;
  333. #ifndef CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT
  334. return;
  335. #endif
  336. if (WARN_ONCE((unsigned long)rx->skb->data & 1,
  337. "unaligned packet at 0x%p\n", rx->skb->data))
  338. return;
  339. if (!ieee80211_is_data_present(hdr->frame_control))
  340. return;
  341. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  342. if (rx->flags & IEEE80211_RX_AMSDU)
  343. hdrlen += ETH_HLEN;
  344. WARN_ONCE(((unsigned long)(rx->skb->data + hdrlen)) & 3,
  345. "unaligned IP payload at 0x%p\n", rx->skb->data + hdrlen);
  346. }
  347. /* rx handlers */
  348. static ieee80211_rx_result debug_noinline
  349. ieee80211_rx_h_passive_scan(struct ieee80211_rx_data *rx)
  350. {
  351. struct ieee80211_local *local = rx->local;
  352. struct sk_buff *skb = rx->skb;
  353. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning)))
  354. return ieee80211_scan_rx(rx->sdata, skb);
  355. if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning) &&
  356. (rx->flags & IEEE80211_RX_IN_SCAN))) {
  357. /* drop all the other packets during a software scan anyway */
  358. if (ieee80211_scan_rx(rx->sdata, skb) != RX_QUEUED)
  359. dev_kfree_skb(skb);
  360. return RX_QUEUED;
  361. }
  362. if (unlikely(rx->flags & IEEE80211_RX_IN_SCAN)) {
  363. /* scanning finished during invoking of handlers */
  364. I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
  365. return RX_DROP_UNUSABLE;
  366. }
  367. return RX_CONTINUE;
  368. }
  369. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  370. {
  371. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  372. if (skb->len < 24 || is_multicast_ether_addr(hdr->addr1))
  373. return 0;
  374. return ieee80211_is_robust_mgmt_frame(hdr);
  375. }
  376. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  377. {
  378. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  379. if (skb->len < 24 || !is_multicast_ether_addr(hdr->addr1))
  380. return 0;
  381. return ieee80211_is_robust_mgmt_frame(hdr);
  382. }
  383. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  384. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  385. {
  386. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  387. struct ieee80211_mmie *mmie;
  388. if (skb->len < 24 + sizeof(*mmie) ||
  389. !is_multicast_ether_addr(hdr->da))
  390. return -1;
  391. if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *) hdr))
  392. return -1; /* not a robust management frame */
  393. mmie = (struct ieee80211_mmie *)
  394. (skb->data + skb->len - sizeof(*mmie));
  395. if (mmie->element_id != WLAN_EID_MMIE ||
  396. mmie->length != sizeof(*mmie) - 2)
  397. return -1;
  398. return le16_to_cpu(mmie->key_id);
  399. }
  400. static ieee80211_rx_result
  401. ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  402. {
  403. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  404. unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  405. char *dev_addr = rx->sdata->dev->dev_addr;
  406. if (ieee80211_is_data(hdr->frame_control)) {
  407. if (is_multicast_ether_addr(hdr->addr1)) {
  408. if (ieee80211_has_tods(hdr->frame_control) ||
  409. !ieee80211_has_fromds(hdr->frame_control))
  410. return RX_DROP_MONITOR;
  411. if (memcmp(hdr->addr3, dev_addr, ETH_ALEN) == 0)
  412. return RX_DROP_MONITOR;
  413. } else {
  414. if (!ieee80211_has_a4(hdr->frame_control))
  415. return RX_DROP_MONITOR;
  416. if (memcmp(hdr->addr4, dev_addr, ETH_ALEN) == 0)
  417. return RX_DROP_MONITOR;
  418. }
  419. }
  420. /* If there is not an established peer link and this is not a peer link
  421. * establisment frame, beacon or probe, drop the frame.
  422. */
  423. if (!rx->sta || sta_plink_state(rx->sta) != PLINK_ESTAB) {
  424. struct ieee80211_mgmt *mgmt;
  425. if (!ieee80211_is_mgmt(hdr->frame_control))
  426. return RX_DROP_MONITOR;
  427. if (ieee80211_is_action(hdr->frame_control)) {
  428. mgmt = (struct ieee80211_mgmt *)hdr;
  429. if (mgmt->u.action.category != MESH_PLINK_CATEGORY)
  430. return RX_DROP_MONITOR;
  431. return RX_CONTINUE;
  432. }
  433. if (ieee80211_is_probe_req(hdr->frame_control) ||
  434. ieee80211_is_probe_resp(hdr->frame_control) ||
  435. ieee80211_is_beacon(hdr->frame_control))
  436. return RX_CONTINUE;
  437. return RX_DROP_MONITOR;
  438. }
  439. #define msh_h_get(h, l) ((struct ieee80211s_hdr *) ((u8 *)h + l))
  440. if (ieee80211_is_data(hdr->frame_control) &&
  441. is_multicast_ether_addr(hdr->addr1) &&
  442. mesh_rmc_check(hdr->addr3, msh_h_get(hdr, hdrlen), rx->sdata))
  443. return RX_DROP_MONITOR;
  444. #undef msh_h_get
  445. return RX_CONTINUE;
  446. }
  447. static ieee80211_rx_result debug_noinline
  448. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  449. {
  450. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  451. /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
  452. if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
  453. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  454. rx->sta->last_seq_ctrl[rx->queue] ==
  455. hdr->seq_ctrl)) {
  456. if (rx->flags & IEEE80211_RX_RA_MATCH) {
  457. rx->local->dot11FrameDuplicateCount++;
  458. rx->sta->num_duplicates++;
  459. }
  460. return RX_DROP_MONITOR;
  461. } else
  462. rx->sta->last_seq_ctrl[rx->queue] = hdr->seq_ctrl;
  463. }
  464. if (unlikely(rx->skb->len < 16)) {
  465. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  466. return RX_DROP_MONITOR;
  467. }
  468. /* Drop disallowed frame classes based on STA auth/assoc state;
  469. * IEEE 802.11, Chap 5.5.
  470. *
  471. * mac80211 filters only based on association state, i.e. it drops
  472. * Class 3 frames from not associated stations. hostapd sends
  473. * deauth/disassoc frames when needed. In addition, hostapd is
  474. * responsible for filtering on both auth and assoc states.
  475. */
  476. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  477. return ieee80211_rx_mesh_check(rx);
  478. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  479. ieee80211_is_pspoll(hdr->frame_control)) &&
  480. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  481. (!rx->sta || !test_sta_flags(rx->sta, WLAN_STA_ASSOC)))) {
  482. if ((!ieee80211_has_fromds(hdr->frame_control) &&
  483. !ieee80211_has_tods(hdr->frame_control) &&
  484. ieee80211_is_data(hdr->frame_control)) ||
  485. !(rx->flags & IEEE80211_RX_RA_MATCH)) {
  486. /* Drop IBSS frames and frames for other hosts
  487. * silently. */
  488. return RX_DROP_MONITOR;
  489. }
  490. return RX_DROP_MONITOR;
  491. }
  492. return RX_CONTINUE;
  493. }
  494. static ieee80211_rx_result debug_noinline
  495. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  496. {
  497. struct sk_buff *skb = rx->skb;
  498. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  499. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  500. int keyidx;
  501. int hdrlen;
  502. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  503. struct ieee80211_key *stakey = NULL;
  504. int mmie_keyidx = -1;
  505. /*
  506. * Key selection 101
  507. *
  508. * There are four types of keys:
  509. * - GTK (group keys)
  510. * - IGTK (group keys for management frames)
  511. * - PTK (pairwise keys)
  512. * - STK (station-to-station pairwise keys)
  513. *
  514. * When selecting a key, we have to distinguish between multicast
  515. * (including broadcast) and unicast frames, the latter can only
  516. * use PTKs and STKs while the former always use GTKs and IGTKs.
  517. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  518. * unicast frames can also use key indices like GTKs. Hence, if we
  519. * don't have a PTK/STK we check the key index for a WEP key.
  520. *
  521. * Note that in a regular BSS, multicast frames are sent by the
  522. * AP only, associated stations unicast the frame to the AP first
  523. * which then multicasts it on their behalf.
  524. *
  525. * There is also a slight problem in IBSS mode: GTKs are negotiated
  526. * with each station, that is something we don't currently handle.
  527. * The spec seems to expect that one negotiates the same key with
  528. * every station but there's no such requirement; VLANs could be
  529. * possible.
  530. */
  531. /*
  532. * No point in finding a key and decrypting if the frame is neither
  533. * addressed to us nor a multicast frame.
  534. */
  535. if (!(rx->flags & IEEE80211_RX_RA_MATCH))
  536. return RX_CONTINUE;
  537. if (rx->sta)
  538. stakey = rcu_dereference(rx->sta->key);
  539. if (!ieee80211_has_protected(hdr->frame_control))
  540. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  541. if (!is_multicast_ether_addr(hdr->addr1) && stakey) {
  542. rx->key = stakey;
  543. /* Skip decryption if the frame is not protected. */
  544. if (!ieee80211_has_protected(hdr->frame_control))
  545. return RX_CONTINUE;
  546. } else if (mmie_keyidx >= 0) {
  547. /* Broadcast/multicast robust management frame / BIP */
  548. if ((status->flag & RX_FLAG_DECRYPTED) &&
  549. (status->flag & RX_FLAG_IV_STRIPPED))
  550. return RX_CONTINUE;
  551. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  552. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  553. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  554. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  555. } else if (!ieee80211_has_protected(hdr->frame_control)) {
  556. /*
  557. * The frame was not protected, so skip decryption. However, we
  558. * need to set rx->key if there is a key that could have been
  559. * used so that the frame may be dropped if encryption would
  560. * have been expected.
  561. */
  562. struct ieee80211_key *key = NULL;
  563. if (ieee80211_is_mgmt(hdr->frame_control) &&
  564. is_multicast_ether_addr(hdr->addr1) &&
  565. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  566. rx->key = key;
  567. else if ((key = rcu_dereference(rx->sdata->default_key)))
  568. rx->key = key;
  569. return RX_CONTINUE;
  570. } else {
  571. /*
  572. * The device doesn't give us the IV so we won't be
  573. * able to look up the key. That's ok though, we
  574. * don't need to decrypt the frame, we just won't
  575. * be able to keep statistics accurate.
  576. * Except for key threshold notifications, should
  577. * we somehow allow the driver to tell us which key
  578. * the hardware used if this flag is set?
  579. */
  580. if ((status->flag & RX_FLAG_DECRYPTED) &&
  581. (status->flag & RX_FLAG_IV_STRIPPED))
  582. return RX_CONTINUE;
  583. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  584. if (rx->skb->len < 8 + hdrlen)
  585. return RX_DROP_UNUSABLE; /* TODO: count this? */
  586. /*
  587. * no need to call ieee80211_wep_get_keyidx,
  588. * it verifies a bunch of things we've done already
  589. */
  590. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  591. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  592. /*
  593. * RSNA-protected unicast frames should always be sent with
  594. * pairwise or station-to-station keys, but for WEP we allow
  595. * using a key index as well.
  596. */
  597. if (rx->key && rx->key->conf.alg != ALG_WEP &&
  598. !is_multicast_ether_addr(hdr->addr1))
  599. rx->key = NULL;
  600. }
  601. if (rx->key) {
  602. rx->key->tx_rx_count++;
  603. /* TODO: add threshold stuff again */
  604. } else {
  605. return RX_DROP_MONITOR;
  606. }
  607. /* Check for weak IVs if possible */
  608. if (rx->sta && rx->key->conf.alg == ALG_WEP &&
  609. ieee80211_is_data(hdr->frame_control) &&
  610. (!(status->flag & RX_FLAG_IV_STRIPPED) ||
  611. !(status->flag & RX_FLAG_DECRYPTED)) &&
  612. ieee80211_wep_is_weak_iv(rx->skb, rx->key))
  613. rx->sta->wep_weak_iv_count++;
  614. switch (rx->key->conf.alg) {
  615. case ALG_WEP:
  616. result = ieee80211_crypto_wep_decrypt(rx);
  617. break;
  618. case ALG_TKIP:
  619. result = ieee80211_crypto_tkip_decrypt(rx);
  620. break;
  621. case ALG_CCMP:
  622. result = ieee80211_crypto_ccmp_decrypt(rx);
  623. break;
  624. case ALG_AES_CMAC:
  625. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  626. break;
  627. }
  628. /* either the frame has been decrypted or will be dropped */
  629. status->flag |= RX_FLAG_DECRYPTED;
  630. return result;
  631. }
  632. static ieee80211_rx_result debug_noinline
  633. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  634. {
  635. struct ieee80211_local *local;
  636. struct ieee80211_hdr *hdr;
  637. struct sk_buff *skb;
  638. local = rx->local;
  639. skb = rx->skb;
  640. hdr = (struct ieee80211_hdr *) skb->data;
  641. if (!local->pspolling)
  642. return RX_CONTINUE;
  643. if (!ieee80211_has_fromds(hdr->frame_control))
  644. /* this is not from AP */
  645. return RX_CONTINUE;
  646. if (!ieee80211_is_data(hdr->frame_control))
  647. return RX_CONTINUE;
  648. if (!ieee80211_has_moredata(hdr->frame_control)) {
  649. /* AP has no more frames buffered for us */
  650. local->pspolling = false;
  651. return RX_CONTINUE;
  652. }
  653. /* more data bit is set, let's request a new frame from the AP */
  654. ieee80211_send_pspoll(local, rx->sdata);
  655. return RX_CONTINUE;
  656. }
  657. static void ap_sta_ps_start(struct sta_info *sta)
  658. {
  659. struct ieee80211_sub_if_data *sdata = sta->sdata;
  660. struct ieee80211_local *local = sdata->local;
  661. atomic_inc(&sdata->bss->num_sta_ps);
  662. set_sta_flags(sta, WLAN_STA_PS_STA);
  663. drv_sta_notify(local, &sdata->vif, STA_NOTIFY_SLEEP, &sta->sta);
  664. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  665. printk(KERN_DEBUG "%s: STA %pM aid %d enters power save mode\n",
  666. sdata->dev->name, sta->sta.addr, sta->sta.aid);
  667. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  668. }
  669. static void ap_sta_ps_end(struct sta_info *sta)
  670. {
  671. struct ieee80211_sub_if_data *sdata = sta->sdata;
  672. atomic_dec(&sdata->bss->num_sta_ps);
  673. clear_sta_flags(sta, WLAN_STA_PS_STA);
  674. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  675. printk(KERN_DEBUG "%s: STA %pM aid %d exits power save mode\n",
  676. sdata->dev->name, sta->sta.addr, sta->sta.aid);
  677. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  678. if (test_sta_flags(sta, WLAN_STA_PS_DRIVER)) {
  679. #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
  680. printk(KERN_DEBUG "%s: STA %pM aid %d driver-ps-blocked\n",
  681. sdata->dev->name, sta->sta.addr, sta->sta.aid);
  682. #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
  683. return;
  684. }
  685. ieee80211_sta_ps_deliver_wakeup(sta);
  686. }
  687. static ieee80211_rx_result debug_noinline
  688. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  689. {
  690. struct sta_info *sta = rx->sta;
  691. struct sk_buff *skb = rx->skb;
  692. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  693. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  694. if (!sta)
  695. return RX_CONTINUE;
  696. /*
  697. * Update last_rx only for IBSS packets which are for the current
  698. * BSSID to avoid keeping the current IBSS network alive in cases
  699. * where other STAs start using different BSSID.
  700. */
  701. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  702. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  703. NL80211_IFTYPE_ADHOC);
  704. if (compare_ether_addr(bssid, rx->sdata->u.ibss.bssid) == 0)
  705. sta->last_rx = jiffies;
  706. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  707. /*
  708. * Mesh beacons will update last_rx when if they are found to
  709. * match the current local configuration when processed.
  710. */
  711. sta->last_rx = jiffies;
  712. }
  713. if (!(rx->flags & IEEE80211_RX_RA_MATCH))
  714. return RX_CONTINUE;
  715. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  716. ieee80211_sta_rx_notify(rx->sdata, hdr);
  717. sta->rx_fragments++;
  718. sta->rx_bytes += rx->skb->len;
  719. sta->last_signal = status->signal;
  720. sta->last_noise = status->noise;
  721. /*
  722. * Change STA power saving mode only at the end of a frame
  723. * exchange sequence.
  724. */
  725. if (!ieee80211_has_morefrags(hdr->frame_control) &&
  726. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  727. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)) {
  728. if (test_sta_flags(sta, WLAN_STA_PS_STA)) {
  729. /*
  730. * Ignore doze->wake transitions that are
  731. * indicated by non-data frames, the standard
  732. * is unclear here, but for example going to
  733. * PS mode and then scanning would cause a
  734. * doze->wake transition for the probe request,
  735. * and that is clearly undesirable.
  736. */
  737. if (ieee80211_is_data(hdr->frame_control) &&
  738. !ieee80211_has_pm(hdr->frame_control))
  739. ap_sta_ps_end(sta);
  740. } else {
  741. if (ieee80211_has_pm(hdr->frame_control))
  742. ap_sta_ps_start(sta);
  743. }
  744. }
  745. /*
  746. * Drop (qos-)data::nullfunc frames silently, since they
  747. * are used only to control station power saving mode.
  748. */
  749. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  750. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  751. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  752. /*
  753. * Update counter and free packet here to avoid
  754. * counting this as a dropped packed.
  755. */
  756. sta->rx_packets++;
  757. dev_kfree_skb(rx->skb);
  758. return RX_QUEUED;
  759. }
  760. return RX_CONTINUE;
  761. } /* ieee80211_rx_h_sta_process */
  762. static inline struct ieee80211_fragment_entry *
  763. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  764. unsigned int frag, unsigned int seq, int rx_queue,
  765. struct sk_buff **skb)
  766. {
  767. struct ieee80211_fragment_entry *entry;
  768. int idx;
  769. idx = sdata->fragment_next;
  770. entry = &sdata->fragments[sdata->fragment_next++];
  771. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  772. sdata->fragment_next = 0;
  773. if (!skb_queue_empty(&entry->skb_list)) {
  774. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  775. struct ieee80211_hdr *hdr =
  776. (struct ieee80211_hdr *) entry->skb_list.next->data;
  777. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  778. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  779. "addr1=%pM addr2=%pM\n",
  780. sdata->dev->name, idx,
  781. jiffies - entry->first_frag_time, entry->seq,
  782. entry->last_frag, hdr->addr1, hdr->addr2);
  783. #endif
  784. __skb_queue_purge(&entry->skb_list);
  785. }
  786. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  787. *skb = NULL;
  788. entry->first_frag_time = jiffies;
  789. entry->seq = seq;
  790. entry->rx_queue = rx_queue;
  791. entry->last_frag = frag;
  792. entry->ccmp = 0;
  793. entry->extra_len = 0;
  794. return entry;
  795. }
  796. static inline struct ieee80211_fragment_entry *
  797. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  798. unsigned int frag, unsigned int seq,
  799. int rx_queue, struct ieee80211_hdr *hdr)
  800. {
  801. struct ieee80211_fragment_entry *entry;
  802. int i, idx;
  803. idx = sdata->fragment_next;
  804. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  805. struct ieee80211_hdr *f_hdr;
  806. idx--;
  807. if (idx < 0)
  808. idx = IEEE80211_FRAGMENT_MAX - 1;
  809. entry = &sdata->fragments[idx];
  810. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  811. entry->rx_queue != rx_queue ||
  812. entry->last_frag + 1 != frag)
  813. continue;
  814. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  815. /*
  816. * Check ftype and addresses are equal, else check next fragment
  817. */
  818. if (((hdr->frame_control ^ f_hdr->frame_control) &
  819. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  820. compare_ether_addr(hdr->addr1, f_hdr->addr1) != 0 ||
  821. compare_ether_addr(hdr->addr2, f_hdr->addr2) != 0)
  822. continue;
  823. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  824. __skb_queue_purge(&entry->skb_list);
  825. continue;
  826. }
  827. return entry;
  828. }
  829. return NULL;
  830. }
  831. static ieee80211_rx_result debug_noinline
  832. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  833. {
  834. struct ieee80211_hdr *hdr;
  835. u16 sc;
  836. __le16 fc;
  837. unsigned int frag, seq;
  838. struct ieee80211_fragment_entry *entry;
  839. struct sk_buff *skb;
  840. hdr = (struct ieee80211_hdr *)rx->skb->data;
  841. fc = hdr->frame_control;
  842. sc = le16_to_cpu(hdr->seq_ctrl);
  843. frag = sc & IEEE80211_SCTL_FRAG;
  844. if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
  845. (rx->skb)->len < 24 ||
  846. is_multicast_ether_addr(hdr->addr1))) {
  847. /* not fragmented */
  848. goto out;
  849. }
  850. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  851. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  852. if (frag == 0) {
  853. /* This is the first fragment of a new frame. */
  854. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  855. rx->queue, &(rx->skb));
  856. if (rx->key && rx->key->conf.alg == ALG_CCMP &&
  857. ieee80211_has_protected(fc)) {
  858. /* Store CCMP PN so that we can verify that the next
  859. * fragment has a sequential PN value. */
  860. entry->ccmp = 1;
  861. memcpy(entry->last_pn,
  862. rx->key->u.ccmp.rx_pn[rx->queue],
  863. CCMP_PN_LEN);
  864. }
  865. return RX_QUEUED;
  866. }
  867. /* This is a fragment for a frame that should already be pending in
  868. * fragment cache. Add this fragment to the end of the pending entry.
  869. */
  870. entry = ieee80211_reassemble_find(rx->sdata, frag, seq, rx->queue, hdr);
  871. if (!entry) {
  872. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  873. return RX_DROP_MONITOR;
  874. }
  875. /* Verify that MPDUs within one MSDU have sequential PN values.
  876. * (IEEE 802.11i, 8.3.3.4.5) */
  877. if (entry->ccmp) {
  878. int i;
  879. u8 pn[CCMP_PN_LEN], *rpn;
  880. if (!rx->key || rx->key->conf.alg != ALG_CCMP)
  881. return RX_DROP_UNUSABLE;
  882. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  883. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  884. pn[i]++;
  885. if (pn[i])
  886. break;
  887. }
  888. rpn = rx->key->u.ccmp.rx_pn[rx->queue];
  889. if (memcmp(pn, rpn, CCMP_PN_LEN))
  890. return RX_DROP_UNUSABLE;
  891. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  892. }
  893. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  894. __skb_queue_tail(&entry->skb_list, rx->skb);
  895. entry->last_frag = frag;
  896. entry->extra_len += rx->skb->len;
  897. if (ieee80211_has_morefrags(fc)) {
  898. rx->skb = NULL;
  899. return RX_QUEUED;
  900. }
  901. rx->skb = __skb_dequeue(&entry->skb_list);
  902. if (skb_tailroom(rx->skb) < entry->extra_len) {
  903. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  904. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  905. GFP_ATOMIC))) {
  906. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  907. __skb_queue_purge(&entry->skb_list);
  908. return RX_DROP_UNUSABLE;
  909. }
  910. }
  911. while ((skb = __skb_dequeue(&entry->skb_list))) {
  912. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  913. dev_kfree_skb(skb);
  914. }
  915. /* Complete frame has been reassembled - process it now */
  916. rx->flags |= IEEE80211_RX_FRAGMENTED;
  917. out:
  918. if (rx->sta)
  919. rx->sta->rx_packets++;
  920. if (is_multicast_ether_addr(hdr->addr1))
  921. rx->local->dot11MulticastReceivedFrameCount++;
  922. else
  923. ieee80211_led_rx(rx->local);
  924. return RX_CONTINUE;
  925. }
  926. static ieee80211_rx_result debug_noinline
  927. ieee80211_rx_h_ps_poll(struct ieee80211_rx_data *rx)
  928. {
  929. struct ieee80211_sub_if_data *sdata = rx->sdata;
  930. __le16 fc = ((struct ieee80211_hdr *)rx->skb->data)->frame_control;
  931. if (likely(!rx->sta || !ieee80211_is_pspoll(fc) ||
  932. !(rx->flags & IEEE80211_RX_RA_MATCH)))
  933. return RX_CONTINUE;
  934. if ((sdata->vif.type != NL80211_IFTYPE_AP) &&
  935. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
  936. return RX_DROP_UNUSABLE;
  937. if (!test_sta_flags(rx->sta, WLAN_STA_PS_DRIVER))
  938. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  939. else
  940. set_sta_flags(rx->sta, WLAN_STA_PSPOLL);
  941. /* Free PS Poll skb here instead of returning RX_DROP that would
  942. * count as an dropped frame. */
  943. dev_kfree_skb(rx->skb);
  944. return RX_QUEUED;
  945. }
  946. static ieee80211_rx_result debug_noinline
  947. ieee80211_rx_h_remove_qos_control(struct ieee80211_rx_data *rx)
  948. {
  949. u8 *data = rx->skb->data;
  950. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)data;
  951. if (!ieee80211_is_data_qos(hdr->frame_control))
  952. return RX_CONTINUE;
  953. /* remove the qos control field, update frame type and meta-data */
  954. memmove(data + IEEE80211_QOS_CTL_LEN, data,
  955. ieee80211_hdrlen(hdr->frame_control) - IEEE80211_QOS_CTL_LEN);
  956. hdr = (struct ieee80211_hdr *)skb_pull(rx->skb, IEEE80211_QOS_CTL_LEN);
  957. /* change frame type to non QOS */
  958. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  959. return RX_CONTINUE;
  960. }
  961. static int
  962. ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  963. {
  964. if (unlikely(!rx->sta ||
  965. !test_sta_flags(rx->sta, WLAN_STA_AUTHORIZED)))
  966. return -EACCES;
  967. return 0;
  968. }
  969. static int
  970. ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  971. {
  972. struct sk_buff *skb = rx->skb;
  973. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  974. /*
  975. * Pass through unencrypted frames if the hardware has
  976. * decrypted them already.
  977. */
  978. if (status->flag & RX_FLAG_DECRYPTED)
  979. return 0;
  980. /* Drop unencrypted frames if key is set. */
  981. if (unlikely(!ieee80211_has_protected(fc) &&
  982. !ieee80211_is_nullfunc(fc) &&
  983. ieee80211_is_data(fc) &&
  984. (rx->key || rx->sdata->drop_unencrypted)))
  985. return -EACCES;
  986. if (rx->sta && test_sta_flags(rx->sta, WLAN_STA_MFP)) {
  987. if (unlikely(ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  988. rx->key))
  989. return -EACCES;
  990. /* BIP does not use Protected field, so need to check MMIE */
  991. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb)
  992. && ieee80211_get_mmie_keyidx(rx->skb) < 0 &&
  993. rx->key))
  994. return -EACCES;
  995. /*
  996. * When using MFP, Action frames are not allowed prior to
  997. * having configured keys.
  998. */
  999. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1000. ieee80211_is_robust_mgmt_frame(
  1001. (struct ieee80211_hdr *) rx->skb->data)))
  1002. return -EACCES;
  1003. }
  1004. return 0;
  1005. }
  1006. static int
  1007. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx)
  1008. {
  1009. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1010. struct net_device *dev = sdata->dev;
  1011. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1012. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->use_4addr &&
  1013. ieee80211_has_a4(hdr->frame_control))
  1014. return -1;
  1015. if (sdata->use_4addr && is_multicast_ether_addr(hdr->addr1))
  1016. return -1;
  1017. return ieee80211_data_to_8023(rx->skb, dev->dev_addr, sdata->vif.type);
  1018. }
  1019. /*
  1020. * requires that rx->skb is a frame with ethernet header
  1021. */
  1022. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1023. {
  1024. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1025. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1026. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1027. /*
  1028. * Allow EAPOL frames to us/the PAE group address regardless
  1029. * of whether the frame was encrypted or not.
  1030. */
  1031. if (ehdr->h_proto == htons(ETH_P_PAE) &&
  1032. (compare_ether_addr(ehdr->h_dest, rx->sdata->dev->dev_addr) == 0 ||
  1033. compare_ether_addr(ehdr->h_dest, pae_group_addr) == 0))
  1034. return true;
  1035. if (ieee80211_802_1x_port_control(rx) ||
  1036. ieee80211_drop_unencrypted(rx, fc))
  1037. return false;
  1038. return true;
  1039. }
  1040. /*
  1041. * requires that rx->skb is a frame with ethernet header
  1042. */
  1043. static void
  1044. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1045. {
  1046. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1047. struct net_device *dev = sdata->dev;
  1048. struct ieee80211_local *local = rx->local;
  1049. struct sk_buff *skb, *xmit_skb;
  1050. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1051. struct sta_info *dsta;
  1052. skb = rx->skb;
  1053. xmit_skb = NULL;
  1054. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1055. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1056. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1057. (rx->flags & IEEE80211_RX_RA_MATCH) && !rx->sdata->use_4addr) {
  1058. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1059. /*
  1060. * send multicast frames both to higher layers in
  1061. * local net stack and back to the wireless medium
  1062. */
  1063. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1064. if (!xmit_skb && net_ratelimit())
  1065. printk(KERN_DEBUG "%s: failed to clone "
  1066. "multicast frame\n", dev->name);
  1067. } else {
  1068. dsta = sta_info_get(local, skb->data);
  1069. if (dsta && dsta->sdata->dev == dev) {
  1070. /*
  1071. * The destination station is associated to
  1072. * this AP (in this VLAN), so send the frame
  1073. * directly to it and do not pass it to local
  1074. * net stack.
  1075. */
  1076. xmit_skb = skb;
  1077. skb = NULL;
  1078. }
  1079. }
  1080. }
  1081. if (skb) {
  1082. int align __maybe_unused;
  1083. #if defined(CONFIG_MAC80211_DEBUG_PACKET_ALIGNMENT) || !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  1084. /*
  1085. * 'align' will only take the values 0 or 2 here
  1086. * since all frames are required to be aligned
  1087. * to 2-byte boundaries when being passed to
  1088. * mac80211. That also explains the __skb_push()
  1089. * below.
  1090. */
  1091. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1092. if (align) {
  1093. if (WARN_ON(skb_headroom(skb) < 3)) {
  1094. dev_kfree_skb(skb);
  1095. skb = NULL;
  1096. } else {
  1097. u8 *data = skb->data;
  1098. size_t len = skb_headlen(skb);
  1099. skb->data -= align;
  1100. memmove(skb->data, data, len);
  1101. skb_set_tail_pointer(skb, len);
  1102. }
  1103. }
  1104. #endif
  1105. if (skb) {
  1106. /* deliver to local stack */
  1107. skb->protocol = eth_type_trans(skb, dev);
  1108. memset(skb->cb, 0, sizeof(skb->cb));
  1109. netif_rx(skb);
  1110. }
  1111. }
  1112. if (xmit_skb) {
  1113. /* send to wireless media */
  1114. xmit_skb->protocol = htons(ETH_P_802_3);
  1115. skb_reset_network_header(xmit_skb);
  1116. skb_reset_mac_header(xmit_skb);
  1117. dev_queue_xmit(xmit_skb);
  1118. }
  1119. }
  1120. static ieee80211_rx_result debug_noinline
  1121. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1122. {
  1123. struct net_device *dev = rx->sdata->dev;
  1124. struct ieee80211_local *local = rx->local;
  1125. u16 ethertype;
  1126. u8 *payload;
  1127. struct sk_buff *skb = rx->skb, *frame = NULL;
  1128. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1129. __le16 fc = hdr->frame_control;
  1130. const struct ethhdr *eth;
  1131. int remaining, err;
  1132. u8 dst[ETH_ALEN];
  1133. u8 src[ETH_ALEN];
  1134. if (unlikely(!ieee80211_is_data(fc)))
  1135. return RX_CONTINUE;
  1136. if (unlikely(!ieee80211_is_data_present(fc)))
  1137. return RX_DROP_MONITOR;
  1138. if (!(rx->flags & IEEE80211_RX_AMSDU))
  1139. return RX_CONTINUE;
  1140. err = __ieee80211_data_to_8023(rx);
  1141. if (unlikely(err))
  1142. return RX_DROP_UNUSABLE;
  1143. skb->dev = dev;
  1144. dev->stats.rx_packets++;
  1145. dev->stats.rx_bytes += skb->len;
  1146. /* skip the wrapping header */
  1147. eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
  1148. if (!eth)
  1149. return RX_DROP_UNUSABLE;
  1150. while (skb != frame) {
  1151. u8 padding;
  1152. __be16 len = eth->h_proto;
  1153. unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
  1154. remaining = skb->len;
  1155. memcpy(dst, eth->h_dest, ETH_ALEN);
  1156. memcpy(src, eth->h_source, ETH_ALEN);
  1157. padding = ((4 - subframe_len) & 0x3);
  1158. /* the last MSDU has no padding */
  1159. if (subframe_len > remaining)
  1160. return RX_DROP_UNUSABLE;
  1161. skb_pull(skb, sizeof(struct ethhdr));
  1162. /* if last subframe reuse skb */
  1163. if (remaining <= subframe_len + padding)
  1164. frame = skb;
  1165. else {
  1166. /*
  1167. * Allocate and reserve two bytes more for payload
  1168. * alignment since sizeof(struct ethhdr) is 14.
  1169. */
  1170. frame = dev_alloc_skb(
  1171. ALIGN(local->hw.extra_tx_headroom, 4) +
  1172. subframe_len + 2);
  1173. if (frame == NULL)
  1174. return RX_DROP_UNUSABLE;
  1175. skb_reserve(frame,
  1176. ALIGN(local->hw.extra_tx_headroom, 4) +
  1177. sizeof(struct ethhdr) + 2);
  1178. memcpy(skb_put(frame, ntohs(len)), skb->data,
  1179. ntohs(len));
  1180. eth = (struct ethhdr *) skb_pull(skb, ntohs(len) +
  1181. padding);
  1182. if (!eth) {
  1183. dev_kfree_skb(frame);
  1184. return RX_DROP_UNUSABLE;
  1185. }
  1186. }
  1187. skb_reset_network_header(frame);
  1188. frame->dev = dev;
  1189. frame->priority = skb->priority;
  1190. rx->skb = frame;
  1191. payload = frame->data;
  1192. ethertype = (payload[6] << 8) | payload[7];
  1193. if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
  1194. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  1195. compare_ether_addr(payload,
  1196. bridge_tunnel_header) == 0)) {
  1197. /* remove RFC1042 or Bridge-Tunnel
  1198. * encapsulation and replace EtherType */
  1199. skb_pull(frame, 6);
  1200. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1201. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1202. } else {
  1203. memcpy(skb_push(frame, sizeof(__be16)),
  1204. &len, sizeof(__be16));
  1205. memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
  1206. memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
  1207. }
  1208. if (!ieee80211_frame_allowed(rx, fc)) {
  1209. if (skb == frame) /* last frame */
  1210. return RX_DROP_UNUSABLE;
  1211. dev_kfree_skb(frame);
  1212. continue;
  1213. }
  1214. ieee80211_deliver_skb(rx);
  1215. }
  1216. return RX_QUEUED;
  1217. }
  1218. #ifdef CONFIG_MAC80211_MESH
  1219. static ieee80211_rx_result
  1220. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1221. {
  1222. struct ieee80211_hdr *hdr;
  1223. struct ieee80211s_hdr *mesh_hdr;
  1224. unsigned int hdrlen;
  1225. struct sk_buff *skb = rx->skb, *fwd_skb;
  1226. struct ieee80211_local *local = rx->local;
  1227. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1228. hdr = (struct ieee80211_hdr *) skb->data;
  1229. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1230. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1231. if (!ieee80211_is_data(hdr->frame_control))
  1232. return RX_CONTINUE;
  1233. if (!mesh_hdr->ttl)
  1234. /* illegal frame */
  1235. return RX_DROP_MONITOR;
  1236. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1237. struct mesh_path *mppath;
  1238. char *proxied_addr;
  1239. char *mpp_addr;
  1240. if (is_multicast_ether_addr(hdr->addr1)) {
  1241. mpp_addr = hdr->addr3;
  1242. proxied_addr = mesh_hdr->eaddr1;
  1243. } else {
  1244. mpp_addr = hdr->addr4;
  1245. proxied_addr = mesh_hdr->eaddr2;
  1246. }
  1247. rcu_read_lock();
  1248. mppath = mpp_path_lookup(proxied_addr, sdata);
  1249. if (!mppath) {
  1250. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1251. } else {
  1252. spin_lock_bh(&mppath->state_lock);
  1253. mppath->exp_time = jiffies;
  1254. if (compare_ether_addr(mppath->mpp, mpp_addr) != 0)
  1255. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1256. spin_unlock_bh(&mppath->state_lock);
  1257. }
  1258. rcu_read_unlock();
  1259. }
  1260. /* Frame has reached destination. Don't forward */
  1261. if (!is_multicast_ether_addr(hdr->addr1) &&
  1262. compare_ether_addr(sdata->dev->dev_addr, hdr->addr3) == 0)
  1263. return RX_CONTINUE;
  1264. mesh_hdr->ttl--;
  1265. if (rx->flags & IEEE80211_RX_RA_MATCH) {
  1266. if (!mesh_hdr->ttl)
  1267. IEEE80211_IFSTA_MESH_CTR_INC(&rx->sdata->u.mesh,
  1268. dropped_frames_ttl);
  1269. else {
  1270. struct ieee80211_hdr *fwd_hdr;
  1271. struct ieee80211_tx_info *info;
  1272. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1273. if (!fwd_skb && net_ratelimit())
  1274. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1275. sdata->dev->name);
  1276. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1277. memcpy(fwd_hdr->addr2, sdata->dev->dev_addr, ETH_ALEN);
  1278. info = IEEE80211_SKB_CB(fwd_skb);
  1279. memset(info, 0, sizeof(*info));
  1280. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1281. info->control.vif = &rx->sdata->vif;
  1282. ieee80211_select_queue(local, fwd_skb);
  1283. if (is_multicast_ether_addr(fwd_hdr->addr1))
  1284. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1285. fwded_mcast);
  1286. else {
  1287. int err;
  1288. /*
  1289. * Save TA to addr1 to send TA a path error if a
  1290. * suitable next hop is not found
  1291. */
  1292. memcpy(fwd_hdr->addr1, fwd_hdr->addr2,
  1293. ETH_ALEN);
  1294. err = mesh_nexthop_lookup(fwd_skb, sdata);
  1295. /* Failed to immediately resolve next hop:
  1296. * fwded frame was dropped or will be added
  1297. * later to the pending skb queue. */
  1298. if (err)
  1299. return RX_DROP_MONITOR;
  1300. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1301. fwded_unicast);
  1302. }
  1303. IEEE80211_IFSTA_MESH_CTR_INC(&sdata->u.mesh,
  1304. fwded_frames);
  1305. ieee80211_add_pending_skb(local, fwd_skb);
  1306. }
  1307. }
  1308. if (is_multicast_ether_addr(hdr->addr1) ||
  1309. sdata->dev->flags & IFF_PROMISC)
  1310. return RX_CONTINUE;
  1311. else
  1312. return RX_DROP_MONITOR;
  1313. }
  1314. #endif
  1315. static ieee80211_rx_result debug_noinline
  1316. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1317. {
  1318. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1319. struct net_device *dev = sdata->dev;
  1320. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1321. __le16 fc = hdr->frame_control;
  1322. int err;
  1323. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1324. return RX_CONTINUE;
  1325. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1326. return RX_DROP_MONITOR;
  1327. /*
  1328. * Allow the cooked monitor interface of an AP to see 4-addr frames so
  1329. * that a 4-addr station can be detected and moved into a separate VLAN
  1330. */
  1331. if (ieee80211_has_a4(hdr->frame_control) &&
  1332. sdata->vif.type == NL80211_IFTYPE_AP)
  1333. return RX_DROP_MONITOR;
  1334. err = __ieee80211_data_to_8023(rx);
  1335. if (unlikely(err))
  1336. return RX_DROP_UNUSABLE;
  1337. if (!ieee80211_frame_allowed(rx, fc))
  1338. return RX_DROP_MONITOR;
  1339. rx->skb->dev = dev;
  1340. dev->stats.rx_packets++;
  1341. dev->stats.rx_bytes += rx->skb->len;
  1342. ieee80211_deliver_skb(rx);
  1343. return RX_QUEUED;
  1344. }
  1345. static ieee80211_rx_result debug_noinline
  1346. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1347. {
  1348. struct ieee80211_local *local = rx->local;
  1349. struct ieee80211_hw *hw = &local->hw;
  1350. struct sk_buff *skb = rx->skb;
  1351. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1352. struct tid_ampdu_rx *tid_agg_rx;
  1353. u16 start_seq_num;
  1354. u16 tid;
  1355. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1356. return RX_CONTINUE;
  1357. if (ieee80211_is_back_req(bar->frame_control)) {
  1358. if (!rx->sta)
  1359. return RX_DROP_MONITOR;
  1360. tid = le16_to_cpu(bar->control) >> 12;
  1361. if (rx->sta->ampdu_mlme.tid_state_rx[tid]
  1362. != HT_AGG_STATE_OPERATIONAL)
  1363. return RX_DROP_MONITOR;
  1364. tid_agg_rx = rx->sta->ampdu_mlme.tid_rx[tid];
  1365. start_seq_num = le16_to_cpu(bar->start_seq_num) >> 4;
  1366. /* reset session timer */
  1367. if (tid_agg_rx->timeout)
  1368. mod_timer(&tid_agg_rx->session_timer,
  1369. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1370. /* release stored frames up to start of BAR */
  1371. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1372. kfree_skb(skb);
  1373. return RX_QUEUED;
  1374. }
  1375. return RX_CONTINUE;
  1376. }
  1377. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1378. struct ieee80211_mgmt *mgmt,
  1379. size_t len)
  1380. {
  1381. struct ieee80211_local *local = sdata->local;
  1382. struct sk_buff *skb;
  1383. struct ieee80211_mgmt *resp;
  1384. if (compare_ether_addr(mgmt->da, sdata->dev->dev_addr) != 0) {
  1385. /* Not to own unicast address */
  1386. return;
  1387. }
  1388. if (compare_ether_addr(mgmt->sa, sdata->u.mgd.bssid) != 0 ||
  1389. compare_ether_addr(mgmt->bssid, sdata->u.mgd.bssid) != 0) {
  1390. /* Not from the current AP or not associated yet. */
  1391. return;
  1392. }
  1393. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1394. /* Too short SA Query request frame */
  1395. return;
  1396. }
  1397. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1398. if (skb == NULL)
  1399. return;
  1400. skb_reserve(skb, local->hw.extra_tx_headroom);
  1401. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1402. memset(resp, 0, 24);
  1403. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1404. memcpy(resp->sa, sdata->dev->dev_addr, ETH_ALEN);
  1405. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1406. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1407. IEEE80211_STYPE_ACTION);
  1408. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1409. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1410. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1411. memcpy(resp->u.action.u.sa_query.trans_id,
  1412. mgmt->u.action.u.sa_query.trans_id,
  1413. WLAN_SA_QUERY_TR_ID_LEN);
  1414. ieee80211_tx_skb(sdata, skb, 1);
  1415. }
  1416. static ieee80211_rx_result debug_noinline
  1417. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1418. {
  1419. struct ieee80211_local *local = rx->local;
  1420. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1421. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1422. int len = rx->skb->len;
  1423. if (!ieee80211_is_action(mgmt->frame_control))
  1424. return RX_CONTINUE;
  1425. if (!rx->sta)
  1426. return RX_DROP_MONITOR;
  1427. if (!(rx->flags & IEEE80211_RX_RA_MATCH))
  1428. return RX_DROP_MONITOR;
  1429. if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
  1430. return RX_DROP_MONITOR;
  1431. /* all categories we currently handle have action_code */
  1432. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1433. return RX_DROP_MONITOR;
  1434. switch (mgmt->u.action.category) {
  1435. case WLAN_CATEGORY_BACK:
  1436. /*
  1437. * The aggregation code is not prepared to handle
  1438. * anything but STA/AP due to the BSSID handling;
  1439. * IBSS could work in the code but isn't supported
  1440. * by drivers or the standard.
  1441. */
  1442. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1443. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1444. sdata->vif.type != NL80211_IFTYPE_AP)
  1445. return RX_DROP_MONITOR;
  1446. switch (mgmt->u.action.u.addba_req.action_code) {
  1447. case WLAN_ACTION_ADDBA_REQ:
  1448. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1449. sizeof(mgmt->u.action.u.addba_req)))
  1450. return RX_DROP_MONITOR;
  1451. ieee80211_process_addba_request(local, rx->sta, mgmt, len);
  1452. break;
  1453. case WLAN_ACTION_ADDBA_RESP:
  1454. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1455. sizeof(mgmt->u.action.u.addba_resp)))
  1456. return RX_DROP_MONITOR;
  1457. ieee80211_process_addba_resp(local, rx->sta, mgmt, len);
  1458. break;
  1459. case WLAN_ACTION_DELBA:
  1460. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1461. sizeof(mgmt->u.action.u.delba)))
  1462. return RX_DROP_MONITOR;
  1463. ieee80211_process_delba(sdata, rx->sta, mgmt, len);
  1464. break;
  1465. }
  1466. break;
  1467. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1468. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1469. return RX_DROP_MONITOR;
  1470. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1471. return RX_DROP_MONITOR;
  1472. switch (mgmt->u.action.u.measurement.action_code) {
  1473. case WLAN_ACTION_SPCT_MSR_REQ:
  1474. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1475. sizeof(mgmt->u.action.u.measurement)))
  1476. return RX_DROP_MONITOR;
  1477. ieee80211_process_measurement_req(sdata, mgmt, len);
  1478. break;
  1479. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1480. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1481. sizeof(mgmt->u.action.u.chan_switch)))
  1482. return RX_DROP_MONITOR;
  1483. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1484. return RX_DROP_MONITOR;
  1485. if (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN))
  1486. return RX_DROP_MONITOR;
  1487. return ieee80211_sta_rx_mgmt(sdata, rx->skb);
  1488. }
  1489. break;
  1490. case WLAN_CATEGORY_SA_QUERY:
  1491. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1492. sizeof(mgmt->u.action.u.sa_query)))
  1493. return RX_DROP_MONITOR;
  1494. switch (mgmt->u.action.u.sa_query.action) {
  1495. case WLAN_ACTION_SA_QUERY_REQUEST:
  1496. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1497. return RX_DROP_MONITOR;
  1498. ieee80211_process_sa_query_req(sdata, mgmt, len);
  1499. break;
  1500. case WLAN_ACTION_SA_QUERY_RESPONSE:
  1501. /*
  1502. * SA Query response is currently only used in AP mode
  1503. * and it is processed in user space.
  1504. */
  1505. return RX_CONTINUE;
  1506. }
  1507. break;
  1508. default:
  1509. return RX_CONTINUE;
  1510. }
  1511. rx->sta->rx_packets++;
  1512. dev_kfree_skb(rx->skb);
  1513. return RX_QUEUED;
  1514. }
  1515. static ieee80211_rx_result debug_noinline
  1516. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  1517. {
  1518. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1519. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1520. if (!(rx->flags & IEEE80211_RX_RA_MATCH))
  1521. return RX_DROP_MONITOR;
  1522. if (ieee80211_drop_unencrypted(rx, mgmt->frame_control))
  1523. return RX_DROP_MONITOR;
  1524. if (ieee80211_vif_is_mesh(&sdata->vif))
  1525. return ieee80211_mesh_rx_mgmt(sdata, rx->skb);
  1526. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  1527. return ieee80211_ibss_rx_mgmt(sdata, rx->skb);
  1528. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1529. return ieee80211_sta_rx_mgmt(sdata, rx->skb);
  1530. return RX_DROP_MONITOR;
  1531. }
  1532. static void ieee80211_rx_michael_mic_report(struct ieee80211_hdr *hdr,
  1533. struct ieee80211_rx_data *rx)
  1534. {
  1535. int keyidx;
  1536. unsigned int hdrlen;
  1537. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1538. if (rx->skb->len >= hdrlen + 4)
  1539. keyidx = rx->skb->data[hdrlen + 3] >> 6;
  1540. else
  1541. keyidx = -1;
  1542. if (!rx->sta) {
  1543. /*
  1544. * Some hardware seem to generate incorrect Michael MIC
  1545. * reports; ignore them to avoid triggering countermeasures.
  1546. */
  1547. return;
  1548. }
  1549. if (!ieee80211_has_protected(hdr->frame_control))
  1550. return;
  1551. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && keyidx) {
  1552. /*
  1553. * APs with pairwise keys should never receive Michael MIC
  1554. * errors for non-zero keyidx because these are reserved for
  1555. * group keys and only the AP is sending real multicast
  1556. * frames in the BSS.
  1557. */
  1558. return;
  1559. }
  1560. if (!ieee80211_is_data(hdr->frame_control) &&
  1561. !ieee80211_is_auth(hdr->frame_control))
  1562. return;
  1563. mac80211_ev_michael_mic_failure(rx->sdata, keyidx, hdr, NULL,
  1564. GFP_ATOMIC);
  1565. }
  1566. /* TODO: use IEEE80211_RX_FRAGMENTED */
  1567. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  1568. struct ieee80211_rate *rate)
  1569. {
  1570. struct ieee80211_sub_if_data *sdata;
  1571. struct ieee80211_local *local = rx->local;
  1572. struct ieee80211_rtap_hdr {
  1573. struct ieee80211_radiotap_header hdr;
  1574. u8 flags;
  1575. u8 rate_or_pad;
  1576. __le16 chan_freq;
  1577. __le16 chan_flags;
  1578. } __attribute__ ((packed)) *rthdr;
  1579. struct sk_buff *skb = rx->skb, *skb2;
  1580. struct net_device *prev_dev = NULL;
  1581. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1582. if (rx->flags & IEEE80211_RX_CMNTR_REPORTED)
  1583. goto out_free_skb;
  1584. if (skb_headroom(skb) < sizeof(*rthdr) &&
  1585. pskb_expand_head(skb, sizeof(*rthdr), 0, GFP_ATOMIC))
  1586. goto out_free_skb;
  1587. rthdr = (void *)skb_push(skb, sizeof(*rthdr));
  1588. memset(rthdr, 0, sizeof(*rthdr));
  1589. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1590. rthdr->hdr.it_present =
  1591. cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  1592. (1 << IEEE80211_RADIOTAP_CHANNEL));
  1593. if (rate) {
  1594. rthdr->rate_or_pad = rate->bitrate / 5;
  1595. rthdr->hdr.it_present |=
  1596. cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  1597. }
  1598. rthdr->chan_freq = cpu_to_le16(status->freq);
  1599. if (status->band == IEEE80211_BAND_5GHZ)
  1600. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_OFDM |
  1601. IEEE80211_CHAN_5GHZ);
  1602. else
  1603. rthdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_DYN |
  1604. IEEE80211_CHAN_2GHZ);
  1605. skb_set_mac_header(skb, 0);
  1606. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1607. skb->pkt_type = PACKET_OTHERHOST;
  1608. skb->protocol = htons(ETH_P_802_2);
  1609. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1610. if (!netif_running(sdata->dev))
  1611. continue;
  1612. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  1613. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  1614. continue;
  1615. if (prev_dev) {
  1616. skb2 = skb_clone(skb, GFP_ATOMIC);
  1617. if (skb2) {
  1618. skb2->dev = prev_dev;
  1619. netif_rx(skb2);
  1620. }
  1621. }
  1622. prev_dev = sdata->dev;
  1623. sdata->dev->stats.rx_packets++;
  1624. sdata->dev->stats.rx_bytes += skb->len;
  1625. }
  1626. if (prev_dev) {
  1627. skb->dev = prev_dev;
  1628. netif_rx(skb);
  1629. skb = NULL;
  1630. } else
  1631. goto out_free_skb;
  1632. rx->flags |= IEEE80211_RX_CMNTR_REPORTED;
  1633. return;
  1634. out_free_skb:
  1635. dev_kfree_skb(skb);
  1636. }
  1637. static void ieee80211_invoke_rx_handlers(struct ieee80211_sub_if_data *sdata,
  1638. struct ieee80211_rx_data *rx,
  1639. struct sk_buff *skb,
  1640. struct ieee80211_rate *rate)
  1641. {
  1642. ieee80211_rx_result res = RX_DROP_MONITOR;
  1643. rx->skb = skb;
  1644. rx->sdata = sdata;
  1645. #define CALL_RXH(rxh) \
  1646. do { \
  1647. res = rxh(rx); \
  1648. if (res != RX_CONTINUE) \
  1649. goto rxh_done; \
  1650. } while (0);
  1651. CALL_RXH(ieee80211_rx_h_passive_scan)
  1652. CALL_RXH(ieee80211_rx_h_check)
  1653. CALL_RXH(ieee80211_rx_h_decrypt)
  1654. CALL_RXH(ieee80211_rx_h_check_more_data)
  1655. CALL_RXH(ieee80211_rx_h_sta_process)
  1656. CALL_RXH(ieee80211_rx_h_defragment)
  1657. CALL_RXH(ieee80211_rx_h_ps_poll)
  1658. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  1659. /* must be after MMIC verify so header is counted in MPDU mic */
  1660. CALL_RXH(ieee80211_rx_h_remove_qos_control)
  1661. CALL_RXH(ieee80211_rx_h_amsdu)
  1662. #ifdef CONFIG_MAC80211_MESH
  1663. if (ieee80211_vif_is_mesh(&sdata->vif))
  1664. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  1665. #endif
  1666. CALL_RXH(ieee80211_rx_h_data)
  1667. CALL_RXH(ieee80211_rx_h_ctrl)
  1668. CALL_RXH(ieee80211_rx_h_action)
  1669. CALL_RXH(ieee80211_rx_h_mgmt)
  1670. #undef CALL_RXH
  1671. rxh_done:
  1672. switch (res) {
  1673. case RX_DROP_MONITOR:
  1674. I802_DEBUG_INC(sdata->local->rx_handlers_drop);
  1675. if (rx->sta)
  1676. rx->sta->rx_dropped++;
  1677. /* fall through */
  1678. case RX_CONTINUE:
  1679. ieee80211_rx_cooked_monitor(rx, rate);
  1680. break;
  1681. case RX_DROP_UNUSABLE:
  1682. I802_DEBUG_INC(sdata->local->rx_handlers_drop);
  1683. if (rx->sta)
  1684. rx->sta->rx_dropped++;
  1685. dev_kfree_skb(rx->skb);
  1686. break;
  1687. case RX_QUEUED:
  1688. I802_DEBUG_INC(sdata->local->rx_handlers_queued);
  1689. break;
  1690. }
  1691. }
  1692. /* main receive path */
  1693. static int prepare_for_handlers(struct ieee80211_sub_if_data *sdata,
  1694. struct ieee80211_rx_data *rx,
  1695. struct ieee80211_hdr *hdr)
  1696. {
  1697. struct sk_buff *skb = rx->skb;
  1698. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1699. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  1700. int multicast = is_multicast_ether_addr(hdr->addr1);
  1701. switch (sdata->vif.type) {
  1702. case NL80211_IFTYPE_STATION:
  1703. if (!bssid && !sdata->use_4addr)
  1704. return 0;
  1705. if (!multicast &&
  1706. compare_ether_addr(sdata->dev->dev_addr, hdr->addr1) != 0) {
  1707. if (!(sdata->dev->flags & IFF_PROMISC))
  1708. return 0;
  1709. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  1710. }
  1711. break;
  1712. case NL80211_IFTYPE_ADHOC:
  1713. if (!bssid)
  1714. return 0;
  1715. if (ieee80211_is_beacon(hdr->frame_control)) {
  1716. return 1;
  1717. }
  1718. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  1719. if (!(rx->flags & IEEE80211_RX_IN_SCAN))
  1720. return 0;
  1721. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  1722. } else if (!multicast &&
  1723. compare_ether_addr(sdata->dev->dev_addr,
  1724. hdr->addr1) != 0) {
  1725. if (!(sdata->dev->flags & IFF_PROMISC))
  1726. return 0;
  1727. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  1728. } else if (!rx->sta) {
  1729. int rate_idx;
  1730. if (status->flag & RX_FLAG_HT)
  1731. rate_idx = 0; /* TODO: HT rates */
  1732. else
  1733. rate_idx = status->rate_idx;
  1734. rx->sta = ieee80211_ibss_add_sta(sdata, bssid, hdr->addr2,
  1735. BIT(rate_idx));
  1736. }
  1737. break;
  1738. case NL80211_IFTYPE_MESH_POINT:
  1739. if (!multicast &&
  1740. compare_ether_addr(sdata->dev->dev_addr,
  1741. hdr->addr1) != 0) {
  1742. if (!(sdata->dev->flags & IFF_PROMISC))
  1743. return 0;
  1744. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  1745. }
  1746. break;
  1747. case NL80211_IFTYPE_AP_VLAN:
  1748. case NL80211_IFTYPE_AP:
  1749. if (!bssid) {
  1750. if (compare_ether_addr(sdata->dev->dev_addr,
  1751. hdr->addr1))
  1752. return 0;
  1753. } else if (!ieee80211_bssid_match(bssid,
  1754. sdata->dev->dev_addr)) {
  1755. if (!(rx->flags & IEEE80211_RX_IN_SCAN))
  1756. return 0;
  1757. rx->flags &= ~IEEE80211_RX_RA_MATCH;
  1758. }
  1759. break;
  1760. case NL80211_IFTYPE_WDS:
  1761. if (bssid || !ieee80211_is_data(hdr->frame_control))
  1762. return 0;
  1763. if (compare_ether_addr(sdata->u.wds.remote_addr, hdr->addr2))
  1764. return 0;
  1765. break;
  1766. case NL80211_IFTYPE_MONITOR:
  1767. case NL80211_IFTYPE_UNSPECIFIED:
  1768. case __NL80211_IFTYPE_AFTER_LAST:
  1769. /* should never get here */
  1770. WARN_ON(1);
  1771. break;
  1772. }
  1773. return 1;
  1774. }
  1775. /*
  1776. * This is the actual Rx frames handler. as it blongs to Rx path it must
  1777. * be called with rcu_read_lock protection.
  1778. */
  1779. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  1780. struct sk_buff *skb,
  1781. struct ieee80211_rate *rate)
  1782. {
  1783. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1784. struct ieee80211_local *local = hw_to_local(hw);
  1785. struct ieee80211_sub_if_data *sdata;
  1786. struct ieee80211_hdr *hdr;
  1787. struct ieee80211_rx_data rx;
  1788. int prepares;
  1789. struct ieee80211_sub_if_data *prev = NULL;
  1790. struct sk_buff *skb_new;
  1791. hdr = (struct ieee80211_hdr *)skb->data;
  1792. memset(&rx, 0, sizeof(rx));
  1793. rx.skb = skb;
  1794. rx.local = local;
  1795. if (ieee80211_is_data(hdr->frame_control) || ieee80211_is_mgmt(hdr->frame_control))
  1796. local->dot11ReceivedFragmentCount++;
  1797. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  1798. test_bit(SCAN_OFF_CHANNEL, &local->scanning)))
  1799. rx.flags |= IEEE80211_RX_IN_SCAN;
  1800. ieee80211_parse_qos(&rx);
  1801. ieee80211_verify_alignment(&rx);
  1802. rx.sta = sta_info_get(local, hdr->addr2);
  1803. if (rx.sta)
  1804. rx.sdata = rx.sta->sdata;
  1805. if (rx.sdata && ieee80211_is_data(hdr->frame_control)) {
  1806. rx.flags |= IEEE80211_RX_RA_MATCH;
  1807. prepares = prepare_for_handlers(rx.sdata, &rx, hdr);
  1808. if (prepares) {
  1809. if (status->flag & RX_FLAG_MMIC_ERROR) {
  1810. if (rx.flags & IEEE80211_RX_RA_MATCH)
  1811. ieee80211_rx_michael_mic_report(hdr, &rx);
  1812. } else
  1813. prev = rx.sdata;
  1814. }
  1815. } else list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1816. if (!netif_running(sdata->dev))
  1817. continue;
  1818. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1819. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1820. continue;
  1821. rx.flags |= IEEE80211_RX_RA_MATCH;
  1822. prepares = prepare_for_handlers(sdata, &rx, hdr);
  1823. if (!prepares)
  1824. continue;
  1825. if (status->flag & RX_FLAG_MMIC_ERROR) {
  1826. rx.sdata = sdata;
  1827. if (rx.flags & IEEE80211_RX_RA_MATCH)
  1828. ieee80211_rx_michael_mic_report(hdr, &rx);
  1829. continue;
  1830. }
  1831. /*
  1832. * frame is destined for this interface, but if it's not
  1833. * also for the previous one we handle that after the
  1834. * loop to avoid copying the SKB once too much
  1835. */
  1836. if (!prev) {
  1837. prev = sdata;
  1838. continue;
  1839. }
  1840. /*
  1841. * frame was destined for the previous interface
  1842. * so invoke RX handlers for it
  1843. */
  1844. skb_new = skb_copy(skb, GFP_ATOMIC);
  1845. if (!skb_new) {
  1846. if (net_ratelimit())
  1847. printk(KERN_DEBUG "%s: failed to copy "
  1848. "multicast frame for %s\n",
  1849. wiphy_name(local->hw.wiphy),
  1850. prev->dev->name);
  1851. continue;
  1852. }
  1853. ieee80211_invoke_rx_handlers(prev, &rx, skb_new, rate);
  1854. prev = sdata;
  1855. }
  1856. if (prev)
  1857. ieee80211_invoke_rx_handlers(prev, &rx, skb, rate);
  1858. else
  1859. dev_kfree_skb(skb);
  1860. }
  1861. #define SEQ_MODULO 0x1000
  1862. #define SEQ_MASK 0xfff
  1863. static inline int seq_less(u16 sq1, u16 sq2)
  1864. {
  1865. return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
  1866. }
  1867. static inline u16 seq_inc(u16 sq)
  1868. {
  1869. return (sq + 1) & SEQ_MASK;
  1870. }
  1871. static inline u16 seq_sub(u16 sq1, u16 sq2)
  1872. {
  1873. return (sq1 - sq2) & SEQ_MASK;
  1874. }
  1875. static void ieee80211_release_reorder_frame(struct ieee80211_hw *hw,
  1876. struct tid_ampdu_rx *tid_agg_rx,
  1877. int index)
  1878. {
  1879. struct ieee80211_supported_band *sband;
  1880. struct ieee80211_rate *rate = NULL;
  1881. struct sk_buff *skb = tid_agg_rx->reorder_buf[index];
  1882. struct ieee80211_rx_status *status;
  1883. if (!skb)
  1884. goto no_frame;
  1885. status = IEEE80211_SKB_RXCB(skb);
  1886. /* release the reordered frames to stack */
  1887. sband = hw->wiphy->bands[status->band];
  1888. if (!(status->flag & RX_FLAG_HT))
  1889. rate = &sband->bitrates[status->rate_idx];
  1890. __ieee80211_rx_handle_packet(hw, skb, rate);
  1891. tid_agg_rx->stored_mpdu_num--;
  1892. tid_agg_rx->reorder_buf[index] = NULL;
  1893. no_frame:
  1894. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  1895. }
  1896. static void ieee80211_release_reorder_frames(struct ieee80211_hw *hw,
  1897. struct tid_ampdu_rx *tid_agg_rx,
  1898. u16 head_seq_num)
  1899. {
  1900. int index;
  1901. while (seq_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  1902. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  1903. tid_agg_rx->buf_size;
  1904. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  1905. }
  1906. }
  1907. /*
  1908. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  1909. * the skb was added to the buffer longer than this time ago, the earlier
  1910. * frames that have not yet been received are assumed to be lost and the skb
  1911. * can be released for processing. This may also release other skb's from the
  1912. * reorder buffer if there are no additional gaps between the frames.
  1913. */
  1914. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  1915. /*
  1916. * As this function belongs to the RX path it must be under
  1917. * rcu_read_lock protection. It returns false if the frame
  1918. * can be processed immediately, true if it was consumed.
  1919. */
  1920. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_hw *hw,
  1921. struct tid_ampdu_rx *tid_agg_rx,
  1922. struct sk_buff *skb)
  1923. {
  1924. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1925. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  1926. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1927. u16 head_seq_num, buf_size;
  1928. int index;
  1929. buf_size = tid_agg_rx->buf_size;
  1930. head_seq_num = tid_agg_rx->head_seq_num;
  1931. /* frame with out of date sequence number */
  1932. if (seq_less(mpdu_seq_num, head_seq_num)) {
  1933. dev_kfree_skb(skb);
  1934. return true;
  1935. }
  1936. /*
  1937. * If frame the sequence number exceeds our buffering window
  1938. * size release some previous frames to make room for this one.
  1939. */
  1940. if (!seq_less(mpdu_seq_num, head_seq_num + buf_size)) {
  1941. head_seq_num = seq_inc(seq_sub(mpdu_seq_num, buf_size));
  1942. /* release stored frames up to new head to stack */
  1943. ieee80211_release_reorder_frames(hw, tid_agg_rx, head_seq_num);
  1944. }
  1945. /* Now the new frame is always in the range of the reordering buffer */
  1946. index = seq_sub(mpdu_seq_num, tid_agg_rx->ssn) % tid_agg_rx->buf_size;
  1947. /* check if we already stored this frame */
  1948. if (tid_agg_rx->reorder_buf[index]) {
  1949. dev_kfree_skb(skb);
  1950. return true;
  1951. }
  1952. /*
  1953. * If the current MPDU is in the right order and nothing else
  1954. * is stored we can process it directly, no need to buffer it.
  1955. */
  1956. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  1957. tid_agg_rx->stored_mpdu_num == 0) {
  1958. tid_agg_rx->head_seq_num = seq_inc(tid_agg_rx->head_seq_num);
  1959. return false;
  1960. }
  1961. /* put the frame in the reordering buffer */
  1962. tid_agg_rx->reorder_buf[index] = skb;
  1963. tid_agg_rx->reorder_time[index] = jiffies;
  1964. tid_agg_rx->stored_mpdu_num++;
  1965. /* release the buffer until next missing frame */
  1966. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  1967. tid_agg_rx->buf_size;
  1968. if (!tid_agg_rx->reorder_buf[index] &&
  1969. tid_agg_rx->stored_mpdu_num > 1) {
  1970. /*
  1971. * No buffers ready to be released, but check whether any
  1972. * frames in the reorder buffer have timed out.
  1973. */
  1974. int j;
  1975. int skipped = 1;
  1976. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  1977. j = (j + 1) % tid_agg_rx->buf_size) {
  1978. if (!tid_agg_rx->reorder_buf[j]) {
  1979. skipped++;
  1980. continue;
  1981. }
  1982. if (!time_after(jiffies, tid_agg_rx->reorder_time[j] +
  1983. HT_RX_REORDER_BUF_TIMEOUT))
  1984. break;
  1985. #ifdef CONFIG_MAC80211_HT_DEBUG
  1986. if (net_ratelimit())
  1987. printk(KERN_DEBUG "%s: release an RX reorder "
  1988. "frame due to timeout on earlier "
  1989. "frames\n",
  1990. wiphy_name(hw->wiphy));
  1991. #endif
  1992. ieee80211_release_reorder_frame(hw, tid_agg_rx, j);
  1993. /*
  1994. * Increment the head seq# also for the skipped slots.
  1995. */
  1996. tid_agg_rx->head_seq_num =
  1997. (tid_agg_rx->head_seq_num + skipped) & SEQ_MASK;
  1998. skipped = 0;
  1999. }
  2000. } else while (tid_agg_rx->reorder_buf[index]) {
  2001. ieee80211_release_reorder_frame(hw, tid_agg_rx, index);
  2002. index = seq_sub(tid_agg_rx->head_seq_num, tid_agg_rx->ssn) %
  2003. tid_agg_rx->buf_size;
  2004. }
  2005. return true;
  2006. }
  2007. /*
  2008. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  2009. * true if the MPDU was buffered, false if it should be processed.
  2010. */
  2011. static bool ieee80211_rx_reorder_ampdu(struct ieee80211_local *local,
  2012. struct sk_buff *skb)
  2013. {
  2014. struct ieee80211_hw *hw = &local->hw;
  2015. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2016. struct sta_info *sta;
  2017. struct tid_ampdu_rx *tid_agg_rx;
  2018. u16 sc;
  2019. int tid;
  2020. if (!ieee80211_is_data_qos(hdr->frame_control))
  2021. return false;
  2022. /*
  2023. * filter the QoS data rx stream according to
  2024. * STA/TID and check if this STA/TID is on aggregation
  2025. */
  2026. sta = sta_info_get(local, hdr->addr2);
  2027. if (!sta)
  2028. return false;
  2029. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  2030. if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_OPERATIONAL)
  2031. return false;
  2032. tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
  2033. /* qos null data frames are excluded */
  2034. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  2035. return false;
  2036. /* new, potentially un-ordered, ampdu frame - process it */
  2037. /* reset session timer */
  2038. if (tid_agg_rx->timeout)
  2039. mod_timer(&tid_agg_rx->session_timer,
  2040. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2041. /* if this mpdu is fragmented - terminate rx aggregation session */
  2042. sc = le16_to_cpu(hdr->seq_ctrl);
  2043. if (sc & IEEE80211_SCTL_FRAG) {
  2044. ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
  2045. tid, 0, WLAN_REASON_QSTA_REQUIRE_SETUP);
  2046. dev_kfree_skb(skb);
  2047. return true;
  2048. }
  2049. return ieee80211_sta_manage_reorder_buf(hw, tid_agg_rx, skb);
  2050. }
  2051. /*
  2052. * This is the receive path handler. It is called by a low level driver when an
  2053. * 802.11 MPDU is received from the hardware.
  2054. */
  2055. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2056. {
  2057. struct ieee80211_local *local = hw_to_local(hw);
  2058. struct ieee80211_rate *rate = NULL;
  2059. struct ieee80211_supported_band *sband;
  2060. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2061. WARN_ON_ONCE(softirq_count() == 0);
  2062. if (WARN_ON(status->band < 0 ||
  2063. status->band >= IEEE80211_NUM_BANDS))
  2064. goto drop;
  2065. sband = local->hw.wiphy->bands[status->band];
  2066. if (WARN_ON(!sband))
  2067. goto drop;
  2068. /*
  2069. * If we're suspending, it is possible although not too likely
  2070. * that we'd be receiving frames after having already partially
  2071. * quiesced the stack. We can't process such frames then since
  2072. * that might, for example, cause stations to be added or other
  2073. * driver callbacks be invoked.
  2074. */
  2075. if (unlikely(local->quiescing || local->suspended))
  2076. goto drop;
  2077. /*
  2078. * The same happens when we're not even started,
  2079. * but that's worth a warning.
  2080. */
  2081. if (WARN_ON(!local->started))
  2082. goto drop;
  2083. if (status->flag & RX_FLAG_HT) {
  2084. /*
  2085. * rate_idx is MCS index, which can be [0-76] as documented on:
  2086. *
  2087. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2088. *
  2089. * Anything else would be some sort of driver or hardware error.
  2090. * The driver should catch hardware errors.
  2091. */
  2092. if (WARN((status->rate_idx < 0 ||
  2093. status->rate_idx > 76),
  2094. "Rate marked as an HT rate but passed "
  2095. "status->rate_idx is not "
  2096. "an MCS index [0-76]: %d (0x%02x)\n",
  2097. status->rate_idx,
  2098. status->rate_idx))
  2099. goto drop;
  2100. } else {
  2101. if (WARN_ON(status->rate_idx < 0 ||
  2102. status->rate_idx >= sband->n_bitrates))
  2103. goto drop;
  2104. rate = &sband->bitrates[status->rate_idx];
  2105. }
  2106. /*
  2107. * key references and virtual interfaces are protected using RCU
  2108. * and this requires that we are in a read-side RCU section during
  2109. * receive processing
  2110. */
  2111. rcu_read_lock();
  2112. /*
  2113. * Frames with failed FCS/PLCP checksum are not returned,
  2114. * all other frames are returned without radiotap header
  2115. * if it was previously present.
  2116. * Also, frames with less than 16 bytes are dropped.
  2117. */
  2118. skb = ieee80211_rx_monitor(local, skb, rate);
  2119. if (!skb) {
  2120. rcu_read_unlock();
  2121. return;
  2122. }
  2123. /*
  2124. * In theory, the block ack reordering should happen after duplicate
  2125. * removal (ieee80211_rx_h_check(), which is an RX handler). As such,
  2126. * the call to ieee80211_rx_reorder_ampdu() should really be moved to
  2127. * happen as a new RX handler between ieee80211_rx_h_check and
  2128. * ieee80211_rx_h_decrypt. This cleanup may eventually happen, but for
  2129. * the time being, the call can be here since RX reorder buf processing
  2130. * will implicitly skip duplicates. We could, in theory at least,
  2131. * process frames that ieee80211_rx_h_passive_scan would drop (e.g.,
  2132. * frames from other than operational channel), but that should not
  2133. * happen in normal networks.
  2134. */
  2135. if (!ieee80211_rx_reorder_ampdu(local, skb))
  2136. __ieee80211_rx_handle_packet(hw, skb, rate);
  2137. rcu_read_unlock();
  2138. return;
  2139. drop:
  2140. kfree_skb(skb);
  2141. }
  2142. EXPORT_SYMBOL(ieee80211_rx);
  2143. /* This is a version of the rx handler that can be called from hard irq
  2144. * context. Post the skb on the queue and schedule the tasklet */
  2145. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2146. {
  2147. struct ieee80211_local *local = hw_to_local(hw);
  2148. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2149. skb->pkt_type = IEEE80211_RX_MSG;
  2150. skb_queue_tail(&local->skb_queue, skb);
  2151. tasklet_schedule(&local->tasklet);
  2152. }
  2153. EXPORT_SYMBOL(ieee80211_rx_irqsafe);