rx.c 71 KB

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