rx.c 59 KB

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