rx.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007-2010 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/jiffies.h>
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/export.h>
  20. #include <net/mac80211.h>
  21. #include <net/ieee80211_radiotap.h>
  22. #include <asm/unaligned.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "led.h"
  26. #include "mesh.h"
  27. #include "wep.h"
  28. #include "wpa.h"
  29. #include "tkip.h"
  30. #include "wme.h"
  31. #include "rate.h"
  32. static inline void ieee80211_rx_stats(struct net_device *dev, u32 len)
  33. {
  34. struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
  35. u64_stats_update_begin(&tstats->syncp);
  36. tstats->rx_packets++;
  37. tstats->rx_bytes += len;
  38. u64_stats_update_end(&tstats->syncp);
  39. }
  40. /*
  41. * monitor mode reception
  42. *
  43. * This function cleans up the SKB, i.e. it removes all the stuff
  44. * only useful for monitoring.
  45. */
  46. static struct sk_buff *remove_monitor_info(struct ieee80211_local *local,
  47. struct sk_buff *skb,
  48. unsigned int rtap_vendor_space)
  49. {
  50. if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)) {
  51. if (likely(skb->len > FCS_LEN))
  52. __pskb_trim(skb, skb->len - FCS_LEN);
  53. else {
  54. /* driver bug */
  55. WARN_ON(1);
  56. dev_kfree_skb(skb);
  57. return NULL;
  58. }
  59. }
  60. __pskb_pull(skb, rtap_vendor_space);
  61. return skb;
  62. }
  63. static inline bool should_drop_frame(struct sk_buff *skb, int present_fcs_len,
  64. unsigned int rtap_vendor_space)
  65. {
  66. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  67. struct ieee80211_hdr *hdr;
  68. hdr = (void *)(skb->data + rtap_vendor_space);
  69. if (status->flag & (RX_FLAG_FAILED_FCS_CRC |
  70. RX_FLAG_FAILED_PLCP_CRC |
  71. RX_FLAG_AMPDU_IS_ZEROLEN))
  72. return true;
  73. if (unlikely(skb->len < 16 + present_fcs_len + rtap_vendor_space))
  74. return true;
  75. if (ieee80211_is_ctl(hdr->frame_control) &&
  76. !ieee80211_is_pspoll(hdr->frame_control) &&
  77. !ieee80211_is_back_req(hdr->frame_control))
  78. return true;
  79. return false;
  80. }
  81. static int
  82. ieee80211_rx_radiotap_hdrlen(struct ieee80211_local *local,
  83. struct ieee80211_rx_status *status,
  84. struct sk_buff *skb)
  85. {
  86. int len;
  87. /* always present fields */
  88. len = sizeof(struct ieee80211_radiotap_header) + 8;
  89. /* allocate extra bitmaps */
  90. if (status->chains)
  91. len += 4 * hweight8(status->chains);
  92. if (ieee80211_have_rx_timestamp(status)) {
  93. len = ALIGN(len, 8);
  94. len += 8;
  95. }
  96. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
  97. len += 1;
  98. /* antenna field, if we don't have per-chain info */
  99. if (!status->chains)
  100. len += 1;
  101. /* padding for RX_FLAGS if necessary */
  102. len = ALIGN(len, 2);
  103. if (status->flag & RX_FLAG_HT) /* HT info */
  104. len += 3;
  105. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  106. len = ALIGN(len, 4);
  107. len += 8;
  108. }
  109. if (status->flag & RX_FLAG_VHT) {
  110. len = ALIGN(len, 2);
  111. len += 12;
  112. }
  113. if (status->chains) {
  114. /* antenna and antenna signal fields */
  115. len += 2 * hweight8(status->chains);
  116. }
  117. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  118. struct ieee80211_vendor_radiotap *rtap = (void *)skb->data;
  119. /* vendor presence bitmap */
  120. len += 4;
  121. /* alignment for fixed 6-byte vendor data header */
  122. len = ALIGN(len, 2);
  123. /* vendor data header */
  124. len += 6;
  125. if (WARN_ON(rtap->align == 0))
  126. rtap->align = 1;
  127. len = ALIGN(len, rtap->align);
  128. len += rtap->len + rtap->pad;
  129. }
  130. return len;
  131. }
  132. /*
  133. * ieee80211_add_rx_radiotap_header - add radiotap header
  134. *
  135. * add a radiotap header containing all the fields which the hardware provided.
  136. */
  137. static void
  138. ieee80211_add_rx_radiotap_header(struct ieee80211_local *local,
  139. struct sk_buff *skb,
  140. struct ieee80211_rate *rate,
  141. int rtap_len, bool has_fcs)
  142. {
  143. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  144. struct ieee80211_radiotap_header *rthdr;
  145. unsigned char *pos;
  146. __le32 *it_present;
  147. u32 it_present_val;
  148. u16 rx_flags = 0;
  149. u16 channel_flags = 0;
  150. int mpdulen, chain;
  151. unsigned long chains = status->chains;
  152. struct ieee80211_vendor_radiotap rtap = {};
  153. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  154. rtap = *(struct ieee80211_vendor_radiotap *)skb->data;
  155. /* rtap.len and rtap.pad are undone immediately */
  156. skb_pull(skb, sizeof(rtap) + rtap.len + rtap.pad);
  157. }
  158. mpdulen = skb->len;
  159. if (!(has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS)))
  160. mpdulen += FCS_LEN;
  161. rthdr = (struct ieee80211_radiotap_header *)skb_push(skb, rtap_len);
  162. memset(rthdr, 0, rtap_len - rtap.len - rtap.pad);
  163. it_present = &rthdr->it_present;
  164. /* radiotap header, set always present flags */
  165. rthdr->it_len = cpu_to_le16(rtap_len);
  166. it_present_val = BIT(IEEE80211_RADIOTAP_FLAGS) |
  167. BIT(IEEE80211_RADIOTAP_CHANNEL) |
  168. BIT(IEEE80211_RADIOTAP_RX_FLAGS);
  169. if (!status->chains)
  170. it_present_val |= BIT(IEEE80211_RADIOTAP_ANTENNA);
  171. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  172. it_present_val |=
  173. BIT(IEEE80211_RADIOTAP_EXT) |
  174. BIT(IEEE80211_RADIOTAP_RADIOTAP_NAMESPACE);
  175. put_unaligned_le32(it_present_val, it_present);
  176. it_present++;
  177. it_present_val = BIT(IEEE80211_RADIOTAP_ANTENNA) |
  178. BIT(IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  179. }
  180. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  181. it_present_val |= BIT(IEEE80211_RADIOTAP_VENDOR_NAMESPACE) |
  182. BIT(IEEE80211_RADIOTAP_EXT);
  183. put_unaligned_le32(it_present_val, it_present);
  184. it_present++;
  185. it_present_val = rtap.present;
  186. }
  187. put_unaligned_le32(it_present_val, it_present);
  188. pos = (void *)(it_present + 1);
  189. /* the order of the following fields is important */
  190. /* IEEE80211_RADIOTAP_TSFT */
  191. if (ieee80211_have_rx_timestamp(status)) {
  192. /* padding */
  193. while ((pos - (u8 *)rthdr) & 7)
  194. *pos++ = 0;
  195. put_unaligned_le64(
  196. ieee80211_calculate_rx_timestamp(local, status,
  197. mpdulen, 0),
  198. pos);
  199. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_TSFT);
  200. pos += 8;
  201. }
  202. /* IEEE80211_RADIOTAP_FLAGS */
  203. if (has_fcs && ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
  204. *pos |= IEEE80211_RADIOTAP_F_FCS;
  205. if (status->flag & (RX_FLAG_FAILED_FCS_CRC | RX_FLAG_FAILED_PLCP_CRC))
  206. *pos |= IEEE80211_RADIOTAP_F_BADFCS;
  207. if (status->flag & RX_FLAG_SHORTPRE)
  208. *pos |= IEEE80211_RADIOTAP_F_SHORTPRE;
  209. pos++;
  210. /* IEEE80211_RADIOTAP_RATE */
  211. if (!rate || status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) {
  212. /*
  213. * Without rate information don't add it. If we have,
  214. * MCS information is a separate field in radiotap,
  215. * added below. The byte here is needed as padding
  216. * for the channel though, so initialise it to 0.
  217. */
  218. *pos = 0;
  219. } else {
  220. int shift = 0;
  221. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_RATE);
  222. if (status->flag & RX_FLAG_10MHZ)
  223. shift = 1;
  224. else if (status->flag & RX_FLAG_5MHZ)
  225. shift = 2;
  226. *pos = DIV_ROUND_UP(rate->bitrate, 5 * (1 << shift));
  227. }
  228. pos++;
  229. /* IEEE80211_RADIOTAP_CHANNEL */
  230. put_unaligned_le16(status->freq, pos);
  231. pos += 2;
  232. if (status->flag & RX_FLAG_10MHZ)
  233. channel_flags |= IEEE80211_CHAN_HALF;
  234. else if (status->flag & RX_FLAG_5MHZ)
  235. channel_flags |= IEEE80211_CHAN_QUARTER;
  236. if (status->band == IEEE80211_BAND_5GHZ)
  237. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ;
  238. else if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
  239. channel_flags |= IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
  240. else if (rate && rate->flags & IEEE80211_RATE_ERP_G)
  241. channel_flags |= IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ;
  242. else if (rate)
  243. channel_flags |= IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ;
  244. else
  245. channel_flags |= IEEE80211_CHAN_2GHZ;
  246. put_unaligned_le16(channel_flags, pos);
  247. pos += 2;
  248. /* IEEE80211_RADIOTAP_DBM_ANTSIGNAL */
  249. if (ieee80211_hw_check(&local->hw, SIGNAL_DBM) &&
  250. !(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  251. *pos = status->signal;
  252. rthdr->it_present |=
  253. cpu_to_le32(1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL);
  254. pos++;
  255. }
  256. /* IEEE80211_RADIOTAP_LOCK_QUALITY is missing */
  257. if (!status->chains) {
  258. /* IEEE80211_RADIOTAP_ANTENNA */
  259. *pos = status->antenna;
  260. pos++;
  261. }
  262. /* IEEE80211_RADIOTAP_DB_ANTNOISE is not used */
  263. /* IEEE80211_RADIOTAP_RX_FLAGS */
  264. /* ensure 2 byte alignment for the 2 byte field as required */
  265. if ((pos - (u8 *)rthdr) & 1)
  266. *pos++ = 0;
  267. if (status->flag & RX_FLAG_FAILED_PLCP_CRC)
  268. rx_flags |= IEEE80211_RADIOTAP_F_RX_BADPLCP;
  269. put_unaligned_le16(rx_flags, pos);
  270. pos += 2;
  271. if (status->flag & RX_FLAG_HT) {
  272. unsigned int stbc;
  273. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_MCS);
  274. *pos++ = local->hw.radiotap_mcs_details;
  275. *pos = 0;
  276. if (status->flag & RX_FLAG_SHORT_GI)
  277. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  278. if (status->flag & RX_FLAG_40MHZ)
  279. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  280. if (status->flag & RX_FLAG_HT_GF)
  281. *pos |= IEEE80211_RADIOTAP_MCS_FMT_GF;
  282. if (status->flag & RX_FLAG_LDPC)
  283. *pos |= IEEE80211_RADIOTAP_MCS_FEC_LDPC;
  284. stbc = (status->flag & RX_FLAG_STBC_MASK) >> RX_FLAG_STBC_SHIFT;
  285. *pos |= stbc << IEEE80211_RADIOTAP_MCS_STBC_SHIFT;
  286. pos++;
  287. *pos++ = status->rate_idx;
  288. }
  289. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  290. u16 flags = 0;
  291. /* ensure 4 byte alignment */
  292. while ((pos - (u8 *)rthdr) & 3)
  293. pos++;
  294. rthdr->it_present |=
  295. cpu_to_le32(1 << IEEE80211_RADIOTAP_AMPDU_STATUS);
  296. put_unaligned_le32(status->ampdu_reference, pos);
  297. pos += 4;
  298. if (status->flag & RX_FLAG_AMPDU_REPORT_ZEROLEN)
  299. flags |= IEEE80211_RADIOTAP_AMPDU_REPORT_ZEROLEN;
  300. if (status->flag & RX_FLAG_AMPDU_IS_ZEROLEN)
  301. flags |= IEEE80211_RADIOTAP_AMPDU_IS_ZEROLEN;
  302. if (status->flag & RX_FLAG_AMPDU_LAST_KNOWN)
  303. flags |= IEEE80211_RADIOTAP_AMPDU_LAST_KNOWN;
  304. if (status->flag & RX_FLAG_AMPDU_IS_LAST)
  305. flags |= IEEE80211_RADIOTAP_AMPDU_IS_LAST;
  306. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_ERROR)
  307. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_ERR;
  308. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  309. flags |= IEEE80211_RADIOTAP_AMPDU_DELIM_CRC_KNOWN;
  310. put_unaligned_le16(flags, pos);
  311. pos += 2;
  312. if (status->flag & RX_FLAG_AMPDU_DELIM_CRC_KNOWN)
  313. *pos++ = status->ampdu_delimiter_crc;
  314. else
  315. *pos++ = 0;
  316. *pos++ = 0;
  317. }
  318. if (status->flag & RX_FLAG_VHT) {
  319. u16 known = local->hw.radiotap_vht_details;
  320. rthdr->it_present |= cpu_to_le32(1 << IEEE80211_RADIOTAP_VHT);
  321. put_unaligned_le16(known, pos);
  322. pos += 2;
  323. /* flags */
  324. if (status->flag & RX_FLAG_SHORT_GI)
  325. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_SGI;
  326. /* in VHT, STBC is binary */
  327. if (status->flag & RX_FLAG_STBC_MASK)
  328. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_STBC;
  329. if (status->vht_flag & RX_VHT_FLAG_BF)
  330. *pos |= IEEE80211_RADIOTAP_VHT_FLAG_BEAMFORMED;
  331. pos++;
  332. /* bandwidth */
  333. if (status->vht_flag & RX_VHT_FLAG_80MHZ)
  334. *pos++ = 4;
  335. else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
  336. *pos++ = 11;
  337. else if (status->flag & RX_FLAG_40MHZ)
  338. *pos++ = 1;
  339. else /* 20 MHz */
  340. *pos++ = 0;
  341. /* MCS/NSS */
  342. *pos = (status->rate_idx << 4) | status->vht_nss;
  343. pos += 4;
  344. /* coding field */
  345. if (status->flag & RX_FLAG_LDPC)
  346. *pos |= IEEE80211_RADIOTAP_CODING_LDPC_USER0;
  347. pos++;
  348. /* group ID */
  349. pos++;
  350. /* partial_aid */
  351. pos += 2;
  352. }
  353. for_each_set_bit(chain, &chains, IEEE80211_MAX_CHAINS) {
  354. *pos++ = status->chain_signal[chain];
  355. *pos++ = chain;
  356. }
  357. if (status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  358. /* ensure 2 byte alignment for the vendor field as required */
  359. if ((pos - (u8 *)rthdr) & 1)
  360. *pos++ = 0;
  361. *pos++ = rtap.oui[0];
  362. *pos++ = rtap.oui[1];
  363. *pos++ = rtap.oui[2];
  364. *pos++ = rtap.subns;
  365. put_unaligned_le16(rtap.len, pos);
  366. pos += 2;
  367. /* align the actual payload as requested */
  368. while ((pos - (u8 *)rthdr) & (rtap.align - 1))
  369. *pos++ = 0;
  370. /* data (and possible padding) already follows */
  371. }
  372. }
  373. /*
  374. * This function copies a received frame to all monitor interfaces and
  375. * returns a cleaned-up SKB that no longer includes the FCS nor the
  376. * radiotap header the driver might have added.
  377. */
  378. static struct sk_buff *
  379. ieee80211_rx_monitor(struct ieee80211_local *local, struct sk_buff *origskb,
  380. struct ieee80211_rate *rate)
  381. {
  382. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(origskb);
  383. struct ieee80211_sub_if_data *sdata;
  384. int rt_hdrlen, needed_headroom;
  385. struct sk_buff *skb, *skb2;
  386. struct net_device *prev_dev = NULL;
  387. int present_fcs_len = 0;
  388. unsigned int rtap_vendor_space = 0;
  389. if (unlikely(status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA)) {
  390. struct ieee80211_vendor_radiotap *rtap = (void *)origskb->data;
  391. rtap_vendor_space = sizeof(*rtap) + rtap->len + rtap->pad;
  392. }
  393. /*
  394. * First, we may need to make a copy of the skb because
  395. * (1) we need to modify it for radiotap (if not present), and
  396. * (2) the other RX handlers will modify the skb we got.
  397. *
  398. * We don't need to, of course, if we aren't going to return
  399. * the SKB because it has a bad FCS/PLCP checksum.
  400. */
  401. if (ieee80211_hw_check(&local->hw, RX_INCLUDES_FCS))
  402. present_fcs_len = FCS_LEN;
  403. /* ensure hdr->frame_control and vendor radiotap data are in skb head */
  404. if (!pskb_may_pull(origskb, 2 + rtap_vendor_space)) {
  405. dev_kfree_skb(origskb);
  406. return NULL;
  407. }
  408. if (!local->monitors) {
  409. if (should_drop_frame(origskb, present_fcs_len,
  410. rtap_vendor_space)) {
  411. dev_kfree_skb(origskb);
  412. return NULL;
  413. }
  414. return remove_monitor_info(local, origskb, rtap_vendor_space);
  415. }
  416. /* room for the radiotap header based on driver features */
  417. rt_hdrlen = ieee80211_rx_radiotap_hdrlen(local, status, origskb);
  418. needed_headroom = rt_hdrlen - rtap_vendor_space;
  419. if (should_drop_frame(origskb, present_fcs_len, rtap_vendor_space)) {
  420. /* only need to expand headroom if necessary */
  421. skb = origskb;
  422. origskb = NULL;
  423. /*
  424. * This shouldn't trigger often because most devices have an
  425. * RX header they pull before we get here, and that should
  426. * be big enough for our radiotap information. We should
  427. * probably export the length to drivers so that we can have
  428. * them allocate enough headroom to start with.
  429. */
  430. if (skb_headroom(skb) < needed_headroom &&
  431. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC)) {
  432. dev_kfree_skb(skb);
  433. return NULL;
  434. }
  435. } else {
  436. /*
  437. * Need to make a copy and possibly remove radiotap header
  438. * and FCS from the original.
  439. */
  440. skb = skb_copy_expand(origskb, needed_headroom, 0, GFP_ATOMIC);
  441. origskb = remove_monitor_info(local, origskb,
  442. rtap_vendor_space);
  443. if (!skb)
  444. return origskb;
  445. }
  446. /* prepend radiotap information */
  447. ieee80211_add_rx_radiotap_header(local, skb, rate, rt_hdrlen, true);
  448. skb_reset_mac_header(skb);
  449. skb->ip_summed = CHECKSUM_UNNECESSARY;
  450. skb->pkt_type = PACKET_OTHERHOST;
  451. skb->protocol = htons(ETH_P_802_2);
  452. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  453. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  454. continue;
  455. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES)
  456. continue;
  457. if (!ieee80211_sdata_running(sdata))
  458. continue;
  459. if (prev_dev) {
  460. skb2 = skb_clone(skb, GFP_ATOMIC);
  461. if (skb2) {
  462. skb2->dev = prev_dev;
  463. netif_receive_skb(skb2);
  464. }
  465. }
  466. prev_dev = sdata->dev;
  467. ieee80211_rx_stats(sdata->dev, skb->len);
  468. }
  469. if (prev_dev) {
  470. skb->dev = prev_dev;
  471. netif_receive_skb(skb);
  472. } else
  473. dev_kfree_skb(skb);
  474. return origskb;
  475. }
  476. static void ieee80211_parse_qos(struct ieee80211_rx_data *rx)
  477. {
  478. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  479. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  480. int tid, seqno_idx, security_idx;
  481. /* does the frame have a qos control field? */
  482. if (ieee80211_is_data_qos(hdr->frame_control)) {
  483. u8 *qc = ieee80211_get_qos_ctl(hdr);
  484. /* frame has qos control */
  485. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  486. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  487. status->rx_flags |= IEEE80211_RX_AMSDU;
  488. seqno_idx = tid;
  489. security_idx = tid;
  490. } else {
  491. /*
  492. * IEEE 802.11-2007, 7.1.3.4.1 ("Sequence Number field"):
  493. *
  494. * Sequence numbers for management frames, QoS data
  495. * frames with a broadcast/multicast address in the
  496. * Address 1 field, and all non-QoS data frames sent
  497. * by QoS STAs are assigned using an additional single
  498. * modulo-4096 counter, [...]
  499. *
  500. * We also use that counter for non-QoS STAs.
  501. */
  502. seqno_idx = IEEE80211_NUM_TIDS;
  503. security_idx = 0;
  504. if (ieee80211_is_mgmt(hdr->frame_control))
  505. security_idx = IEEE80211_NUM_TIDS;
  506. tid = 0;
  507. }
  508. rx->seqno_idx = seqno_idx;
  509. rx->security_idx = security_idx;
  510. /* Set skb->priority to 1d tag if highest order bit of TID is not set.
  511. * For now, set skb->priority to 0 for other cases. */
  512. rx->skb->priority = (tid > 7) ? 0 : tid;
  513. }
  514. /**
  515. * DOC: Packet alignment
  516. *
  517. * Drivers always need to pass packets that are aligned to two-byte boundaries
  518. * to the stack.
  519. *
  520. * Additionally, should, if possible, align the payload data in a way that
  521. * guarantees that the contained IP header is aligned to a four-byte
  522. * boundary. In the case of regular frames, this simply means aligning the
  523. * payload to a four-byte boundary (because either the IP header is directly
  524. * contained, or IV/RFC1042 headers that have a length divisible by four are
  525. * in front of it). If the payload data is not properly aligned and the
  526. * architecture doesn't support efficient unaligned operations, mac80211
  527. * will align the data.
  528. *
  529. * With A-MSDU frames, however, the payload data address must yield two modulo
  530. * four because there are 14-byte 802.3 headers within the A-MSDU frames that
  531. * push the IP header further back to a multiple of four again. Thankfully, the
  532. * specs were sane enough this time around to require padding each A-MSDU
  533. * subframe to a length that is a multiple of four.
  534. *
  535. * Padding like Atheros hardware adds which is between the 802.11 header and
  536. * the payload is not supported, the driver is required to move the 802.11
  537. * header to be directly in front of the payload in that case.
  538. */
  539. static void ieee80211_verify_alignment(struct ieee80211_rx_data *rx)
  540. {
  541. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  542. WARN_ONCE((unsigned long)rx->skb->data & 1,
  543. "unaligned packet at 0x%p\n", rx->skb->data);
  544. #endif
  545. }
  546. /* rx handlers */
  547. static int ieee80211_is_unicast_robust_mgmt_frame(struct sk_buff *skb)
  548. {
  549. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  550. if (is_multicast_ether_addr(hdr->addr1))
  551. return 0;
  552. return ieee80211_is_robust_mgmt_frame(skb);
  553. }
  554. static int ieee80211_is_multicast_robust_mgmt_frame(struct sk_buff *skb)
  555. {
  556. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  557. if (!is_multicast_ether_addr(hdr->addr1))
  558. return 0;
  559. return ieee80211_is_robust_mgmt_frame(skb);
  560. }
  561. /* Get the BIP key index from MMIE; return -1 if this is not a BIP frame */
  562. static int ieee80211_get_mmie_keyidx(struct sk_buff *skb)
  563. {
  564. struct ieee80211_mgmt *hdr = (struct ieee80211_mgmt *) skb->data;
  565. struct ieee80211_mmie *mmie;
  566. struct ieee80211_mmie_16 *mmie16;
  567. if (skb->len < 24 + sizeof(*mmie) || !is_multicast_ether_addr(hdr->da))
  568. return -1;
  569. if (!ieee80211_is_robust_mgmt_frame(skb))
  570. return -1; /* not a robust management frame */
  571. mmie = (struct ieee80211_mmie *)
  572. (skb->data + skb->len - sizeof(*mmie));
  573. if (mmie->element_id == WLAN_EID_MMIE &&
  574. mmie->length == sizeof(*mmie) - 2)
  575. return le16_to_cpu(mmie->key_id);
  576. mmie16 = (struct ieee80211_mmie_16 *)
  577. (skb->data + skb->len - sizeof(*mmie16));
  578. if (skb->len >= 24 + sizeof(*mmie16) &&
  579. mmie16->element_id == WLAN_EID_MMIE &&
  580. mmie16->length == sizeof(*mmie16) - 2)
  581. return le16_to_cpu(mmie16->key_id);
  582. return -1;
  583. }
  584. static int iwl80211_get_cs_keyid(const struct ieee80211_cipher_scheme *cs,
  585. struct sk_buff *skb)
  586. {
  587. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  588. __le16 fc;
  589. int hdrlen;
  590. u8 keyid;
  591. fc = hdr->frame_control;
  592. hdrlen = ieee80211_hdrlen(fc);
  593. if (skb->len < hdrlen + cs->hdr_len)
  594. return -EINVAL;
  595. skb_copy_bits(skb, hdrlen + cs->key_idx_off, &keyid, 1);
  596. keyid &= cs->key_idx_mask;
  597. keyid >>= cs->key_idx_shift;
  598. return keyid;
  599. }
  600. static ieee80211_rx_result ieee80211_rx_mesh_check(struct ieee80211_rx_data *rx)
  601. {
  602. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  603. char *dev_addr = rx->sdata->vif.addr;
  604. if (ieee80211_is_data(hdr->frame_control)) {
  605. if (is_multicast_ether_addr(hdr->addr1)) {
  606. if (ieee80211_has_tods(hdr->frame_control) ||
  607. !ieee80211_has_fromds(hdr->frame_control))
  608. return RX_DROP_MONITOR;
  609. if (ether_addr_equal(hdr->addr3, dev_addr))
  610. return RX_DROP_MONITOR;
  611. } else {
  612. if (!ieee80211_has_a4(hdr->frame_control))
  613. return RX_DROP_MONITOR;
  614. if (ether_addr_equal(hdr->addr4, dev_addr))
  615. return RX_DROP_MONITOR;
  616. }
  617. }
  618. /* If there is not an established peer link and this is not a peer link
  619. * establisment frame, beacon or probe, drop the frame.
  620. */
  621. if (!rx->sta || sta_plink_state(rx->sta) != NL80211_PLINK_ESTAB) {
  622. struct ieee80211_mgmt *mgmt;
  623. if (!ieee80211_is_mgmt(hdr->frame_control))
  624. return RX_DROP_MONITOR;
  625. if (ieee80211_is_action(hdr->frame_control)) {
  626. u8 category;
  627. /* make sure category field is present */
  628. if (rx->skb->len < IEEE80211_MIN_ACTION_SIZE)
  629. return RX_DROP_MONITOR;
  630. mgmt = (struct ieee80211_mgmt *)hdr;
  631. category = mgmt->u.action.category;
  632. if (category != WLAN_CATEGORY_MESH_ACTION &&
  633. category != WLAN_CATEGORY_SELF_PROTECTED)
  634. return RX_DROP_MONITOR;
  635. return RX_CONTINUE;
  636. }
  637. if (ieee80211_is_probe_req(hdr->frame_control) ||
  638. ieee80211_is_probe_resp(hdr->frame_control) ||
  639. ieee80211_is_beacon(hdr->frame_control) ||
  640. ieee80211_is_auth(hdr->frame_control))
  641. return RX_CONTINUE;
  642. return RX_DROP_MONITOR;
  643. }
  644. return RX_CONTINUE;
  645. }
  646. static void ieee80211_release_reorder_frame(struct ieee80211_sub_if_data *sdata,
  647. struct tid_ampdu_rx *tid_agg_rx,
  648. int index,
  649. struct sk_buff_head *frames)
  650. {
  651. struct sk_buff_head *skb_list = &tid_agg_rx->reorder_buf[index];
  652. struct sk_buff *skb;
  653. struct ieee80211_rx_status *status;
  654. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  655. if (skb_queue_empty(skb_list))
  656. goto no_frame;
  657. if (!ieee80211_rx_reorder_ready(skb_list)) {
  658. __skb_queue_purge(skb_list);
  659. goto no_frame;
  660. }
  661. /* release frames from the reorder ring buffer */
  662. tid_agg_rx->stored_mpdu_num--;
  663. while ((skb = __skb_dequeue(skb_list))) {
  664. status = IEEE80211_SKB_RXCB(skb);
  665. status->rx_flags |= IEEE80211_RX_DEFERRED_RELEASE;
  666. __skb_queue_tail(frames, skb);
  667. }
  668. no_frame:
  669. tid_agg_rx->head_seq_num = ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  670. }
  671. static void ieee80211_release_reorder_frames(struct ieee80211_sub_if_data *sdata,
  672. struct tid_ampdu_rx *tid_agg_rx,
  673. u16 head_seq_num,
  674. struct sk_buff_head *frames)
  675. {
  676. int index;
  677. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  678. while (ieee80211_sn_less(tid_agg_rx->head_seq_num, head_seq_num)) {
  679. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  680. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  681. frames);
  682. }
  683. }
  684. /*
  685. * Timeout (in jiffies) for skb's that are waiting in the RX reorder buffer. If
  686. * the skb was added to the buffer longer than this time ago, the earlier
  687. * frames that have not yet been received are assumed to be lost and the skb
  688. * can be released for processing. This may also release other skb's from the
  689. * reorder buffer if there are no additional gaps between the frames.
  690. *
  691. * Callers must hold tid_agg_rx->reorder_lock.
  692. */
  693. #define HT_RX_REORDER_BUF_TIMEOUT (HZ / 10)
  694. static void ieee80211_sta_reorder_release(struct ieee80211_sub_if_data *sdata,
  695. struct tid_ampdu_rx *tid_agg_rx,
  696. struct sk_buff_head *frames)
  697. {
  698. int index, i, j;
  699. lockdep_assert_held(&tid_agg_rx->reorder_lock);
  700. /* release the buffer until next missing frame */
  701. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  702. if (!ieee80211_rx_reorder_ready(&tid_agg_rx->reorder_buf[index]) &&
  703. tid_agg_rx->stored_mpdu_num) {
  704. /*
  705. * No buffers ready to be released, but check whether any
  706. * frames in the reorder buffer have timed out.
  707. */
  708. int skipped = 1;
  709. for (j = (index + 1) % tid_agg_rx->buf_size; j != index;
  710. j = (j + 1) % tid_agg_rx->buf_size) {
  711. if (!ieee80211_rx_reorder_ready(
  712. &tid_agg_rx->reorder_buf[j])) {
  713. skipped++;
  714. continue;
  715. }
  716. if (skipped &&
  717. !time_after(jiffies, tid_agg_rx->reorder_time[j] +
  718. HT_RX_REORDER_BUF_TIMEOUT))
  719. goto set_release_timer;
  720. /* don't leave incomplete A-MSDUs around */
  721. for (i = (index + 1) % tid_agg_rx->buf_size; i != j;
  722. i = (i + 1) % tid_agg_rx->buf_size)
  723. __skb_queue_purge(&tid_agg_rx->reorder_buf[i]);
  724. ht_dbg_ratelimited(sdata,
  725. "release an RX reorder frame due to timeout on earlier frames\n");
  726. ieee80211_release_reorder_frame(sdata, tid_agg_rx, j,
  727. frames);
  728. /*
  729. * Increment the head seq# also for the skipped slots.
  730. */
  731. tid_agg_rx->head_seq_num =
  732. (tid_agg_rx->head_seq_num +
  733. skipped) & IEEE80211_SN_MASK;
  734. skipped = 0;
  735. }
  736. } else while (ieee80211_rx_reorder_ready(
  737. &tid_agg_rx->reorder_buf[index])) {
  738. ieee80211_release_reorder_frame(sdata, tid_agg_rx, index,
  739. frames);
  740. index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  741. }
  742. if (tid_agg_rx->stored_mpdu_num) {
  743. j = index = tid_agg_rx->head_seq_num % tid_agg_rx->buf_size;
  744. for (; j != (index - 1) % tid_agg_rx->buf_size;
  745. j = (j + 1) % tid_agg_rx->buf_size) {
  746. if (ieee80211_rx_reorder_ready(
  747. &tid_agg_rx->reorder_buf[j]))
  748. break;
  749. }
  750. set_release_timer:
  751. if (!tid_agg_rx->removed)
  752. mod_timer(&tid_agg_rx->reorder_timer,
  753. tid_agg_rx->reorder_time[j] + 1 +
  754. HT_RX_REORDER_BUF_TIMEOUT);
  755. } else {
  756. del_timer(&tid_agg_rx->reorder_timer);
  757. }
  758. }
  759. /*
  760. * As this function belongs to the RX path it must be under
  761. * rcu_read_lock protection. It returns false if the frame
  762. * can be processed immediately, true if it was consumed.
  763. */
  764. static bool ieee80211_sta_manage_reorder_buf(struct ieee80211_sub_if_data *sdata,
  765. struct tid_ampdu_rx *tid_agg_rx,
  766. struct sk_buff *skb,
  767. struct sk_buff_head *frames)
  768. {
  769. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  770. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  771. u16 sc = le16_to_cpu(hdr->seq_ctrl);
  772. u16 mpdu_seq_num = (sc & IEEE80211_SCTL_SEQ) >> 4;
  773. u16 head_seq_num, buf_size;
  774. int index;
  775. bool ret = true;
  776. spin_lock(&tid_agg_rx->reorder_lock);
  777. /*
  778. * Offloaded BA sessions have no known starting sequence number so pick
  779. * one from first Rxed frame for this tid after BA was started.
  780. */
  781. if (unlikely(tid_agg_rx->auto_seq)) {
  782. tid_agg_rx->auto_seq = false;
  783. tid_agg_rx->ssn = mpdu_seq_num;
  784. tid_agg_rx->head_seq_num = mpdu_seq_num;
  785. }
  786. buf_size = tid_agg_rx->buf_size;
  787. head_seq_num = tid_agg_rx->head_seq_num;
  788. /* frame with out of date sequence number */
  789. if (ieee80211_sn_less(mpdu_seq_num, head_seq_num)) {
  790. dev_kfree_skb(skb);
  791. goto out;
  792. }
  793. /*
  794. * If frame the sequence number exceeds our buffering window
  795. * size release some previous frames to make room for this one.
  796. */
  797. if (!ieee80211_sn_less(mpdu_seq_num, head_seq_num + buf_size)) {
  798. head_seq_num = ieee80211_sn_inc(
  799. ieee80211_sn_sub(mpdu_seq_num, buf_size));
  800. /* release stored frames up to new head to stack */
  801. ieee80211_release_reorder_frames(sdata, tid_agg_rx,
  802. head_seq_num, frames);
  803. }
  804. /* Now the new frame is always in the range of the reordering buffer */
  805. index = mpdu_seq_num % tid_agg_rx->buf_size;
  806. /* check if we already stored this frame */
  807. if (ieee80211_rx_reorder_ready(&tid_agg_rx->reorder_buf[index])) {
  808. dev_kfree_skb(skb);
  809. goto out;
  810. }
  811. /*
  812. * If the current MPDU is in the right order and nothing else
  813. * is stored we can process it directly, no need to buffer it.
  814. * If it is first but there's something stored, we may be able
  815. * to release frames after this one.
  816. */
  817. if (mpdu_seq_num == tid_agg_rx->head_seq_num &&
  818. tid_agg_rx->stored_mpdu_num == 0) {
  819. if (!(status->flag & RX_FLAG_AMSDU_MORE))
  820. tid_agg_rx->head_seq_num =
  821. ieee80211_sn_inc(tid_agg_rx->head_seq_num);
  822. ret = false;
  823. goto out;
  824. }
  825. /* put the frame in the reordering buffer */
  826. __skb_queue_tail(&tid_agg_rx->reorder_buf[index], skb);
  827. if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  828. tid_agg_rx->reorder_time[index] = jiffies;
  829. tid_agg_rx->stored_mpdu_num++;
  830. ieee80211_sta_reorder_release(sdata, tid_agg_rx, frames);
  831. }
  832. out:
  833. spin_unlock(&tid_agg_rx->reorder_lock);
  834. return ret;
  835. }
  836. /*
  837. * Reorder MPDUs from A-MPDUs, keeping them on a buffer. Returns
  838. * true if the MPDU was buffered, false if it should be processed.
  839. */
  840. static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
  841. struct sk_buff_head *frames)
  842. {
  843. struct sk_buff *skb = rx->skb;
  844. struct ieee80211_local *local = rx->local;
  845. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  846. struct sta_info *sta = rx->sta;
  847. struct tid_ampdu_rx *tid_agg_rx;
  848. u16 sc;
  849. u8 tid, ack_policy;
  850. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  851. is_multicast_ether_addr(hdr->addr1))
  852. goto dont_reorder;
  853. /*
  854. * filter the QoS data rx stream according to
  855. * STA/TID and check if this STA/TID is on aggregation
  856. */
  857. if (!sta)
  858. goto dont_reorder;
  859. ack_policy = *ieee80211_get_qos_ctl(hdr) &
  860. IEEE80211_QOS_CTL_ACK_POLICY_MASK;
  861. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  862. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  863. if (!tid_agg_rx)
  864. goto dont_reorder;
  865. /* qos null data frames are excluded */
  866. if (unlikely(hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_NULLFUNC)))
  867. goto dont_reorder;
  868. /* not part of a BA session */
  869. if (ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  870. ack_policy != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
  871. goto dont_reorder;
  872. /* new, potentially un-ordered, ampdu frame - process it */
  873. /* reset session timer */
  874. if (tid_agg_rx->timeout)
  875. tid_agg_rx->last_rx = jiffies;
  876. /* if this mpdu is fragmented - terminate rx aggregation session */
  877. sc = le16_to_cpu(hdr->seq_ctrl);
  878. if (sc & IEEE80211_SCTL_FRAG) {
  879. skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  880. skb_queue_tail(&rx->sdata->skb_queue, skb);
  881. ieee80211_queue_work(&local->hw, &rx->sdata->work);
  882. return;
  883. }
  884. /*
  885. * No locking needed -- we will only ever process one
  886. * RX packet at a time, and thus own tid_agg_rx. All
  887. * other code manipulating it needs to (and does) make
  888. * sure that we cannot get to it any more before doing
  889. * anything with it.
  890. */
  891. if (ieee80211_sta_manage_reorder_buf(rx->sdata, tid_agg_rx, skb,
  892. frames))
  893. return;
  894. dont_reorder:
  895. __skb_queue_tail(frames, skb);
  896. }
  897. static ieee80211_rx_result debug_noinline
  898. ieee80211_rx_h_check_dup(struct ieee80211_rx_data *rx)
  899. {
  900. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  901. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  902. /*
  903. * Drop duplicate 802.11 retransmissions
  904. * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
  905. */
  906. if (rx->skb->len < 24)
  907. return RX_CONTINUE;
  908. if (ieee80211_is_ctl(hdr->frame_control) ||
  909. ieee80211_is_qos_nullfunc(hdr->frame_control) ||
  910. is_multicast_ether_addr(hdr->addr1))
  911. return RX_CONTINUE;
  912. if (rx->sta) {
  913. if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
  914. rx->sta->last_seq_ctrl[rx->seqno_idx] ==
  915. hdr->seq_ctrl)) {
  916. I802_DEBUG_INC(rx->local->dot11FrameDuplicateCount);
  917. rx->sta->num_duplicates++;
  918. return RX_DROP_UNUSABLE;
  919. } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
  920. rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
  921. }
  922. }
  923. return RX_CONTINUE;
  924. }
  925. static ieee80211_rx_result debug_noinline
  926. ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
  927. {
  928. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  929. if (unlikely(rx->skb->len < 16)) {
  930. I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
  931. return RX_DROP_MONITOR;
  932. }
  933. /* Drop disallowed frame classes based on STA auth/assoc state;
  934. * IEEE 802.11, Chap 5.5.
  935. *
  936. * mac80211 filters only based on association state, i.e. it drops
  937. * Class 3 frames from not associated stations. hostapd sends
  938. * deauth/disassoc frames when needed. In addition, hostapd is
  939. * responsible for filtering on both auth and assoc states.
  940. */
  941. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  942. return ieee80211_rx_mesh_check(rx);
  943. if (unlikely((ieee80211_is_data(hdr->frame_control) ||
  944. ieee80211_is_pspoll(hdr->frame_control)) &&
  945. rx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  946. rx->sdata->vif.type != NL80211_IFTYPE_WDS &&
  947. rx->sdata->vif.type != NL80211_IFTYPE_OCB &&
  948. (!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_ASSOC)))) {
  949. /*
  950. * accept port control frames from the AP even when it's not
  951. * yet marked ASSOC to prevent a race where we don't set the
  952. * assoc bit quickly enough before it sends the first frame
  953. */
  954. if (rx->sta && rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  955. ieee80211_is_data_present(hdr->frame_control)) {
  956. unsigned int hdrlen;
  957. __be16 ethertype;
  958. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  959. if (rx->skb->len < hdrlen + 8)
  960. return RX_DROP_MONITOR;
  961. skb_copy_bits(rx->skb, hdrlen + 6, &ethertype, 2);
  962. if (ethertype == rx->sdata->control_port_protocol)
  963. return RX_CONTINUE;
  964. }
  965. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  966. cfg80211_rx_spurious_frame(rx->sdata->dev,
  967. hdr->addr2,
  968. GFP_ATOMIC))
  969. return RX_DROP_UNUSABLE;
  970. return RX_DROP_MONITOR;
  971. }
  972. return RX_CONTINUE;
  973. }
  974. static ieee80211_rx_result debug_noinline
  975. ieee80211_rx_h_check_more_data(struct ieee80211_rx_data *rx)
  976. {
  977. struct ieee80211_local *local;
  978. struct ieee80211_hdr *hdr;
  979. struct sk_buff *skb;
  980. local = rx->local;
  981. skb = rx->skb;
  982. hdr = (struct ieee80211_hdr *) skb->data;
  983. if (!local->pspolling)
  984. return RX_CONTINUE;
  985. if (!ieee80211_has_fromds(hdr->frame_control))
  986. /* this is not from AP */
  987. return RX_CONTINUE;
  988. if (!ieee80211_is_data(hdr->frame_control))
  989. return RX_CONTINUE;
  990. if (!ieee80211_has_moredata(hdr->frame_control)) {
  991. /* AP has no more frames buffered for us */
  992. local->pspolling = false;
  993. return RX_CONTINUE;
  994. }
  995. /* more data bit is set, let's request a new frame from the AP */
  996. ieee80211_send_pspoll(local, rx->sdata);
  997. return RX_CONTINUE;
  998. }
  999. static void sta_ps_start(struct sta_info *sta)
  1000. {
  1001. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1002. struct ieee80211_local *local = sdata->local;
  1003. struct ps_data *ps;
  1004. int tid;
  1005. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1006. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1007. ps = &sdata->bss->ps;
  1008. else
  1009. return;
  1010. atomic_inc(&ps->num_sta_ps);
  1011. set_sta_flag(sta, WLAN_STA_PS_STA);
  1012. if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
  1013. drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
  1014. ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
  1015. sta->sta.addr, sta->sta.aid);
  1016. ieee80211_clear_fast_xmit(sta);
  1017. if (!sta->sta.txq[0])
  1018. return;
  1019. for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
  1020. struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
  1021. if (!skb_queue_len(&txqi->queue))
  1022. set_bit(tid, &sta->txq_buffered_tids);
  1023. else
  1024. clear_bit(tid, &sta->txq_buffered_tids);
  1025. }
  1026. }
  1027. static void sta_ps_end(struct sta_info *sta)
  1028. {
  1029. ps_dbg(sta->sdata, "STA %pM aid %d exits power save mode\n",
  1030. sta->sta.addr, sta->sta.aid);
  1031. if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  1032. /*
  1033. * Clear the flag only if the other one is still set
  1034. * so that the TX path won't start TX'ing new frames
  1035. * directly ... In the case that the driver flag isn't
  1036. * set ieee80211_sta_ps_deliver_wakeup() will clear it.
  1037. */
  1038. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1039. ps_dbg(sta->sdata, "STA %pM aid %d driver-ps-blocked\n",
  1040. sta->sta.addr, sta->sta.aid);
  1041. return;
  1042. }
  1043. set_sta_flag(sta, WLAN_STA_PS_DELIVER);
  1044. clear_sta_flag(sta, WLAN_STA_PS_STA);
  1045. ieee80211_sta_ps_deliver_wakeup(sta);
  1046. }
  1047. int ieee80211_sta_ps_transition(struct ieee80211_sta *sta, bool start)
  1048. {
  1049. struct sta_info *sta_inf = container_of(sta, struct sta_info, sta);
  1050. bool in_ps;
  1051. WARN_ON(!ieee80211_hw_check(&sta_inf->local->hw, AP_LINK_PS));
  1052. /* Don't let the same PS state be set twice */
  1053. in_ps = test_sta_flag(sta_inf, WLAN_STA_PS_STA);
  1054. if ((start && in_ps) || (!start && !in_ps))
  1055. return -EINVAL;
  1056. if (start)
  1057. sta_ps_start(sta_inf);
  1058. else
  1059. sta_ps_end(sta_inf);
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL(ieee80211_sta_ps_transition);
  1063. static ieee80211_rx_result debug_noinline
  1064. ieee80211_rx_h_uapsd_and_pspoll(struct ieee80211_rx_data *rx)
  1065. {
  1066. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1067. struct ieee80211_hdr *hdr = (void *)rx->skb->data;
  1068. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1069. int tid, ac;
  1070. if (!rx->sta)
  1071. return RX_CONTINUE;
  1072. if (sdata->vif.type != NL80211_IFTYPE_AP &&
  1073. sdata->vif.type != NL80211_IFTYPE_AP_VLAN)
  1074. return RX_CONTINUE;
  1075. /*
  1076. * The device handles station powersave, so don't do anything about
  1077. * uAPSD and PS-Poll frames (the latter shouldn't even come up from
  1078. * it to mac80211 since they're handled.)
  1079. */
  1080. if (ieee80211_hw_check(&sdata->local->hw, AP_LINK_PS))
  1081. return RX_CONTINUE;
  1082. /*
  1083. * Don't do anything if the station isn't already asleep. In
  1084. * the uAPSD case, the station will probably be marked asleep,
  1085. * in the PS-Poll case the station must be confused ...
  1086. */
  1087. if (!test_sta_flag(rx->sta, WLAN_STA_PS_STA))
  1088. return RX_CONTINUE;
  1089. if (unlikely(ieee80211_is_pspoll(hdr->frame_control))) {
  1090. if (!test_sta_flag(rx->sta, WLAN_STA_SP)) {
  1091. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1092. ieee80211_sta_ps_deliver_poll_response(rx->sta);
  1093. else
  1094. set_sta_flag(rx->sta, WLAN_STA_PSPOLL);
  1095. }
  1096. /* Free PS Poll skb here instead of returning RX_DROP that would
  1097. * count as an dropped frame. */
  1098. dev_kfree_skb(rx->skb);
  1099. return RX_QUEUED;
  1100. } else if (!ieee80211_has_morefrags(hdr->frame_control) &&
  1101. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1102. ieee80211_has_pm(hdr->frame_control) &&
  1103. (ieee80211_is_data_qos(hdr->frame_control) ||
  1104. ieee80211_is_qos_nullfunc(hdr->frame_control))) {
  1105. tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  1106. ac = ieee802_1d_to_ac[tid & 7];
  1107. /*
  1108. * If this AC is not trigger-enabled do nothing.
  1109. *
  1110. * NB: This could/should check a separate bitmap of trigger-
  1111. * enabled queues, but for now we only implement uAPSD w/o
  1112. * TSPEC changes to the ACs, so they're always the same.
  1113. */
  1114. if (!(rx->sta->sta.uapsd_queues & BIT(ac)))
  1115. return RX_CONTINUE;
  1116. /* if we are in a service period, do nothing */
  1117. if (test_sta_flag(rx->sta, WLAN_STA_SP))
  1118. return RX_CONTINUE;
  1119. if (!test_sta_flag(rx->sta, WLAN_STA_PS_DRIVER))
  1120. ieee80211_sta_ps_deliver_uapsd(rx->sta);
  1121. else
  1122. set_sta_flag(rx->sta, WLAN_STA_UAPSD);
  1123. }
  1124. return RX_CONTINUE;
  1125. }
  1126. static ieee80211_rx_result debug_noinline
  1127. ieee80211_rx_h_sta_process(struct ieee80211_rx_data *rx)
  1128. {
  1129. struct sta_info *sta = rx->sta;
  1130. struct sk_buff *skb = rx->skb;
  1131. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1132. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1133. int i;
  1134. if (!sta)
  1135. return RX_CONTINUE;
  1136. /*
  1137. * Update last_rx only for IBSS packets which are for the current
  1138. * BSSID and for station already AUTHORIZED to avoid keeping the
  1139. * current IBSS network alive in cases where other STAs start
  1140. * using different BSSID. This will also give the station another
  1141. * chance to restart the authentication/authorization in case
  1142. * something went wrong the first time.
  1143. */
  1144. if (rx->sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  1145. u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len,
  1146. NL80211_IFTYPE_ADHOC);
  1147. if (ether_addr_equal(bssid, rx->sdata->u.ibss.bssid) &&
  1148. test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
  1149. sta->last_rx = jiffies;
  1150. if (ieee80211_is_data(hdr->frame_control) &&
  1151. !is_multicast_ether_addr(hdr->addr1)) {
  1152. sta->last_rx_rate_idx = status->rate_idx;
  1153. sta->last_rx_rate_flag = status->flag;
  1154. sta->last_rx_rate_vht_flag = status->vht_flag;
  1155. sta->last_rx_rate_vht_nss = status->vht_nss;
  1156. }
  1157. }
  1158. } else if (rx->sdata->vif.type == NL80211_IFTYPE_OCB) {
  1159. sta->last_rx = jiffies;
  1160. } else if (!is_multicast_ether_addr(hdr->addr1)) {
  1161. /*
  1162. * Mesh beacons will update last_rx when if they are found to
  1163. * match the current local configuration when processed.
  1164. */
  1165. sta->last_rx = jiffies;
  1166. if (ieee80211_is_data(hdr->frame_control)) {
  1167. sta->last_rx_rate_idx = status->rate_idx;
  1168. sta->last_rx_rate_flag = status->flag;
  1169. sta->last_rx_rate_vht_flag = status->vht_flag;
  1170. sta->last_rx_rate_vht_nss = status->vht_nss;
  1171. }
  1172. }
  1173. if (rx->sdata->vif.type == NL80211_IFTYPE_STATION)
  1174. ieee80211_sta_rx_notify(rx->sdata, hdr);
  1175. sta->rx_fragments++;
  1176. sta->rx_bytes += rx->skb->len;
  1177. if (!(status->flag & RX_FLAG_NO_SIGNAL_VAL)) {
  1178. sta->last_signal = status->signal;
  1179. ewma_add(&sta->avg_signal, -status->signal);
  1180. }
  1181. if (status->chains) {
  1182. sta->chains = status->chains;
  1183. for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
  1184. int signal = status->chain_signal[i];
  1185. if (!(status->chains & BIT(i)))
  1186. continue;
  1187. sta->chain_signal_last[i] = signal;
  1188. ewma_add(&sta->chain_signal_avg[i], -signal);
  1189. }
  1190. }
  1191. /*
  1192. * Change STA power saving mode only at the end of a frame
  1193. * exchange sequence.
  1194. */
  1195. if (!ieee80211_hw_check(&sta->local->hw, AP_LINK_PS) &&
  1196. !ieee80211_has_morefrags(hdr->frame_control) &&
  1197. !(status->rx_flags & IEEE80211_RX_DEFERRED_RELEASE) &&
  1198. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1199. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1200. /* PM bit is only checked in frames where it isn't reserved,
  1201. * in AP mode it's reserved in non-bufferable management frames
  1202. * (cf. IEEE 802.11-2012 8.2.4.1.7 Power Management field)
  1203. */
  1204. (!ieee80211_is_mgmt(hdr->frame_control) ||
  1205. ieee80211_is_bufferable_mmpdu(hdr->frame_control))) {
  1206. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  1207. if (!ieee80211_has_pm(hdr->frame_control))
  1208. sta_ps_end(sta);
  1209. } else {
  1210. if (ieee80211_has_pm(hdr->frame_control))
  1211. sta_ps_start(sta);
  1212. }
  1213. }
  1214. /* mesh power save support */
  1215. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  1216. ieee80211_mps_rx_h_sta_process(sta, hdr);
  1217. /*
  1218. * Drop (qos-)data::nullfunc frames silently, since they
  1219. * are used only to control station power saving mode.
  1220. */
  1221. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  1222. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1223. I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
  1224. /*
  1225. * If we receive a 4-addr nullfunc frame from a STA
  1226. * that was not moved to a 4-addr STA vlan yet send
  1227. * the event to userspace and for older hostapd drop
  1228. * the frame to the monitor interface.
  1229. */
  1230. if (ieee80211_has_a4(hdr->frame_control) &&
  1231. (rx->sdata->vif.type == NL80211_IFTYPE_AP ||
  1232. (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1233. !rx->sdata->u.vlan.sta))) {
  1234. if (!test_and_set_sta_flag(sta, WLAN_STA_4ADDR_EVENT))
  1235. cfg80211_rx_unexpected_4addr_frame(
  1236. rx->sdata->dev, sta->sta.addr,
  1237. GFP_ATOMIC);
  1238. return RX_DROP_MONITOR;
  1239. }
  1240. /*
  1241. * Update counter and free packet here to avoid
  1242. * counting this as a dropped packed.
  1243. */
  1244. sta->rx_packets++;
  1245. dev_kfree_skb(rx->skb);
  1246. return RX_QUEUED;
  1247. }
  1248. return RX_CONTINUE;
  1249. } /* ieee80211_rx_h_sta_process */
  1250. static ieee80211_rx_result debug_noinline
  1251. ieee80211_rx_h_decrypt(struct ieee80211_rx_data *rx)
  1252. {
  1253. struct sk_buff *skb = rx->skb;
  1254. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1255. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1256. int keyidx;
  1257. int hdrlen;
  1258. ieee80211_rx_result result = RX_DROP_UNUSABLE;
  1259. struct ieee80211_key *sta_ptk = NULL;
  1260. int mmie_keyidx = -1;
  1261. __le16 fc;
  1262. const struct ieee80211_cipher_scheme *cs = NULL;
  1263. /*
  1264. * Key selection 101
  1265. *
  1266. * There are four types of keys:
  1267. * - GTK (group keys)
  1268. * - IGTK (group keys for management frames)
  1269. * - PTK (pairwise keys)
  1270. * - STK (station-to-station pairwise keys)
  1271. *
  1272. * When selecting a key, we have to distinguish between multicast
  1273. * (including broadcast) and unicast frames, the latter can only
  1274. * use PTKs and STKs while the former always use GTKs and IGTKs.
  1275. * Unless, of course, actual WEP keys ("pre-RSNA") are used, then
  1276. * unicast frames can also use key indices like GTKs. Hence, if we
  1277. * don't have a PTK/STK we check the key index for a WEP key.
  1278. *
  1279. * Note that in a regular BSS, multicast frames are sent by the
  1280. * AP only, associated stations unicast the frame to the AP first
  1281. * which then multicasts it on their behalf.
  1282. *
  1283. * There is also a slight problem in IBSS mode: GTKs are negotiated
  1284. * with each station, that is something we don't currently handle.
  1285. * The spec seems to expect that one negotiates the same key with
  1286. * every station but there's no such requirement; VLANs could be
  1287. * possible.
  1288. */
  1289. /* start without a key */
  1290. rx->key = NULL;
  1291. fc = hdr->frame_control;
  1292. if (rx->sta) {
  1293. int keyid = rx->sta->ptk_idx;
  1294. if (ieee80211_has_protected(fc) && rx->sta->cipher_scheme) {
  1295. cs = rx->sta->cipher_scheme;
  1296. keyid = iwl80211_get_cs_keyid(cs, rx->skb);
  1297. if (unlikely(keyid < 0))
  1298. return RX_DROP_UNUSABLE;
  1299. }
  1300. sta_ptk = rcu_dereference(rx->sta->ptk[keyid]);
  1301. }
  1302. if (!ieee80211_has_protected(fc))
  1303. mmie_keyidx = ieee80211_get_mmie_keyidx(rx->skb);
  1304. if (!is_multicast_ether_addr(hdr->addr1) && sta_ptk) {
  1305. rx->key = sta_ptk;
  1306. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1307. (status->flag & RX_FLAG_IV_STRIPPED))
  1308. return RX_CONTINUE;
  1309. /* Skip decryption if the frame is not protected. */
  1310. if (!ieee80211_has_protected(fc))
  1311. return RX_CONTINUE;
  1312. } else if (mmie_keyidx >= 0) {
  1313. /* Broadcast/multicast robust management frame / BIP */
  1314. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1315. (status->flag & RX_FLAG_IV_STRIPPED))
  1316. return RX_CONTINUE;
  1317. if (mmie_keyidx < NUM_DEFAULT_KEYS ||
  1318. mmie_keyidx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS)
  1319. return RX_DROP_MONITOR; /* unexpected BIP keyidx */
  1320. if (rx->sta)
  1321. rx->key = rcu_dereference(rx->sta->gtk[mmie_keyidx]);
  1322. if (!rx->key)
  1323. rx->key = rcu_dereference(rx->sdata->keys[mmie_keyidx]);
  1324. } else if (!ieee80211_has_protected(fc)) {
  1325. /*
  1326. * The frame was not protected, so skip decryption. However, we
  1327. * need to set rx->key if there is a key that could have been
  1328. * used so that the frame may be dropped if encryption would
  1329. * have been expected.
  1330. */
  1331. struct ieee80211_key *key = NULL;
  1332. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1333. int i;
  1334. if (ieee80211_is_mgmt(fc) &&
  1335. is_multicast_ether_addr(hdr->addr1) &&
  1336. (key = rcu_dereference(rx->sdata->default_mgmt_key)))
  1337. rx->key = key;
  1338. else {
  1339. if (rx->sta) {
  1340. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1341. key = rcu_dereference(rx->sta->gtk[i]);
  1342. if (key)
  1343. break;
  1344. }
  1345. }
  1346. if (!key) {
  1347. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  1348. key = rcu_dereference(sdata->keys[i]);
  1349. if (key)
  1350. break;
  1351. }
  1352. }
  1353. if (key)
  1354. rx->key = key;
  1355. }
  1356. return RX_CONTINUE;
  1357. } else {
  1358. u8 keyid;
  1359. /*
  1360. * The device doesn't give us the IV so we won't be
  1361. * able to look up the key. That's ok though, we
  1362. * don't need to decrypt the frame, we just won't
  1363. * be able to keep statistics accurate.
  1364. * Except for key threshold notifications, should
  1365. * we somehow allow the driver to tell us which key
  1366. * the hardware used if this flag is set?
  1367. */
  1368. if ((status->flag & RX_FLAG_DECRYPTED) &&
  1369. (status->flag & RX_FLAG_IV_STRIPPED))
  1370. return RX_CONTINUE;
  1371. hdrlen = ieee80211_hdrlen(fc);
  1372. if (cs) {
  1373. keyidx = iwl80211_get_cs_keyid(cs, rx->skb);
  1374. if (unlikely(keyidx < 0))
  1375. return RX_DROP_UNUSABLE;
  1376. } else {
  1377. if (rx->skb->len < 8 + hdrlen)
  1378. return RX_DROP_UNUSABLE; /* TODO: count this? */
  1379. /*
  1380. * no need to call ieee80211_wep_get_keyidx,
  1381. * it verifies a bunch of things we've done already
  1382. */
  1383. skb_copy_bits(rx->skb, hdrlen + 3, &keyid, 1);
  1384. keyidx = keyid >> 6;
  1385. }
  1386. /* check per-station GTK first, if multicast packet */
  1387. if (is_multicast_ether_addr(hdr->addr1) && rx->sta)
  1388. rx->key = rcu_dereference(rx->sta->gtk[keyidx]);
  1389. /* if not found, try default key */
  1390. if (!rx->key) {
  1391. rx->key = rcu_dereference(rx->sdata->keys[keyidx]);
  1392. /*
  1393. * RSNA-protected unicast frames should always be
  1394. * sent with pairwise or station-to-station keys,
  1395. * but for WEP we allow using a key index as well.
  1396. */
  1397. if (rx->key &&
  1398. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1399. rx->key->conf.cipher != WLAN_CIPHER_SUITE_WEP104 &&
  1400. !is_multicast_ether_addr(hdr->addr1))
  1401. rx->key = NULL;
  1402. }
  1403. }
  1404. if (rx->key) {
  1405. if (unlikely(rx->key->flags & KEY_FLAG_TAINTED))
  1406. return RX_DROP_MONITOR;
  1407. rx->key->tx_rx_count++;
  1408. /* TODO: add threshold stuff again */
  1409. } else {
  1410. return RX_DROP_MONITOR;
  1411. }
  1412. switch (rx->key->conf.cipher) {
  1413. case WLAN_CIPHER_SUITE_WEP40:
  1414. case WLAN_CIPHER_SUITE_WEP104:
  1415. result = ieee80211_crypto_wep_decrypt(rx);
  1416. break;
  1417. case WLAN_CIPHER_SUITE_TKIP:
  1418. result = ieee80211_crypto_tkip_decrypt(rx);
  1419. break;
  1420. case WLAN_CIPHER_SUITE_CCMP:
  1421. result = ieee80211_crypto_ccmp_decrypt(
  1422. rx, IEEE80211_CCMP_MIC_LEN);
  1423. break;
  1424. case WLAN_CIPHER_SUITE_CCMP_256:
  1425. result = ieee80211_crypto_ccmp_decrypt(
  1426. rx, IEEE80211_CCMP_256_MIC_LEN);
  1427. break;
  1428. case WLAN_CIPHER_SUITE_AES_CMAC:
  1429. result = ieee80211_crypto_aes_cmac_decrypt(rx);
  1430. break;
  1431. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  1432. result = ieee80211_crypto_aes_cmac_256_decrypt(rx);
  1433. break;
  1434. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  1435. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  1436. result = ieee80211_crypto_aes_gmac_decrypt(rx);
  1437. break;
  1438. case WLAN_CIPHER_SUITE_GCMP:
  1439. case WLAN_CIPHER_SUITE_GCMP_256:
  1440. result = ieee80211_crypto_gcmp_decrypt(rx);
  1441. break;
  1442. default:
  1443. result = ieee80211_crypto_hw_decrypt(rx);
  1444. }
  1445. /* the hdr variable is invalid after the decrypt handlers */
  1446. /* either the frame has been decrypted or will be dropped */
  1447. status->flag |= RX_FLAG_DECRYPTED;
  1448. return result;
  1449. }
  1450. static inline struct ieee80211_fragment_entry *
  1451. ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  1452. unsigned int frag, unsigned int seq, int rx_queue,
  1453. struct sk_buff **skb)
  1454. {
  1455. struct ieee80211_fragment_entry *entry;
  1456. entry = &sdata->fragments[sdata->fragment_next++];
  1457. if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  1458. sdata->fragment_next = 0;
  1459. if (!skb_queue_empty(&entry->skb_list))
  1460. __skb_queue_purge(&entry->skb_list);
  1461. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1462. *skb = NULL;
  1463. entry->first_frag_time = jiffies;
  1464. entry->seq = seq;
  1465. entry->rx_queue = rx_queue;
  1466. entry->last_frag = frag;
  1467. entry->ccmp = 0;
  1468. entry->extra_len = 0;
  1469. return entry;
  1470. }
  1471. static inline struct ieee80211_fragment_entry *
  1472. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1473. unsigned int frag, unsigned int seq,
  1474. int rx_queue, struct ieee80211_hdr *hdr)
  1475. {
  1476. struct ieee80211_fragment_entry *entry;
  1477. int i, idx;
  1478. idx = sdata->fragment_next;
  1479. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1480. struct ieee80211_hdr *f_hdr;
  1481. idx--;
  1482. if (idx < 0)
  1483. idx = IEEE80211_FRAGMENT_MAX - 1;
  1484. entry = &sdata->fragments[idx];
  1485. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1486. entry->rx_queue != rx_queue ||
  1487. entry->last_frag + 1 != frag)
  1488. continue;
  1489. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1490. /*
  1491. * Check ftype and addresses are equal, else check next fragment
  1492. */
  1493. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1494. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1495. !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
  1496. !ether_addr_equal(hdr->addr2, f_hdr->addr2))
  1497. continue;
  1498. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1499. __skb_queue_purge(&entry->skb_list);
  1500. continue;
  1501. }
  1502. return entry;
  1503. }
  1504. return NULL;
  1505. }
  1506. static ieee80211_rx_result debug_noinline
  1507. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1508. {
  1509. struct ieee80211_hdr *hdr;
  1510. u16 sc;
  1511. __le16 fc;
  1512. unsigned int frag, seq;
  1513. struct ieee80211_fragment_entry *entry;
  1514. struct sk_buff *skb;
  1515. struct ieee80211_rx_status *status;
  1516. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1517. fc = hdr->frame_control;
  1518. if (ieee80211_is_ctl(fc))
  1519. return RX_CONTINUE;
  1520. sc = le16_to_cpu(hdr->seq_ctrl);
  1521. frag = sc & IEEE80211_SCTL_FRAG;
  1522. if (is_multicast_ether_addr(hdr->addr1)) {
  1523. I802_DEBUG_INC(rx->local->dot11MulticastReceivedFrameCount);
  1524. goto out_no_led;
  1525. }
  1526. if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
  1527. goto out;
  1528. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1529. if (skb_linearize(rx->skb))
  1530. return RX_DROP_UNUSABLE;
  1531. /*
  1532. * skb_linearize() might change the skb->data and
  1533. * previously cached variables (in this case, hdr) need to
  1534. * be refreshed with the new data.
  1535. */
  1536. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1537. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1538. if (frag == 0) {
  1539. /* This is the first fragment of a new frame. */
  1540. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1541. rx->seqno_idx, &(rx->skb));
  1542. if (rx->key &&
  1543. (rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP ||
  1544. rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP_256) &&
  1545. ieee80211_has_protected(fc)) {
  1546. int queue = rx->security_idx;
  1547. /* Store CCMP PN so that we can verify that the next
  1548. * fragment has a sequential PN value. */
  1549. entry->ccmp = 1;
  1550. memcpy(entry->last_pn,
  1551. rx->key->u.ccmp.rx_pn[queue],
  1552. IEEE80211_CCMP_PN_LEN);
  1553. }
  1554. return RX_QUEUED;
  1555. }
  1556. /* This is a fragment for a frame that should already be pending in
  1557. * fragment cache. Add this fragment to the end of the pending entry.
  1558. */
  1559. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1560. rx->seqno_idx, hdr);
  1561. if (!entry) {
  1562. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1563. return RX_DROP_MONITOR;
  1564. }
  1565. /* Verify that MPDUs within one MSDU have sequential PN values.
  1566. * (IEEE 802.11i, 8.3.3.4.5) */
  1567. if (entry->ccmp) {
  1568. int i;
  1569. u8 pn[IEEE80211_CCMP_PN_LEN], *rpn;
  1570. int queue;
  1571. if (!rx->key ||
  1572. (rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP &&
  1573. rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP_256))
  1574. return RX_DROP_UNUSABLE;
  1575. memcpy(pn, entry->last_pn, IEEE80211_CCMP_PN_LEN);
  1576. for (i = IEEE80211_CCMP_PN_LEN - 1; i >= 0; i--) {
  1577. pn[i]++;
  1578. if (pn[i])
  1579. break;
  1580. }
  1581. queue = rx->security_idx;
  1582. rpn = rx->key->u.ccmp.rx_pn[queue];
  1583. if (memcmp(pn, rpn, IEEE80211_CCMP_PN_LEN))
  1584. return RX_DROP_UNUSABLE;
  1585. memcpy(entry->last_pn, pn, IEEE80211_CCMP_PN_LEN);
  1586. }
  1587. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1588. __skb_queue_tail(&entry->skb_list, rx->skb);
  1589. entry->last_frag = frag;
  1590. entry->extra_len += rx->skb->len;
  1591. if (ieee80211_has_morefrags(fc)) {
  1592. rx->skb = NULL;
  1593. return RX_QUEUED;
  1594. }
  1595. rx->skb = __skb_dequeue(&entry->skb_list);
  1596. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1597. I802_DEBUG_INC(rx->local->rx_expand_skb_head_defrag);
  1598. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1599. GFP_ATOMIC))) {
  1600. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1601. __skb_queue_purge(&entry->skb_list);
  1602. return RX_DROP_UNUSABLE;
  1603. }
  1604. }
  1605. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1606. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1607. dev_kfree_skb(skb);
  1608. }
  1609. /* Complete frame has been reassembled - process it now */
  1610. status = IEEE80211_SKB_RXCB(rx->skb);
  1611. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1612. out:
  1613. ieee80211_led_rx(rx->local);
  1614. out_no_led:
  1615. if (rx->sta)
  1616. rx->sta->rx_packets++;
  1617. return RX_CONTINUE;
  1618. }
  1619. static int ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1620. {
  1621. if (unlikely(!rx->sta || !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1622. return -EACCES;
  1623. return 0;
  1624. }
  1625. static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1626. {
  1627. struct sk_buff *skb = rx->skb;
  1628. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1629. /*
  1630. * Pass through unencrypted frames if the hardware has
  1631. * decrypted them already.
  1632. */
  1633. if (status->flag & RX_FLAG_DECRYPTED)
  1634. return 0;
  1635. /* Drop unencrypted frames if key is set. */
  1636. if (unlikely(!ieee80211_has_protected(fc) &&
  1637. !ieee80211_is_nullfunc(fc) &&
  1638. ieee80211_is_data(fc) && rx->key))
  1639. return -EACCES;
  1640. return 0;
  1641. }
  1642. static int ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1643. {
  1644. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1645. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1646. __le16 fc = hdr->frame_control;
  1647. /*
  1648. * Pass through unencrypted frames if the hardware has
  1649. * decrypted them already.
  1650. */
  1651. if (status->flag & RX_FLAG_DECRYPTED)
  1652. return 0;
  1653. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1654. if (unlikely(!ieee80211_has_protected(fc) &&
  1655. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1656. rx->key)) {
  1657. if (ieee80211_is_deauth(fc) ||
  1658. ieee80211_is_disassoc(fc))
  1659. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1660. rx->skb->data,
  1661. rx->skb->len);
  1662. return -EACCES;
  1663. }
  1664. /* BIP does not use Protected field, so need to check MMIE */
  1665. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1666. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1667. if (ieee80211_is_deauth(fc) ||
  1668. ieee80211_is_disassoc(fc))
  1669. cfg80211_rx_unprot_mlme_mgmt(rx->sdata->dev,
  1670. rx->skb->data,
  1671. rx->skb->len);
  1672. return -EACCES;
  1673. }
  1674. /*
  1675. * When using MFP, Action frames are not allowed prior to
  1676. * having configured keys.
  1677. */
  1678. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1679. ieee80211_is_robust_mgmt_frame(rx->skb)))
  1680. return -EACCES;
  1681. }
  1682. return 0;
  1683. }
  1684. static int
  1685. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1686. {
  1687. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1688. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1689. bool check_port_control = false;
  1690. struct ethhdr *ehdr;
  1691. int ret;
  1692. *port_control = false;
  1693. if (ieee80211_has_a4(hdr->frame_control) &&
  1694. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1695. return -1;
  1696. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1697. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1698. if (!sdata->u.mgd.use_4addr)
  1699. return -1;
  1700. else
  1701. check_port_control = true;
  1702. }
  1703. if (is_multicast_ether_addr(hdr->addr1) &&
  1704. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1705. return -1;
  1706. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1707. if (ret < 0)
  1708. return ret;
  1709. ehdr = (struct ethhdr *) rx->skb->data;
  1710. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1711. *port_control = true;
  1712. else if (check_port_control)
  1713. return -1;
  1714. return 0;
  1715. }
  1716. /*
  1717. * requires that rx->skb is a frame with ethernet header
  1718. */
  1719. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1720. {
  1721. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1722. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1723. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1724. /*
  1725. * Allow EAPOL frames to us/the PAE group address regardless
  1726. * of whether the frame was encrypted or not.
  1727. */
  1728. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1729. (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
  1730. ether_addr_equal(ehdr->h_dest, pae_group_addr)))
  1731. return true;
  1732. if (ieee80211_802_1x_port_control(rx) ||
  1733. ieee80211_drop_unencrypted(rx, fc))
  1734. return false;
  1735. return true;
  1736. }
  1737. /*
  1738. * requires that rx->skb is a frame with ethernet header
  1739. */
  1740. static void
  1741. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1742. {
  1743. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1744. struct net_device *dev = sdata->dev;
  1745. struct sk_buff *skb, *xmit_skb;
  1746. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1747. struct sta_info *dsta;
  1748. skb = rx->skb;
  1749. xmit_skb = NULL;
  1750. ieee80211_rx_stats(dev, skb->len);
  1751. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1752. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1753. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1754. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1755. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1756. /*
  1757. * send multicast frames both to higher layers in
  1758. * local net stack and back to the wireless medium
  1759. */
  1760. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1761. if (!xmit_skb)
  1762. net_info_ratelimited("%s: failed to clone multicast frame\n",
  1763. dev->name);
  1764. } else {
  1765. dsta = sta_info_get(sdata, skb->data);
  1766. if (dsta) {
  1767. /*
  1768. * The destination station is associated to
  1769. * this AP (in this VLAN), so send the frame
  1770. * directly to it and do not pass it to local
  1771. * net stack.
  1772. */
  1773. xmit_skb = skb;
  1774. skb = NULL;
  1775. }
  1776. }
  1777. }
  1778. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1779. if (skb) {
  1780. /* 'align' will only take the values 0 or 2 here since all
  1781. * frames are required to be aligned to 2-byte boundaries
  1782. * when being passed to mac80211; the code here works just
  1783. * as well if that isn't true, but mac80211 assumes it can
  1784. * access fields as 2-byte aligned (e.g. for ether_addr_equal)
  1785. */
  1786. int align;
  1787. align = (unsigned long)(skb->data + sizeof(struct ethhdr)) & 3;
  1788. if (align) {
  1789. if (WARN_ON(skb_headroom(skb) < 3)) {
  1790. dev_kfree_skb(skb);
  1791. skb = NULL;
  1792. } else {
  1793. u8 *data = skb->data;
  1794. size_t len = skb_headlen(skb);
  1795. skb->data -= align;
  1796. memmove(skb->data, data, len);
  1797. skb_set_tail_pointer(skb, len);
  1798. }
  1799. }
  1800. }
  1801. #endif
  1802. if (skb) {
  1803. /* deliver to local stack */
  1804. skb->protocol = eth_type_trans(skb, dev);
  1805. memset(skb->cb, 0, sizeof(skb->cb));
  1806. if (!(rx->flags & IEEE80211_RX_REORDER_TIMER) &&
  1807. rx->local->napi)
  1808. napi_gro_receive(rx->local->napi, skb);
  1809. else
  1810. netif_receive_skb(skb);
  1811. }
  1812. if (xmit_skb) {
  1813. /*
  1814. * Send to wireless media and increase priority by 256 to
  1815. * keep the received priority instead of reclassifying
  1816. * the frame (see cfg80211_classify8021d).
  1817. */
  1818. xmit_skb->priority += 256;
  1819. xmit_skb->protocol = htons(ETH_P_802_3);
  1820. skb_reset_network_header(xmit_skb);
  1821. skb_reset_mac_header(xmit_skb);
  1822. dev_queue_xmit(xmit_skb);
  1823. }
  1824. }
  1825. static ieee80211_rx_result debug_noinline
  1826. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1827. {
  1828. struct net_device *dev = rx->sdata->dev;
  1829. struct sk_buff *skb = rx->skb;
  1830. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1831. __le16 fc = hdr->frame_control;
  1832. struct sk_buff_head frame_list;
  1833. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1834. if (unlikely(!ieee80211_is_data(fc)))
  1835. return RX_CONTINUE;
  1836. if (unlikely(!ieee80211_is_data_present(fc)))
  1837. return RX_DROP_MONITOR;
  1838. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1839. return RX_CONTINUE;
  1840. if (ieee80211_has_a4(hdr->frame_control) &&
  1841. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1842. !rx->sdata->u.vlan.sta)
  1843. return RX_DROP_UNUSABLE;
  1844. if (is_multicast_ether_addr(hdr->addr1) &&
  1845. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1846. rx->sdata->u.vlan.sta) ||
  1847. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1848. rx->sdata->u.mgd.use_4addr)))
  1849. return RX_DROP_UNUSABLE;
  1850. skb->dev = dev;
  1851. __skb_queue_head_init(&frame_list);
  1852. if (skb_linearize(skb))
  1853. return RX_DROP_UNUSABLE;
  1854. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1855. rx->sdata->vif.type,
  1856. rx->local->hw.extra_tx_headroom, true);
  1857. while (!skb_queue_empty(&frame_list)) {
  1858. rx->skb = __skb_dequeue(&frame_list);
  1859. if (!ieee80211_frame_allowed(rx, fc)) {
  1860. dev_kfree_skb(rx->skb);
  1861. continue;
  1862. }
  1863. ieee80211_deliver_skb(rx);
  1864. }
  1865. return RX_QUEUED;
  1866. }
  1867. #ifdef CONFIG_MAC80211_MESH
  1868. static ieee80211_rx_result
  1869. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1870. {
  1871. struct ieee80211_hdr *fwd_hdr, *hdr;
  1872. struct ieee80211_tx_info *info;
  1873. struct ieee80211s_hdr *mesh_hdr;
  1874. struct sk_buff *skb = rx->skb, *fwd_skb;
  1875. struct ieee80211_local *local = rx->local;
  1876. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1877. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1878. u16 q, hdrlen;
  1879. hdr = (struct ieee80211_hdr *) skb->data;
  1880. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1881. /* make sure fixed part of mesh header is there, also checks skb len */
  1882. if (!pskb_may_pull(rx->skb, hdrlen + 6))
  1883. return RX_DROP_MONITOR;
  1884. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1885. /* make sure full mesh header is there, also checks skb len */
  1886. if (!pskb_may_pull(rx->skb,
  1887. hdrlen + ieee80211_get_mesh_hdrlen(mesh_hdr)))
  1888. return RX_DROP_MONITOR;
  1889. /* reload pointers */
  1890. hdr = (struct ieee80211_hdr *) skb->data;
  1891. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1892. if (ieee80211_drop_unencrypted(rx, hdr->frame_control))
  1893. return RX_DROP_MONITOR;
  1894. /* frame is in RMC, don't forward */
  1895. if (ieee80211_is_data(hdr->frame_control) &&
  1896. is_multicast_ether_addr(hdr->addr1) &&
  1897. mesh_rmc_check(rx->sdata, hdr->addr3, mesh_hdr))
  1898. return RX_DROP_MONITOR;
  1899. if (!ieee80211_is_data(hdr->frame_control))
  1900. return RX_CONTINUE;
  1901. if (!mesh_hdr->ttl)
  1902. return RX_DROP_MONITOR;
  1903. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1904. struct mesh_path *mppath;
  1905. char *proxied_addr;
  1906. char *mpp_addr;
  1907. if (is_multicast_ether_addr(hdr->addr1)) {
  1908. mpp_addr = hdr->addr3;
  1909. proxied_addr = mesh_hdr->eaddr1;
  1910. } else if (mesh_hdr->flags & MESH_FLAGS_AE_A5_A6) {
  1911. /* has_a4 already checked in ieee80211_rx_mesh_check */
  1912. mpp_addr = hdr->addr4;
  1913. proxied_addr = mesh_hdr->eaddr2;
  1914. } else {
  1915. return RX_DROP_MONITOR;
  1916. }
  1917. rcu_read_lock();
  1918. mppath = mpp_path_lookup(sdata, proxied_addr);
  1919. if (!mppath) {
  1920. mpp_path_add(sdata, proxied_addr, mpp_addr);
  1921. } else {
  1922. spin_lock_bh(&mppath->state_lock);
  1923. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  1924. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1925. spin_unlock_bh(&mppath->state_lock);
  1926. }
  1927. rcu_read_unlock();
  1928. }
  1929. /* Frame has reached destination. Don't forward */
  1930. if (!is_multicast_ether_addr(hdr->addr1) &&
  1931. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  1932. return RX_CONTINUE;
  1933. q = ieee80211_select_queue_80211(sdata, skb, hdr);
  1934. if (ieee80211_queue_stopped(&local->hw, q)) {
  1935. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  1936. return RX_DROP_MONITOR;
  1937. }
  1938. skb_set_queue_mapping(skb, q);
  1939. if (!--mesh_hdr->ttl) {
  1940. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  1941. goto out;
  1942. }
  1943. if (!ifmsh->mshcfg.dot11MeshForwarding)
  1944. goto out;
  1945. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1946. if (!fwd_skb) {
  1947. net_info_ratelimited("%s: failed to clone mesh frame\n",
  1948. sdata->name);
  1949. goto out;
  1950. }
  1951. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1952. fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
  1953. info = IEEE80211_SKB_CB(fwd_skb);
  1954. memset(info, 0, sizeof(*info));
  1955. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1956. info->control.vif = &rx->sdata->vif;
  1957. info->control.jiffies = jiffies;
  1958. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  1959. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  1960. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1961. /* update power mode indication when forwarding */
  1962. ieee80211_mps_set_frame_flags(sdata, NULL, fwd_hdr);
  1963. } else if (!mesh_nexthop_lookup(sdata, fwd_skb)) {
  1964. /* mesh power mode flags updated in mesh_nexthop_lookup */
  1965. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  1966. } else {
  1967. /* unable to resolve next hop */
  1968. mesh_path_error_tx(sdata, ifmsh->mshcfg.element_ttl,
  1969. fwd_hdr->addr3, 0,
  1970. WLAN_REASON_MESH_PATH_NOFORWARD,
  1971. fwd_hdr->addr2);
  1972. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  1973. kfree_skb(fwd_skb);
  1974. return RX_DROP_MONITOR;
  1975. }
  1976. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  1977. ieee80211_add_pending_skb(local, fwd_skb);
  1978. out:
  1979. if (is_multicast_ether_addr(hdr->addr1))
  1980. return RX_CONTINUE;
  1981. return RX_DROP_MONITOR;
  1982. }
  1983. #endif
  1984. static ieee80211_rx_result debug_noinline
  1985. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1986. {
  1987. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1988. struct ieee80211_local *local = rx->local;
  1989. struct net_device *dev = sdata->dev;
  1990. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1991. __le16 fc = hdr->frame_control;
  1992. bool port_control;
  1993. int err;
  1994. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1995. return RX_CONTINUE;
  1996. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1997. return RX_DROP_MONITOR;
  1998. if (rx->sta) {
  1999. /* The seqno index has the same property as needed
  2000. * for the rx_msdu field, i.e. it is IEEE80211_NUM_TIDS
  2001. * for non-QoS-data frames. Here we know it's a data
  2002. * frame, so count MSDUs.
  2003. */
  2004. rx->sta->rx_msdu[rx->seqno_idx]++;
  2005. }
  2006. /*
  2007. * Send unexpected-4addr-frame event to hostapd. For older versions,
  2008. * also drop the frame to cooked monitor interfaces.
  2009. */
  2010. if (ieee80211_has_a4(hdr->frame_control) &&
  2011. sdata->vif.type == NL80211_IFTYPE_AP) {
  2012. if (rx->sta &&
  2013. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  2014. cfg80211_rx_unexpected_4addr_frame(
  2015. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  2016. return RX_DROP_MONITOR;
  2017. }
  2018. err = __ieee80211_data_to_8023(rx, &port_control);
  2019. if (unlikely(err))
  2020. return RX_DROP_UNUSABLE;
  2021. if (!ieee80211_frame_allowed(rx, fc))
  2022. return RX_DROP_MONITOR;
  2023. /* directly handle TDLS channel switch requests/responses */
  2024. if (unlikely(((struct ethhdr *)rx->skb->data)->h_proto ==
  2025. cpu_to_be16(ETH_P_TDLS))) {
  2026. struct ieee80211_tdls_data *tf = (void *)rx->skb->data;
  2027. if (pskb_may_pull(rx->skb,
  2028. offsetof(struct ieee80211_tdls_data, u)) &&
  2029. tf->payload_type == WLAN_TDLS_SNAP_RFTYPE &&
  2030. tf->category == WLAN_CATEGORY_TDLS &&
  2031. (tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ||
  2032. tf->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE)) {
  2033. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TDLS_CHSW;
  2034. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2035. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2036. if (rx->sta)
  2037. rx->sta->rx_packets++;
  2038. return RX_QUEUED;
  2039. }
  2040. }
  2041. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  2042. unlikely(port_control) && sdata->bss) {
  2043. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  2044. u.ap);
  2045. dev = sdata->dev;
  2046. rx->sdata = sdata;
  2047. }
  2048. rx->skb->dev = dev;
  2049. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  2050. !is_multicast_ether_addr(
  2051. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  2052. (!local->scanning &&
  2053. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  2054. mod_timer(&local->dynamic_ps_timer, jiffies +
  2055. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  2056. }
  2057. ieee80211_deliver_skb(rx);
  2058. return RX_QUEUED;
  2059. }
  2060. static ieee80211_rx_result debug_noinline
  2061. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx, struct sk_buff_head *frames)
  2062. {
  2063. struct sk_buff *skb = rx->skb;
  2064. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  2065. struct tid_ampdu_rx *tid_agg_rx;
  2066. u16 start_seq_num;
  2067. u16 tid;
  2068. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  2069. return RX_CONTINUE;
  2070. if (ieee80211_is_back_req(bar->frame_control)) {
  2071. struct {
  2072. __le16 control, start_seq_num;
  2073. } __packed bar_data;
  2074. struct ieee80211_event event = {
  2075. .type = BAR_RX_EVENT,
  2076. };
  2077. if (!rx->sta)
  2078. return RX_DROP_MONITOR;
  2079. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  2080. &bar_data, sizeof(bar_data)))
  2081. return RX_DROP_MONITOR;
  2082. tid = le16_to_cpu(bar_data.control) >> 12;
  2083. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  2084. if (!tid_agg_rx)
  2085. return RX_DROP_MONITOR;
  2086. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  2087. event.u.ba.tid = tid;
  2088. event.u.ba.ssn = start_seq_num;
  2089. event.u.ba.sta = &rx->sta->sta;
  2090. /* reset session timer */
  2091. if (tid_agg_rx->timeout)
  2092. mod_timer(&tid_agg_rx->session_timer,
  2093. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  2094. spin_lock(&tid_agg_rx->reorder_lock);
  2095. /* release stored frames up to start of BAR */
  2096. ieee80211_release_reorder_frames(rx->sdata, tid_agg_rx,
  2097. start_seq_num, frames);
  2098. spin_unlock(&tid_agg_rx->reorder_lock);
  2099. drv_event_callback(rx->local, rx->sdata, &event);
  2100. kfree_skb(skb);
  2101. return RX_QUEUED;
  2102. }
  2103. /*
  2104. * After this point, we only want management frames,
  2105. * so we can drop all remaining control frames to
  2106. * cooked monitor interfaces.
  2107. */
  2108. return RX_DROP_MONITOR;
  2109. }
  2110. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  2111. struct ieee80211_mgmt *mgmt,
  2112. size_t len)
  2113. {
  2114. struct ieee80211_local *local = sdata->local;
  2115. struct sk_buff *skb;
  2116. struct ieee80211_mgmt *resp;
  2117. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  2118. /* Not to own unicast address */
  2119. return;
  2120. }
  2121. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  2122. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  2123. /* Not from the current AP or not associated yet. */
  2124. return;
  2125. }
  2126. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  2127. /* Too short SA Query request frame */
  2128. return;
  2129. }
  2130. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  2131. if (skb == NULL)
  2132. return;
  2133. skb_reserve(skb, local->hw.extra_tx_headroom);
  2134. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  2135. memset(resp, 0, 24);
  2136. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  2137. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  2138. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  2139. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2140. IEEE80211_STYPE_ACTION);
  2141. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  2142. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  2143. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  2144. memcpy(resp->u.action.u.sa_query.trans_id,
  2145. mgmt->u.action.u.sa_query.trans_id,
  2146. WLAN_SA_QUERY_TR_ID_LEN);
  2147. ieee80211_tx_skb(sdata, skb);
  2148. }
  2149. static ieee80211_rx_result debug_noinline
  2150. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  2151. {
  2152. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2153. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2154. /*
  2155. * From here on, look only at management frames.
  2156. * Data and control frames are already handled,
  2157. * and unknown (reserved) frames are useless.
  2158. */
  2159. if (rx->skb->len < 24)
  2160. return RX_DROP_MONITOR;
  2161. if (!ieee80211_is_mgmt(mgmt->frame_control))
  2162. return RX_DROP_MONITOR;
  2163. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  2164. ieee80211_is_beacon(mgmt->frame_control) &&
  2165. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  2166. int sig = 0;
  2167. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
  2168. sig = status->signal;
  2169. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  2170. rx->skb->data, rx->skb->len,
  2171. status->freq, sig);
  2172. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  2173. }
  2174. if (ieee80211_drop_unencrypted_mgmt(rx))
  2175. return RX_DROP_UNUSABLE;
  2176. return RX_CONTINUE;
  2177. }
  2178. static ieee80211_rx_result debug_noinline
  2179. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  2180. {
  2181. struct ieee80211_local *local = rx->local;
  2182. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2183. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2184. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2185. int len = rx->skb->len;
  2186. if (!ieee80211_is_action(mgmt->frame_control))
  2187. return RX_CONTINUE;
  2188. /* drop too small frames */
  2189. if (len < IEEE80211_MIN_ACTION_SIZE)
  2190. return RX_DROP_UNUSABLE;
  2191. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
  2192. mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
  2193. mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
  2194. return RX_DROP_UNUSABLE;
  2195. switch (mgmt->u.action.category) {
  2196. case WLAN_CATEGORY_HT:
  2197. /* reject HT action frames from stations not supporting HT */
  2198. if (!rx->sta->sta.ht_cap.ht_supported)
  2199. goto invalid;
  2200. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2201. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2202. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2203. sdata->vif.type != NL80211_IFTYPE_AP &&
  2204. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2205. break;
  2206. /* verify action & smps_control/chanwidth are present */
  2207. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2208. goto invalid;
  2209. switch (mgmt->u.action.u.ht_smps.action) {
  2210. case WLAN_HT_ACTION_SMPS: {
  2211. struct ieee80211_supported_band *sband;
  2212. enum ieee80211_smps_mode smps_mode;
  2213. /* convert to HT capability */
  2214. switch (mgmt->u.action.u.ht_smps.smps_control) {
  2215. case WLAN_HT_SMPS_CONTROL_DISABLED:
  2216. smps_mode = IEEE80211_SMPS_OFF;
  2217. break;
  2218. case WLAN_HT_SMPS_CONTROL_STATIC:
  2219. smps_mode = IEEE80211_SMPS_STATIC;
  2220. break;
  2221. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  2222. smps_mode = IEEE80211_SMPS_DYNAMIC;
  2223. break;
  2224. default:
  2225. goto invalid;
  2226. }
  2227. /* if no change do nothing */
  2228. if (rx->sta->sta.smps_mode == smps_mode)
  2229. goto handled;
  2230. rx->sta->sta.smps_mode = smps_mode;
  2231. sband = rx->local->hw.wiphy->bands[status->band];
  2232. rate_control_rate_update(local, sband, rx->sta,
  2233. IEEE80211_RC_SMPS_CHANGED);
  2234. goto handled;
  2235. }
  2236. case WLAN_HT_ACTION_NOTIFY_CHANWIDTH: {
  2237. struct ieee80211_supported_band *sband;
  2238. u8 chanwidth = mgmt->u.action.u.ht_notify_cw.chanwidth;
  2239. enum ieee80211_sta_rx_bandwidth max_bw, new_bw;
  2240. /* If it doesn't support 40 MHz it can't change ... */
  2241. if (!(rx->sta->sta.ht_cap.cap &
  2242. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  2243. goto handled;
  2244. if (chanwidth == IEEE80211_HT_CHANWIDTH_20MHZ)
  2245. max_bw = IEEE80211_STA_RX_BW_20;
  2246. else
  2247. max_bw = ieee80211_sta_cap_rx_bw(rx->sta);
  2248. /* set cur_max_bandwidth and recalc sta bw */
  2249. rx->sta->cur_max_bandwidth = max_bw;
  2250. new_bw = ieee80211_sta_cur_vht_bw(rx->sta);
  2251. if (rx->sta->sta.bandwidth == new_bw)
  2252. goto handled;
  2253. rx->sta->sta.bandwidth = new_bw;
  2254. sband = rx->local->hw.wiphy->bands[status->band];
  2255. rate_control_rate_update(local, sband, rx->sta,
  2256. IEEE80211_RC_BW_CHANGED);
  2257. goto handled;
  2258. }
  2259. default:
  2260. goto invalid;
  2261. }
  2262. break;
  2263. case WLAN_CATEGORY_PUBLIC:
  2264. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2265. goto invalid;
  2266. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2267. break;
  2268. if (!rx->sta)
  2269. break;
  2270. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  2271. break;
  2272. if (mgmt->u.action.u.ext_chan_switch.action_code !=
  2273. WLAN_PUB_ACTION_EXT_CHANSW_ANN)
  2274. break;
  2275. if (len < offsetof(struct ieee80211_mgmt,
  2276. u.action.u.ext_chan_switch.variable))
  2277. goto invalid;
  2278. goto queue;
  2279. case WLAN_CATEGORY_VHT:
  2280. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2281. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2282. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2283. sdata->vif.type != NL80211_IFTYPE_AP &&
  2284. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2285. break;
  2286. /* verify action code is present */
  2287. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2288. goto invalid;
  2289. switch (mgmt->u.action.u.vht_opmode_notif.action_code) {
  2290. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  2291. u8 opmode;
  2292. /* verify opmode is present */
  2293. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  2294. goto invalid;
  2295. opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
  2296. ieee80211_vht_handle_opmode(rx->sdata, rx->sta,
  2297. opmode, status->band,
  2298. false);
  2299. goto handled;
  2300. }
  2301. default:
  2302. break;
  2303. }
  2304. break;
  2305. case WLAN_CATEGORY_BACK:
  2306. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2307. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  2308. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  2309. sdata->vif.type != NL80211_IFTYPE_AP &&
  2310. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2311. break;
  2312. /* verify action_code is present */
  2313. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2314. break;
  2315. switch (mgmt->u.action.u.addba_req.action_code) {
  2316. case WLAN_ACTION_ADDBA_REQ:
  2317. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2318. sizeof(mgmt->u.action.u.addba_req)))
  2319. goto invalid;
  2320. break;
  2321. case WLAN_ACTION_ADDBA_RESP:
  2322. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2323. sizeof(mgmt->u.action.u.addba_resp)))
  2324. goto invalid;
  2325. break;
  2326. case WLAN_ACTION_DELBA:
  2327. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2328. sizeof(mgmt->u.action.u.delba)))
  2329. goto invalid;
  2330. break;
  2331. default:
  2332. goto invalid;
  2333. }
  2334. goto queue;
  2335. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2336. /* verify action_code is present */
  2337. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  2338. break;
  2339. switch (mgmt->u.action.u.measurement.action_code) {
  2340. case WLAN_ACTION_SPCT_MSR_REQ:
  2341. if (status->band != IEEE80211_BAND_5GHZ)
  2342. break;
  2343. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2344. sizeof(mgmt->u.action.u.measurement)))
  2345. break;
  2346. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2347. break;
  2348. ieee80211_process_measurement_req(sdata, mgmt, len);
  2349. goto handled;
  2350. case WLAN_ACTION_SPCT_CHL_SWITCH: {
  2351. u8 *bssid;
  2352. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2353. sizeof(mgmt->u.action.u.chan_switch)))
  2354. break;
  2355. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  2356. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2357. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2358. break;
  2359. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  2360. bssid = sdata->u.mgd.bssid;
  2361. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  2362. bssid = sdata->u.ibss.bssid;
  2363. else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  2364. bssid = mgmt->sa;
  2365. else
  2366. break;
  2367. if (!ether_addr_equal(mgmt->bssid, bssid))
  2368. break;
  2369. goto queue;
  2370. }
  2371. }
  2372. break;
  2373. case WLAN_CATEGORY_SA_QUERY:
  2374. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2375. sizeof(mgmt->u.action.u.sa_query)))
  2376. break;
  2377. switch (mgmt->u.action.u.sa_query.action) {
  2378. case WLAN_ACTION_SA_QUERY_REQUEST:
  2379. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2380. break;
  2381. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2382. goto handled;
  2383. }
  2384. break;
  2385. case WLAN_CATEGORY_SELF_PROTECTED:
  2386. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2387. sizeof(mgmt->u.action.u.self_prot.action_code)))
  2388. break;
  2389. switch (mgmt->u.action.u.self_prot.action_code) {
  2390. case WLAN_SP_MESH_PEERING_OPEN:
  2391. case WLAN_SP_MESH_PEERING_CLOSE:
  2392. case WLAN_SP_MESH_PEERING_CONFIRM:
  2393. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2394. goto invalid;
  2395. if (sdata->u.mesh.user_mpm)
  2396. /* userspace handles this frame */
  2397. break;
  2398. goto queue;
  2399. case WLAN_SP_MGK_INFORM:
  2400. case WLAN_SP_MGK_ACK:
  2401. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2402. goto invalid;
  2403. break;
  2404. }
  2405. break;
  2406. case WLAN_CATEGORY_MESH_ACTION:
  2407. if (len < (IEEE80211_MIN_ACTION_SIZE +
  2408. sizeof(mgmt->u.action.u.mesh_action.action_code)))
  2409. break;
  2410. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2411. break;
  2412. if (mesh_action_is_path_sel(mgmt) &&
  2413. !mesh_path_sel_is_hwmp(sdata))
  2414. break;
  2415. goto queue;
  2416. }
  2417. return RX_CONTINUE;
  2418. invalid:
  2419. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2420. /* will return in the next handlers */
  2421. return RX_CONTINUE;
  2422. handled:
  2423. if (rx->sta)
  2424. rx->sta->rx_packets++;
  2425. dev_kfree_skb(rx->skb);
  2426. return RX_QUEUED;
  2427. queue:
  2428. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2429. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2430. ieee80211_queue_work(&local->hw, &sdata->work);
  2431. if (rx->sta)
  2432. rx->sta->rx_packets++;
  2433. return RX_QUEUED;
  2434. }
  2435. static ieee80211_rx_result debug_noinline
  2436. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2437. {
  2438. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2439. int sig = 0;
  2440. /* skip known-bad action frames and return them in the next handler */
  2441. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2442. return RX_CONTINUE;
  2443. /*
  2444. * Getting here means the kernel doesn't know how to handle
  2445. * it, but maybe userspace does ... include returned frames
  2446. * so userspace can register for those to know whether ones
  2447. * it transmitted were processed or returned.
  2448. */
  2449. if (ieee80211_hw_check(&rx->local->hw, SIGNAL_DBM))
  2450. sig = status->signal;
  2451. if (cfg80211_rx_mgmt(&rx->sdata->wdev, status->freq, sig,
  2452. rx->skb->data, rx->skb->len, 0)) {
  2453. if (rx->sta)
  2454. rx->sta->rx_packets++;
  2455. dev_kfree_skb(rx->skb);
  2456. return RX_QUEUED;
  2457. }
  2458. return RX_CONTINUE;
  2459. }
  2460. static ieee80211_rx_result debug_noinline
  2461. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2462. {
  2463. struct ieee80211_local *local = rx->local;
  2464. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2465. struct sk_buff *nskb;
  2466. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2467. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2468. if (!ieee80211_is_action(mgmt->frame_control))
  2469. return RX_CONTINUE;
  2470. /*
  2471. * For AP mode, hostapd is responsible for handling any action
  2472. * frames that we didn't handle, including returning unknown
  2473. * ones. For all other modes we will return them to the sender,
  2474. * setting the 0x80 bit in the action category, as required by
  2475. * 802.11-2012 9.24.4.
  2476. * Newer versions of hostapd shall also use the management frame
  2477. * registration mechanisms, but older ones still use cooked
  2478. * monitor interfaces so push all frames there.
  2479. */
  2480. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2481. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2482. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2483. return RX_DROP_MONITOR;
  2484. if (is_multicast_ether_addr(mgmt->da))
  2485. return RX_DROP_MONITOR;
  2486. /* do not return rejected action frames */
  2487. if (mgmt->u.action.category & 0x80)
  2488. return RX_DROP_UNUSABLE;
  2489. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2490. GFP_ATOMIC);
  2491. if (nskb) {
  2492. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2493. nmgmt->u.action.category |= 0x80;
  2494. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2495. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2496. memset(nskb->cb, 0, sizeof(nskb->cb));
  2497. if (rx->sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  2498. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(nskb);
  2499. info->flags = IEEE80211_TX_CTL_TX_OFFCHAN |
  2500. IEEE80211_TX_INTFL_OFFCHAN_TX_OK |
  2501. IEEE80211_TX_CTL_NO_CCK_RATE;
  2502. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  2503. info->hw_queue =
  2504. local->hw.offchannel_tx_hw_queue;
  2505. }
  2506. __ieee80211_tx_skb_tid_band(rx->sdata, nskb, 7,
  2507. status->band);
  2508. }
  2509. dev_kfree_skb(rx->skb);
  2510. return RX_QUEUED;
  2511. }
  2512. static ieee80211_rx_result debug_noinline
  2513. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2514. {
  2515. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2516. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2517. __le16 stype;
  2518. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2519. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2520. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2521. sdata->vif.type != NL80211_IFTYPE_OCB &&
  2522. sdata->vif.type != NL80211_IFTYPE_STATION)
  2523. return RX_DROP_MONITOR;
  2524. switch (stype) {
  2525. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2526. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2527. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2528. /* process for all: mesh, mlme, ibss */
  2529. break;
  2530. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2531. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2532. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2533. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2534. if (is_multicast_ether_addr(mgmt->da) &&
  2535. !is_broadcast_ether_addr(mgmt->da))
  2536. return RX_DROP_MONITOR;
  2537. /* process only for station */
  2538. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2539. return RX_DROP_MONITOR;
  2540. break;
  2541. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2542. /* process only for ibss and mesh */
  2543. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2544. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2545. return RX_DROP_MONITOR;
  2546. break;
  2547. default:
  2548. return RX_DROP_MONITOR;
  2549. }
  2550. /* queue up frame and kick off work to process it */
  2551. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2552. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2553. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2554. if (rx->sta)
  2555. rx->sta->rx_packets++;
  2556. return RX_QUEUED;
  2557. }
  2558. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2559. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2560. struct ieee80211_rate *rate)
  2561. {
  2562. struct ieee80211_sub_if_data *sdata;
  2563. struct ieee80211_local *local = rx->local;
  2564. struct sk_buff *skb = rx->skb, *skb2;
  2565. struct net_device *prev_dev = NULL;
  2566. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2567. int needed_headroom;
  2568. /*
  2569. * If cooked monitor has been processed already, then
  2570. * don't do it again. If not, set the flag.
  2571. */
  2572. if (rx->flags & IEEE80211_RX_CMNTR)
  2573. goto out_free_skb;
  2574. rx->flags |= IEEE80211_RX_CMNTR;
  2575. /* If there are no cooked monitor interfaces, just free the SKB */
  2576. if (!local->cooked_mntrs)
  2577. goto out_free_skb;
  2578. /* vendor data is long removed here */
  2579. status->flag &= ~RX_FLAG_RADIOTAP_VENDOR_DATA;
  2580. /* room for the radiotap header based on driver features */
  2581. needed_headroom = ieee80211_rx_radiotap_hdrlen(local, status, skb);
  2582. if (skb_headroom(skb) < needed_headroom &&
  2583. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2584. goto out_free_skb;
  2585. /* prepend radiotap information */
  2586. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2587. false);
  2588. skb_set_mac_header(skb, 0);
  2589. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2590. skb->pkt_type = PACKET_OTHERHOST;
  2591. skb->protocol = htons(ETH_P_802_2);
  2592. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2593. if (!ieee80211_sdata_running(sdata))
  2594. continue;
  2595. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2596. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2597. continue;
  2598. if (prev_dev) {
  2599. skb2 = skb_clone(skb, GFP_ATOMIC);
  2600. if (skb2) {
  2601. skb2->dev = prev_dev;
  2602. netif_receive_skb(skb2);
  2603. }
  2604. }
  2605. prev_dev = sdata->dev;
  2606. ieee80211_rx_stats(sdata->dev, skb->len);
  2607. }
  2608. if (prev_dev) {
  2609. skb->dev = prev_dev;
  2610. netif_receive_skb(skb);
  2611. return;
  2612. }
  2613. out_free_skb:
  2614. dev_kfree_skb(skb);
  2615. }
  2616. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2617. ieee80211_rx_result res)
  2618. {
  2619. switch (res) {
  2620. case RX_DROP_MONITOR:
  2621. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2622. if (rx->sta)
  2623. rx->sta->rx_dropped++;
  2624. /* fall through */
  2625. case RX_CONTINUE: {
  2626. struct ieee80211_rate *rate = NULL;
  2627. struct ieee80211_supported_band *sband;
  2628. struct ieee80211_rx_status *status;
  2629. status = IEEE80211_SKB_RXCB((rx->skb));
  2630. sband = rx->local->hw.wiphy->bands[status->band];
  2631. if (!(status->flag & RX_FLAG_HT) &&
  2632. !(status->flag & RX_FLAG_VHT))
  2633. rate = &sband->bitrates[status->rate_idx];
  2634. ieee80211_rx_cooked_monitor(rx, rate);
  2635. break;
  2636. }
  2637. case RX_DROP_UNUSABLE:
  2638. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2639. if (rx->sta)
  2640. rx->sta->rx_dropped++;
  2641. dev_kfree_skb(rx->skb);
  2642. break;
  2643. case RX_QUEUED:
  2644. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2645. break;
  2646. }
  2647. }
  2648. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
  2649. struct sk_buff_head *frames)
  2650. {
  2651. ieee80211_rx_result res = RX_DROP_MONITOR;
  2652. struct sk_buff *skb;
  2653. #define CALL_RXH(rxh) \
  2654. do { \
  2655. res = rxh(rx); \
  2656. if (res != RX_CONTINUE) \
  2657. goto rxh_next; \
  2658. } while (0);
  2659. /* Lock here to avoid hitting all of the data used in the RX
  2660. * path (e.g. key data, station data, ...) concurrently when
  2661. * a frame is released from the reorder buffer due to timeout
  2662. * from the timer, potentially concurrently with RX from the
  2663. * driver.
  2664. */
  2665. spin_lock_bh(&rx->local->rx_path_lock);
  2666. while ((skb = __skb_dequeue(frames))) {
  2667. /*
  2668. * all the other fields are valid across frames
  2669. * that belong to an aMPDU since they are on the
  2670. * same TID from the same station
  2671. */
  2672. rx->skb = skb;
  2673. CALL_RXH(ieee80211_rx_h_check_more_data)
  2674. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
  2675. CALL_RXH(ieee80211_rx_h_sta_process)
  2676. CALL_RXH(ieee80211_rx_h_decrypt)
  2677. CALL_RXH(ieee80211_rx_h_defragment)
  2678. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2679. /* must be after MMIC verify so header is counted in MPDU mic */
  2680. #ifdef CONFIG_MAC80211_MESH
  2681. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2682. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2683. #endif
  2684. CALL_RXH(ieee80211_rx_h_amsdu)
  2685. CALL_RXH(ieee80211_rx_h_data)
  2686. /* special treatment -- needs the queue */
  2687. res = ieee80211_rx_h_ctrl(rx, frames);
  2688. if (res != RX_CONTINUE)
  2689. goto rxh_next;
  2690. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2691. CALL_RXH(ieee80211_rx_h_action)
  2692. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2693. CALL_RXH(ieee80211_rx_h_action_return)
  2694. CALL_RXH(ieee80211_rx_h_mgmt)
  2695. rxh_next:
  2696. ieee80211_rx_handlers_result(rx, res);
  2697. #undef CALL_RXH
  2698. }
  2699. spin_unlock_bh(&rx->local->rx_path_lock);
  2700. }
  2701. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2702. {
  2703. struct sk_buff_head reorder_release;
  2704. ieee80211_rx_result res = RX_DROP_MONITOR;
  2705. __skb_queue_head_init(&reorder_release);
  2706. #define CALL_RXH(rxh) \
  2707. do { \
  2708. res = rxh(rx); \
  2709. if (res != RX_CONTINUE) \
  2710. goto rxh_next; \
  2711. } while (0);
  2712. CALL_RXH(ieee80211_rx_h_check_dup)
  2713. CALL_RXH(ieee80211_rx_h_check)
  2714. ieee80211_rx_reorder_ampdu(rx, &reorder_release);
  2715. ieee80211_rx_handlers(rx, &reorder_release);
  2716. return;
  2717. rxh_next:
  2718. ieee80211_rx_handlers_result(rx, res);
  2719. #undef CALL_RXH
  2720. }
  2721. /*
  2722. * This function makes calls into the RX path, therefore
  2723. * it has to be invoked under RCU read lock.
  2724. */
  2725. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2726. {
  2727. struct sk_buff_head frames;
  2728. struct ieee80211_rx_data rx = {
  2729. .sta = sta,
  2730. .sdata = sta->sdata,
  2731. .local = sta->local,
  2732. /* This is OK -- must be QoS data frame */
  2733. .security_idx = tid,
  2734. .seqno_idx = tid,
  2735. .flags = IEEE80211_RX_REORDER_TIMER,
  2736. };
  2737. struct tid_ampdu_rx *tid_agg_rx;
  2738. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2739. if (!tid_agg_rx)
  2740. return;
  2741. __skb_queue_head_init(&frames);
  2742. spin_lock(&tid_agg_rx->reorder_lock);
  2743. ieee80211_sta_reorder_release(sta->sdata, tid_agg_rx, &frames);
  2744. spin_unlock(&tid_agg_rx->reorder_lock);
  2745. if (!skb_queue_empty(&frames)) {
  2746. struct ieee80211_event event = {
  2747. .type = BA_FRAME_TIMEOUT,
  2748. .u.ba.tid = tid,
  2749. .u.ba.sta = &sta->sta,
  2750. };
  2751. drv_event_callback(rx.local, rx.sdata, &event);
  2752. }
  2753. ieee80211_rx_handlers(&rx, &frames);
  2754. }
  2755. /* main receive path */
  2756. static bool ieee80211_accept_frame(struct ieee80211_rx_data *rx)
  2757. {
  2758. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2759. struct sk_buff *skb = rx->skb;
  2760. struct ieee80211_hdr *hdr = (void *)skb->data;
  2761. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2762. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2763. int multicast = is_multicast_ether_addr(hdr->addr1);
  2764. switch (sdata->vif.type) {
  2765. case NL80211_IFTYPE_STATION:
  2766. if (!bssid && !sdata->u.mgd.use_4addr)
  2767. return false;
  2768. if (multicast)
  2769. return true;
  2770. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  2771. case NL80211_IFTYPE_ADHOC:
  2772. if (!bssid)
  2773. return false;
  2774. if (ether_addr_equal(sdata->vif.addr, hdr->addr2) ||
  2775. ether_addr_equal(sdata->u.ibss.bssid, hdr->addr2))
  2776. return false;
  2777. if (ieee80211_is_beacon(hdr->frame_control))
  2778. return true;
  2779. if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid))
  2780. return false;
  2781. if (!multicast &&
  2782. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2783. return false;
  2784. if (!rx->sta) {
  2785. int rate_idx;
  2786. if (status->flag & (RX_FLAG_HT | RX_FLAG_VHT))
  2787. rate_idx = 0; /* TODO: HT/VHT rates */
  2788. else
  2789. rate_idx = status->rate_idx;
  2790. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  2791. BIT(rate_idx));
  2792. }
  2793. return true;
  2794. case NL80211_IFTYPE_OCB:
  2795. if (!bssid)
  2796. return false;
  2797. if (ieee80211_is_beacon(hdr->frame_control))
  2798. return false;
  2799. if (!is_broadcast_ether_addr(bssid))
  2800. return false;
  2801. if (!multicast &&
  2802. !ether_addr_equal(sdata->dev->dev_addr, hdr->addr1))
  2803. return false;
  2804. if (!rx->sta) {
  2805. int rate_idx;
  2806. if (status->flag & RX_FLAG_HT)
  2807. rate_idx = 0; /* TODO: HT rates */
  2808. else
  2809. rate_idx = status->rate_idx;
  2810. ieee80211_ocb_rx_no_sta(sdata, bssid, hdr->addr2,
  2811. BIT(rate_idx));
  2812. }
  2813. return true;
  2814. case NL80211_IFTYPE_MESH_POINT:
  2815. if (multicast)
  2816. return true;
  2817. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  2818. case NL80211_IFTYPE_AP_VLAN:
  2819. case NL80211_IFTYPE_AP:
  2820. if (!bssid)
  2821. return ether_addr_equal(sdata->vif.addr, hdr->addr1);
  2822. if (!ieee80211_bssid_match(bssid, sdata->vif.addr)) {
  2823. /*
  2824. * Accept public action frames even when the
  2825. * BSSID doesn't match, this is used for P2P
  2826. * and location updates. Note that mac80211
  2827. * itself never looks at these frames.
  2828. */
  2829. if (!multicast &&
  2830. !ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2831. return false;
  2832. if (ieee80211_is_public_action(hdr, skb->len))
  2833. return true;
  2834. return ieee80211_is_beacon(hdr->frame_control);
  2835. }
  2836. if (!ieee80211_has_tods(hdr->frame_control)) {
  2837. /* ignore data frames to TDLS-peers */
  2838. if (ieee80211_is_data(hdr->frame_control))
  2839. return false;
  2840. /* ignore action frames to TDLS-peers */
  2841. if (ieee80211_is_action(hdr->frame_control) &&
  2842. !ether_addr_equal(bssid, hdr->addr1))
  2843. return false;
  2844. }
  2845. return true;
  2846. case NL80211_IFTYPE_WDS:
  2847. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2848. return false;
  2849. return ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2);
  2850. case NL80211_IFTYPE_P2P_DEVICE:
  2851. return ieee80211_is_public_action(hdr, skb->len) ||
  2852. ieee80211_is_probe_req(hdr->frame_control) ||
  2853. ieee80211_is_probe_resp(hdr->frame_control) ||
  2854. ieee80211_is_beacon(hdr->frame_control);
  2855. default:
  2856. break;
  2857. }
  2858. WARN_ON_ONCE(1);
  2859. return false;
  2860. }
  2861. /*
  2862. * This function returns whether or not the SKB
  2863. * was destined for RX processing or not, which,
  2864. * if consume is true, is equivalent to whether
  2865. * or not the skb was consumed.
  2866. */
  2867. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2868. struct sk_buff *skb, bool consume)
  2869. {
  2870. struct ieee80211_local *local = rx->local;
  2871. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2872. rx->skb = skb;
  2873. if (!ieee80211_accept_frame(rx))
  2874. return false;
  2875. if (!consume) {
  2876. skb = skb_copy(skb, GFP_ATOMIC);
  2877. if (!skb) {
  2878. if (net_ratelimit())
  2879. wiphy_debug(local->hw.wiphy,
  2880. "failed to copy skb for %s\n",
  2881. sdata->name);
  2882. return true;
  2883. }
  2884. rx->skb = skb;
  2885. }
  2886. ieee80211_invoke_rx_handlers(rx);
  2887. return true;
  2888. }
  2889. /*
  2890. * This is the actual Rx frames handler. as it belongs to Rx path it must
  2891. * be called with rcu_read_lock protection.
  2892. */
  2893. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2894. struct sk_buff *skb)
  2895. {
  2896. struct ieee80211_local *local = hw_to_local(hw);
  2897. struct ieee80211_sub_if_data *sdata;
  2898. struct ieee80211_hdr *hdr;
  2899. __le16 fc;
  2900. struct ieee80211_rx_data rx;
  2901. struct ieee80211_sub_if_data *prev;
  2902. struct sta_info *sta, *prev_sta;
  2903. struct rhash_head *tmp;
  2904. int err = 0;
  2905. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2906. memset(&rx, 0, sizeof(rx));
  2907. rx.skb = skb;
  2908. rx.local = local;
  2909. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2910. I802_DEBUG_INC(local->dot11ReceivedFragmentCount);
  2911. if (ieee80211_is_mgmt(fc)) {
  2912. /* drop frame if too short for header */
  2913. if (skb->len < ieee80211_hdrlen(fc))
  2914. err = -ENOBUFS;
  2915. else
  2916. err = skb_linearize(skb);
  2917. } else {
  2918. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2919. }
  2920. if (err) {
  2921. dev_kfree_skb(skb);
  2922. return;
  2923. }
  2924. hdr = (struct ieee80211_hdr *)skb->data;
  2925. ieee80211_parse_qos(&rx);
  2926. ieee80211_verify_alignment(&rx);
  2927. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control) ||
  2928. ieee80211_is_beacon(hdr->frame_control)))
  2929. ieee80211_scan_rx(local, skb);
  2930. if (ieee80211_is_data(fc)) {
  2931. const struct bucket_table *tbl;
  2932. prev_sta = NULL;
  2933. tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
  2934. for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
  2935. if (!prev_sta) {
  2936. prev_sta = sta;
  2937. continue;
  2938. }
  2939. rx.sta = prev_sta;
  2940. rx.sdata = prev_sta->sdata;
  2941. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2942. prev_sta = sta;
  2943. }
  2944. if (prev_sta) {
  2945. rx.sta = prev_sta;
  2946. rx.sdata = prev_sta->sdata;
  2947. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2948. return;
  2949. goto out;
  2950. }
  2951. }
  2952. prev = NULL;
  2953. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2954. if (!ieee80211_sdata_running(sdata))
  2955. continue;
  2956. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2957. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2958. continue;
  2959. /*
  2960. * frame is destined for this interface, but if it's
  2961. * not also for the previous one we handle that after
  2962. * the loop to avoid copying the SKB once too much
  2963. */
  2964. if (!prev) {
  2965. prev = sdata;
  2966. continue;
  2967. }
  2968. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2969. rx.sdata = prev;
  2970. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2971. prev = sdata;
  2972. }
  2973. if (prev) {
  2974. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2975. rx.sdata = prev;
  2976. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2977. return;
  2978. }
  2979. out:
  2980. dev_kfree_skb(skb);
  2981. }
  2982. /*
  2983. * This is the receive path handler. It is called by a low level driver when an
  2984. * 802.11 MPDU is received from the hardware.
  2985. */
  2986. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2987. {
  2988. struct ieee80211_local *local = hw_to_local(hw);
  2989. struct ieee80211_rate *rate = NULL;
  2990. struct ieee80211_supported_band *sband;
  2991. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2992. WARN_ON_ONCE(softirq_count() == 0);
  2993. if (WARN_ON(status->band >= IEEE80211_NUM_BANDS))
  2994. goto drop;
  2995. sband = local->hw.wiphy->bands[status->band];
  2996. if (WARN_ON(!sband))
  2997. goto drop;
  2998. /*
  2999. * If we're suspending, it is possible although not too likely
  3000. * that we'd be receiving frames after having already partially
  3001. * quiesced the stack. We can't process such frames then since
  3002. * that might, for example, cause stations to be added or other
  3003. * driver callbacks be invoked.
  3004. */
  3005. if (unlikely(local->quiescing || local->suspended))
  3006. goto drop;
  3007. /* We might be during a HW reconfig, prevent Rx for the same reason */
  3008. if (unlikely(local->in_reconfig))
  3009. goto drop;
  3010. /*
  3011. * The same happens when we're not even started,
  3012. * but that's worth a warning.
  3013. */
  3014. if (WARN_ON(!local->started))
  3015. goto drop;
  3016. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  3017. /*
  3018. * Validate the rate, unless a PLCP error means that
  3019. * we probably can't have a valid rate here anyway.
  3020. */
  3021. if (status->flag & RX_FLAG_HT) {
  3022. /*
  3023. * rate_idx is MCS index, which can be [0-76]
  3024. * as documented on:
  3025. *
  3026. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  3027. *
  3028. * Anything else would be some sort of driver or
  3029. * hardware error. The driver should catch hardware
  3030. * errors.
  3031. */
  3032. if (WARN(status->rate_idx > 76,
  3033. "Rate marked as an HT rate but passed "
  3034. "status->rate_idx is not "
  3035. "an MCS index [0-76]: %d (0x%02x)\n",
  3036. status->rate_idx,
  3037. status->rate_idx))
  3038. goto drop;
  3039. } else if (status->flag & RX_FLAG_VHT) {
  3040. if (WARN_ONCE(status->rate_idx > 9 ||
  3041. !status->vht_nss ||
  3042. status->vht_nss > 8,
  3043. "Rate marked as a VHT rate but data is invalid: MCS: %d, NSS: %d\n",
  3044. status->rate_idx, status->vht_nss))
  3045. goto drop;
  3046. } else {
  3047. if (WARN_ON(status->rate_idx >= sband->n_bitrates))
  3048. goto drop;
  3049. rate = &sband->bitrates[status->rate_idx];
  3050. }
  3051. }
  3052. status->rx_flags = 0;
  3053. /*
  3054. * key references and virtual interfaces are protected using RCU
  3055. * and this requires that we are in a read-side RCU section during
  3056. * receive processing
  3057. */
  3058. rcu_read_lock();
  3059. /*
  3060. * Frames with failed FCS/PLCP checksum are not returned,
  3061. * all other frames are returned without radiotap header
  3062. * if it was previously present.
  3063. * Also, frames with less than 16 bytes are dropped.
  3064. */
  3065. skb = ieee80211_rx_monitor(local, skb, rate);
  3066. if (!skb) {
  3067. rcu_read_unlock();
  3068. return;
  3069. }
  3070. ieee80211_tpt_led_trig_rx(local,
  3071. ((struct ieee80211_hdr *)skb->data)->frame_control,
  3072. skb->len);
  3073. __ieee80211_rx_handle_packet(hw, skb);
  3074. rcu_read_unlock();
  3075. return;
  3076. drop:
  3077. kfree_skb(skb);
  3078. }
  3079. EXPORT_SYMBOL(ieee80211_rx);
  3080. /* This is a version of the rx handler that can be called from hard irq
  3081. * context. Post the skb on the queue and schedule the tasklet */
  3082. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  3083. {
  3084. struct ieee80211_local *local = hw_to_local(hw);
  3085. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  3086. skb->pkt_type = IEEE80211_RX_MSG;
  3087. skb_queue_tail(&local->skb_queue, skb);
  3088. tasklet_schedule(&local->tasklet);
  3089. }
  3090. EXPORT_SYMBOL(ieee80211_rx_irqsafe);