rx.c 81 KB

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