rx.c 50 KB

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