rx.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119
  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++ = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  181. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  182. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  183. *pos = 0;
  184. if (status->flag & RX_FLAG_SHORT_GI)
  185. *pos |= IEEE80211_RADIOTAP_MCS_SGI;
  186. if (status->flag & RX_FLAG_40MHZ)
  187. *pos |= IEEE80211_RADIOTAP_MCS_BW_40;
  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. printk(KERN_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. printk(KERN_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. printk(KERN_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. printk(KERN_DEBUG "%s: RX reassembly removed oldest "
  1183. "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
  1184. "addr1=%pM addr2=%pM\n",
  1185. sdata->name, idx,
  1186. jiffies - entry->first_frag_time, entry->seq,
  1187. entry->last_frag, hdr->addr1, hdr->addr2);
  1188. #endif
  1189. __skb_queue_purge(&entry->skb_list);
  1190. }
  1191. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  1192. *skb = NULL;
  1193. entry->first_frag_time = jiffies;
  1194. entry->seq = seq;
  1195. entry->rx_queue = rx_queue;
  1196. entry->last_frag = frag;
  1197. entry->ccmp = 0;
  1198. entry->extra_len = 0;
  1199. return entry;
  1200. }
  1201. static inline struct ieee80211_fragment_entry *
  1202. ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  1203. unsigned int frag, unsigned int seq,
  1204. int rx_queue, struct ieee80211_hdr *hdr)
  1205. {
  1206. struct ieee80211_fragment_entry *entry;
  1207. int i, idx;
  1208. idx = sdata->fragment_next;
  1209. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  1210. struct ieee80211_hdr *f_hdr;
  1211. idx--;
  1212. if (idx < 0)
  1213. idx = IEEE80211_FRAGMENT_MAX - 1;
  1214. entry = &sdata->fragments[idx];
  1215. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  1216. entry->rx_queue != rx_queue ||
  1217. entry->last_frag + 1 != frag)
  1218. continue;
  1219. f_hdr = (struct ieee80211_hdr *)entry->skb_list.next->data;
  1220. /*
  1221. * Check ftype and addresses are equal, else check next fragment
  1222. */
  1223. if (((hdr->frame_control ^ f_hdr->frame_control) &
  1224. cpu_to_le16(IEEE80211_FCTL_FTYPE)) ||
  1225. !ether_addr_equal(hdr->addr1, f_hdr->addr1) ||
  1226. !ether_addr_equal(hdr->addr2, f_hdr->addr2))
  1227. continue;
  1228. if (time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
  1229. __skb_queue_purge(&entry->skb_list);
  1230. continue;
  1231. }
  1232. return entry;
  1233. }
  1234. return NULL;
  1235. }
  1236. static ieee80211_rx_result debug_noinline
  1237. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  1238. {
  1239. struct ieee80211_hdr *hdr;
  1240. u16 sc;
  1241. __le16 fc;
  1242. unsigned int frag, seq;
  1243. struct ieee80211_fragment_entry *entry;
  1244. struct sk_buff *skb;
  1245. struct ieee80211_rx_status *status;
  1246. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1247. fc = hdr->frame_control;
  1248. sc = le16_to_cpu(hdr->seq_ctrl);
  1249. frag = sc & IEEE80211_SCTL_FRAG;
  1250. if (likely((!ieee80211_has_morefrags(fc) && frag == 0) ||
  1251. (rx->skb)->len < 24 ||
  1252. is_multicast_ether_addr(hdr->addr1))) {
  1253. /* not fragmented */
  1254. goto out;
  1255. }
  1256. I802_DEBUG_INC(rx->local->rx_handlers_fragments);
  1257. if (skb_linearize(rx->skb))
  1258. return RX_DROP_UNUSABLE;
  1259. /*
  1260. * skb_linearize() might change the skb->data and
  1261. * previously cached variables (in this case, hdr) need to
  1262. * be refreshed with the new data.
  1263. */
  1264. hdr = (struct ieee80211_hdr *)rx->skb->data;
  1265. seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  1266. if (frag == 0) {
  1267. /* This is the first fragment of a new frame. */
  1268. entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  1269. rx->seqno_idx, &(rx->skb));
  1270. if (rx->key && rx->key->conf.cipher == WLAN_CIPHER_SUITE_CCMP &&
  1271. ieee80211_has_protected(fc)) {
  1272. int queue = rx->security_idx;
  1273. /* Store CCMP PN so that we can verify that the next
  1274. * fragment has a sequential PN value. */
  1275. entry->ccmp = 1;
  1276. memcpy(entry->last_pn,
  1277. rx->key->u.ccmp.rx_pn[queue],
  1278. CCMP_PN_LEN);
  1279. }
  1280. return RX_QUEUED;
  1281. }
  1282. /* This is a fragment for a frame that should already be pending in
  1283. * fragment cache. Add this fragment to the end of the pending entry.
  1284. */
  1285. entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  1286. rx->seqno_idx, hdr);
  1287. if (!entry) {
  1288. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1289. return RX_DROP_MONITOR;
  1290. }
  1291. /* Verify that MPDUs within one MSDU have sequential PN values.
  1292. * (IEEE 802.11i, 8.3.3.4.5) */
  1293. if (entry->ccmp) {
  1294. int i;
  1295. u8 pn[CCMP_PN_LEN], *rpn;
  1296. int queue;
  1297. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_CCMP)
  1298. return RX_DROP_UNUSABLE;
  1299. memcpy(pn, entry->last_pn, CCMP_PN_LEN);
  1300. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  1301. pn[i]++;
  1302. if (pn[i])
  1303. break;
  1304. }
  1305. queue = rx->security_idx;
  1306. rpn = rx->key->u.ccmp.rx_pn[queue];
  1307. if (memcmp(pn, rpn, CCMP_PN_LEN))
  1308. return RX_DROP_UNUSABLE;
  1309. memcpy(entry->last_pn, pn, CCMP_PN_LEN);
  1310. }
  1311. skb_pull(rx->skb, ieee80211_hdrlen(fc));
  1312. __skb_queue_tail(&entry->skb_list, rx->skb);
  1313. entry->last_frag = frag;
  1314. entry->extra_len += rx->skb->len;
  1315. if (ieee80211_has_morefrags(fc)) {
  1316. rx->skb = NULL;
  1317. return RX_QUEUED;
  1318. }
  1319. rx->skb = __skb_dequeue(&entry->skb_list);
  1320. if (skb_tailroom(rx->skb) < entry->extra_len) {
  1321. I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
  1322. if (unlikely(pskb_expand_head(rx->skb, 0, entry->extra_len,
  1323. GFP_ATOMIC))) {
  1324. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  1325. __skb_queue_purge(&entry->skb_list);
  1326. return RX_DROP_UNUSABLE;
  1327. }
  1328. }
  1329. while ((skb = __skb_dequeue(&entry->skb_list))) {
  1330. memcpy(skb_put(rx->skb, skb->len), skb->data, skb->len);
  1331. dev_kfree_skb(skb);
  1332. }
  1333. /* Complete frame has been reassembled - process it now */
  1334. status = IEEE80211_SKB_RXCB(rx->skb);
  1335. status->rx_flags |= IEEE80211_RX_FRAGMENTED;
  1336. out:
  1337. if (rx->sta)
  1338. rx->sta->rx_packets++;
  1339. if (is_multicast_ether_addr(hdr->addr1))
  1340. rx->local->dot11MulticastReceivedFrameCount++;
  1341. else
  1342. ieee80211_led_rx(rx->local);
  1343. return RX_CONTINUE;
  1344. }
  1345. static int
  1346. ieee80211_802_1x_port_control(struct ieee80211_rx_data *rx)
  1347. {
  1348. if (unlikely(!rx->sta ||
  1349. !test_sta_flag(rx->sta, WLAN_STA_AUTHORIZED)))
  1350. return -EACCES;
  1351. return 0;
  1352. }
  1353. static int
  1354. ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
  1355. {
  1356. struct sk_buff *skb = rx->skb;
  1357. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1358. /*
  1359. * Pass through unencrypted frames if the hardware has
  1360. * decrypted them already.
  1361. */
  1362. if (status->flag & RX_FLAG_DECRYPTED)
  1363. return 0;
  1364. /* Drop unencrypted frames if key is set. */
  1365. if (unlikely(!ieee80211_has_protected(fc) &&
  1366. !ieee80211_is_nullfunc(fc) &&
  1367. ieee80211_is_data(fc) &&
  1368. (rx->key || rx->sdata->drop_unencrypted)))
  1369. return -EACCES;
  1370. return 0;
  1371. }
  1372. static int
  1373. ieee80211_drop_unencrypted_mgmt(struct ieee80211_rx_data *rx)
  1374. {
  1375. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1376. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1377. __le16 fc = hdr->frame_control;
  1378. /*
  1379. * Pass through unencrypted frames if the hardware has
  1380. * decrypted them already.
  1381. */
  1382. if (status->flag & RX_FLAG_DECRYPTED)
  1383. return 0;
  1384. if (rx->sta && test_sta_flag(rx->sta, WLAN_STA_MFP)) {
  1385. if (unlikely(!ieee80211_has_protected(fc) &&
  1386. ieee80211_is_unicast_robust_mgmt_frame(rx->skb) &&
  1387. rx->key)) {
  1388. if (ieee80211_is_deauth(fc))
  1389. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1390. rx->skb->data,
  1391. rx->skb->len);
  1392. else if (ieee80211_is_disassoc(fc))
  1393. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1394. rx->skb->data,
  1395. rx->skb->len);
  1396. return -EACCES;
  1397. }
  1398. /* BIP does not use Protected field, so need to check MMIE */
  1399. if (unlikely(ieee80211_is_multicast_robust_mgmt_frame(rx->skb) &&
  1400. ieee80211_get_mmie_keyidx(rx->skb) < 0)) {
  1401. if (ieee80211_is_deauth(fc))
  1402. cfg80211_send_unprot_deauth(rx->sdata->dev,
  1403. rx->skb->data,
  1404. rx->skb->len);
  1405. else if (ieee80211_is_disassoc(fc))
  1406. cfg80211_send_unprot_disassoc(rx->sdata->dev,
  1407. rx->skb->data,
  1408. rx->skb->len);
  1409. return -EACCES;
  1410. }
  1411. /*
  1412. * When using MFP, Action frames are not allowed prior to
  1413. * having configured keys.
  1414. */
  1415. if (unlikely(ieee80211_is_action(fc) && !rx->key &&
  1416. ieee80211_is_robust_mgmt_frame(
  1417. (struct ieee80211_hdr *) rx->skb->data)))
  1418. return -EACCES;
  1419. }
  1420. return 0;
  1421. }
  1422. static int
  1423. __ieee80211_data_to_8023(struct ieee80211_rx_data *rx, bool *port_control)
  1424. {
  1425. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1426. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1427. bool check_port_control = false;
  1428. struct ethhdr *ehdr;
  1429. int ret;
  1430. *port_control = false;
  1431. if (ieee80211_has_a4(hdr->frame_control) &&
  1432. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && !sdata->u.vlan.sta)
  1433. return -1;
  1434. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1435. !!sdata->u.mgd.use_4addr != !!ieee80211_has_a4(hdr->frame_control)) {
  1436. if (!sdata->u.mgd.use_4addr)
  1437. return -1;
  1438. else
  1439. check_port_control = true;
  1440. }
  1441. if (is_multicast_ether_addr(hdr->addr1) &&
  1442. sdata->vif.type == NL80211_IFTYPE_AP_VLAN && sdata->u.vlan.sta)
  1443. return -1;
  1444. ret = ieee80211_data_to_8023(rx->skb, sdata->vif.addr, sdata->vif.type);
  1445. if (ret < 0)
  1446. return ret;
  1447. ehdr = (struct ethhdr *) rx->skb->data;
  1448. if (ehdr->h_proto == rx->sdata->control_port_protocol)
  1449. *port_control = true;
  1450. else if (check_port_control)
  1451. return -1;
  1452. return 0;
  1453. }
  1454. /*
  1455. * requires that rx->skb is a frame with ethernet header
  1456. */
  1457. static bool ieee80211_frame_allowed(struct ieee80211_rx_data *rx, __le16 fc)
  1458. {
  1459. static const u8 pae_group_addr[ETH_ALEN] __aligned(2)
  1460. = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x03 };
  1461. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1462. /*
  1463. * Allow EAPOL frames to us/the PAE group address regardless
  1464. * of whether the frame was encrypted or not.
  1465. */
  1466. if (ehdr->h_proto == rx->sdata->control_port_protocol &&
  1467. (ether_addr_equal(ehdr->h_dest, rx->sdata->vif.addr) ||
  1468. ether_addr_equal(ehdr->h_dest, pae_group_addr)))
  1469. return true;
  1470. if (ieee80211_802_1x_port_control(rx) ||
  1471. ieee80211_drop_unencrypted(rx, fc))
  1472. return false;
  1473. return true;
  1474. }
  1475. /*
  1476. * requires that rx->skb is a frame with ethernet header
  1477. */
  1478. static void
  1479. ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
  1480. {
  1481. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1482. struct net_device *dev = sdata->dev;
  1483. struct sk_buff *skb, *xmit_skb;
  1484. struct ethhdr *ehdr = (struct ethhdr *) rx->skb->data;
  1485. struct sta_info *dsta;
  1486. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1487. skb = rx->skb;
  1488. xmit_skb = NULL;
  1489. if ((sdata->vif.type == NL80211_IFTYPE_AP ||
  1490. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
  1491. !(sdata->flags & IEEE80211_SDATA_DONT_BRIDGE_PACKETS) &&
  1492. (status->rx_flags & IEEE80211_RX_RA_MATCH) &&
  1493. (sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->u.vlan.sta)) {
  1494. if (is_multicast_ether_addr(ehdr->h_dest)) {
  1495. /*
  1496. * send multicast frames both to higher layers in
  1497. * local net stack and back to the wireless medium
  1498. */
  1499. xmit_skb = skb_copy(skb, GFP_ATOMIC);
  1500. if (!xmit_skb && net_ratelimit())
  1501. printk(KERN_DEBUG "%s: failed to clone "
  1502. "multicast frame\n", dev->name);
  1503. } else {
  1504. dsta = sta_info_get(sdata, skb->data);
  1505. if (dsta) {
  1506. /*
  1507. * The destination station is associated to
  1508. * this AP (in this VLAN), so send the frame
  1509. * directly to it and do not pass it to local
  1510. * net stack.
  1511. */
  1512. xmit_skb = skb;
  1513. skb = NULL;
  1514. }
  1515. }
  1516. }
  1517. if (skb) {
  1518. int align __maybe_unused;
  1519. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1520. /*
  1521. * 'align' will only take the values 0 or 2 here
  1522. * since all frames are required to be aligned
  1523. * to 2-byte boundaries when being passed to
  1524. * mac80211. That also explains the __skb_push()
  1525. * below.
  1526. */
  1527. align = ((unsigned long)(skb->data + sizeof(struct ethhdr))) & 3;
  1528. if (align) {
  1529. if (WARN_ON(skb_headroom(skb) < 3)) {
  1530. dev_kfree_skb(skb);
  1531. skb = NULL;
  1532. } else {
  1533. u8 *data = skb->data;
  1534. size_t len = skb_headlen(skb);
  1535. skb->data -= align;
  1536. memmove(skb->data, data, len);
  1537. skb_set_tail_pointer(skb, len);
  1538. }
  1539. }
  1540. #endif
  1541. if (skb) {
  1542. /* deliver to local stack */
  1543. skb->protocol = eth_type_trans(skb, dev);
  1544. memset(skb->cb, 0, sizeof(skb->cb));
  1545. netif_receive_skb(skb);
  1546. }
  1547. }
  1548. if (xmit_skb) {
  1549. /*
  1550. * Send to wireless media and increase priority by 256 to
  1551. * keep the received priority instead of reclassifying
  1552. * the frame (see cfg80211_classify8021d).
  1553. */
  1554. xmit_skb->priority += 256;
  1555. xmit_skb->protocol = htons(ETH_P_802_3);
  1556. skb_reset_network_header(xmit_skb);
  1557. skb_reset_mac_header(xmit_skb);
  1558. dev_queue_xmit(xmit_skb);
  1559. }
  1560. }
  1561. static ieee80211_rx_result debug_noinline
  1562. ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
  1563. {
  1564. struct net_device *dev = rx->sdata->dev;
  1565. struct sk_buff *skb = rx->skb;
  1566. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1567. __le16 fc = hdr->frame_control;
  1568. struct sk_buff_head frame_list;
  1569. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1570. if (unlikely(!ieee80211_is_data(fc)))
  1571. return RX_CONTINUE;
  1572. if (unlikely(!ieee80211_is_data_present(fc)))
  1573. return RX_DROP_MONITOR;
  1574. if (!(status->rx_flags & IEEE80211_RX_AMSDU))
  1575. return RX_CONTINUE;
  1576. if (ieee80211_has_a4(hdr->frame_control) &&
  1577. rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1578. !rx->sdata->u.vlan.sta)
  1579. return RX_DROP_UNUSABLE;
  1580. if (is_multicast_ether_addr(hdr->addr1) &&
  1581. ((rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1582. rx->sdata->u.vlan.sta) ||
  1583. (rx->sdata->vif.type == NL80211_IFTYPE_STATION &&
  1584. rx->sdata->u.mgd.use_4addr)))
  1585. return RX_DROP_UNUSABLE;
  1586. skb->dev = dev;
  1587. __skb_queue_head_init(&frame_list);
  1588. if (skb_linearize(skb))
  1589. return RX_DROP_UNUSABLE;
  1590. ieee80211_amsdu_to_8023s(skb, &frame_list, dev->dev_addr,
  1591. rx->sdata->vif.type,
  1592. rx->local->hw.extra_tx_headroom, true);
  1593. while (!skb_queue_empty(&frame_list)) {
  1594. rx->skb = __skb_dequeue(&frame_list);
  1595. if (!ieee80211_frame_allowed(rx, fc)) {
  1596. dev_kfree_skb(rx->skb);
  1597. continue;
  1598. }
  1599. dev->stats.rx_packets++;
  1600. dev->stats.rx_bytes += rx->skb->len;
  1601. ieee80211_deliver_skb(rx);
  1602. }
  1603. return RX_QUEUED;
  1604. }
  1605. #ifdef CONFIG_MAC80211_MESH
  1606. static ieee80211_rx_result
  1607. ieee80211_rx_h_mesh_fwding(struct ieee80211_rx_data *rx)
  1608. {
  1609. struct ieee80211_hdr *fwd_hdr, *hdr;
  1610. struct ieee80211_tx_info *info;
  1611. struct ieee80211s_hdr *mesh_hdr;
  1612. struct sk_buff *skb = rx->skb, *fwd_skb;
  1613. struct ieee80211_local *local = rx->local;
  1614. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1615. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  1616. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1617. __le16 reason = cpu_to_le16(WLAN_REASON_MESH_PATH_NOFORWARD);
  1618. u16 q, hdrlen;
  1619. hdr = (struct ieee80211_hdr *) skb->data;
  1620. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1621. mesh_hdr = (struct ieee80211s_hdr *) (skb->data + hdrlen);
  1622. /* frame is in RMC, don't forward */
  1623. if (ieee80211_is_data(hdr->frame_control) &&
  1624. is_multicast_ether_addr(hdr->addr1) &&
  1625. mesh_rmc_check(hdr->addr3, mesh_hdr, rx->sdata))
  1626. return RX_DROP_MONITOR;
  1627. if (!ieee80211_is_data(hdr->frame_control))
  1628. return RX_CONTINUE;
  1629. if (!mesh_hdr->ttl)
  1630. return RX_DROP_MONITOR;
  1631. if (mesh_hdr->flags & MESH_FLAGS_AE) {
  1632. struct mesh_path *mppath;
  1633. char *proxied_addr;
  1634. char *mpp_addr;
  1635. if (is_multicast_ether_addr(hdr->addr1)) {
  1636. mpp_addr = hdr->addr3;
  1637. proxied_addr = mesh_hdr->eaddr1;
  1638. } else {
  1639. mpp_addr = hdr->addr4;
  1640. proxied_addr = mesh_hdr->eaddr2;
  1641. }
  1642. rcu_read_lock();
  1643. mppath = mpp_path_lookup(proxied_addr, sdata);
  1644. if (!mppath) {
  1645. mpp_path_add(proxied_addr, mpp_addr, sdata);
  1646. } else {
  1647. spin_lock_bh(&mppath->state_lock);
  1648. if (!ether_addr_equal(mppath->mpp, mpp_addr))
  1649. memcpy(mppath->mpp, mpp_addr, ETH_ALEN);
  1650. spin_unlock_bh(&mppath->state_lock);
  1651. }
  1652. rcu_read_unlock();
  1653. }
  1654. /* Frame has reached destination. Don't forward */
  1655. if (!is_multicast_ether_addr(hdr->addr1) &&
  1656. ether_addr_equal(sdata->vif.addr, hdr->addr3))
  1657. return RX_CONTINUE;
  1658. q = ieee80211_select_queue_80211(local, skb, hdr);
  1659. if (ieee80211_queue_stopped(&local->hw, q)) {
  1660. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_congestion);
  1661. return RX_DROP_MONITOR;
  1662. }
  1663. skb_set_queue_mapping(skb, q);
  1664. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1665. goto out;
  1666. if (!--mesh_hdr->ttl) {
  1667. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_ttl);
  1668. return RX_DROP_MONITOR;
  1669. }
  1670. if (!ifmsh->mshcfg.dot11MeshForwarding)
  1671. goto out;
  1672. fwd_skb = skb_copy(skb, GFP_ATOMIC);
  1673. if (!fwd_skb) {
  1674. if (net_ratelimit())
  1675. printk(KERN_DEBUG "%s: failed to clone mesh frame\n",
  1676. sdata->name);
  1677. goto out;
  1678. }
  1679. fwd_hdr = (struct ieee80211_hdr *) fwd_skb->data;
  1680. info = IEEE80211_SKB_CB(fwd_skb);
  1681. memset(info, 0, sizeof(*info));
  1682. info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
  1683. info->control.vif = &rx->sdata->vif;
  1684. info->control.jiffies = jiffies;
  1685. if (is_multicast_ether_addr(fwd_hdr->addr1)) {
  1686. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_mcast);
  1687. memcpy(fwd_hdr->addr2, sdata->vif.addr, ETH_ALEN);
  1688. } else if (!mesh_nexthop_lookup(fwd_skb, sdata)) {
  1689. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_unicast);
  1690. } else {
  1691. /* unable to resolve next hop */
  1692. mesh_path_error_tx(ifmsh->mshcfg.element_ttl, fwd_hdr->addr3,
  1693. 0, reason, fwd_hdr->addr2, sdata);
  1694. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, dropped_frames_no_route);
  1695. kfree_skb(fwd_skb);
  1696. return RX_DROP_MONITOR;
  1697. }
  1698. IEEE80211_IFSTA_MESH_CTR_INC(ifmsh, fwded_frames);
  1699. ieee80211_add_pending_skb(local, fwd_skb);
  1700. out:
  1701. if (is_multicast_ether_addr(hdr->addr1) ||
  1702. sdata->dev->flags & IFF_PROMISC)
  1703. return RX_CONTINUE;
  1704. else
  1705. return RX_DROP_MONITOR;
  1706. }
  1707. #endif
  1708. static ieee80211_rx_result debug_noinline
  1709. ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
  1710. {
  1711. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1712. struct ieee80211_local *local = rx->local;
  1713. struct net_device *dev = sdata->dev;
  1714. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  1715. __le16 fc = hdr->frame_control;
  1716. bool port_control;
  1717. int err;
  1718. if (unlikely(!ieee80211_is_data(hdr->frame_control)))
  1719. return RX_CONTINUE;
  1720. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  1721. return RX_DROP_MONITOR;
  1722. /*
  1723. * Send unexpected-4addr-frame event to hostapd. For older versions,
  1724. * also drop the frame to cooked monitor interfaces.
  1725. */
  1726. if (ieee80211_has_a4(hdr->frame_control) &&
  1727. sdata->vif.type == NL80211_IFTYPE_AP) {
  1728. if (rx->sta &&
  1729. !test_and_set_sta_flag(rx->sta, WLAN_STA_4ADDR_EVENT))
  1730. cfg80211_rx_unexpected_4addr_frame(
  1731. rx->sdata->dev, rx->sta->sta.addr, GFP_ATOMIC);
  1732. return RX_DROP_MONITOR;
  1733. }
  1734. err = __ieee80211_data_to_8023(rx, &port_control);
  1735. if (unlikely(err))
  1736. return RX_DROP_UNUSABLE;
  1737. if (!ieee80211_frame_allowed(rx, fc))
  1738. return RX_DROP_MONITOR;
  1739. if (rx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1740. unlikely(port_control) && sdata->bss) {
  1741. sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
  1742. u.ap);
  1743. dev = sdata->dev;
  1744. rx->sdata = sdata;
  1745. }
  1746. rx->skb->dev = dev;
  1747. dev->stats.rx_packets++;
  1748. dev->stats.rx_bytes += rx->skb->len;
  1749. if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
  1750. !is_multicast_ether_addr(
  1751. ((struct ethhdr *)rx->skb->data)->h_dest) &&
  1752. (!local->scanning &&
  1753. !test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))) {
  1754. mod_timer(&local->dynamic_ps_timer, jiffies +
  1755. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  1756. }
  1757. ieee80211_deliver_skb(rx);
  1758. return RX_QUEUED;
  1759. }
  1760. static ieee80211_rx_result debug_noinline
  1761. ieee80211_rx_h_ctrl(struct ieee80211_rx_data *rx)
  1762. {
  1763. struct ieee80211_local *local = rx->local;
  1764. struct ieee80211_hw *hw = &local->hw;
  1765. struct sk_buff *skb = rx->skb;
  1766. struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
  1767. struct tid_ampdu_rx *tid_agg_rx;
  1768. u16 start_seq_num;
  1769. u16 tid;
  1770. if (likely(!ieee80211_is_ctl(bar->frame_control)))
  1771. return RX_CONTINUE;
  1772. if (ieee80211_is_back_req(bar->frame_control)) {
  1773. struct {
  1774. __le16 control, start_seq_num;
  1775. } __packed bar_data;
  1776. if (!rx->sta)
  1777. return RX_DROP_MONITOR;
  1778. if (skb_copy_bits(skb, offsetof(struct ieee80211_bar, control),
  1779. &bar_data, sizeof(bar_data)))
  1780. return RX_DROP_MONITOR;
  1781. tid = le16_to_cpu(bar_data.control) >> 12;
  1782. tid_agg_rx = rcu_dereference(rx->sta->ampdu_mlme.tid_rx[tid]);
  1783. if (!tid_agg_rx)
  1784. return RX_DROP_MONITOR;
  1785. start_seq_num = le16_to_cpu(bar_data.start_seq_num) >> 4;
  1786. /* reset session timer */
  1787. if (tid_agg_rx->timeout)
  1788. mod_timer(&tid_agg_rx->session_timer,
  1789. TU_TO_EXP_TIME(tid_agg_rx->timeout));
  1790. spin_lock(&tid_agg_rx->reorder_lock);
  1791. /* release stored frames up to start of BAR */
  1792. ieee80211_release_reorder_frames(hw, tid_agg_rx, start_seq_num);
  1793. spin_unlock(&tid_agg_rx->reorder_lock);
  1794. kfree_skb(skb);
  1795. return RX_QUEUED;
  1796. }
  1797. /*
  1798. * After this point, we only want management frames,
  1799. * so we can drop all remaining control frames to
  1800. * cooked monitor interfaces.
  1801. */
  1802. return RX_DROP_MONITOR;
  1803. }
  1804. static void ieee80211_process_sa_query_req(struct ieee80211_sub_if_data *sdata,
  1805. struct ieee80211_mgmt *mgmt,
  1806. size_t len)
  1807. {
  1808. struct ieee80211_local *local = sdata->local;
  1809. struct sk_buff *skb;
  1810. struct ieee80211_mgmt *resp;
  1811. if (!ether_addr_equal(mgmt->da, sdata->vif.addr)) {
  1812. /* Not to own unicast address */
  1813. return;
  1814. }
  1815. if (!ether_addr_equal(mgmt->sa, sdata->u.mgd.bssid) ||
  1816. !ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) {
  1817. /* Not from the current AP or not associated yet. */
  1818. return;
  1819. }
  1820. if (len < 24 + 1 + sizeof(resp->u.action.u.sa_query)) {
  1821. /* Too short SA Query request frame */
  1822. return;
  1823. }
  1824. skb = dev_alloc_skb(sizeof(*resp) + local->hw.extra_tx_headroom);
  1825. if (skb == NULL)
  1826. return;
  1827. skb_reserve(skb, local->hw.extra_tx_headroom);
  1828. resp = (struct ieee80211_mgmt *) skb_put(skb, 24);
  1829. memset(resp, 0, 24);
  1830. memcpy(resp->da, mgmt->sa, ETH_ALEN);
  1831. memcpy(resp->sa, sdata->vif.addr, ETH_ALEN);
  1832. memcpy(resp->bssid, sdata->u.mgd.bssid, ETH_ALEN);
  1833. resp->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1834. IEEE80211_STYPE_ACTION);
  1835. skb_put(skb, 1 + sizeof(resp->u.action.u.sa_query));
  1836. resp->u.action.category = WLAN_CATEGORY_SA_QUERY;
  1837. resp->u.action.u.sa_query.action = WLAN_ACTION_SA_QUERY_RESPONSE;
  1838. memcpy(resp->u.action.u.sa_query.trans_id,
  1839. mgmt->u.action.u.sa_query.trans_id,
  1840. WLAN_SA_QUERY_TR_ID_LEN);
  1841. ieee80211_tx_skb(sdata, skb);
  1842. }
  1843. static ieee80211_rx_result debug_noinline
  1844. ieee80211_rx_h_mgmt_check(struct ieee80211_rx_data *rx)
  1845. {
  1846. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1847. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1848. /*
  1849. * From here on, look only at management frames.
  1850. * Data and control frames are already handled,
  1851. * and unknown (reserved) frames are useless.
  1852. */
  1853. if (rx->skb->len < 24)
  1854. return RX_DROP_MONITOR;
  1855. if (!ieee80211_is_mgmt(mgmt->frame_control))
  1856. return RX_DROP_MONITOR;
  1857. if (rx->sdata->vif.type == NL80211_IFTYPE_AP &&
  1858. ieee80211_is_beacon(mgmt->frame_control) &&
  1859. !(rx->flags & IEEE80211_RX_BEACON_REPORTED)) {
  1860. int sig = 0;
  1861. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  1862. sig = status->signal;
  1863. cfg80211_report_obss_beacon(rx->local->hw.wiphy,
  1864. rx->skb->data, rx->skb->len,
  1865. status->freq, sig, GFP_ATOMIC);
  1866. rx->flags |= IEEE80211_RX_BEACON_REPORTED;
  1867. }
  1868. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1869. return RX_DROP_MONITOR;
  1870. if (ieee80211_drop_unencrypted_mgmt(rx))
  1871. return RX_DROP_UNUSABLE;
  1872. return RX_CONTINUE;
  1873. }
  1874. static ieee80211_rx_result debug_noinline
  1875. ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
  1876. {
  1877. struct ieee80211_local *local = rx->local;
  1878. struct ieee80211_sub_if_data *sdata = rx->sdata;
  1879. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  1880. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  1881. int len = rx->skb->len;
  1882. if (!ieee80211_is_action(mgmt->frame_control))
  1883. return RX_CONTINUE;
  1884. /* drop too small frames */
  1885. if (len < IEEE80211_MIN_ACTION_SIZE)
  1886. return RX_DROP_UNUSABLE;
  1887. if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
  1888. return RX_DROP_UNUSABLE;
  1889. if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
  1890. return RX_DROP_UNUSABLE;
  1891. switch (mgmt->u.action.category) {
  1892. case WLAN_CATEGORY_HT:
  1893. /* reject HT action frames from stations not supporting HT */
  1894. if (!rx->sta->sta.ht_cap.ht_supported)
  1895. goto invalid;
  1896. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1897. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  1898. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1899. sdata->vif.type != NL80211_IFTYPE_AP &&
  1900. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1901. break;
  1902. /* verify action & smps_control are present */
  1903. if (len < IEEE80211_MIN_ACTION_SIZE + 2)
  1904. goto invalid;
  1905. switch (mgmt->u.action.u.ht_smps.action) {
  1906. case WLAN_HT_ACTION_SMPS: {
  1907. struct ieee80211_supported_band *sband;
  1908. u8 smps;
  1909. /* convert to HT capability */
  1910. switch (mgmt->u.action.u.ht_smps.smps_control) {
  1911. case WLAN_HT_SMPS_CONTROL_DISABLED:
  1912. smps = WLAN_HT_CAP_SM_PS_DISABLED;
  1913. break;
  1914. case WLAN_HT_SMPS_CONTROL_STATIC:
  1915. smps = WLAN_HT_CAP_SM_PS_STATIC;
  1916. break;
  1917. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  1918. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  1919. break;
  1920. default:
  1921. goto invalid;
  1922. }
  1923. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  1924. /* if no change do nothing */
  1925. if ((rx->sta->sta.ht_cap.cap &
  1926. IEEE80211_HT_CAP_SM_PS) == smps)
  1927. goto handled;
  1928. rx->sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SM_PS;
  1929. rx->sta->sta.ht_cap.cap |= smps;
  1930. sband = rx->local->hw.wiphy->bands[status->band];
  1931. rate_control_rate_update(local, sband, rx->sta,
  1932. IEEE80211_RC_SMPS_CHANGED);
  1933. goto handled;
  1934. }
  1935. default:
  1936. goto invalid;
  1937. }
  1938. break;
  1939. case WLAN_CATEGORY_BACK:
  1940. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  1941. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  1942. sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1943. sdata->vif.type != NL80211_IFTYPE_AP &&
  1944. sdata->vif.type != NL80211_IFTYPE_ADHOC)
  1945. break;
  1946. /* verify action_code is present */
  1947. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1948. break;
  1949. switch (mgmt->u.action.u.addba_req.action_code) {
  1950. case WLAN_ACTION_ADDBA_REQ:
  1951. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1952. sizeof(mgmt->u.action.u.addba_req)))
  1953. goto invalid;
  1954. break;
  1955. case WLAN_ACTION_ADDBA_RESP:
  1956. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1957. sizeof(mgmt->u.action.u.addba_resp)))
  1958. goto invalid;
  1959. break;
  1960. case WLAN_ACTION_DELBA:
  1961. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1962. sizeof(mgmt->u.action.u.delba)))
  1963. goto invalid;
  1964. break;
  1965. default:
  1966. goto invalid;
  1967. }
  1968. goto queue;
  1969. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1970. if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
  1971. break;
  1972. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1973. break;
  1974. /* verify action_code is present */
  1975. if (len < IEEE80211_MIN_ACTION_SIZE + 1)
  1976. break;
  1977. switch (mgmt->u.action.u.measurement.action_code) {
  1978. case WLAN_ACTION_SPCT_MSR_REQ:
  1979. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1980. sizeof(mgmt->u.action.u.measurement)))
  1981. break;
  1982. ieee80211_process_measurement_req(sdata, mgmt, len);
  1983. goto handled;
  1984. case WLAN_ACTION_SPCT_CHL_SWITCH:
  1985. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1986. sizeof(mgmt->u.action.u.chan_switch)))
  1987. break;
  1988. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1989. break;
  1990. if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
  1991. break;
  1992. goto queue;
  1993. }
  1994. break;
  1995. case WLAN_CATEGORY_SA_QUERY:
  1996. if (len < (IEEE80211_MIN_ACTION_SIZE +
  1997. sizeof(mgmt->u.action.u.sa_query)))
  1998. break;
  1999. switch (mgmt->u.action.u.sa_query.action) {
  2000. case WLAN_ACTION_SA_QUERY_REQUEST:
  2001. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2002. break;
  2003. ieee80211_process_sa_query_req(sdata, mgmt, len);
  2004. goto handled;
  2005. }
  2006. break;
  2007. case WLAN_CATEGORY_SELF_PROTECTED:
  2008. switch (mgmt->u.action.u.self_prot.action_code) {
  2009. case WLAN_SP_MESH_PEERING_OPEN:
  2010. case WLAN_SP_MESH_PEERING_CLOSE:
  2011. case WLAN_SP_MESH_PEERING_CONFIRM:
  2012. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2013. goto invalid;
  2014. if (sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
  2015. /* userspace handles this frame */
  2016. break;
  2017. goto queue;
  2018. case WLAN_SP_MGK_INFORM:
  2019. case WLAN_SP_MGK_ACK:
  2020. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2021. goto invalid;
  2022. break;
  2023. }
  2024. break;
  2025. case WLAN_CATEGORY_MESH_ACTION:
  2026. if (!ieee80211_vif_is_mesh(&sdata->vif))
  2027. break;
  2028. if (mesh_action_is_path_sel(mgmt) &&
  2029. (!mesh_path_sel_is_hwmp(sdata)))
  2030. break;
  2031. goto queue;
  2032. }
  2033. return RX_CONTINUE;
  2034. invalid:
  2035. status->rx_flags |= IEEE80211_RX_MALFORMED_ACTION_FRM;
  2036. /* will return in the next handlers */
  2037. return RX_CONTINUE;
  2038. handled:
  2039. if (rx->sta)
  2040. rx->sta->rx_packets++;
  2041. dev_kfree_skb(rx->skb);
  2042. return RX_QUEUED;
  2043. queue:
  2044. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2045. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2046. ieee80211_queue_work(&local->hw, &sdata->work);
  2047. if (rx->sta)
  2048. rx->sta->rx_packets++;
  2049. return RX_QUEUED;
  2050. }
  2051. static ieee80211_rx_result debug_noinline
  2052. ieee80211_rx_h_userspace_mgmt(struct ieee80211_rx_data *rx)
  2053. {
  2054. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2055. int sig = 0;
  2056. /* skip known-bad action frames and return them in the next handler */
  2057. if (status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM)
  2058. return RX_CONTINUE;
  2059. /*
  2060. * Getting here means the kernel doesn't know how to handle
  2061. * it, but maybe userspace does ... include returned frames
  2062. * so userspace can register for those to know whether ones
  2063. * it transmitted were processed or returned.
  2064. */
  2065. if (rx->local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  2066. sig = status->signal;
  2067. if (cfg80211_rx_mgmt(rx->sdata->dev, status->freq, sig,
  2068. rx->skb->data, rx->skb->len,
  2069. GFP_ATOMIC)) {
  2070. if (rx->sta)
  2071. rx->sta->rx_packets++;
  2072. dev_kfree_skb(rx->skb);
  2073. return RX_QUEUED;
  2074. }
  2075. return RX_CONTINUE;
  2076. }
  2077. static ieee80211_rx_result debug_noinline
  2078. ieee80211_rx_h_action_return(struct ieee80211_rx_data *rx)
  2079. {
  2080. struct ieee80211_local *local = rx->local;
  2081. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) rx->skb->data;
  2082. struct sk_buff *nskb;
  2083. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2084. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  2085. if (!ieee80211_is_action(mgmt->frame_control))
  2086. return RX_CONTINUE;
  2087. /*
  2088. * For AP mode, hostapd is responsible for handling any action
  2089. * frames that we didn't handle, including returning unknown
  2090. * ones. For all other modes we will return them to the sender,
  2091. * setting the 0x80 bit in the action category, as required by
  2092. * 802.11-2007 7.3.1.11.
  2093. * Newer versions of hostapd shall also use the management frame
  2094. * registration mechanisms, but older ones still use cooked
  2095. * monitor interfaces so push all frames there.
  2096. */
  2097. if (!(status->rx_flags & IEEE80211_RX_MALFORMED_ACTION_FRM) &&
  2098. (sdata->vif.type == NL80211_IFTYPE_AP ||
  2099. sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
  2100. return RX_DROP_MONITOR;
  2101. /* do not return rejected action frames */
  2102. if (mgmt->u.action.category & 0x80)
  2103. return RX_DROP_UNUSABLE;
  2104. nskb = skb_copy_expand(rx->skb, local->hw.extra_tx_headroom, 0,
  2105. GFP_ATOMIC);
  2106. if (nskb) {
  2107. struct ieee80211_mgmt *nmgmt = (void *)nskb->data;
  2108. nmgmt->u.action.category |= 0x80;
  2109. memcpy(nmgmt->da, nmgmt->sa, ETH_ALEN);
  2110. memcpy(nmgmt->sa, rx->sdata->vif.addr, ETH_ALEN);
  2111. memset(nskb->cb, 0, sizeof(nskb->cb));
  2112. ieee80211_tx_skb(rx->sdata, nskb);
  2113. }
  2114. dev_kfree_skb(rx->skb);
  2115. return RX_QUEUED;
  2116. }
  2117. static ieee80211_rx_result debug_noinline
  2118. ieee80211_rx_h_mgmt(struct ieee80211_rx_data *rx)
  2119. {
  2120. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2121. struct ieee80211_mgmt *mgmt = (void *)rx->skb->data;
  2122. __le16 stype;
  2123. stype = mgmt->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
  2124. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  2125. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2126. sdata->vif.type != NL80211_IFTYPE_STATION)
  2127. return RX_DROP_MONITOR;
  2128. switch (stype) {
  2129. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  2130. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  2131. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  2132. /* process for all: mesh, mlme, ibss */
  2133. break;
  2134. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  2135. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  2136. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  2137. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  2138. if (is_multicast_ether_addr(mgmt->da) &&
  2139. !is_broadcast_ether_addr(mgmt->da))
  2140. return RX_DROP_MONITOR;
  2141. /* process only for station */
  2142. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  2143. return RX_DROP_MONITOR;
  2144. break;
  2145. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  2146. /* process only for ibss */
  2147. if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
  2148. return RX_DROP_MONITOR;
  2149. break;
  2150. default:
  2151. return RX_DROP_MONITOR;
  2152. }
  2153. /* queue up frame and kick off work to process it */
  2154. rx->skb->pkt_type = IEEE80211_SDATA_QUEUE_TYPE_FRAME;
  2155. skb_queue_tail(&sdata->skb_queue, rx->skb);
  2156. ieee80211_queue_work(&rx->local->hw, &sdata->work);
  2157. if (rx->sta)
  2158. rx->sta->rx_packets++;
  2159. return RX_QUEUED;
  2160. }
  2161. /* TODO: use IEEE80211_RX_FRAGMENTED */
  2162. static void ieee80211_rx_cooked_monitor(struct ieee80211_rx_data *rx,
  2163. struct ieee80211_rate *rate)
  2164. {
  2165. struct ieee80211_sub_if_data *sdata;
  2166. struct ieee80211_local *local = rx->local;
  2167. struct sk_buff *skb = rx->skb, *skb2;
  2168. struct net_device *prev_dev = NULL;
  2169. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2170. int needed_headroom;
  2171. /*
  2172. * If cooked monitor has been processed already, then
  2173. * don't do it again. If not, set the flag.
  2174. */
  2175. if (rx->flags & IEEE80211_RX_CMNTR)
  2176. goto out_free_skb;
  2177. rx->flags |= IEEE80211_RX_CMNTR;
  2178. /* If there are no cooked monitor interfaces, just free the SKB */
  2179. if (!local->cooked_mntrs)
  2180. goto out_free_skb;
  2181. /* room for the radiotap header based on driver features */
  2182. needed_headroom = ieee80211_rx_radiotap_len(local, status);
  2183. if (skb_headroom(skb) < needed_headroom &&
  2184. pskb_expand_head(skb, needed_headroom, 0, GFP_ATOMIC))
  2185. goto out_free_skb;
  2186. /* prepend radiotap information */
  2187. ieee80211_add_rx_radiotap_header(local, skb, rate, needed_headroom,
  2188. false);
  2189. skb_set_mac_header(skb, 0);
  2190. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2191. skb->pkt_type = PACKET_OTHERHOST;
  2192. skb->protocol = htons(ETH_P_802_2);
  2193. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2194. if (!ieee80211_sdata_running(sdata))
  2195. continue;
  2196. if (sdata->vif.type != NL80211_IFTYPE_MONITOR ||
  2197. !(sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
  2198. continue;
  2199. if (prev_dev) {
  2200. skb2 = skb_clone(skb, GFP_ATOMIC);
  2201. if (skb2) {
  2202. skb2->dev = prev_dev;
  2203. netif_receive_skb(skb2);
  2204. }
  2205. }
  2206. prev_dev = sdata->dev;
  2207. sdata->dev->stats.rx_packets++;
  2208. sdata->dev->stats.rx_bytes += skb->len;
  2209. }
  2210. if (prev_dev) {
  2211. skb->dev = prev_dev;
  2212. netif_receive_skb(skb);
  2213. return;
  2214. }
  2215. out_free_skb:
  2216. dev_kfree_skb(skb);
  2217. }
  2218. static void ieee80211_rx_handlers_result(struct ieee80211_rx_data *rx,
  2219. ieee80211_rx_result res)
  2220. {
  2221. switch (res) {
  2222. case RX_DROP_MONITOR:
  2223. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2224. if (rx->sta)
  2225. rx->sta->rx_dropped++;
  2226. /* fall through */
  2227. case RX_CONTINUE: {
  2228. struct ieee80211_rate *rate = NULL;
  2229. struct ieee80211_supported_band *sband;
  2230. struct ieee80211_rx_status *status;
  2231. status = IEEE80211_SKB_RXCB((rx->skb));
  2232. sband = rx->local->hw.wiphy->bands[status->band];
  2233. if (!(status->flag & RX_FLAG_HT))
  2234. rate = &sband->bitrates[status->rate_idx];
  2235. ieee80211_rx_cooked_monitor(rx, rate);
  2236. break;
  2237. }
  2238. case RX_DROP_UNUSABLE:
  2239. I802_DEBUG_INC(rx->sdata->local->rx_handlers_drop);
  2240. if (rx->sta)
  2241. rx->sta->rx_dropped++;
  2242. dev_kfree_skb(rx->skb);
  2243. break;
  2244. case RX_QUEUED:
  2245. I802_DEBUG_INC(rx->sdata->local->rx_handlers_queued);
  2246. break;
  2247. }
  2248. }
  2249. static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx)
  2250. {
  2251. ieee80211_rx_result res = RX_DROP_MONITOR;
  2252. struct sk_buff *skb;
  2253. #define CALL_RXH(rxh) \
  2254. do { \
  2255. res = rxh(rx); \
  2256. if (res != RX_CONTINUE) \
  2257. goto rxh_next; \
  2258. } while (0);
  2259. spin_lock(&rx->local->rx_skb_queue.lock);
  2260. if (rx->local->running_rx_handler)
  2261. goto unlock;
  2262. rx->local->running_rx_handler = true;
  2263. while ((skb = __skb_dequeue(&rx->local->rx_skb_queue))) {
  2264. spin_unlock(&rx->local->rx_skb_queue.lock);
  2265. /*
  2266. * all the other fields are valid across frames
  2267. * that belong to an aMPDU since they are on the
  2268. * same TID from the same station
  2269. */
  2270. rx->skb = skb;
  2271. CALL_RXH(ieee80211_rx_h_decrypt)
  2272. CALL_RXH(ieee80211_rx_h_check_more_data)
  2273. CALL_RXH(ieee80211_rx_h_uapsd_and_pspoll)
  2274. CALL_RXH(ieee80211_rx_h_sta_process)
  2275. CALL_RXH(ieee80211_rx_h_defragment)
  2276. CALL_RXH(ieee80211_rx_h_michael_mic_verify)
  2277. /* must be after MMIC verify so header is counted in MPDU mic */
  2278. #ifdef CONFIG_MAC80211_MESH
  2279. if (ieee80211_vif_is_mesh(&rx->sdata->vif))
  2280. CALL_RXH(ieee80211_rx_h_mesh_fwding);
  2281. #endif
  2282. CALL_RXH(ieee80211_rx_h_amsdu)
  2283. CALL_RXH(ieee80211_rx_h_data)
  2284. CALL_RXH(ieee80211_rx_h_ctrl);
  2285. CALL_RXH(ieee80211_rx_h_mgmt_check)
  2286. CALL_RXH(ieee80211_rx_h_action)
  2287. CALL_RXH(ieee80211_rx_h_userspace_mgmt)
  2288. CALL_RXH(ieee80211_rx_h_action_return)
  2289. CALL_RXH(ieee80211_rx_h_mgmt)
  2290. rxh_next:
  2291. ieee80211_rx_handlers_result(rx, res);
  2292. spin_lock(&rx->local->rx_skb_queue.lock);
  2293. #undef CALL_RXH
  2294. }
  2295. rx->local->running_rx_handler = false;
  2296. unlock:
  2297. spin_unlock(&rx->local->rx_skb_queue.lock);
  2298. }
  2299. static void ieee80211_invoke_rx_handlers(struct ieee80211_rx_data *rx)
  2300. {
  2301. ieee80211_rx_result res = RX_DROP_MONITOR;
  2302. #define CALL_RXH(rxh) \
  2303. do { \
  2304. res = rxh(rx); \
  2305. if (res != RX_CONTINUE) \
  2306. goto rxh_next; \
  2307. } while (0);
  2308. CALL_RXH(ieee80211_rx_h_passive_scan)
  2309. CALL_RXH(ieee80211_rx_h_check)
  2310. ieee80211_rx_reorder_ampdu(rx);
  2311. ieee80211_rx_handlers(rx);
  2312. return;
  2313. rxh_next:
  2314. ieee80211_rx_handlers_result(rx, res);
  2315. #undef CALL_RXH
  2316. }
  2317. /*
  2318. * This function makes calls into the RX path, therefore
  2319. * it has to be invoked under RCU read lock.
  2320. */
  2321. void ieee80211_release_reorder_timeout(struct sta_info *sta, int tid)
  2322. {
  2323. struct ieee80211_rx_data rx = {
  2324. .sta = sta,
  2325. .sdata = sta->sdata,
  2326. .local = sta->local,
  2327. /* This is OK -- must be QoS data frame */
  2328. .security_idx = tid,
  2329. .seqno_idx = tid,
  2330. .flags = 0,
  2331. };
  2332. struct tid_ampdu_rx *tid_agg_rx;
  2333. tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
  2334. if (!tid_agg_rx)
  2335. return;
  2336. spin_lock(&tid_agg_rx->reorder_lock);
  2337. ieee80211_sta_reorder_release(&sta->local->hw, tid_agg_rx);
  2338. spin_unlock(&tid_agg_rx->reorder_lock);
  2339. ieee80211_rx_handlers(&rx);
  2340. }
  2341. /* main receive path */
  2342. static int prepare_for_handlers(struct ieee80211_rx_data *rx,
  2343. struct ieee80211_hdr *hdr)
  2344. {
  2345. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2346. struct sk_buff *skb = rx->skb;
  2347. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2348. u8 *bssid = ieee80211_get_bssid(hdr, skb->len, sdata->vif.type);
  2349. int multicast = is_multicast_ether_addr(hdr->addr1);
  2350. switch (sdata->vif.type) {
  2351. case NL80211_IFTYPE_STATION:
  2352. if (!bssid && !sdata->u.mgd.use_4addr)
  2353. return 0;
  2354. if (!multicast &&
  2355. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2356. if (!(sdata->dev->flags & IFF_PROMISC) ||
  2357. sdata->u.mgd.use_4addr)
  2358. return 0;
  2359. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2360. }
  2361. break;
  2362. case NL80211_IFTYPE_ADHOC:
  2363. if (!bssid)
  2364. return 0;
  2365. if (ieee80211_is_beacon(hdr->frame_control)) {
  2366. return 1;
  2367. }
  2368. else if (!ieee80211_bssid_match(bssid, sdata->u.ibss.bssid)) {
  2369. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN))
  2370. return 0;
  2371. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2372. } else if (!multicast &&
  2373. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2374. if (!(sdata->dev->flags & IFF_PROMISC))
  2375. return 0;
  2376. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2377. } else if (!rx->sta) {
  2378. int rate_idx;
  2379. if (status->flag & RX_FLAG_HT)
  2380. rate_idx = 0; /* TODO: HT rates */
  2381. else
  2382. rate_idx = status->rate_idx;
  2383. ieee80211_ibss_rx_no_sta(sdata, bssid, hdr->addr2,
  2384. BIT(rate_idx));
  2385. }
  2386. break;
  2387. case NL80211_IFTYPE_MESH_POINT:
  2388. if (!multicast &&
  2389. !ether_addr_equal(sdata->vif.addr, hdr->addr1)) {
  2390. if (!(sdata->dev->flags & IFF_PROMISC))
  2391. return 0;
  2392. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2393. }
  2394. break;
  2395. case NL80211_IFTYPE_AP_VLAN:
  2396. case NL80211_IFTYPE_AP:
  2397. if (!bssid) {
  2398. if (!ether_addr_equal(sdata->vif.addr, hdr->addr1))
  2399. return 0;
  2400. } else if (!ieee80211_bssid_match(bssid,
  2401. sdata->vif.addr)) {
  2402. /*
  2403. * Accept public action frames even when the
  2404. * BSSID doesn't match, this is used for P2P
  2405. * and location updates. Note that mac80211
  2406. * itself never looks at these frames.
  2407. */
  2408. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2409. ieee80211_is_public_action(hdr, skb->len))
  2410. return 1;
  2411. if (!(status->rx_flags & IEEE80211_RX_IN_SCAN) &&
  2412. !ieee80211_is_beacon(hdr->frame_control))
  2413. return 0;
  2414. status->rx_flags &= ~IEEE80211_RX_RA_MATCH;
  2415. }
  2416. break;
  2417. case NL80211_IFTYPE_WDS:
  2418. if (bssid || !ieee80211_is_data(hdr->frame_control))
  2419. return 0;
  2420. if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
  2421. return 0;
  2422. break;
  2423. default:
  2424. /* should never get here */
  2425. WARN_ON(1);
  2426. break;
  2427. }
  2428. return 1;
  2429. }
  2430. /*
  2431. * This function returns whether or not the SKB
  2432. * was destined for RX processing or not, which,
  2433. * if consume is true, is equivalent to whether
  2434. * or not the skb was consumed.
  2435. */
  2436. static bool ieee80211_prepare_and_rx_handle(struct ieee80211_rx_data *rx,
  2437. struct sk_buff *skb, bool consume)
  2438. {
  2439. struct ieee80211_local *local = rx->local;
  2440. struct ieee80211_sub_if_data *sdata = rx->sdata;
  2441. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2442. struct ieee80211_hdr *hdr = (void *)skb->data;
  2443. int prepares;
  2444. rx->skb = skb;
  2445. status->rx_flags |= IEEE80211_RX_RA_MATCH;
  2446. prepares = prepare_for_handlers(rx, hdr);
  2447. if (!prepares)
  2448. return false;
  2449. if (!consume) {
  2450. skb = skb_copy(skb, GFP_ATOMIC);
  2451. if (!skb) {
  2452. if (net_ratelimit())
  2453. wiphy_debug(local->hw.wiphy,
  2454. "failed to copy skb for %s\n",
  2455. sdata->name);
  2456. return true;
  2457. }
  2458. rx->skb = skb;
  2459. }
  2460. ieee80211_invoke_rx_handlers(rx);
  2461. return true;
  2462. }
  2463. /*
  2464. * This is the actual Rx frames handler. as it blongs to Rx path it must
  2465. * be called with rcu_read_lock protection.
  2466. */
  2467. static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
  2468. struct sk_buff *skb)
  2469. {
  2470. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2471. struct ieee80211_local *local = hw_to_local(hw);
  2472. struct ieee80211_sub_if_data *sdata;
  2473. struct ieee80211_hdr *hdr;
  2474. __le16 fc;
  2475. struct ieee80211_rx_data rx;
  2476. struct ieee80211_sub_if_data *prev;
  2477. struct sta_info *sta, *tmp, *prev_sta;
  2478. int err = 0;
  2479. fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
  2480. memset(&rx, 0, sizeof(rx));
  2481. rx.skb = skb;
  2482. rx.local = local;
  2483. if (ieee80211_is_data(fc) || ieee80211_is_mgmt(fc))
  2484. local->dot11ReceivedFragmentCount++;
  2485. if (unlikely(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  2486. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning) ||
  2487. test_bit(SCAN_SW_SCANNING, &local->scanning)))
  2488. status->rx_flags |= IEEE80211_RX_IN_SCAN;
  2489. if (ieee80211_is_mgmt(fc))
  2490. err = skb_linearize(skb);
  2491. else
  2492. err = !pskb_may_pull(skb, ieee80211_hdrlen(fc));
  2493. if (err) {
  2494. dev_kfree_skb(skb);
  2495. return;
  2496. }
  2497. hdr = (struct ieee80211_hdr *)skb->data;
  2498. ieee80211_parse_qos(&rx);
  2499. ieee80211_verify_alignment(&rx);
  2500. if (ieee80211_is_data(fc)) {
  2501. prev_sta = NULL;
  2502. for_each_sta_info(local, hdr->addr2, sta, tmp) {
  2503. if (!prev_sta) {
  2504. prev_sta = sta;
  2505. continue;
  2506. }
  2507. rx.sta = prev_sta;
  2508. rx.sdata = prev_sta->sdata;
  2509. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2510. prev_sta = sta;
  2511. }
  2512. if (prev_sta) {
  2513. rx.sta = prev_sta;
  2514. rx.sdata = prev_sta->sdata;
  2515. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2516. return;
  2517. goto out;
  2518. }
  2519. }
  2520. prev = NULL;
  2521. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2522. if (!ieee80211_sdata_running(sdata))
  2523. continue;
  2524. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  2525. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  2526. continue;
  2527. /*
  2528. * frame is destined for this interface, but if it's
  2529. * not also for the previous one we handle that after
  2530. * the loop to avoid copying the SKB once too much
  2531. */
  2532. if (!prev) {
  2533. prev = sdata;
  2534. continue;
  2535. }
  2536. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2537. rx.sdata = prev;
  2538. ieee80211_prepare_and_rx_handle(&rx, skb, false);
  2539. prev = sdata;
  2540. }
  2541. if (prev) {
  2542. rx.sta = sta_info_get_bss(prev, hdr->addr2);
  2543. rx.sdata = prev;
  2544. if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
  2545. return;
  2546. }
  2547. out:
  2548. dev_kfree_skb(skb);
  2549. }
  2550. /*
  2551. * This is the receive path handler. It is called by a low level driver when an
  2552. * 802.11 MPDU is received from the hardware.
  2553. */
  2554. void ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2555. {
  2556. struct ieee80211_local *local = hw_to_local(hw);
  2557. struct ieee80211_rate *rate = NULL;
  2558. struct ieee80211_supported_band *sband;
  2559. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  2560. WARN_ON_ONCE(softirq_count() == 0);
  2561. if (WARN_ON(status->band < 0 ||
  2562. status->band >= IEEE80211_NUM_BANDS))
  2563. goto drop;
  2564. sband = local->hw.wiphy->bands[status->band];
  2565. if (WARN_ON(!sband))
  2566. goto drop;
  2567. /*
  2568. * If we're suspending, it is possible although not too likely
  2569. * that we'd be receiving frames after having already partially
  2570. * quiesced the stack. We can't process such frames then since
  2571. * that might, for example, cause stations to be added or other
  2572. * driver callbacks be invoked.
  2573. */
  2574. if (unlikely(local->quiescing || local->suspended))
  2575. goto drop;
  2576. /*
  2577. * The same happens when we're not even started,
  2578. * but that's worth a warning.
  2579. */
  2580. if (WARN_ON(!local->started))
  2581. goto drop;
  2582. if (likely(!(status->flag & RX_FLAG_FAILED_PLCP_CRC))) {
  2583. /*
  2584. * Validate the rate, unless a PLCP error means that
  2585. * we probably can't have a valid rate here anyway.
  2586. */
  2587. if (status->flag & RX_FLAG_HT) {
  2588. /*
  2589. * rate_idx is MCS index, which can be [0-76]
  2590. * as documented on:
  2591. *
  2592. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  2593. *
  2594. * Anything else would be some sort of driver or
  2595. * hardware error. The driver should catch hardware
  2596. * errors.
  2597. */
  2598. if (WARN((status->rate_idx < 0 ||
  2599. status->rate_idx > 76),
  2600. "Rate marked as an HT rate but passed "
  2601. "status->rate_idx is not "
  2602. "an MCS index [0-76]: %d (0x%02x)\n",
  2603. status->rate_idx,
  2604. status->rate_idx))
  2605. goto drop;
  2606. } else {
  2607. if (WARN_ON(status->rate_idx < 0 ||
  2608. status->rate_idx >= sband->n_bitrates))
  2609. goto drop;
  2610. rate = &sband->bitrates[status->rate_idx];
  2611. }
  2612. }
  2613. status->rx_flags = 0;
  2614. /*
  2615. * key references and virtual interfaces are protected using RCU
  2616. * and this requires that we are in a read-side RCU section during
  2617. * receive processing
  2618. */
  2619. rcu_read_lock();
  2620. /*
  2621. * Frames with failed FCS/PLCP checksum are not returned,
  2622. * all other frames are returned without radiotap header
  2623. * if it was previously present.
  2624. * Also, frames with less than 16 bytes are dropped.
  2625. */
  2626. skb = ieee80211_rx_monitor(local, skb, rate);
  2627. if (!skb) {
  2628. rcu_read_unlock();
  2629. return;
  2630. }
  2631. ieee80211_tpt_led_trig_rx(local,
  2632. ((struct ieee80211_hdr *)skb->data)->frame_control,
  2633. skb->len);
  2634. __ieee80211_rx_handle_packet(hw, skb);
  2635. rcu_read_unlock();
  2636. return;
  2637. drop:
  2638. kfree_skb(skb);
  2639. }
  2640. EXPORT_SYMBOL(ieee80211_rx);
  2641. /* This is a version of the rx handler that can be called from hard irq
  2642. * context. Post the skb on the queue and schedule the tasklet */
  2643. void ieee80211_rx_irqsafe(struct ieee80211_hw *hw, struct sk_buff *skb)
  2644. {
  2645. struct ieee80211_local *local = hw_to_local(hw);
  2646. BUILD_BUG_ON(sizeof(struct ieee80211_rx_status) > sizeof(skb->cb));
  2647. skb->pkt_type = IEEE80211_RX_MSG;
  2648. skb_queue_tail(&local->skb_queue, skb);
  2649. tasklet_schedule(&local->tasklet);
  2650. }
  2651. EXPORT_SYMBOL(ieee80211_rx_irqsafe);