rx.c 72 KB

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