rx.c 44 KB

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