rx.c 96 KB

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