rx.c 72 KB

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