rx.c 86 KB

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