rx.c 88 KB

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