rx.c 81 KB

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