rx.c 71 KB

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