rx.c 72 KB

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