rx.c 102 KB

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