rx.c 85 KB

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