rx.c 84 KB

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