rx.c 70 KB

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