rx.c 86 KB

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