rx.c 70 KB

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