rx.c 84 KB

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