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