rx.c 103 KB

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