rx.c 71 KB

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