rx.c 101 KB

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