rx.c 85 KB

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