Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * Original code based Host AP (software wireless LAN access point) driver
  3  * for Intersil Prism2/2.5/3 - hostap.o module, common routines
  4  *
  5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6  * <jkmaline@cc.hut.fi>
  7  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
  8  * Copyright (c) 2004, Intel Corporation
  9  *
 10  * This program is free software; you can redistribute it and/or modify
 11  * it under the terms of the GNU General Public License version 2 as
 12  * published by the Free Software Foundation. See README and COPYING for
 13  * more details.
 14  ******************************************************************************
 15 
 16   Few modifications for Realtek's Wi-Fi drivers by
 17   Andrea Merello <andreamrl@tiscali.it>
 18 
 19   A special thanks goes to Realtek for their support !
 20 
 21 ******************************************************************************/
 22 
 23 
 24 #include <linux/compiler.h>
 25 //#include <linux/config.h>
 26 #include <linux/errno.h>
 27 #include <linux/if_arp.h>
 28 #include <linux/in6.h>
 29 #include <linux/in.h>
 30 #include <linux/ip.h>
 31 #include <linux/kernel.h>
 32 #include <linux/module.h>
 33 #include <linux/netdevice.h>
 34 #include <linux/pci.h>
 35 #include <linux/proc_fs.h>
 36 #include <linux/skbuff.h>
 37 #include <linux/slab.h>
 38 #include <linux/tcp.h>
 39 #include <linux/types.h>
 40 #include <linux/version.h>
 41 #include <linux/wireless.h>
 42 #include <linux/etherdevice.h>
 43 #include <asm/uaccess.h>
 44 #include <linux/ctype.h>
 45 
 46 #include "ieee80211.h"
 47 #ifdef ENABLE_DOT11D
 48 #include "dot11d.h"
 49 #endif
 50 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
 51                                         struct sk_buff *skb,
 52                                         struct ieee80211_rx_stats *rx_stats)
 53 {
 54         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 55         u16 fc = le16_to_cpu(hdr->frame_ctl);
 56 
 57         skb->dev = ieee->dev;
 58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
 59         skb_reset_mac_header(skb);
 60 #else
 61         skb->mac.raw = skb->data;
 62 #endif
 63         skb_pull(skb, ieee80211_get_hdrlen(fc));
 64         skb->pkt_type = PACKET_OTHERHOST;
 65         skb->protocol = __constant_htons(ETH_P_80211_RAW);
 66         memset(skb->cb, 0, sizeof(skb->cb));
 67         netif_rx(skb);
 68 }
 69 
 70 
 71 /* Called only as a tasklet (software IRQ) */
 72 static struct ieee80211_frag_entry *
 73 ieee80211_frag_cache_find(struct ieee80211_device *ieee, unsigned int seq,
 74                           unsigned int frag, u8 tid,u8 *src, u8 *dst)
 75 {
 76         struct ieee80211_frag_entry *entry;
 77         int i;
 78 
 79         for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
 80                 entry = &ieee->frag_cache[tid][i];
 81                 if (entry->skb != NULL &&
 82                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
 83                         IEEE80211_DEBUG_FRAG(
 84                                 "expiring fragment cache entry "
 85                                 "seq=%u last_frag=%u\n",
 86                                 entry->seq, entry->last_frag);
 87                         dev_kfree_skb_any(entry->skb);
 88                         entry->skb = NULL;
 89                 }
 90 
 91                 if (entry->skb != NULL && entry->seq == seq &&
 92                     (entry->last_frag + 1 == frag || frag == -1) &&
 93                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
 94                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
 95                         return entry;
 96         }
 97 
 98         return NULL;
 99 }
100 
101 /* Called only as a tasklet (software IRQ) */
102 static struct sk_buff *
103 ieee80211_frag_cache_get(struct ieee80211_device *ieee,
104                          struct ieee80211_hdr *hdr)
105 {
106         struct sk_buff *skb = NULL;
107         u16 fc = le16_to_cpu(hdr->frame_ctl);
108         u16 sc = le16_to_cpu(hdr->seq_ctl);
109         unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
110         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
111         struct ieee80211_frag_entry *entry;
112         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
113         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
114         u8 tid;
115 
116 #ifdef _RTL8187_EXT_PATCH_
117         if(ieee->iw_mode == ieee->iw_ext_mode)
118         {
119                 tid = (hdr->addr2[ETH_ALEN-2] ^ hdr->addr2[ETH_ALEN-1]) & IEEE80211_QOS_TID;
120         }
121         else
122 #endif
123         if (((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
124           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)hdr;
125           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
126           tid = UP2AC(tid);
127           tid ++;
128         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
129           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS *)hdr;
130           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
131           tid = UP2AC(tid);
132           tid ++;
133         } else {
134           tid = 0;
135         }
136 
137         if (frag == 0) {
138                 /* Reserve enough space to fit maximum frame length */
139                 skb = dev_alloc_skb(ieee->dev->mtu +
140                                     sizeof(struct ieee80211_hdr) +
141                                     8 /* LLC */ +
142                                     2 /* alignment */ +
143                                     8 /* WEP */ +
144                                     ETH_ALEN /* WDS */ +
145                                     (IEEE80211_QOS_HAS_SEQ(fc)?2:0) /* QOS Control */);
146                 if (skb == NULL)
147                         return NULL;
148 
149                 entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
150                 ieee->frag_next_idx[tid]++;
151                 if (ieee->frag_next_idx[tid] >= IEEE80211_FRAG_CACHE_LEN)
152                         ieee->frag_next_idx[tid] = 0;
153 
154                 if (entry->skb != NULL)
155                         dev_kfree_skb_any(entry->skb);
156 
157                 entry->first_frag_time = jiffies;
158                 entry->seq = seq;
159                 entry->last_frag = frag;
160                 entry->skb = skb;
161                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
162                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
163         } else {
164                 /* received a fragment of a frame for which the head fragment
165                  * should have already been received */
166                 entry = ieee80211_frag_cache_find(ieee, seq, frag, tid,hdr->addr2,
167                                                   hdr->addr1);
168                 if (entry != NULL) {
169                         entry->last_frag = frag;
170                         skb = entry->skb;
171                 }
172         }
173 
174         return skb;
175 }
176 
177 
178 /* Called only as a tasklet (software IRQ) */
179 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
180                                            struct ieee80211_hdr *hdr)
181 {
182         u16 fc = le16_to_cpu(hdr->frame_ctl);
183         u16 sc = le16_to_cpu(hdr->seq_ctl);
184         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
185         struct ieee80211_frag_entry *entry;
186         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
187         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
188         u8 tid;
189 
190 #ifdef _RTL8187_EXT_PATCH_
191         if(ieee->iw_mode == ieee->iw_ext_mode)
192         {
193                 tid = (hdr->addr2[ETH_ALEN-2] ^ hdr->addr2[ETH_ALEN-1]) & IEEE80211_QOS_TID;
194         }
195         else
196 #endif
197         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
198           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)hdr;
199           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
200           tid = UP2AC(tid);
201           tid ++;
202         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
203           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS *)hdr;
204           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
205           tid = UP2AC(tid);
206           tid ++;
207         } else {
208           tid = 0;
209         }
210 
211         entry = ieee80211_frag_cache_find(ieee, seq, -1, tid,hdr->addr2,
212                                           hdr->addr1);
213 
214         if (entry == NULL) {
215                 IEEE80211_DEBUG_FRAG(
216                         "could not invalidate fragment cache "
217                         "entry (seq=%u)\n", seq);
218                 return -1;
219         }
220 
221         entry->skb = NULL;
222         return 0;
223 }
224 
225 
226 
227 /* ieee80211_rx_frame_mgtmt
228  *
229  * Responsible for handling management control frames
230  *
231  * Called by ieee80211_rx */
232 static inline int
233 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
234                         struct ieee80211_rx_stats *rx_stats, u16 type,
235                         u16 stype)
236 {
237         struct ieee80211_hdr *hdr;
238 
239         // cheat the the hdr type
240         hdr = (struct ieee80211_hdr *)skb->data;
241 
242         /* On the struct stats definition there is written that
243          * this is not mandatory.... but seems that the probe
244          * response parser uses it
245          */
246         rx_stats->len = skb->len;
247         ieee80211_rx_mgt(ieee,(struct ieee80211_hdr *)skb->data,rx_stats);
248 
249         if((ieee->state == IEEE80211_LINKED)&&(memcmp(hdr->addr3,ieee->current_network.bssid,ETH_ALEN))) {
250                 dev_kfree_skb_any(skb);
251                 return 0;
252         }
253 
254         ieee80211_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
255 
256         dev_kfree_skb_any(skb);
257 
258         return 0;
259 
260         #ifdef NOT_YET
261         if (ieee->iw_mode == IW_MODE_MASTER) {
262                 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
263                        ieee->dev->name);
264                 return 0;
265 /*
266   hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr *)
267   skb->data);*/
268         }
269 
270         if (ieee->hostapd && type == IEEE80211_TYPE_MGMT) {
271                 if (stype == WLAN_FC_STYPE_BEACON &&
272                     ieee->iw_mode == IW_MODE_MASTER) {
273                         struct sk_buff *skb2;
274                         /* Process beacon frames also in kernel driver to
275                          * update STA(AP) table statistics */
276                         skb2 = skb_clone(skb, GFP_ATOMIC);
277                         if (skb2)
278                                 hostap_rx(skb2->dev, skb2, rx_stats);
279                 }
280 
281                 /* send management frames to the user space daemon for
282                  * processing */
283                 ieee->apdevstats.rx_packets++;
284                 ieee->apdevstats.rx_bytes += skb->len;
285                 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
286                 return 0;
287         }
288 
289             if (ieee->iw_mode == IW_MODE_MASTER) {
290                 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
291                         printk(KERN_DEBUG "%s: unknown management frame "
292                                "(type=0x%02x, stype=0x%02x) dropped\n",
293                                skb->dev->name, type, stype);
294                         return -1;
295                 }
296 
297                 hostap_rx(skb->dev, skb, rx_stats);
298                 return 0;
299         }
300 
301         printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
302                "received in non-Host AP mode\n", skb->dev->name);
303         return -1;
304         #endif
305 }
306 
307 
308 
309 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
310 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
311 static unsigned char rfc1042_header[] =
312 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
313 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
314 static unsigned char bridge_tunnel_header[] =
315 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
316 /* No encapsulation header if EtherType < 0x600 (=length) */
317 
318 /* Called by ieee80211_rx_frame_decrypt */
319 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
320                                     struct sk_buff *skb, size_t hdrlen)
321 {
322         struct net_device *dev = ieee->dev;
323         u16 fc, ethertype;
324         struct ieee80211_hdr *hdr;
325         u8 *pos;
326 
327         if (skb->len < 24)
328                 return 0;
329 
330         hdr = (struct ieee80211_hdr *) skb->data;
331         fc = le16_to_cpu(hdr->frame_ctl);
332 
333         /* check that the frame is unicast frame to us */
334         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
335             IEEE80211_FCTL_TODS &&
336             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
337             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
338                 /* ToDS frame with own addr BSSID and DA */
339         } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
340                    IEEE80211_FCTL_FROMDS &&
341                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
342                 /* FromDS frame with own addr as DA */
343         } else
344                 return 0;
345 
346         if (skb->len < 24 + 8)
347                 return 0;
348 
349         /* check for port access entity Ethernet type */
350 //      pos = skb->data + 24;
351         pos = skb->data + hdrlen;
352         ethertype = (pos[6] << 8) | pos[7];
353         if (ethertype == ETH_P_PAE)
354                 return 1;
355 
356         return 0;
357 }
358 
359 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
360 static inline int
361 ieee80211_rx_frame_decrypt(struct ieee80211_device* ieee, struct sk_buff *skb,
362                            struct ieee80211_crypt_data *crypt)
363 {
364         struct ieee80211_hdr *hdr;
365         int res, hdrlen;
366 
367         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
368                 return 0;
369 
370         hdr = (struct ieee80211_hdr *) skb->data;
371 #ifdef _RTL8187_EXT_PATCH_
372         if((ieee->iw_mode == ieee->iw_ext_mode) && (ieee->ext_patch_ieee80211_rx_frame_get_hdrlen))
373         {
374                 hdrlen = ieee->ext_patch_ieee80211_rx_frame_get_hdrlen(ieee, skb);
375         }
376         else
377 #endif
378         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
379 
380 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
381         if (ieee->tkip_countermeasures &&
382             strcmp(crypt->ops->name, "TKIP") == 0) {
383                 if (net_ratelimit()) {
384                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
385                                "received packet from " MAC_FMT "\n",
386                                ieee->dev->name, MAC_ARG(hdr->addr2));
387                 }
388                 return -1;
389         }
390 #endif
391 
392         atomic_inc(&crypt->refcnt);
393         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
394         atomic_dec(&crypt->refcnt);
395         if (res < 0) {
396                 IEEE80211_DEBUG_DROP(
397                         "decryption failed (SA=" MAC_FMT
398                         ") res=%d\n", MAC_ARG(hdr->addr2), res);
399                 if (res == -2)
400                         IEEE80211_DEBUG_DROP("Decryption failed ICV "
401                                              "mismatch (key %d)\n",
402                                              skb->data[hdrlen + 3] >> 6);
403                 ieee->ieee_stats.rx_discards_undecryptable++;
404                 return -1;
405         }
406 
407         return res;
408 }
409 
410 
411 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
412 static inline int
413 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device* ieee, struct sk_buff *skb,
414                              int keyidx, struct ieee80211_crypt_data *crypt)
415 {
416         struct ieee80211_hdr *hdr;
417         int res, hdrlen;
418 
419         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
420                 return 0;
421 
422         hdr = (struct ieee80211_hdr *) skb->data;
423 #ifdef _RTL8187_EXT_PATCH_
424         if((ieee->iw_mode == ieee->iw_ext_mode) && (ieee->ext_patch_ieee80211_rx_frame_get_hdrlen))
425         {
426                 hdrlen = ieee->ext_patch_ieee80211_rx_frame_get_hdrlen(ieee, skb);
427         }
428         else
429 #endif
430         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
431 
432         atomic_inc(&crypt->refcnt);
433         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
434         atomic_dec(&crypt->refcnt);
435         if (res < 0) {
436                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
437                        " (SA=" MAC_FMT " keyidx=%d)\n",
438                        ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
439                 return -1;
440         }
441 
442         return 0;
443 }
444 
445 
446 /* this function is stolen from ipw2200 driver*/
447 #define IEEE_PACKET_RETRY_TIME (5*HZ)
448 static int is_duplicate_packet(struct ieee80211_device *ieee,
449                                       struct ieee80211_hdr *header)
450 {
451         u16 fc = le16_to_cpu(header->frame_ctl);
452         u16 sc = le16_to_cpu(header->seq_ctl);
453         u16 seq = WLAN_GET_SEQ_SEQ(sc);
454         u16 frag = WLAN_GET_SEQ_FRAG(sc);
455         u16 *last_seq, *last_frag;
456         unsigned long *last_time;
457         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
458         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
459         u8 tid;
460 
461 #ifdef _RTL8187_EXT_PATCH_
462         if(ieee->iw_mode == ieee->iw_ext_mode)
463         {
464                 tid = (header->addr2[ETH_ALEN-2] ^ header->addr2[ETH_ALEN-1]) & IEEE80211_QOS_TID;
465         }
466         else
467 #endif
468         //TO2DS and QoS
469         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
470           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)header;
471           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
472           tid = UP2AC(tid);
473           tid ++;
474         } else if(IEEE80211_QOS_HAS_SEQ(fc)) { //QoS
475           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS*)header;
476           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
477           tid = UP2AC(tid);
478           tid ++;
479         } else { // no QoS
480           tid = 0;
481         }
482         switch (ieee->iw_mode) {
483         case IW_MODE_ADHOC:
484         {
485                 struct list_head *p;
486                 struct ieee_ibss_seq *entry = NULL;
487                 u8 *mac = header->addr2;
488                 int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
489                 //for (pos = (head)->next; pos != (head); pos = pos->next)
490                 __list_for_each(p, &ieee->ibss_mac_hash[index]) {
491                         entry = list_entry(p, struct ieee_ibss_seq, list);
492                         if (!memcmp(entry->mac, mac, ETH_ALEN))
493                                 break;
494                 }
495         //      if (memcmp(entry->mac, mac, ETH_ALEN)){
496                 if (p == &ieee->ibss_mac_hash[index]) {
497                         entry = kmalloc(sizeof(struct ieee_ibss_seq), GFP_ATOMIC);
498                         if (!entry) {
499                                 printk(KERN_WARNING "Cannot malloc new mac entry\n");
500                                 return 0;
501                         }
502                         memcpy(entry->mac, mac, ETH_ALEN);
503                         entry->seq_num[tid] = seq;
504                         entry->frag_num[tid] = frag;
505                         entry->packet_time[tid] = jiffies;
506                         list_add(&entry->list, &ieee->ibss_mac_hash[index]);
507                         return 0;
508                 }
509                 last_seq = &entry->seq_num[tid];
510                 last_frag = &entry->frag_num[tid];
511                 last_time = &entry->packet_time[tid];
512                 break;
513         }
514 
515         case IW_MODE_INFRA:
516                 last_seq = &ieee->last_rxseq_num[tid];
517                 last_frag = &ieee->last_rxfrag_num[tid];
518                 last_time = &ieee->last_packet_time[tid];
519 
520                 break;
521         default:
522 #ifdef _RTL8187_EXT_PATCH_
523                 if(ieee->iw_mode == ieee->iw_ext_mode)
524                 {
525                         last_seq = &ieee->last_rxseq_num[tid];
526                         last_frag = &ieee->last_rxfrag_num[tid];
527                         last_time = &ieee->last_packet_time[tid];
528                         break;
529                 }
530                 else
531 #endif
532                 return 0;
533         }
534 
535 //      if(tid != 0) {
536 //              printk(KERN_WARNING ":)))))))))))%x %x %x, fc(%x)\n", tid, *last_seq, seq, header->frame_ctl);
537 //      }
538         if ((*last_seq == seq) &&
539             time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
540                 if (*last_frag == frag){
541                         //printk(KERN_WARNING "[1] go drop!\n");
542                         goto drop;
543 
544                 }
545                 if (*last_frag + 1 != frag)
546                         /* out-of-order fragment */
547                         //printk(KERN_WARNING "[2] go drop!\n");
548                         goto drop;
549         } else
550                 *last_seq = seq;
551 
552         *last_frag = frag;
553         *last_time = jiffies;
554         return 0;
555 
556 drop:
557 //      BUG_ON(!(fc & IEEE80211_FCTL_RETRY));
558 //      printk("DUP\n");
559 
560         return 1;
561 }
562 
563 
564 /* All received frames are sent to this function. @skb contains the frame in
565  * IEEE 802.11 format, i.e., in the format it was sent over air.
566  * This function is called only as a tasklet (software IRQ). */
567 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
568                  struct ieee80211_rx_stats *rx_stats)
569 {
570         struct net_device *dev = ieee->dev;
571         //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
572         struct ieee80211_hdr *hdr;
573         //struct ieee80211_hdr_3addr_QOS *hdr;
574 
575         size_t hdrlen;
576         u16 fc, type, stype, sc;
577         struct net_device_stats *stats;
578         unsigned int frag;
579         u8 *payload;
580         u16 ethertype;
581 #ifdef NOT_YET
582         struct net_device *wds = NULL;
583         struct sk_buff *skb2 = NULL;
584         struct net_device *wds = NULL;
585         int frame_authorized = 0;
586         int from_assoc_ap = 0;
587         void *sta = NULL;
588 #endif
589 //      u16 QOS_ctl = 0;
590         u8 dst[ETH_ALEN];
591         u8 src[ETH_ALEN];
592         u8 bssid[ETH_ALEN];
593         struct ieee80211_crypt_data *crypt = NULL;
594         int keyidx = 0;
595 
596         //Added for mesh by Lawrence.
597 #ifdef _RTL8187_EXT_PATCH_
598         u8 status;
599         u32 flags;
600 #endif
601         // cheat the the hdr type
602         hdr = (struct ieee80211_hdr *)skb->data;
603         stats = &ieee->stats;
604 
605         if (skb->len < 10) {
606                 printk(KERN_INFO "%s: SKB length < 10\n",
607                        dev->name);
608                 goto rx_dropped;
609         }
610 
611         fc = le16_to_cpu(hdr->frame_ctl);
612         type = WLAN_FC_GET_TYPE(fc);
613         stype = WLAN_FC_GET_STYPE(fc);
614         sc = le16_to_cpu(hdr->seq_ctl);
615 
616         frag = WLAN_GET_SEQ_FRAG(sc);
617 
618 //YJ,add,080828,for keep alive
619         if((fc & IEEE80211_FCTL_TODS) != IEEE80211_FCTL_TODS)
620         {
621                 if(!memcmp(hdr->addr1,dev->dev_addr, ETH_ALEN))
622                 {
623                         ieee->NumRxUnicast++;
624                 }
625         }
626         else
627         {
628                 if(!memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN))
629                 {
630                         ieee->NumRxUnicast++;
631                 }
632         }
633 //YJ,add,080828,for keep alive,end
634 
635 #ifdef _RTL8187_EXT_PATCH_
636         if((ieee->iw_mode == ieee->iw_ext_mode) && (ieee->ext_patch_ieee80211_rx_frame_get_hdrlen))
637         {
638                 hdrlen = ieee->ext_patch_ieee80211_rx_frame_get_hdrlen(ieee, skb);
639                 if(skb->len < hdrlen)
640                         goto rx_dropped;
641         }
642         else
643 #endif
644         hdrlen = ieee80211_get_hdrlen(fc);
645 
646 #ifdef NOT_YET
647 #if WIRELESS_EXT > 15
648         /* Put this code here so that we avoid duplicating it in all
649          * Rx paths. - Jean II */
650 #ifdef IW_WIRELESS_SPY          /* defined in iw_handler.h */
651         /* If spy monitoring on */
652         if (iface->spy_data.spy_number > 0) {
653                 struct iw_quality wstats;
654                 wstats.level = rx_stats->signal;
655                 wstats.noise = rx_stats->noise;
656                 wstats.updated = 6;     /* No qual value */
657                 /* Update spy records */
658                 wireless_spy_update(dev, hdr->addr2, &wstats);
659         }
660 #endif /* IW_WIRELESS_SPY */
661 #endif /* WIRELESS_EXT > 15 */
662         hostap_update_rx_stats(local->ap, hdr, rx_stats);
663 #endif
664 
665 #if WIRELESS_EXT > 15
666         if (ieee->iw_mode == IW_MODE_MONITOR) {
667                 ieee80211_monitor_rx(ieee, skb, rx_stats);
668                 stats->rx_packets++;
669                 stats->rx_bytes += skb->len;
670                 return 1;
671         }
672 #endif
673         if (ieee->host_decrypt) {
674                 int idx = 0;
675                 if (skb->len >= hdrlen + 3)
676                         idx = skb->data[hdrlen + 3] >> 6;
677                 crypt = ieee->crypt[idx];
678 #ifdef NOT_YET
679                 sta = NULL;
680 
681                 /* Use station specific key to override default keys if the
682                  * receiver address is a unicast address ("individual RA"). If
683                  * bcrx_sta_key parameter is set, station specific key is used
684                  * even with broad/multicast targets (this is against IEEE
685                  * 802.11, but makes it easier to use different keys with
686                  * stations that do not support WEP key mapping). */
687 
688                 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
689                         (void) hostap_handle_sta_crypto(local, hdr, &crypt,
690                                                         &sta);
691 #endif
692 
693                 /* allow NULL decrypt to indicate an station specific override
694                  * for default encryption */
695                 if (crypt && (crypt->ops == NULL ||
696                               crypt->ops->decrypt_mpdu == NULL))
697                         crypt = NULL;
698 
699                 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
700                         /* This seems to be triggered by some (multicast?)
701                          * frames from other than current BSS, so just drop the
702                          * frames silently instead of filling system log with
703                          * these reports. */
704                         IEEE80211_DEBUG_DROP("Decryption failed (not set)"
705                                              " (SA=" MAC_FMT ")\n",
706                                              MAC_ARG(hdr->addr2));
707                         ieee->ieee_stats.rx_discards_undecryptable++;
708                         goto rx_dropped;
709                 }
710         }
711 
712         if (skb->len < IEEE80211_DATA_HDR3_LEN)
713                 goto rx_dropped;
714 
715 #ifdef _RTL8187_EXT_PATCH_
716         if( ieee->iw_mode == ieee->iw_ext_mode && ieee->ext_patch_ieee80211_rx_mgt_update_expire )
717                 ieee->ext_patch_ieee80211_rx_mgt_update_expire( ieee, skb );
718 #endif
719 
720         // if QoS enabled, should check the sequence for each of the AC
721         if (is_duplicate_packet(ieee, hdr))
722                 goto rx_dropped;
723 
724 
725         if (type == IEEE80211_FTYPE_MGMT) {
726 
727         #if 0
728                 if ( stype == IEEE80211_STYPE_AUTH &&
729                     fc & IEEE80211_FCTL_WEP && ieee->host_decrypt &&
730                     (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0)
731                 {
732                         printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
733                                "from " MAC_FMT "\n", dev->name,
734                                MAC_ARG(hdr->addr2));
735                         /* TODO: could inform hostapd about this so that it
736                          * could send auth failure report */
737                         goto rx_dropped;
738                 }
739         #endif
740 
741 
742                 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
743                         goto rx_dropped;
744                 else
745                         goto rx_exit;
746         }
747 #ifdef _RTL8187_EXT_PATCH_
748         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_on_rx)
749         {
750                 if(ieee->ext_patch_ieee80211_rx_on_rx(ieee, skb, rx_stats, type, stype)==0)
751                 {
752                         goto rx_exit;
753                 }
754         }
755 #endif
756 
757         /* Data frame - extract src/dst addresses */
758         switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
759         case IEEE80211_FCTL_FROMDS:
760                 memcpy(dst, hdr->addr1, ETH_ALEN);
761                 memcpy(src, hdr->addr3, ETH_ALEN);
762                 memcpy(bssid,hdr->addr2,ETH_ALEN);
763                 break;
764         case IEEE80211_FCTL_TODS:
765                 memcpy(dst, hdr->addr3, ETH_ALEN);
766                 memcpy(src, hdr->addr2, ETH_ALEN);
767                 memcpy(bssid,hdr->addr1,ETH_ALEN);
768                 break;
769         case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
770                 if (skb->len < IEEE80211_DATA_HDR4_LEN)
771                         goto rx_dropped;
772                 memcpy(dst, hdr->addr3, ETH_ALEN);
773                 memcpy(src, hdr->addr4, ETH_ALEN);
774                 memcpy(bssid, ieee->current_network.bssid, ETH_ALEN);
775                 break;
776         case 0:
777                 memcpy(dst, hdr->addr1, ETH_ALEN);
778                 memcpy(src, hdr->addr2, ETH_ALEN);
779                 memcpy(bssid,hdr->addr3,ETH_ALEN);
780                 break;
781         }
782 
783 #ifdef NOT_YET
784         if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
785                 goto rx_dropped;
786         if (wds) {
787                 skb->dev = dev = wds;
788                 stats = hostap_get_stats(dev);
789         }
790 
791         if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
792             (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS &&
793             ieee->stadev &&
794             memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
795                 /* Frame from BSSID of the AP for which we are a client */
796                 skb->dev = dev = ieee->stadev;
797                 stats = hostap_get_stats(dev);
798                 from_assoc_ap = 1;
799         }
800 #endif
801 
802         dev->last_rx = jiffies;
803 
804 #ifdef NOT_YET
805         if ((ieee->iw_mode == IW_MODE_MASTER ||
806              ieee->iw_mode == IW_MODE_REPEAT) &&
807             !from_assoc_ap) {
808                 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
809                                              wds != NULL)) {
810                 case AP_RX_CONTINUE_NOT_AUTHORIZED:
811                         frame_authorized = 0;
812                         break;
813                 case AP_RX_CONTINUE:
814                         frame_authorized = 1;
815                         break;
816                 case AP_RX_DROP:
817                         goto rx_dropped;
818                 case AP_RX_EXIT:
819                         goto rx_exit;
820                 }
821         }
822 #endif
823 
824 #ifdef _RTL8187_EXT_PATCH_
825         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_is_valid_framectl)
826         {
827                 if(ieee->ext_patch_ieee80211_rx_is_valid_framectl(ieee, fc, type, stype)==0)
828                         goto rx_dropped;
829         }
830         else
831 #endif
832         /* Nullfunc frames may have PS-bit set, so they must be passed to
833          * hostap_handle_sta_rx() before being dropped here. */
834         if (stype != IEEE80211_STYPE_DATA &&
835             stype != IEEE80211_STYPE_DATA_CFACK &&
836             stype != IEEE80211_STYPE_DATA_CFPOLL &&
837             stype != IEEE80211_STYPE_DATA_CFACKPOLL&&
838             stype != IEEE80211_STYPE_QOS_DATA//add by David,2006.8.4
839             ) {
840                 if (stype != IEEE80211_STYPE_NULLFUNC)
841                         IEEE80211_DEBUG_DROP(
842                                 "RX: dropped data frame "
843                                 "with no data (type=0x%02x, "
844                                 "subtype=0x%02x, len=%d)\n",
845                                 type, stype, skb->len);
846                 goto rx_dropped;
847         }
848         if(memcmp(bssid,ieee->current_network.bssid,ETH_ALEN)) {
849                 goto rx_dropped;
850         }
851 
852         ieee->NumRxDataInPeriod++;
853         ieee->NumRxOkTotal++;
854         /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
855 
856         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
857             (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
858                 goto rx_dropped;
859 
860         hdr = (struct ieee80211_hdr *) skb->data;
861 
862         /* skb: hdr + (possibly fragmented) plaintext payload */
863         // PR: FIXME: hostap has additional conditions in the "if" below:
864         // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
865         if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
866                 int flen;
867                 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
868                 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
869 
870                 if (!frag_skb) {
871                         IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
872                                         "Rx cannot get skb from fragment "
873                                         "cache (morefrag=%d seq=%u frag=%u)\n",
874                                         (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
875                                         WLAN_GET_SEQ_SEQ(sc), frag);
876                         goto rx_dropped;
877                 }
878                 flen = skb->len;
879                 if (frag != 0)
880                         flen -= hdrlen;
881 
882                 if (frag_skb->tail + flen > frag_skb->end) {
883                         printk(KERN_WARNING "%s: host decrypted and "
884                                "reassembled frame did not fit skb\n",
885                                dev->name);
886                         ieee80211_frag_cache_invalidate(ieee, hdr);
887                         goto rx_dropped;
888                 }
889 
890                 if (frag == 0) {
891                         /* copy first fragment (including full headers) into
892                          * beginning of the fragment cache skb */
893                         memcpy(skb_put(frag_skb, flen), skb->data, flen);
894                 } else {
895                         /* append frame payload to the end of the fragment
896                          * cache skb */
897                         memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
898                                flen);
899                 }
900                 dev_kfree_skb_any(skb);
901                 skb = NULL;
902 
903                 if (fc & IEEE80211_FCTL_MOREFRAGS) {
904                         /* more fragments expected - leave the skb in fragment
905                          * cache for now; it will be delivered to upper layers
906                          * after all fragments have been received */
907                         goto rx_exit;
908                 }
909 
910                 /* this was the last fragment and the frame will be
911                  * delivered, so remove skb from fragment cache */
912                 skb = frag_skb;
913                 hdr = (struct ieee80211_hdr *) skb->data;
914                 ieee80211_frag_cache_invalidate(ieee, hdr);
915         }
916 
917         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
918          * encrypted/authenticated */
919         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
920             ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
921                 goto rx_dropped;
922 
923         hdr = (struct ieee80211_hdr *) skb->data;
924         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
925                 if (/*ieee->ieee802_1x &&*/
926                     ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
927 
928 #ifdef CONFIG_IEEE80211_DEBUG
929                         /* pass unencrypted EAPOL frames even if encryption is
930                          * configured */
931                         struct eapol *eap = (struct eapol *)(skb->data +
932                                 24);
933                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
934                                                 eap_get_type(eap->type));
935 #endif
936                 } else {
937                         IEEE80211_DEBUG_DROP(
938                                 "encryption configured, but RX "
939                                 "frame not encrypted (SA=" MAC_FMT ")\n",
940                                 MAC_ARG(hdr->addr2));
941                         goto rx_dropped;
942                 }
943         }
944 
945 #ifdef CONFIG_IEEE80211_DEBUG
946         if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
947             ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
948                         struct eapol *eap = (struct eapol *)(skb->data +
949                                 24);
950                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
951                                                 eap_get_type(eap->type));
952         }
953 #endif
954 
955         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
956             !ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
957                 IEEE80211_DEBUG_DROP(
958                         "dropped unencrypted RX data "
959                         "frame from " MAC_FMT
960                         " (drop_unencrypted=1)\n",
961                         MAC_ARG(hdr->addr2));
962                 goto rx_dropped;
963         }
964 /*
965         if(ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
966                 printk(KERN_WARNING "RX: IEEE802.1X EPAOL frame!\n");
967         }
968 */
969         /* skb: hdr + (possible reassembled) full plaintext payload */
970         payload = skb->data + hdrlen;
971         ethertype = (payload[6] << 8) | payload[7];
972 
973 #ifdef NOT_YET
974         /* If IEEE 802.1X is used, check whether the port is authorized to send
975          * the received frame. */
976         if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
977                 if (ethertype == ETH_P_PAE) {
978                         printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
979                                dev->name);
980                         if (ieee->hostapd && ieee->apdev) {
981                                 /* Send IEEE 802.1X frames to the user
982                                  * space daemon for processing */
983                                 prism2_rx_80211(ieee->apdev, skb, rx_stats,
984                                                 PRISM2_RX_MGMT);
985                                 ieee->apdevstats.rx_packets++;
986                                 ieee->apdevstats.rx_bytes += skb->len;
987                                 goto rx_exit;
988                         }
989                 } else if (!frame_authorized) {
990                         printk(KERN_DEBUG "%s: dropped frame from "
991                                "unauthorized port (IEEE 802.1X): "
992                                "ethertype=0x%04x\n",
993                                dev->name, ethertype);
994                         goto rx_dropped;
995                 }
996         }
997 #endif
998 
999 #ifdef _RTL8187_EXT_PATCH_
1000         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_rx_process_dataframe)
1001         {
1002         //Added for mesh rx interrupt.
1003                 //spin_lock_irqsave(&ieee->lock,flags);
1004                 status = ieee->ext_patch_ieee80211_rx_process_dataframe(ieee, skb, rx_stats);
1005                 //spin_unlock_irqrestore(&ieee->lock,flags);
1006 
1007                 if(status)
1008 //      if(ieee->ext_patch_ieee80211_rx_process_dataframe(ieee, skb, rx_stats))
1009                         goto rx_exit;
1010                 else
1011                         goto rx_dropped;
1012         }
1013 #endif
1014 
1015         /* convert hdr + possible LLC headers into Ethernet header */
1016         if (skb->len - hdrlen >= 8 &&
1017             ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
1018               ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1019              memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1020                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1021                  * replace EtherType */
1022                 skb_pull(skb, hdrlen + SNAP_SIZE);
1023                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1024                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1025         } else {
1026                 u16 len;
1027                 /* Leave Ethernet header part of hdr and full payload */
1028                 skb_pull(skb, hdrlen);
1029                 len = htons(skb->len);
1030                 memcpy(skb_push(skb, 2), &len, 2);
1031                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1032                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1033         }
1034 
1035 #ifdef NOT_YET
1036         if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
1037                     IEEE80211_FCTL_TODS) &&
1038             skb->len >= ETH_HLEN + ETH_ALEN) {
1039                 /* Non-standard frame: get addr4 from its bogus location after
1040                  * the payload */
1041                 memcpy(skb->data + ETH_ALEN,
1042                        skb->data + skb->len - ETH_ALEN, ETH_ALEN);
1043                 skb_trim(skb, skb->len - ETH_ALEN);
1044         }
1045 #endif
1046 
1047         stats->rx_packets++;
1048         stats->rx_bytes += skb->len;
1049 
1050 #ifdef NOT_YET
1051         if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
1052             ieee->ap->bridge_packets) {
1053                 if (dst[0] & 0x01) {
1054                         /* copy multicast frame both to the higher layers and
1055                          * to the wireless media */
1056                         ieee->ap->bridged_multicast++;
1057                         skb2 = skb_clone(skb, GFP_ATOMIC);
1058                         if (skb2 == NULL)
1059                                 printk(KERN_DEBUG "%s: skb_clone failed for "
1060                                        "multicast frame\n", dev->name);
1061                 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
1062                         /* send frame directly to the associated STA using
1063                          * wireless media and not passing to higher layers */
1064                         ieee->ap->bridged_unicast++;
1065                         skb2 = skb;
1066                         skb = NULL;
1067                 }
1068         }
1069 
1070         if (skb2 != NULL) {
1071                 /* send to wireless media */
1072                 skb2->protocol = __constant_htons(ETH_P_802_3);
1073                 skb2->mac.raw = skb2->nh.raw = skb2->data;
1074                 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
1075                 skb2->dev = dev;
1076                 dev_queue_xmit(skb2);
1077         }
1078 
1079 #endif
1080         if (skb) {
1081                 skb->protocol = eth_type_trans(skb, dev);
1082                 memset(skb->cb, 0, sizeof(skb->cb));
1083                 skb->dev = dev;
1084                 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
1085                 ieee->last_rx_ps_time = jiffies;
1086                 netif_rx(skb);
1087         }
1088 
1089  rx_exit:
1090 #ifdef NOT_YET
1091         if (sta)
1092                 hostap_handle_sta_release(sta);
1093 #endif
1094         return 1;
1095 
1096  rx_dropped:
1097         stats->rx_dropped++;
1098 
1099         /* Returning 0 indicates to caller that we have not handled the SKB--
1100          * so it is still allocated and can be used again by underlying
1101          * hardware as a DMA target */
1102         return 0;
1103 }
1104 
1105 #ifdef _RTL8187_EXT_PATCH_
1106 int ieee_ext_skb_p80211_to_ether(struct sk_buff *skb, int hdrlen, u8 *dst, u8 *src)
1107 {
1108         u8 *payload;
1109         u16 ethertype;
1110 
1111         /* skb: hdr + (possible reassembled) full plaintext payload */
1112         payload = skb->data + hdrlen;
1113         ethertype = (payload[6] << 8) | payload[7];
1114 
1115         /* convert hdr + possible LLC headers into Ethernet header */
1116         if (skb->len - hdrlen >= 8 &&
1117             ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
1118               ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1119              memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1120                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1121                  * replace EtherType */
1122                 skb_pull(skb, hdrlen + SNAP_SIZE);
1123                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1124                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1125         } else {
1126                 u16 len;
1127                 /* Leave Ethernet header part of hdr and full payload */
1128                 skb_pull(skb, hdrlen);
1129                 len = htons(skb->len);
1130                 memcpy(skb_push(skb, 2), &len, 2);
1131                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1132                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1133         }
1134 
1135         return 1;
1136 }
1137 #endif // _RTL8187_EXT_PATCH_
1138 
1139 
1140 #define MGMT_FRAME_FIXED_PART_LENGTH            0x24
1141 
1142 static inline int ieee80211_is_ofdm_rate(u8 rate)
1143 {
1144         switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
1145         case IEEE80211_OFDM_RATE_6MB:
1146         case IEEE80211_OFDM_RATE_9MB:
1147         case IEEE80211_OFDM_RATE_12MB:
1148         case IEEE80211_OFDM_RATE_18MB:
1149         case IEEE80211_OFDM_RATE_24MB:
1150         case IEEE80211_OFDM_RATE_36MB:
1151         case IEEE80211_OFDM_RATE_48MB:
1152         case IEEE80211_OFDM_RATE_54MB:
1153                 return 1;
1154         }
1155         return 0;
1156 }
1157 
1158 static inline int ieee80211_SignalStrengthTranslate(
1159         int  CurrSS
1160         )
1161 {
1162         int RetSS;
1163 
1164         // Step 1. Scale mapping.
1165         if(CurrSS >= 71 && CurrSS <= 100)
1166         {
1167                 RetSS = 90 + ((CurrSS - 70) / 3);
1168         }
1169         else if(CurrSS >= 41 && CurrSS <= 70)
1170         {
1171                 RetSS = 78 + ((CurrSS - 40) / 3);
1172         }
1173         else if(CurrSS >= 31 && CurrSS <= 40)
1174         {
1175                 RetSS = 66 + (CurrSS - 30);
1176         }
1177         else if(CurrSS >= 21 && CurrSS <= 30)
1178         {
1179                 RetSS = 54 + (CurrSS - 20);
1180         }
1181         else if(CurrSS >= 5 && CurrSS <= 20)
1182         {
1183                 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
1184         }
1185         else if(CurrSS == 4)
1186         {
1187                 RetSS = 36;
1188         }
1189         else if(CurrSS == 3)
1190         {
1191                 RetSS = 27;
1192         }
1193         else if(CurrSS == 2)
1194         {
1195                 RetSS = 18;
1196         }
1197         else if(CurrSS == 1)
1198         {
1199                 RetSS = 9;
1200         }
1201         else
1202         {
1203                 RetSS = CurrSS;
1204         }
1205         //RT_TRACE(COMP_DBG, DBG_LOUD, ("##### After Mapping:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
1206 
1207         // Step 2. Smoothing.
1208 
1209         //RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$$$ After Smoothing:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
1210 
1211         return RetSS;
1212 }
1213 
1214 #ifdef ENABLE_DOT11D
1215 static inline void ieee80211_extract_country_ie(
1216         struct ieee80211_device *ieee,
1217         struct ieee80211_info_element *info_element,
1218         struct ieee80211_network *network,
1219         u8 * addr2
1220 )
1221 {
1222 #if 0
1223         u32 i = 0;
1224         u8 * p = (u8*)info_element->data;
1225         printk("-----------------------\n");
1226         printk("%s Country IE:", network->ssid);
1227         for(i=0; i<info_element->len; i++)
1228                 printk("\t%2.2x", *(p+i));
1229         printk("\n-----------------------\n");
1230 #endif
1231         if(IS_DOT11D_ENABLE(ieee))
1232         {
1233                 if(info_element->len!= 0)
1234                 {
1235                         memcpy(network->CountryIeBuf, info_element->data, info_element->len);
1236                         network->CountryIeLen = info_element->len;
1237 
1238                         if(!IS_COUNTRY_IE_VALID(ieee))
1239                         {
1240                                 Dot11d_UpdateCountryIe(ieee, addr2, info_element->len, info_element->data);
1241                         }
1242                 }
1243 
1244                 //
1245                 // 070305, rcnjko: I update country IE watch dog here because
1246                 // some AP (e.g. Cisco 1242) don't include country IE in their
1247                 // probe response frame.
1248                 //
1249                 if(IS_EQUAL_CIE_SRC(ieee, addr2) )
1250                 {
1251                         UPDATE_CIE_WATCHDOG(ieee);
1252                 }
1253         }
1254 
1255 }
1256 #endif
1257 
1258 int
1259 ieee80211_TranslateToDbm(
1260         unsigned char SignalStrengthIndex       // 0-100 index.
1261         )
1262 {
1263         unsigned char SignalPower; // in dBm.
1264 
1265         // Translate to dBm (x=0.5y-95).
1266         SignalPower = (int)SignalStrengthIndex * 7 / 10;
1267         SignalPower -= 95;
1268 
1269         return SignalPower;
1270 }
1271 inline int ieee80211_network_init(
1272         struct ieee80211_device *ieee,
1273         struct ieee80211_probe_response *beacon,
1274         struct ieee80211_network *network,
1275         struct ieee80211_rx_stats *stats)
1276 {
1277 #ifdef CONFIG_IEEE80211_DEBUG
1278         char rates_str[64];
1279         char *p;
1280 #endif
1281         struct ieee80211_info_element *info_element;
1282         u16 left;
1283         u8 i;
1284         short offset;
1285         u8 curRate = 0,hOpRate = 0,curRate_ex = 0;
1286 
1287         /* Pull out fixed field data */
1288         memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
1289         network->capability = beacon->capability;
1290         network->last_scanned = jiffies;
1291         network->time_stamp[0] = beacon->time_stamp[0];
1292         network->time_stamp[1] = beacon->time_stamp[1];
1293         network->beacon_interval = beacon->beacon_interval;
1294         /* Where to pull this? beacon->listen_interval;*/
1295         network->listen_interval = 0x0A;
1296         network->rates_len = network->rates_ex_len = 0;
1297         network->last_associate = 0;
1298         network->ssid_len = 0;
1299         network->flags = 0;
1300         network->atim_window = 0;
1301         network->QoS_Enable = 0;
1302 //by amy 080312
1303         network->HighestOperaRate = 0;
1304 //by amy 080312
1305 #ifdef THOMAS_TURBO
1306         network->Turbo_Enable = 0;
1307 #endif
1308 #ifdef ENABLE_DOT11D
1309         network->CountryIeLen = 0;
1310         memset(network->CountryIeBuf, 0, MAX_IE_LEN);
1311 #endif
1312 
1313         if (stats->freq == IEEE80211_52GHZ_BAND) {
1314                 /* for A band (No DS info) */
1315                 network->channel = stats->received_channel;
1316         } else
1317                 network->flags |= NETWORK_HAS_CCK;
1318 
1319         network->wpa_ie_len = 0;
1320         network->rsn_ie_len = 0;
1321 
1322         info_element = &beacon->info_element;
1323         left = stats->len - ((void *)info_element - (void *)beacon);
1324         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
1325                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
1326                         IEEE80211_DEBUG_SCAN("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%d left=%d.\n",
1327                                              info_element->len + sizeof(struct ieee80211_info_element),
1328                                              left);
1329                         return 1;
1330                 }
1331 
1332                 switch (info_element->id) {
1333                 case MFIE_TYPE_SSID:
1334                         if (ieee80211_is_empty_essid(info_element->data,
1335                                                      info_element->len)) {
1336                                 network->flags |= NETWORK_EMPTY_ESSID;
1337                                 break;
1338                         }
1339 
1340                         network->ssid_len = min(info_element->len,
1341                                                 (u8)IW_ESSID_MAX_SIZE);
1342                         memcpy(network->ssid, info_element->data, network->ssid_len);
1343                         if (network->ssid_len < IW_ESSID_MAX_SIZE)
1344                                 memset(network->ssid + network->ssid_len, 0,
1345                                        IW_ESSID_MAX_SIZE - network->ssid_len);
1346 
1347                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
1348                                              network->ssid, network->ssid_len);
1349                         break;
1350 
1351                 case MFIE_TYPE_RATES:
1352 #ifdef CONFIG_IEEE80211_DEBUG
1353                         p = rates_str;
1354 #endif
1355                         network->rates_len = min(info_element->len, MAX_RATES_LENGTH);
1356                         for (i = 0; i < network->rates_len; i++) {
1357                                 network->rates[i] = info_element->data[i];
1358                                 curRate = network->rates[i] & 0x7f;
1359                                 if( hOpRate < curRate )
1360                                         hOpRate = curRate;
1361 #ifdef CONFIG_IEEE80211_DEBUG
1362                                 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
1363 #endif
1364                                 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
1365                                         network->flags |= NETWORK_HAS_OFDM;
1366                                         if (info_element->data[i] &
1367                                             IEEE80211_BASIC_RATE_MASK)
1368                                                 network->flags &=
1369                                                         ~NETWORK_HAS_CCK;
1370                                 }
1371                         }
1372 
1373                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES: '%s' (%d)\n",
1374                                              rates_str, network->rates_len);
1375                         break;
1376 
1377                 case MFIE_TYPE_RATES_EX:
1378 #ifdef CONFIG_IEEE80211_DEBUG
1379                         p = rates_str;
1380 #endif
1381                         network->rates_ex_len = min(info_element->len, MAX_RATES_EX_LENGTH);
1382                         for (i = 0; i < network->rates_ex_len; i++) {
1383                                 network->rates_ex[i] = info_element->data[i];
1384                                 curRate_ex = network->rates_ex[i] & 0x7f;
1385                                 if( hOpRate < curRate_ex )
1386                                         hOpRate = curRate_ex;
1387 #ifdef CONFIG_IEEE80211_DEBUG
1388                                 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
1389 #endif
1390                                 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
1391                                         network->flags |= NETWORK_HAS_OFDM;
1392                                         if (info_element->data[i] &
1393                                             IEEE80211_BASIC_RATE_MASK)
1394                                                 network->flags &=
1395                                                         ~NETWORK_HAS_CCK;
1396                                 }
1397                         }
1398 
1399                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
1400                                              rates_str, network->rates_ex_len);
1401                         break;
1402 
1403                 case MFIE_TYPE_DS_SET:
1404                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
1405                                              info_element->data[0]);
1406                         if (stats->freq == IEEE80211_24GHZ_BAND)
1407                                 network->channel = info_element->data[0];
1408                         break;
1409 
1410                 case MFIE_TYPE_FH_SET:
1411                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
1412                         break;
1413 
1414                 case MFIE_TYPE_CF_SET:
1415                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
1416                         break;
1417 
1418                 case MFIE_TYPE_TIM:
1419 
1420                         if(info_element->len < 4)
1421                                 break;
1422 
1423                         network->dtim_period = info_element->data[1];
1424 
1425                         if(ieee->state != IEEE80211_LINKED)
1426                                 break;
1427 #if 0
1428                         network->last_dtim_sta_time[0] = stats->mac_time[0];
1429 #else
1430                         network->last_dtim_sta_time[0] = jiffies;
1431 #endif
1432                         network->last_dtim_sta_time[1] = stats->mac_time[1];
1433 
1434                         network->dtim_data = IEEE80211_DTIM_VALID;
1435 
1436                         if(info_element->data[0] != 0)
1437                                 break;
1438 
1439                         if(info_element->data[2] & 1)
1440                                 network->dtim_data |= IEEE80211_DTIM_MBCAST;
1441 
1442                         offset = (info_element->data[2] >> 1)*2;
1443 
1444                         //printk("offset1:%x aid:%x\n",offset, ieee->assoc_id);
1445 
1446                         /* add and modified for ps 2008.1.22 */
1447                         if(ieee->assoc_id < 8*offset ||
1448                                 ieee->assoc_id > 8*(offset + info_element->len -3)) {
1449                                 break;
1450                         }
1451 
1452                         offset = (ieee->assoc_id/8) - offset;// + ((aid % 8)? 0 : 1) ;
1453 
1454                 //      printk("offset:%x data:%x, ucast:%d\n", offset,
1455                         //      info_element->data[3+offset] ,
1456                         //      info_element->data[3+offset] & (1<<(ieee->assoc_id%8)));
1457 
1458                         if(info_element->data[3+offset] & (1<<(ieee->assoc_id%8))) {
1459                                 network->dtim_data |= IEEE80211_DTIM_UCAST;
1460                         }
1461                         break;
1462 
1463                 case MFIE_TYPE_IBSS_SET:
1464                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
1465                         break;
1466 
1467                 case MFIE_TYPE_CHALLENGE:
1468                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
1469                         break;
1470 
1471                 case MFIE_TYPE_GENERIC:
1472                         //nic is 87B
1473                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
1474                                              info_element->len);
1475                         if (info_element->len >= 4  &&
1476                             info_element->data[0] == 0x00 &&
1477                             info_element->data[1] == 0x50 &&
1478                             info_element->data[2] == 0xf2 &&
1479                             info_element->data[3] == 0x01) {
1480                                 network->wpa_ie_len = min(info_element->len + 2,
1481                                                          MAX_WPA_IE_LEN);
1482                                 memcpy(network->wpa_ie, info_element,
1483                                        network->wpa_ie_len);
1484                         }
1485 
1486 #ifdef THOMAS_TURBO
1487                         if (info_element->len == 7 &&
1488                             info_element->data[0] == 0x00 &&
1489                             info_element->data[1] == 0xe0 &&
1490                             info_element->data[2] == 0x4c &&
1491                             info_element->data[3] == 0x01 &&
1492                             info_element->data[4] == 0x02) {
1493                                 network->Turbo_Enable = 1;
1494                         }
1495 #endif
1496                         if (1 == stats->nic_type) {//nic 87
1497                                 break;
1498                         }
1499 
1500                         if (info_element->len >= 5  &&
1501                             info_element->data[0] == 0x00 &&
1502                             info_element->data[1] == 0x50 &&
1503                             info_element->data[2] == 0xf2 &&
1504                             info_element->data[3] == 0x02 &&
1505                             info_element->data[4] == 0x00) {
1506                                 //printk(KERN_WARNING "wmm info updated: %x\n", info_element->data[6]);
1507                                 //WMM Information Element
1508                                 network->wmm_info = info_element->data[6];
1509                                 network->QoS_Enable = 1;
1510                         }
1511 
1512                         if (info_element->len >= 8  &&
1513                             info_element->data[0] == 0x00 &&
1514                             info_element->data[1] == 0x50 &&
1515                             info_element->data[2] == 0xf2 &&
1516                             info_element->data[3] == 0x02 &&
1517                             info_element->data[4] == 0x01) {
1518                                 // Not care about version at present.
1519                                 //WMM Information Element
1520                                 //printk(KERN_WARNING "wmm info&param updated: %x\n", info_element->data[6]);
1521                                 network->wmm_info = info_element->data[6];
1522                                 //WMM Parameter Element
1523                                 memcpy(network->wmm_param, (u8 *)(info_element->data + 8),(info_element->len - 8));
1524                                 network->QoS_Enable = 1;
1525                         }
1526                         break;
1527 
1528                 case MFIE_TYPE_RSN:
1529                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
1530                                              info_element->len);
1531                         network->rsn_ie_len = min(info_element->len + 2,
1532                                                  MAX_WPA_IE_LEN);
1533                         memcpy(network->rsn_ie, info_element,
1534                                network->rsn_ie_len);
1535                         break;
1536 #ifdef ENABLE_DOT11D
1537                 case MFIE_TYPE_COUNTRY:
1538                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_COUNTRY: %d bytes\n",
1539                                              info_element->len);
1540 //                      printk("=====>Receive <%s> Country IE\n",network->ssid);
1541                         ieee80211_extract_country_ie(ieee, info_element, network, beacon->header.addr2);
1542                         break;
1543 #endif
1544                 default:
1545                         IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
1546                                              info_element->id);
1547                         break;
1548                 }
1549 
1550                 left -= sizeof(struct ieee80211_info_element_hdr) +
1551                         info_element->len;
1552                 info_element = (struct ieee80211_info_element *)
1553                         &info_element->data[info_element->len];
1554         }
1555 //by amy 080312
1556         network->HighestOperaRate = hOpRate;
1557 //by amy 080312
1558         network->mode = 0;
1559         if (stats->freq == IEEE80211_52GHZ_BAND)
1560                 network->mode = IEEE_A;
1561         else {
1562                 if (network->flags & NETWORK_HAS_OFDM)
1563                         network->mode |= IEEE_G;
1564                 if (network->flags & NETWORK_HAS_CCK)
1565                         network->mode |= IEEE_B;
1566         }
1567 
1568         if (network->mode == 0) {
1569                 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
1570                                      "network.\n",
1571                                      escape_essid(network->ssid,
1572                                                   network->ssid_len),
1573                                      MAC_ARG(network->bssid));
1574                 return 1;
1575         }
1576 
1577         if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
1578                 network->flags |= NETWORK_EMPTY_ESSID;
1579 #if 0
1580         stats->signal = ieee80211_SignalStrengthTranslate(stats->signal);
1581 #endif
1582         stats->signal = ieee80211_TranslateToDbm(stats->signalstrength);
1583         //stats->noise = stats->signal - stats->noise;
1584         stats->noise = ieee80211_TranslateToDbm(100 - stats->signalstrength) - 25;
1585         memcpy(&network->stats, stats, sizeof(network->stats));
1586 
1587         return 0;
1588 }
1589 
1590 static inline int is_same_network(struct ieee80211_network *src,
1591                                   struct ieee80211_network *dst,
1592                                   struct ieee80211_device * ieee)
1593 {
1594         /* A network is only a duplicate if the channel, BSSID, ESSID
1595          * and the capability field (in particular IBSS and BSS) all match.
1596          * We treat all <hidden> with the same BSSID and channel
1597          * as one network */
1598         return (((src->ssid_len == dst->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) &&  //YJ,mod,080819,for hidden ap
1599                 //((src->ssid_len == dst->ssid_len) &&
1600                 (src->channel == dst->channel) &&
1601                 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1602                 (!memcmp(src->ssid, dst->ssid, src->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) && //YJ,mod,080819,for hidden ap
1603                 //!memcmp(src->ssid, dst->ssid, src->ssid_len) &&
1604                 ((src->capability & WLAN_CAPABILITY_IBSS) ==
1605                 (dst->capability & WLAN_CAPABILITY_IBSS)) &&
1606                 ((src->capability & WLAN_CAPABILITY_BSS) ==
1607                 (dst->capability & WLAN_CAPABILITY_BSS)));
1608 }
1609 
1610 inline void update_network(struct ieee80211_network *dst,
1611                                   struct ieee80211_network *src)
1612 {
1613         unsigned char quality = src->stats.signalstrength;
1614         unsigned char signal = 0;
1615         unsigned char noise = 0;
1616         if(dst->stats.signalstrength > 0) {
1617                 quality = (dst->stats.signalstrength * 5 + src->stats.signalstrength + 5)/6;
1618         }
1619         signal = ieee80211_TranslateToDbm(quality);
1620         //noise = signal - src->stats.noise;
1621         if(dst->stats.noise > 0)
1622                 noise = (dst->stats.noise * 5 + src->stats.noise)/6;
1623         //if(strcmp(dst->ssid, "linksys_lzm000") == 0)
1624 //      printk("ssid:%s, quality:%d, signal:%d\n", dst->ssid, quality, signal);
1625         memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1626         dst->stats.signalstrength = quality;
1627         dst->stats.signal = signal;
1628 //      printk("==================>stats.signal is %d\n",dst->stats.signal);
1629         dst->stats.noise = noise;
1630 
1631 
1632         dst->capability = src->capability;
1633         memcpy(dst->rates, src->rates, src->rates_len);
1634         dst->rates_len = src->rates_len;
1635         memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1636         dst->rates_ex_len = src->rates_ex_len;
1637         dst->HighestOperaRate= src->HighestOperaRate;
1638         //printk("==========>in %s: src->ssid is %s,chan is %d\n",__func__,src->ssid,src->channel);
1639 
1640         //YJ,add,080819,for hidden ap
1641         if(src->ssid_len > 0)
1642         {
1643                 //if(src->ssid_len == 13)
1644                 //      printk("=====================>>>>>>>> Dst ssid: %s Src ssid: %s\n", dst->ssid, src->ssid);
1645                 memset(dst->ssid, 0, dst->ssid_len);
1646                 dst->ssid_len = src->ssid_len;
1647                 memcpy(dst->ssid, src->ssid, src->ssid_len);
1648         }
1649         //YJ,add,080819,for hidden ap,end
1650 
1651         dst->channel = src->channel;
1652         dst->mode = src->mode;
1653         dst->flags = src->flags;
1654         dst->time_stamp[0] = src->time_stamp[0];
1655         dst->time_stamp[1] = src->time_stamp[1];
1656 
1657         dst->beacon_interval = src->beacon_interval;
1658         dst->listen_interval = src->listen_interval;
1659         dst->atim_window = src->atim_window;
1660         dst->dtim_period = src->dtim_period;
1661         dst->dtim_data = src->dtim_data;
1662         dst->last_dtim_sta_time[0] = src->last_dtim_sta_time[0];
1663         dst->last_dtim_sta_time[1] = src->last_dtim_sta_time[1];
1664 //      printk("update:%s, dtim_period:%x, dtim_data:%x\n", src->ssid, src->dtim_period, src->dtim_data);
1665         memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1666         dst->wpa_ie_len = src->wpa_ie_len;
1667         memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1668         dst->rsn_ie_len = src->rsn_ie_len;
1669 
1670         dst->last_scanned = jiffies;
1671         /* dst->last_associate is not overwritten */
1672 // disable QoS process now, added by David 2006/7/25
1673 #if 1
1674         dst->wmm_info = src->wmm_info; //sure to exist in beacon or probe response frame.
1675 /*
1676         if((dst->wmm_info^src->wmm_info)&0x0f) {//Param Set Count change, update Parameter
1677           memcpy(dst->wmm_param, src->wmm_param, IEEE80211_AC_PRAM_LEN);
1678         }
1679 */
1680         if(src->wmm_param[0].ac_aci_acm_aifsn|| \
1681            src->wmm_param[1].ac_aci_acm_aifsn|| \
1682            src->wmm_param[2].ac_aci_acm_aifsn|| \
1683            src->wmm_param[3].ac_aci_acm_aifsn) {
1684           memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
1685         }
1686         dst->QoS_Enable = src->QoS_Enable;
1687 #else
1688         dst->QoS_Enable = 1;//for Rtl8187 simulation
1689 #endif
1690         dst->SignalStrength = src->SignalStrength;
1691 #ifdef THOMAS_TURBO
1692         dst->Turbo_Enable = src->Turbo_Enable;
1693 #endif
1694 #ifdef ENABLE_DOT11D
1695         dst->CountryIeLen = src->CountryIeLen;
1696         memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
1697 #endif
1698 }
1699 
1700 
1701 inline void ieee80211_process_probe_response(
1702         struct ieee80211_device *ieee,
1703         struct ieee80211_probe_response *beacon,
1704         struct ieee80211_rx_stats *stats)
1705 {
1706         struct ieee80211_network network;
1707         struct ieee80211_network *target;
1708         struct ieee80211_network *oldest = NULL;
1709 #ifdef CONFIG_IEEE80211_DEBUG
1710         struct ieee80211_info_element *info_element = &beacon->info_element;
1711 #endif
1712         unsigned long flags;
1713         short renew;
1714         u8 wmm_info;
1715         u8 is_beacon = (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_BEACON)? 1:0;  //YJ,add,080819,for hidden ap
1716 
1717         memset(&network, 0, sizeof(struct ieee80211_network));
1718 //rz
1719 #ifdef _RTL8187_EXT_PATCH_
1720         if((ieee->iw_mode == ieee->iw_ext_mode) && ieee->ext_patch_ieee80211_process_probe_response_1) {
1721                 ieee->ext_patch_ieee80211_process_probe_response_1(ieee, beacon, stats);
1722                 return;
1723         }
1724 #endif
1725 
1726         IEEE80211_DEBUG_SCAN(
1727                 "'%s' (" MAC_FMT "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1728                 escape_essid(info_element->data, info_element->len),
1729                 MAC_ARG(beacon->header.addr3),
1730                 (beacon->capability & (1<<0xf)) ? '1' : '',
1731                 (beacon->capability & (1<<0xe)) ? '1' : '',
1732                 (beacon->capability & (1<<0xd)) ? '1' : '',
1733                 (beacon->capability & (1<<0xc)) ? '1' : '',
1734                 (beacon->capability & (1<<0xb)) ? '1' : '',
1735                 (beacon->capability & (1<<0xa)) ? '1' : '',
1736                 (beacon->capability & (1<<0x9)) ? '1' : '',
1737                 (beacon->capability & (1<<0x8)) ? '1' : '',
1738                 (beacon->capability & (1<<0x7)) ? '1' : '',
1739                 (beacon->capability & (1<<0x6)) ? '1' : '',
1740                 (beacon->capability & (1<<0x5)) ? '1' : '',
1741                 (beacon->capability & (1<<0x4)) ? '1' : '',
1742                 (beacon->capability & (1<<0x3)) ? '1' : '',
1743                 (beacon->capability & (1<<0x2)) ? '1' : '',
1744                 (beacon->capability & (1<<0x1)) ? '1' : '',
1745                 (beacon->capability & (1<<0x0)) ? '1' : '');
1746 #if 0
1747         if(strcmp(escape_essid(beacon->info_element.data, beacon->info_element.len), "rtl_softap") == 0)
1748         {
1749                 if(WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_BEACON)
1750                 {
1751                         u32 i = 0, len = stats->len;
1752                         u8 * p = (u8*)beacon;
1753                         printk("-----------------------\n");
1754                         printk("rtl_softap Beacon:");
1755                         for(i=0; i<len; i++)
1756                                 printk("\t%2.2x", *(p+i));
1757                         printk("\n-----------------------\n");
1758                 }
1759         }
1760 #endif
1761         if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1762                 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1763                                      escape_essid(info_element->data,
1764                                                   info_element->len),
1765                                      MAC_ARG(beacon->header.addr3),
1766                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1767                                      IEEE80211_STYPE_PROBE_RESP ?
1768                                      "PROBE RESPONSE" : "BEACON");
1769                 return;
1770         }
1771 
1772 #ifdef ENABLE_DOT11D
1773         // For Asus EeePc request,
1774         // (1) if wireless adapter receive get any 802.11d country code in AP beacon,
1775         //         wireless adapter should follow the country code.
1776         // (2)  If there is no any country code in beacon,
1777         //       then wireless adapter should do active scan from ch1~11 and
1778         //       passive scan from ch12~14
1779         if(ieee->bGlobalDomain)
1780         {
1781                 if (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_PROBE_RESP)
1782                 {
1783                         // Case 1: Country code
1784                         if(IS_COUNTRY_IE_VALID(ieee) )
1785                         {
1786                                 if( !IsLegalChannel(ieee, network.channel) )
1787                                 {
1788                                         printk("GetScanInfo(): For Country code, filter probe response at channel(%d).\n", network.channel);
1789                                         return;
1790                                 }
1791                         }
1792                         // Case 2: No any country code.
1793                         else
1794                         {
1795                                 // Filter over channel ch12~14
1796                                 if(network.channel > 11)
1797                                 {
1798                                         printk("GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n", network.channel);
1799                                         return;
1800                                 }
1801                         }
1802                 }
1803                 else
1804                 {
1805                         // Case 1: Country code
1806                         if(IS_COUNTRY_IE_VALID(ieee) )
1807                         {
1808                                 if( !IsLegalChannel(ieee, network.channel) )
1809                                 {
1810                                         printk("GetScanInfo(): For Country code, filter beacon at channel(%d).\n",network.channel);
1811                                         return;
1812                                 }
1813                         }
1814                         // Case 2: No any country code.
1815                         else
1816                         {
1817                                 // Filter over channel ch12~14
1818                                 if(network.channel > 14)
1819                                 {
1820                                         printk("GetScanInfo(): For Global Domain, filter beacon at channel(%d).\n",network.channel);
1821                                         return;
1822                                 }
1823                         }
1824                 }
1825         }
1826 #endif
1827         /* The network parsed correctly -- so now we scan our known networks
1828          * to see if we can find it in our list.
1829          *
1830          * NOTE:  This search is definitely not optimized.  Once its doing
1831          *        the "right thing" we'll optimize it for efficiency if
1832          *        necessary */
1833 
1834         /* Search for this entry in the list and update it if it is
1835          * already there. */
1836 
1837         spin_lock_irqsave(&ieee->lock, flags);
1838 
1839         if(is_same_network(&ieee->current_network, &network, ieee)) {
1840                 wmm_info = ieee->current_network.wmm_info;
1841                 //YJ,add,080819,for hidden ap
1842                 if(is_beacon == 0)
1843                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & ieee->current_network.flags);
1844                 else if(ieee->state == IEEE80211_LINKED)
1845                         ieee->NumRxBcnInPeriod++;
1846                 //YJ,add,080819,for hidden ap,end
1847                 //printk("====>network.ssid=%s cur_ssid=%s\n", network.ssid, ieee->current_network.ssid);
1848                 update_network(&ieee->current_network, &network);
1849         }
1850 
1851         list_for_each_entry(target, &ieee->network_list, list) {
1852                 if (is_same_network(target, &network, ieee))
1853                         break;
1854                 if ((oldest == NULL) ||
1855                     (target->last_scanned < oldest->last_scanned))
1856                         oldest = target;
1857         }
1858 
1859         /* If we didn't find a match, then get a new network slot to initialize
1860          * with this beacon's information */
1861         if (&target->list == &ieee->network_list) {
1862                 if (list_empty(&ieee->network_free_list)) {
1863                         /* If there are no more slots, expire the oldest */
1864                         list_del(&oldest->list);
1865                         target = oldest;
1866                         IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1867                                              "network list.\n",
1868                                              escape_essid(target->ssid,
1869                                                           target->ssid_len),
1870                                              MAC_ARG(target->bssid));
1871                 } else {
1872                         /* Otherwise just pull from the free list */
1873                         target = list_entry(ieee->network_free_list.next,
1874                                             struct ieee80211_network, list);
1875                         list_del(ieee->network_free_list.next);
1876                 }
1877 
1878 
1879 #ifdef CONFIG_IEEE80211_DEBUG
1880                 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1881                                      escape_essid(network.ssid,
1882                                                   network.ssid_len),
1883                                      MAC_ARG(network.bssid),
1884                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1885                                      IEEE80211_STYPE_PROBE_RESP ?
1886                                      "PROBE RESPONSE" : "BEACON");
1887 #endif
1888 
1889 #ifdef _RTL8187_EXT_PATCH_
1890         network.ext_entry = target->ext_entry;
1891 #endif
1892                 memcpy(target, &network, sizeof(*target));
1893                 list_add_tail(&target->list, &ieee->network_list);
1894                 if(ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
1895                         ieee80211_softmac_new_net(ieee,&network);
1896         } else {
1897                 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1898                                      escape_essid(target->ssid,
1899                                                   target->ssid_len),
1900                                      MAC_ARG(target->bssid),
1901                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1902                                      IEEE80211_STYPE_PROBE_RESP ?
1903                                      "PROBE RESPONSE" : "BEACON");
1904 
1905                 /* we have an entry and we are going to update it. But this entry may
1906                  * be already expired. In this case we do the same as we found a new
1907                  * net and call the new_net handler
1908                  */
1909                 renew = !time_after(target->last_scanned + ieee->scan_age, jiffies);
1910                 //YJ,add,080819,for hidden ap
1911                 if(is_beacon == 0)
1912                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & target->flags);
1913                 //if(strncmp(network.ssid, "linksys-c",9) == 0)
1914                 //      printk("====>2 network.ssid=%s FLAG=%d target.ssid=%s FLAG=%d\n", network.ssid, network.flags, target->ssid, target->flags);
1915                 if(((network.flags & NETWORK_EMPTY_ESSID) == NETWORK_EMPTY_ESSID) \
1916                     && (((network.ssid_len > 0) && (strncmp(target->ssid, network.ssid, network.ssid_len)))\
1917                     ||((ieee->current_network.ssid_len == network.ssid_len)&&(strncmp(ieee->current_network.ssid, network.ssid, network.ssid_len) == 0)&&(ieee->state == IEEE80211_NOLINK))))
1918                         renew = 1;
1919                 //YJ,add,080819,for hidden ap,end
1920                 update_network(target, &network);
1921                 if(renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
1922                         ieee80211_softmac_new_net(ieee,&network);
1923         }
1924 
1925         spin_unlock_irqrestore(&ieee->lock, flags);
1926 }
1927 
1928 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1929                       struct ieee80211_hdr *header,
1930                       struct ieee80211_rx_stats *stats)
1931 {
1932         switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
1933 
1934         case IEEE80211_STYPE_BEACON:
1935                 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1936                                      WLAN_FC_GET_STYPE(header->frame_ctl));
1937                 IEEE80211_DEBUG_SCAN("Beacon\n");
1938                 ieee80211_process_probe_response(
1939                         ieee, (struct ieee80211_probe_response *)header, stats);
1940                 break;
1941 
1942         case IEEE80211_STYPE_PROBE_RESP:
1943                 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1944                                      WLAN_FC_GET_STYPE(header->frame_ctl));
1945                 IEEE80211_DEBUG_SCAN("Probe response\n");
1946                 ieee80211_process_probe_response(
1947                         ieee, (struct ieee80211_probe_response *)header, stats);
1948                 break;
1949 //rz
1950 #ifdef _RTL8187_EXT_PATCH_
1951         case IEEE80211_STYPE_PROBE_REQ:
1952                 IEEE80211_DEBUG_MGMT("received PROBE REQUEST (%d)\n",
1953                                      WLAN_FC_GET_STYPE(header->frame_ctl));
1954                 IEEE80211_DEBUG_SCAN("Probe request\n");
1955                 ///
1956                 if( ieee->iw_mode == ieee->iw_ext_mode && ieee->ext_patch_ieee80211_rx_mgt_on_probe_req )
1957                         ieee->ext_patch_ieee80211_rx_mgt_on_probe_req( ieee, (struct ieee80211_probe_request *)header, stats);
1958                 break;
1959 #endif // _RTL8187_EXT_PATCH_
1960 
1961         }
1962 }
1963 
1964 #if 0
1965 EXPORT_SYMBOL(ieee80211_rx_mgt);
1966 EXPORT_SYMBOL(ieee80211_rx);
1967 EXPORT_SYMBOL(ieee80211_network_init);
1968 #ifdef _RTL8187_EXT_PATCH_
1969 EXPORT_SYMBOL(ieee_ext_skb_p80211_to_ether);
1970 #endif
1971 #endif
1972 
  This page was automatically generated by the LXR engine.