rx.c 64 KB

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