rx.c 81 KB

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