rx.c 86 KB

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