rx.c 57 KB

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