rx.c 81 KB

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