rx.c 70 KB

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