rx.c 49 KB

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