]> Pileus Git - ~andy/linux/blob - drivers/staging/rtl8192e/rtllib_rx.c
96f824ba81292572f4aded91202b34e27fdb8e10
[~andy/linux] / drivers / staging / rtl8192e / rtllib_rx.c
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/errno.h>
26 #include <linux/if_arp.h>
27 #include <linux/in6.h>
28 #include <linux/in.h>
29 #include <linux/ip.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/pci.h>
34 #include <linux/proc_fs.h>
35 #include <linux/skbuff.h>
36 #include <linux/slab.h>
37 #include <linux/tcp.h>
38 #include <linux/types.h>
39 #include <linux/version.h>
40 #include <linux/wireless.h>
41 #include <linux/etherdevice.h>
42 #include <asm/uaccess.h>
43 #include <linux/ctype.h>
44
45 #include "rtllib.h"
46 #ifdef ENABLE_DOT11D
47 #include "dot11d.h"
48 #endif
49
50 #if defined CONFIG_CFG_80211
51 #include <linux/crc32.h>
52
53 struct ieee80211_channel *rtllib_get_channel(struct wiphy *wiphy,
54                                                   int freq)
55 {
56         enum ieee80211_band band;
57         struct ieee80211_supported_band *sband;
58         int i;
59
60         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
61                 sband = wiphy->bands[band];
62
63                 if (!sband)
64                         continue;
65
66                 for (i = 0; i < sband->n_channels; i++) {
67                         if (sband->channels[i].center_freq == freq)
68                                 return &sband->channels[i];
69                 }
70         }
71
72         return NULL;
73 }
74
75 int rtllib_channel_to_frequency(int chan)
76 {
77         if (chan < 14)
78                 return 2407 + chan * 5;
79
80         if (chan == 14)
81                 return 2484;
82
83         /* FIXME: 802.11j 17.3.8.3.2 */
84         return (chan + 1000) * 5;
85 }
86
87 u32 rtllib_parse_elems_crc(u8 *start, size_t len,
88                                struct ieee802_11_elems *elems,
89                                u64 filter, u32 crc)
90 {
91         size_t left = len;
92         u8 *pos = start;
93         bool calc_crc = filter != 0;
94
95         memset(elems, 0, sizeof(*elems));
96         elems->ie_start = start;
97         elems->total_len = len;
98
99         while (left >= 2) {
100                 u8 id, elen;
101
102                 id = *pos++;
103                 elen = *pos++;
104                 left -= 2;
105
106                 if (elen > left)
107                         break;
108
109                 if (calc_crc && id < 64 && (filter & BIT(id)))
110                         crc = crc32_be(crc, pos - 2, elen + 2);
111
112                 switch (id) {
113                 case WLAN_EID_SSID:
114                         elems->ssid = pos;
115                         elems->ssid_len = elen;
116                         break;
117                 case WLAN_EID_SUPP_RATES:
118                         elems->supp_rates = pos;
119                         elems->supp_rates_len = elen;
120                         break;
121                 case WLAN_EID_FH_PARAMS:
122                         elems->fh_params = pos;
123                         elems->fh_params_len = elen;
124                         break;
125                 case WLAN_EID_DS_PARAMS:
126                         elems->ds_params = pos;
127                         elems->ds_params_len = elen;
128                         break;
129                 case WLAN_EID_CF_PARAMS:
130                         elems->cf_params = pos;
131                         elems->cf_params_len = elen;
132                         break;
133                 case WLAN_EID_TIM:
134                         if (elen >= sizeof(struct ieee80211_tim_ie)) {
135                                 elems->tim = (void *)pos;
136                                 elems->tim_len = elen;
137                         }
138                         break;
139                 case WLAN_EID_IBSS_PARAMS:
140                         elems->ibss_params = pos;
141                         elems->ibss_params_len = elen;
142                         break;
143                 case WLAN_EID_CHALLENGE:
144                         elems->challenge = pos;
145                         elems->challenge_len = elen;
146                         break;
147                 case WLAN_EID_VENDOR_SPECIFIC:
148                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
149                             pos[2] == 0xf2) {
150                                 /* Microsoft OUI (00:50:F2) */
151
152                                 if (calc_crc)
153                                         crc = crc32_be(crc, pos - 2, elen + 2);
154
155                                 if (pos[3] == 1) {
156                                         /* OUI Type 1 - WPA IE */
157                                         elems->wpa = pos;
158                                         elems->wpa_len = elen;
159                                 } else if (elen >= 5 && pos[3] == 2) {
160                                         /* OUI Type 2 - WMM IE */
161                                         if (pos[4] == 0) {
162                                                 elems->wmm_info = pos;
163                                                 elems->wmm_info_len = elen;
164                                         } else if (pos[4] == 1) {
165                                                 elems->wmm_param = pos;
166                                                 elems->wmm_param_len = elen;
167                                         }
168                                 }
169                         }
170                         break;
171                 case WLAN_EID_RSN:
172                         elems->rsn = pos;
173                         elems->rsn_len = elen;
174                         break;
175                 case WLAN_EID_ERP_INFO:
176                         elems->erp_info = pos;
177                         elems->erp_info_len = elen;
178                         break;
179                 case WLAN_EID_EXT_SUPP_RATES:
180                         elems->ext_supp_rates = pos;
181                         elems->ext_supp_rates_len = elen;
182                         break;
183                 case WLAN_EID_HT_CAPABILITY:
184                         if (elen >= sizeof(struct ieee80211_ht_cap))
185                                 elems->ht_cap_elem = (void *)pos;
186                         break;
187                 case WLAN_EID_HT_INFORMATION:
188                         if (elen >= sizeof(struct ieee80211_ht_info))
189                                 elems->ht_info_elem = (void *)pos;
190                         break;
191                 case WLAN_EID_MESH_ID:
192                         elems->mesh_id = pos;
193                         elems->mesh_id_len = elen;
194                         break;
195                 case WLAN_EID_MESH_CONFIG:
196                         elems->mesh_config = pos;
197                         elems->mesh_config_len = elen;
198                         break;
199                 case WLAN_EID_PEER_LINK:
200                         elems->peer_link = pos;
201                         elems->peer_link_len = elen;
202                         break;
203                 case WLAN_EID_PREQ:
204                         elems->preq = pos;
205                         elems->preq_len = elen;
206                         break;
207                 case WLAN_EID_PREP:
208                         elems->prep = pos;
209                         elems->prep_len = elen;
210                         break;
211                 case WLAN_EID_PERR:
212                         elems->perr = pos;
213                         elems->perr_len = elen;
214                         break;
215                 case WLAN_EID_CHANNEL_SWITCH:
216                         elems->ch_switch_elem = pos;
217                         elems->ch_switch_elem_len = elen;
218                         break;
219                 case WLAN_EID_QUIET:
220                         if (!elems->quiet_elem) {
221                                 elems->quiet_elem = pos;
222                                 elems->quiet_elem_len = elen;
223                         }
224                         elems->num_of_quiet_elem++;
225                         break;
226                 case WLAN_EID_COUNTRY:
227                         elems->country_elem = pos;
228                         elems->country_elem_len = elen;
229                         break;
230                 case WLAN_EID_PWR_CONSTRAINT:
231                         elems->pwr_constr_elem = pos;
232                         elems->pwr_constr_elem_len = elen;
233                         break;
234                 case WLAN_EID_TIMEOUT_INTERVAL:
235                         elems->timeout_int = pos;
236                         elems->timeout_int_len = elen;
237                         break;
238                 default:
239                         break;
240                 }
241
242                 left -= elen;
243                 pos += elen;
244         }
245
246         return crc;
247 }
248
249 void rtllib_parse_elems(u8 *start, size_t len,
250                             struct ieee802_11_elems *elems)
251 {
252         rtllib_parse_elems_crc(start, len, elems, 0, 0);
253 }
254
255 void ieee80211_scan_rx(struct rtllib_device *ieee, struct sk_buff *skb, struct rtllib_rx_stats *rx_status)
256 {
257         struct rtllib_hdr_4addr *header = (struct rtllib_hdr_4addr *)skb->data ;
258         struct ieee80211_mgmt *mgmt;
259         struct ieee80211_bss *bss;
260         u8 *elements;
261         struct ieee80211_channel *channel;
262         size_t baselen;
263         int freq;
264         __le16 fc;
265         bool presp, beacon = false;
266         struct ieee802_11_elems elems;
267         s32 signal = 0;
268
269         if (skb->len < 2)
270                 return;
271
272         mgmt = (struct ieee80211_mgmt *) skb->data;
273         fc = mgmt->frame_control;
274
275         if (skb->len < 24)
276                 return;
277
278         presp = (WLAN_FC_GET_STYPE(header->frame_ctl) == RTLLIB_STYPE_PROBE_RESP);
279         if (presp) {
280                 /* ignore ProbeResp to foreign address */
281                 if (memcmp(mgmt->da, ieee->dev->dev_addr, ETH_ALEN))
282                         return ;;
283
284                 presp = true;
285                 elements = mgmt->u.probe_resp.variable;
286                 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
287         } else {
288                 beacon =  (WLAN_FC_GET_STYPE(header->frame_ctl) == RTLLIB_STYPE_BEACON);
289                 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
290                 elements = mgmt->u.beacon.variable;
291         }
292
293         if (!presp && !beacon)
294                 return;
295
296         if (baselen > skb->len)
297                 return;
298
299         rtllib_parse_elems(elements, skb->len - baselen, &elems);
300
301         if (elems.ds_params && elems.ds_params_len == 1)
302                 freq = rtllib_channel_to_frequency(elems.ds_params[0]);
303         else
304                 return;
305
306         channel = rtllib_get_channel(ieee->wdev.wiphy, freq);
307
308         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
309                 return;
310
311                 signal = rx_status->signal * 100;
312
313         bss = (void *)cfg80211_inform_bss_frame(ieee->wdev.wiphy, channel,
314                                                 mgmt, skb->len, signal, GFP_ATOMIC);
315
316         return;
317 }
318 #endif
319
320 static inline void rtllib_monitor_rx(struct rtllib_device *ieee,
321                                 struct sk_buff *skb,struct rtllib_rx_stats *rx_status,
322                                 size_t hdr_length)
323 {
324         skb->dev = ieee->dev;
325         skb_reset_mac_header(skb);
326         skb_pull(skb, hdr_length);
327         skb->pkt_type = PACKET_OTHERHOST;
328         skb->protocol = __constant_htons(ETH_P_80211_RAW);
329         memset(skb->cb, 0, sizeof(skb->cb));
330         netif_rx(skb);
331 }
332
333 /* Called only as a tasklet (software IRQ) */
334 static struct rtllib_frag_entry *
335 rtllib_frag_cache_find(struct rtllib_device *ieee, unsigned int seq,
336                           unsigned int frag, u8 tid,u8 *src, u8 *dst)
337 {
338         struct rtllib_frag_entry *entry;
339         int i;
340
341         for (i = 0; i < RTLLIB_FRAG_CACHE_LEN; i++) {
342                 entry = &ieee->frag_cache[tid][i];
343                 if (entry->skb != NULL &&
344                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
345                         RTLLIB_DEBUG_FRAG(
346                                 "expiring fragment cache entry "
347                                 "seq=%u last_frag=%u\n",
348                                 entry->seq, entry->last_frag);
349                         dev_kfree_skb_any(entry->skb);
350                         entry->skb = NULL;
351                 }
352
353                 if (entry->skb != NULL && entry->seq == seq &&
354                     (entry->last_frag + 1 == frag || frag == -1) &&
355                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
356                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
357                         return entry;
358         }
359
360         return NULL;
361 }
362
363 /* Called only as a tasklet (software IRQ) */
364 static struct sk_buff *
365 rtllib_frag_cache_get(struct rtllib_device *ieee,
366                          struct rtllib_hdr_4addr *hdr)
367 {
368         struct sk_buff *skb = NULL;
369         u16 fc = le16_to_cpu(hdr->frame_ctl);
370         u16 sc = le16_to_cpu(hdr->seq_ctl);
371         unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
372         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
373         struct rtllib_frag_entry *entry;
374         struct rtllib_hdr_3addrqos *hdr_3addrqos;
375         struct rtllib_hdr_4addrqos *hdr_4addrqos;
376         u8 tid;
377
378         if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS)&&RTLLIB_QOS_HAS_SEQ(fc)) {
379           hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
380           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
381           tid = UP2AC(tid);
382           tid ++;
383         } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
384           hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
385           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
386           tid = UP2AC(tid);
387           tid ++;
388         } else {
389           tid = 0;
390         }
391
392         if (frag == 0) {
393                 /* Reserve enough space to fit maximum frame length */
394                 skb = dev_alloc_skb(ieee->dev->mtu +
395                                     sizeof(struct rtllib_hdr_4addr) +
396                                     8 /* LLC */ +
397                                     2 /* alignment */ +
398                                     8 /* WEP */ +
399                                     ETH_ALEN /* WDS */ +
400                                     (RTLLIB_QOS_HAS_SEQ(fc)?2:0) /* QOS Control */);
401                 if (skb == NULL)
402                         return NULL;
403
404                 entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
405                 ieee->frag_next_idx[tid]++;
406                 if (ieee->frag_next_idx[tid] >= RTLLIB_FRAG_CACHE_LEN)
407                         ieee->frag_next_idx[tid] = 0;
408
409                 if (entry->skb != NULL)
410                         dev_kfree_skb_any(entry->skb);
411
412                 entry->first_frag_time = jiffies;
413                 entry->seq = seq;
414                 entry->last_frag = frag;
415                 entry->skb = skb;
416                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
417                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
418         } else {
419                 /* received a fragment of a frame for which the head fragment
420                  * should have already been received */
421                 entry = rtllib_frag_cache_find(ieee, seq, frag, tid,hdr->addr2,
422                                                   hdr->addr1);
423                 if (entry != NULL) {
424                         entry->last_frag = frag;
425                         skb = entry->skb;
426                 }
427         }
428
429         return skb;
430 }
431
432
433 /* Called only as a tasklet (software IRQ) */
434 static int rtllib_frag_cache_invalidate(struct rtllib_device *ieee,
435                                            struct rtllib_hdr_4addr *hdr)
436 {
437         u16 fc = le16_to_cpu(hdr->frame_ctl);
438         u16 sc = le16_to_cpu(hdr->seq_ctl);
439         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
440         struct rtllib_frag_entry *entry;
441         struct rtllib_hdr_3addrqos *hdr_3addrqos;
442         struct rtllib_hdr_4addrqos *hdr_4addrqos;
443         u8 tid;
444
445         if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS)&&RTLLIB_QOS_HAS_SEQ(fc)) {
446           hdr_4addrqos = (struct rtllib_hdr_4addrqos *)hdr;
447           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
448           tid = UP2AC(tid);
449           tid ++;
450         } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
451           hdr_3addrqos = (struct rtllib_hdr_3addrqos *)hdr;
452           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
453           tid = UP2AC(tid);
454           tid ++;
455         } else {
456           tid = 0;
457         }
458
459         entry = rtllib_frag_cache_find(ieee, seq, -1, tid,hdr->addr2,
460                                           hdr->addr1);
461
462         if (entry == NULL) {
463                 RTLLIB_DEBUG_FRAG(
464                         "could not invalidate fragment cache "
465                         "entry (seq=%u)\n", seq);
466                 return -1;
467         }
468
469         entry->skb = NULL;
470         return 0;
471 }
472
473
474
475 /* rtllib_rx_frame_mgtmt
476  *
477  * Responsible for handling management control frames
478  *
479  * Called by rtllib_rx */
480 static inline int
481 rtllib_rx_frame_mgmt(struct rtllib_device *ieee, struct sk_buff *skb,
482                         struct rtllib_rx_stats *rx_stats, u16 type,
483                         u16 stype)
484 {
485         /* On the struct stats definition there is written that
486          * this is not mandatory.... but seems that the probe
487          * response parser uses it
488          */
489         struct rtllib_hdr_3addr * hdr = (struct rtllib_hdr_3addr *)skb->data;
490
491         rx_stats->len = skb->len;
492         rtllib_rx_mgt(ieee,skb,rx_stats);
493         if ((memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN))) {
494                 dev_kfree_skb_any(skb);
495                 return 0;
496         }
497         rtllib_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
498
499         dev_kfree_skb_any(skb);
500
501         return 0;
502
503 #ifdef NOT_YET
504         if (ieee->iw_mode == IW_MODE_MASTER) {
505                 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
506                        ieee->dev->name);
507                 return 0;
508 /*
509   hostap_update_sta_ps(ieee, (struct hostap_rtllib_hdr_4addr *)
510   skb->data);*/
511         }
512
513         if (ieee->hostapd && type == RTLLIB_TYPE_MGMT) {
514                 if (stype == WLAN_FC_STYPE_BEACON &&
515                     ieee->iw_mode == IW_MODE_MASTER) {
516                         struct sk_buff *skb2;
517                         /* Process beacon frames also in kernel driver to
518                          * update STA(AP) table statistics */
519                         skb2 = skb_clone(skb, GFP_ATOMIC);
520                         if (skb2)
521                                 hostap_rx(skb2->dev, skb2, rx_stats);
522                 }
523
524                 /* send management frames to the user space daemon for
525                  * processing */
526                 ieee->apdevstats.rx_packets++;
527                 ieee->apdevstats.rx_bytes += skb->len;
528                 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
529                 return 0;
530         }
531
532             if (ieee->iw_mode == IW_MODE_MASTER) {
533                 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
534                         printk(KERN_DEBUG "%s: unknown management frame "
535                                "(type=0x%02x, stype=0x%02x) dropped\n",
536                                skb->dev->name, type, stype);
537                         return -1;
538                 }
539
540                 hostap_rx(skb->dev, skb, rx_stats);
541                 return 0;
542         }
543
544         printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
545                "received in non-Host AP mode\n", skb->dev->name);
546         return -1;
547 #endif
548 }
549
550 #ifndef CONFIG_CFG_80211
551 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
552 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
553 static unsigned char rfc1042_header[] =
554 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
555 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
556 static unsigned char bridge_tunnel_header[] =
557 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
558 /* No encapsulation header if EtherType < 0x600 (=length) */
559 #endif
560
561 /* Called by rtllib_rx_frame_decrypt */
562 static int rtllib_is_eapol_frame(struct rtllib_device *ieee,
563                                     struct sk_buff *skb, size_t hdrlen)
564 {
565         struct net_device *dev = ieee->dev;
566         u16 fc, ethertype;
567         struct rtllib_hdr_4addr *hdr;
568         u8 *pos;
569
570         if (skb->len < 24)
571                 return 0;
572
573         hdr = (struct rtllib_hdr_4addr *) skb->data;
574         fc = le16_to_cpu(hdr->frame_ctl);
575
576         /* check that the frame is unicast frame to us */
577         if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
578             RTLLIB_FCTL_TODS &&
579             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
580             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
581                 /* ToDS frame with own addr BSSID and DA */
582         } else if ((fc & (RTLLIB_FCTL_TODS | RTLLIB_FCTL_FROMDS)) ==
583                    RTLLIB_FCTL_FROMDS &&
584                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
585                 /* FromDS frame with own addr as DA */
586         } else
587                 return 0;
588
589         if (skb->len < 24 + 8)
590                 return 0;
591
592         /* check for port access entity Ethernet type */
593         pos = skb->data + hdrlen;
594         ethertype = (pos[6] << 8) | pos[7];
595         if (ethertype == ETH_P_PAE)
596                 return 1;
597
598         return 0;
599 }
600
601 /* Called only as a tasklet (software IRQ), by rtllib_rx */
602 static inline int
603 rtllib_rx_frame_decrypt(struct rtllib_device* ieee, struct sk_buff *skb,
604                            struct rtllib_crypt_data *crypt)
605 {
606         struct rtllib_hdr_4addr *hdr;
607         int res, hdrlen;
608
609         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
610                 return 0;
611
612         if (ieee->hwsec_active)
613         {
614                 cb_desc *tcb_desc = (cb_desc *)(skb->cb+ MAX_DEV_ADDR_SIZE);
615                 tcb_desc->bHwSec = 1;
616
617                 if (ieee->need_sw_enc)
618                         tcb_desc->bHwSec = 0;
619         }
620
621         hdr = (struct rtllib_hdr_4addr *) skb->data;
622         hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
623
624 #ifdef CONFIG_RTLLIB_CRYPT_TKIP
625         if (ieee->tkip_countermeasures &&
626             strcmp(crypt->ops->name, "TKIP") == 0) {
627                 if (net_ratelimit()) {
628                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
629                                "received packet from " MAC_FMT "\n",
630                                ieee->dev->name, MAC_ARG(hdr->addr2));
631                 }
632                 return -1;
633         }
634 #endif
635
636         atomic_inc(&crypt->refcnt);
637         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
638         atomic_dec(&crypt->refcnt);
639         if (res < 0) {
640                 RTLLIB_DEBUG_DROP(
641                         "decryption failed (SA=" MAC_FMT
642                         ") res=%d\n", MAC_ARG(hdr->addr2), res);
643                 if (res == -2)
644                         RTLLIB_DEBUG_DROP("Decryption failed ICV "
645                                              "mismatch (key %d)\n",
646                                              skb->data[hdrlen + 3] >> 6);
647                 ieee->ieee_stats.rx_discards_undecryptable++;
648                 return -1;
649         }
650
651         return res;
652 }
653
654
655 /* Called only as a tasklet (software IRQ), by rtllib_rx */
656 static inline int
657 rtllib_rx_frame_decrypt_msdu(struct rtllib_device* ieee, struct sk_buff *skb,
658                              int keyidx, struct rtllib_crypt_data *crypt)
659 {
660         struct rtllib_hdr_4addr *hdr;
661         int res, hdrlen;
662
663         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
664                 return 0;
665         if (ieee->hwsec_active)
666         {
667                 cb_desc *tcb_desc = (cb_desc *)(skb->cb+ MAX_DEV_ADDR_SIZE);
668                 tcb_desc->bHwSec = 1;
669
670                 if (ieee->need_sw_enc)
671                         tcb_desc->bHwSec = 0;
672         }
673
674         hdr = (struct rtllib_hdr_4addr *) skb->data;
675         hdrlen = rtllib_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
676
677         atomic_inc(&crypt->refcnt);
678         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv,ieee);
679         atomic_dec(&crypt->refcnt);
680         if (res < 0) {
681                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
682                        " (SA=" MAC_FMT " keyidx=%d)\n",
683                        ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
684                 return -1;
685         }
686
687         return 0;
688 }
689
690
691 /* this function is stolen from ipw2200 driver*/
692 #define IEEE_PACKET_RETRY_TIME (5*HZ)
693 static int is_duplicate_packet(struct rtllib_device *ieee,
694                                       struct rtllib_hdr_4addr *header)
695 {
696         u16 fc = le16_to_cpu(header->frame_ctl);
697         u16 sc = le16_to_cpu(header->seq_ctl);
698         u16 seq = WLAN_GET_SEQ_SEQ(sc);
699         u16 frag = WLAN_GET_SEQ_FRAG(sc);
700         u16 *last_seq, *last_frag;
701         unsigned long *last_time;
702         struct rtllib_hdr_3addrqos *hdr_3addrqos;
703         struct rtllib_hdr_4addrqos *hdr_4addrqos;
704         u8 tid;
705
706         if (((fc & RTLLIB_FCTL_DSTODS) == RTLLIB_FCTL_DSTODS)&&RTLLIB_QOS_HAS_SEQ(fc)) {
707           hdr_4addrqos = (struct rtllib_hdr_4addrqos *)header;
708           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & RTLLIB_QCTL_TID;
709           tid = UP2AC(tid);
710           tid ++;
711         } else if (RTLLIB_QOS_HAS_SEQ(fc)) {
712           hdr_3addrqos = (struct rtllib_hdr_3addrqos*)header;
713           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & RTLLIB_QCTL_TID;
714           tid = UP2AC(tid);
715           tid ++;
716         } else {
717           tid = 0;
718         }
719
720         switch (ieee->iw_mode) {
721         case IW_MODE_ADHOC:
722         {
723                 struct list_head *p;
724                 struct ieee_ibss_seq *entry = NULL;
725                 u8 *mac = header->addr2;
726                 int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
727                 list_for_each(p, &ieee->ibss_mac_hash[index]) {
728                         entry = list_entry(p, struct ieee_ibss_seq, list);
729                         if (!memcmp(entry->mac, mac, ETH_ALEN))
730                                 break;
731                 }
732                 if (p == &ieee->ibss_mac_hash[index]) {
733                         entry = kmalloc(sizeof(struct ieee_ibss_seq), GFP_ATOMIC);
734                         if (!entry) {
735                                 printk(KERN_WARNING "Cannot malloc new mac entry\n");
736                                 return 0;
737                         }
738                         memcpy(entry->mac, mac, ETH_ALEN);
739                         entry->seq_num[tid] = seq;
740                         entry->frag_num[tid] = frag;
741                         entry->packet_time[tid] = jiffies;
742                         list_add(&entry->list, &ieee->ibss_mac_hash[index]);
743                         return 0;
744                 }
745                 last_seq = &entry->seq_num[tid];
746                 last_frag = &entry->frag_num[tid];
747                 last_time = &entry->packet_time[tid];
748                 break;
749         }
750
751         case IW_MODE_INFRA:
752                 last_seq = &ieee->last_rxseq_num[tid];
753                 last_frag = &ieee->last_rxfrag_num[tid];
754                 last_time = &ieee->last_packet_time[tid];
755                 break;
756         default:
757                 return 0;
758         }
759
760         if ((*last_seq == seq) &&
761             time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
762                 if (*last_frag == frag){
763                         goto drop;
764
765                 }
766                 if (*last_frag + 1 != frag)
767                         /* out-of-order fragment */
768                         goto drop;
769         } else
770                 *last_seq = seq;
771
772         *last_frag = frag;
773         *last_time = jiffies;
774         return 0;
775
776 drop:
777
778         return 1;
779 }
780 bool
781 AddReorderEntry(
782         PRX_TS_RECORD                   pTS,
783         PRX_REORDER_ENTRY               pReorderEntry
784         )
785 {
786         struct list_head *pList = &pTS->RxPendingPktList;
787
788         while(pList->next != &pTS->RxPendingPktList)
789         {
790                 if ( SN_LESS(pReorderEntry->SeqNum, ((PRX_REORDER_ENTRY)list_entry(pList->next,RX_REORDER_ENTRY,List))->SeqNum) )
791                 {
792                         pList = pList->next;
793                 }
794                 else if ( SN_EQUAL(pReorderEntry->SeqNum, ((PRX_REORDER_ENTRY)list_entry(pList->next,RX_REORDER_ENTRY,List))->SeqNum) )
795                 {
796                         return false;
797                 }
798                 else
799                 {
800                         break;
801                 }
802         }
803         pReorderEntry->List.next = pList->next;
804         pReorderEntry->List.next->prev = &pReorderEntry->List;
805         pReorderEntry->List.prev = pList;
806         pList->next = &pReorderEntry->List;
807
808         return true;
809 }
810
811 void rtllib_indicate_packets(struct rtllib_device *ieee, struct rtllib_rxb** prxbIndicateArray,u8  index)
812 {
813         struct net_device_stats *stats = &ieee->stats;
814         u8 i = 0 , j=0;
815         u16 ethertype;
816         for (j = 0; j < index; j++) {
817                 struct rtllib_rxb* prxb = prxbIndicateArray[j];
818                 for (i = 0; i<prxb->nr_subframes; i++) {
819                         struct sk_buff *sub_skb = prxb->subframes[i];
820
821                 /* convert hdr + possible LLC headers into Ethernet header */
822                         ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
823                         if (sub_skb->len >= 8 &&
824                                 ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
825                                   ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
826                                  memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
827                         /* remove RFC1042 or Bridge-Tunnel encapsulation and
828                          * replace EtherType */
829                                 skb_pull(sub_skb, SNAP_SIZE);
830                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
831                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
832                         } else {
833                                 u16 len;
834                         /* Leave Ethernet header part of hdr and full payload */
835                                 len = htons(sub_skb->len);
836                                 memcpy(skb_push(sub_skb, 2), &len, 2);
837                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
838                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
839                         }
840
841                 /* Indicat the packets to upper layer */
842                         if (sub_skb) {
843                                 stats->rx_packets++;
844                                 stats->rx_bytes += sub_skb->len;
845
846                                 memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
847                                 sub_skb->protocol = eth_type_trans(sub_skb, ieee->dev);
848                                 sub_skb->dev = ieee->dev;
849                                 sub_skb->dev->stats.rx_packets++;
850                                 sub_skb->dev->stats.rx_bytes += sub_skb->len;
851 #ifdef TCP_CSUM_OFFLOAD_RX
852                                 if ( prxb->tcp_csum_valid)
853                                         sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
854                                 else
855                                         sub_skb->ip_summed = CHECKSUM_NONE;
856
857 #else
858                                 sub_skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
859 #endif
860                                 ieee->last_rx_ps_time = jiffies;
861                                 netif_rx(sub_skb);
862                         }
863                 }
864                 kfree(prxb);
865                 prxb = NULL;
866         }
867 }
868
869 void
870 rtllib_FlushRxTsPendingPkts(struct rtllib_device *ieee, PRX_TS_RECORD pTS)
871 {
872         PRX_REORDER_ENTRY       pRxReorderEntry;
873         struct rtllib_rxb*              RfdArray[REORDER_WIN_SIZE];
874         u8                                      RfdCnt = 0;
875
876
877         del_timer_sync(&pTS->RxPktPendingTimer);
878         while(!list_empty(&pTS->RxPendingPktList))
879         {
880                 if (RfdCnt >= REORDER_WIN_SIZE){
881                         printk("-------------->%s() error! RfdCnt >= REORDER_WIN_SIZE\n", __func__);
882                         break;
883                 }
884
885                 pRxReorderEntry = (PRX_REORDER_ENTRY)list_entry(pTS->RxPendingPktList.prev,RX_REORDER_ENTRY,List);
886                 RTLLIB_DEBUG(RTLLIB_DL_REORDER,"%s(): Indicate SeqNum %d!\n",__func__, pRxReorderEntry->SeqNum);
887                 list_del_init(&pRxReorderEntry->List);
888
889                 RfdArray[RfdCnt] = pRxReorderEntry->prxb;
890
891                 RfdCnt = RfdCnt + 1;
892                 list_add_tail(&pRxReorderEntry->List, &ieee->RxReorder_Unused_List);
893         }
894         rtllib_indicate_packets(ieee, RfdArray, RfdCnt);
895
896         pTS->RxIndicateSeq = 0xffff;
897
898 #ifdef MERGE_TO_DO
899 #endif
900 }
901
902
903 void RxReorderIndicatePacket( struct rtllib_device *ieee,
904                 struct rtllib_rxb* prxb,
905                 PRX_TS_RECORD           pTS,
906                 u16                     SeqNum)
907 {
908         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
909         PRX_REORDER_ENTRY       pReorderEntry = NULL;
910         struct rtllib_rxb* prxbIndicateArray[REORDER_WIN_SIZE];
911         u8                      WinSize = pHTInfo->RxReorderWinSize;
912         u16                     WinEnd = 0;
913         u8                      index = 0;
914         bool                    bMatchWinStart = false, bPktInBuf = false;
915         unsigned long flags;
916
917         RTLLIB_DEBUG(RTLLIB_DL_REORDER,"%s(): Seq is %d,pTS->RxIndicateSeq is %d, WinSize is %d\n",__func__,SeqNum,pTS->RxIndicateSeq,WinSize);
918
919         spin_lock_irqsave(&(ieee->reorder_spinlock), flags);
920
921         WinEnd = (pTS->RxIndicateSeq + WinSize -1)%4096;
922         /* Rx Reorder initialize condition.*/
923         if (pTS->RxIndicateSeq == 0xffff) {
924                 pTS->RxIndicateSeq = SeqNum;
925         }
926
927         /* Drop out the packet which SeqNum is smaller than WinStart */
928         if (SN_LESS(SeqNum, pTS->RxIndicateSeq)) {
929                 RTLLIB_DEBUG(RTLLIB_DL_REORDER,"Packet Drop! IndicateSeq: %d, NewSeq: %d\n",
930                                  pTS->RxIndicateSeq, SeqNum);
931                 pHTInfo->RxReorderDropCounter++;
932                 {
933                         int i;
934                         for (i =0; i < prxb->nr_subframes; i++) {
935                                 dev_kfree_skb(prxb->subframes[i]);
936                         }
937                         kfree(prxb);
938                         prxb = NULL;
939                 }
940                 spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
941                 return;
942         }
943
944         /*
945          * Sliding window manipulation. Conditions includes:
946          * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
947          * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
948          */
949         if (SN_EQUAL(SeqNum, pTS->RxIndicateSeq)) {
950                 pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
951                 bMatchWinStart = true;
952         } else if (SN_LESS(WinEnd, SeqNum)) {
953                 if (SeqNum >= (WinSize - 1)) {
954                         pTS->RxIndicateSeq = SeqNum + 1 -WinSize;
955                 } else {
956                         pTS->RxIndicateSeq = 4095 - (WinSize - (SeqNum +1)) + 1;
957                 }
958                 RTLLIB_DEBUG(RTLLIB_DL_REORDER, "Window Shift! IndicateSeq: %d, NewSeq: %d\n",pTS->RxIndicateSeq, SeqNum);
959         }
960
961         /*
962          * Indication process.
963          * After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets
964          * with the SeqNum smaller than latest WinStart and buffer other packets.
965          */
966         /* For Rx Reorder condition:
967          * 1. All packets with SeqNum smaller than WinStart => Indicate
968          * 2. All packets with SeqNum larger than or equal to WinStart => Buffer it.
969          */
970         if (bMatchWinStart) {
971                 /* Current packet is going to be indicated.*/
972                 RTLLIB_DEBUG(RTLLIB_DL_REORDER, "Packets indication!! IndicateSeq: %d, NewSeq: %d\n",\
973                                 pTS->RxIndicateSeq, SeqNum);
974                 prxbIndicateArray[0] = prxb;
975                 index = 1;
976         } else {
977                 /* Current packet is going to be inserted into pending list.*/
978                 if (!list_empty(&ieee->RxReorder_Unused_List)) {
979                         pReorderEntry = (PRX_REORDER_ENTRY)list_entry(ieee->RxReorder_Unused_List.next,RX_REORDER_ENTRY,List);
980                         list_del_init(&pReorderEntry->List);
981
982                         /* Make a reorder entry and insert into a the packet list.*/
983                         pReorderEntry->SeqNum = SeqNum;
984                         pReorderEntry->prxb = prxb;
985
986                         if (!AddReorderEntry(pTS, pReorderEntry)) {
987                                 RTLLIB_DEBUG(RTLLIB_DL_REORDER, "%s(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n",
988                                         __func__, pTS->RxIndicateSeq, SeqNum);
989                                 list_add_tail(&pReorderEntry->List,&ieee->RxReorder_Unused_List);
990                                 {
991                                         int i;
992                                         for (i =0; i < prxb->nr_subframes; i++) {
993                                                 dev_kfree_skb(prxb->subframes[i]);
994                                         }
995                                         kfree(prxb);
996                                         prxb = NULL;
997                                 }
998                         } else {
999                                 RTLLIB_DEBUG(RTLLIB_DL_REORDER,
1000                                          "Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n",pTS->RxIndicateSeq, SeqNum);
1001                         }
1002                 }
1003                 else {
1004                         /*
1005                          * Packets are dropped if there is not enough reorder entries.
1006                          * This part shall be modified!! We can just indicate all the
1007                          * packets in buffer and get reorder entries.
1008                          */
1009                         RTLLIB_DEBUG(RTLLIB_DL_ERR, "RxReorderIndicatePacket(): There is no reorder entry!! Packet is dropped!!\n");
1010                         {
1011                                 int i;
1012                                 for (i =0; i < prxb->nr_subframes; i++) {
1013                                         dev_kfree_skb(prxb->subframes[i]);
1014                                 }
1015                                 kfree(prxb);
1016                                 prxb = NULL;
1017                         }
1018                 }
1019         }
1020
1021         /* Check if there is any packet need indicate.*/
1022         while(!list_empty(&pTS->RxPendingPktList)) {
1023                 RTLLIB_DEBUG(RTLLIB_DL_REORDER,"%s(): start RREORDER indicate\n",__func__);
1024
1025                 pReorderEntry = (PRX_REORDER_ENTRY)list_entry(pTS->RxPendingPktList.prev,RX_REORDER_ENTRY,List);
1026                 if ( SN_LESS(pReorderEntry->SeqNum, pTS->RxIndicateSeq) ||
1027                                 SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
1028                 {
1029                         /* This protect buffer from overflow. */
1030                         if (index >= REORDER_WIN_SIZE) {
1031                                 RTLLIB_DEBUG(RTLLIB_DL_ERR, "RxReorderIndicatePacket(): Buffer overflow!! \n");
1032                                 bPktInBuf = true;
1033                                 break;
1034                         }
1035
1036                         list_del_init(&pReorderEntry->List);
1037
1038                         if (SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
1039                                 pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
1040
1041                         prxbIndicateArray[index] = pReorderEntry->prxb;
1042                         RTLLIB_DEBUG(RTLLIB_DL_REORDER,"%s(): Indicate SeqNum %d!\n",__func__, pReorderEntry->SeqNum);
1043                         index++;
1044
1045                         list_add_tail(&pReorderEntry->List,&ieee->RxReorder_Unused_List);
1046                 } else {
1047                         bPktInBuf = true;
1048                         break;
1049                 }
1050         }
1051
1052         /* Handling pending timer. Set this timer to prevent from long time Rx buffering.*/
1053         if (index>0) {
1054                 if (timer_pending(&pTS->RxPktPendingTimer)){
1055                         del_timer_sync(&pTS->RxPktPendingTimer);
1056                 }
1057                 pTS->RxTimeoutIndicateSeq = 0xffff;
1058
1059                 if (index>REORDER_WIN_SIZE){
1060                         RTLLIB_DEBUG(RTLLIB_DL_ERR, "RxReorderIndicatePacket(): Rx Reorer buffer full!! \n");
1061                         spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
1062                         return;
1063                 }
1064                 rtllib_indicate_packets(ieee, prxbIndicateArray, index);
1065                 bPktInBuf = false;
1066         }
1067
1068         if (bPktInBuf && pTS->RxTimeoutIndicateSeq==0xffff) {
1069                 RTLLIB_DEBUG(RTLLIB_DL_REORDER,"%s(): SET rx timeout timer\n", __func__);
1070                 pTS->RxTimeoutIndicateSeq = pTS->RxIndicateSeq;
1071                 mod_timer(&pTS->RxPktPendingTimer,  jiffies + MSECS(pHTInfo->RxReorderPendingTime));
1072         }
1073         spin_unlock_irqrestore(&(ieee->reorder_spinlock), flags);
1074 }
1075
1076 u8 parse_subframe(struct rtllib_device* ieee,struct sk_buff *skb,
1077                   struct rtllib_rx_stats *rx_stats,
1078                   struct rtllib_rxb *rxb,u8* src,u8* dst)
1079 {
1080         struct rtllib_hdr_3addr  *hdr = (struct rtllib_hdr_3addr* )skb->data;
1081         u16             fc = le16_to_cpu(hdr->frame_ctl);
1082
1083         u16             LLCOffset= sizeof(struct rtllib_hdr_3addr);
1084         u16             ChkLength;
1085         bool            bIsAggregateFrame = false;
1086         u16             nSubframe_Length;
1087         u8              nPadding_Length = 0;
1088         u16             SeqNum=0;
1089         struct sk_buff *sub_skb;
1090         u8             *data_ptr;
1091         /* just for debug purpose */
1092         SeqNum = WLAN_GET_SEQ_SEQ(le16_to_cpu(hdr->seq_ctl));
1093         if ((RTLLIB_QOS_HAS_SEQ(fc))&&\
1094                         (((frameqos *)(skb->data + RTLLIB_3ADDR_LEN))->field.reserved)) {
1095                 bIsAggregateFrame = true;
1096         }
1097
1098         if (RTLLIB_QOS_HAS_SEQ(fc)) {
1099                 LLCOffset += 2;
1100         }
1101         if (rx_stats->bContainHTC) {
1102                 LLCOffset += sHTCLng;
1103         }
1104
1105         ChkLength = LLCOffset;/* + (Frame_WEP(frame)!=0 ?Adapter->MgntInfo.SecurityInfo.EncryptionHeadOverhead:0);*/
1106
1107         if ( skb->len <= ChkLength ) {
1108                 return 0;
1109         }
1110
1111         skb_pull(skb, LLCOffset);
1112         ieee->bIsAggregateFrame = bIsAggregateFrame;
1113         if (!bIsAggregateFrame) {
1114                 rxb->nr_subframes = 1;
1115
1116                 /* altered by clark 3/30/2010
1117                  * The buffer size of the skb indicated to upper layer
1118                  * must be less than 5000, or the defraged IP datagram
1119                  * in the IP layer will exceed "ipfrag_high_tresh" and be
1120                  * discarded. so there must not use the function
1121                  * "skb_copy" and "skb_clone" for "skb".
1122                  */
1123
1124                 /* Allocate new skb for releasing to upper layer */
1125                 sub_skb = dev_alloc_skb(RTLLIB_SKBBUFFER_SIZE);
1126                 skb_reserve(sub_skb, 12);
1127                 data_ptr = (u8 *)skb_put(sub_skb, skb->len);
1128                 memcpy(data_ptr, skb->data, skb->len);
1129                 sub_skb->dev = ieee->dev;
1130
1131                 rxb->subframes[0] = sub_skb;
1132
1133                 memcpy(rxb->src,src,ETH_ALEN);
1134                 memcpy(rxb->dst,dst,ETH_ALEN);
1135                 rxb->subframes[0]->dev = ieee->dev;
1136                 return 1;
1137         } else {
1138                 rxb->nr_subframes = 0;
1139                 memcpy(rxb->src,src,ETH_ALEN);
1140                 memcpy(rxb->dst,dst,ETH_ALEN);
1141                 while(skb->len > ETHERNET_HEADER_SIZE) {
1142                         /* Offset 12 denote 2 mac address */
1143                         nSubframe_Length = *((u16*)(skb->data + 12));
1144                         nSubframe_Length = (nSubframe_Length>>8) + (nSubframe_Length<<8);
1145
1146                         if (skb->len<(ETHERNET_HEADER_SIZE + nSubframe_Length)) {
1147                                 printk("%s: A-MSDU parse error!! pRfd->nTotalSubframe : %d\n",\
1148                                                 __func__,rxb->nr_subframes);
1149                                 printk("%s: A-MSDU parse error!! Subframe Length: %d\n",__func__, nSubframe_Length);
1150                                 printk("nRemain_Length is %d and nSubframe_Length is : %d\n",skb->len,nSubframe_Length);
1151                                 printk("The Packet SeqNum is %d\n",SeqNum);
1152                                 return 0;
1153                         }
1154
1155                         /* move the data point to data content */
1156                         skb_pull(skb, ETHERNET_HEADER_SIZE);
1157
1158                         /* altered by clark 3/30/2010
1159                          * The buffer size of the skb indicated to upper layer
1160                          * must be less than 5000, or the defraged IP datagram
1161                          * in the IP layer will exceed "ipfrag_high_tresh" and be
1162                          * discarded. so there must not use the function
1163                          * "skb_copy" and "skb_clone" for "skb".
1164                          */
1165
1166                         /* Allocate new skb for releasing to upper layer */
1167                         sub_skb = dev_alloc_skb(nSubframe_Length + 12);
1168                         skb_reserve(sub_skb, 12);
1169                         data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
1170                         memcpy(data_ptr,skb->data,nSubframe_Length);
1171
1172                         sub_skb->dev = ieee->dev;
1173                         rxb->subframes[rxb->nr_subframes++] = sub_skb;
1174                         if (rxb->nr_subframes >= MAX_SUBFRAME_COUNT) {
1175                                 RTLLIB_DEBUG_RX("ParseSubframe(): Too many Subframes! Packets dropped!\n");
1176                                 break;
1177                         }
1178                         skb_pull(skb,nSubframe_Length);
1179
1180                         if (skb->len != 0) {
1181                                 nPadding_Length = 4 - ((nSubframe_Length + ETHERNET_HEADER_SIZE) % 4);
1182                                 if (nPadding_Length == 4) {
1183                                         nPadding_Length = 0;
1184                                 }
1185
1186                                 if (skb->len < nPadding_Length) {
1187                                         return 0;
1188                                 }
1189
1190                                 skb_pull(skb,nPadding_Length);
1191                         }
1192                 }
1193
1194                 return rxb->nr_subframes;
1195         }
1196 }
1197
1198
1199 size_t rtllib_rx_get_hdrlen(struct rtllib_device *ieee, struct sk_buff *skb,
1200                  struct rtllib_rx_stats *rx_stats)
1201 {
1202         struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1203         u16 fc = le16_to_cpu(hdr->frame_ctl);
1204         size_t hdrlen = 0;
1205
1206         hdrlen = rtllib_get_hdrlen(fc);
1207         if (HTCCheck(ieee, skb->data)) {
1208                 if (net_ratelimit())
1209                         printk("%s: find HTCControl!\n", __func__);
1210                 hdrlen += 4;
1211                 rx_stats->bContainHTC = 1;
1212         }
1213
1214          if (RTLLIB_QOS_HAS_SEQ(fc))
1215                 rx_stats->bIsQosData = 1;
1216
1217         return hdrlen;
1218 }
1219
1220 int rtllib_rx_check_duplicate(struct rtllib_device *ieee, struct sk_buff *skb, u8 multicast)
1221 {
1222         struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1223         u16 fc, sc;
1224         u8 frag, type, stype;
1225
1226         fc = le16_to_cpu(hdr->frame_ctl);
1227         type = WLAN_FC_GET_TYPE(fc);
1228         stype = WLAN_FC_GET_STYPE(fc);
1229         sc = le16_to_cpu(hdr->seq_ctl);
1230         frag = WLAN_GET_SEQ_FRAG(sc);
1231
1232         if ( (ieee->pHTInfo->bCurRxReorderEnable == false) ||
1233                 !ieee->current_network.qos_data.active ||
1234                 !IsDataFrame(skb->data) ||
1235                 IsLegacyDataFrame(skb->data)) {
1236                 if (!((type == RTLLIB_FTYPE_MGMT) && (stype == RTLLIB_STYPE_BEACON))){
1237                         if (is_duplicate_packet(ieee, hdr)){
1238                                 return -1;
1239                         }
1240                 }
1241         } else {
1242                 PRX_TS_RECORD pRxTS = NULL;
1243                 if (GetTs(ieee, (PTS_COMMON_INFO*) &pRxTS, hdr->addr2,
1244                         (u8)Frame_QoSTID((u8*)(skb->data)), RX_DIR, true)) {
1245                         if ((fc & (1<<11)) && (frag == pRxTS->RxLastFragNum) &&
1246                             (WLAN_GET_SEQ_SEQ(sc) == pRxTS->RxLastSeqNum)) {
1247                                 return -1;
1248                         } else {
1249                                 pRxTS->RxLastFragNum = frag;
1250                                 pRxTS->RxLastSeqNum = WLAN_GET_SEQ_SEQ(sc);
1251                         }
1252                 } else {
1253                         RTLLIB_DEBUG(RTLLIB_DL_ERR, "ERR!!%s(): No TS!! Skip the check!!\n",__func__);
1254                         return -1;
1255                 }
1256         }
1257
1258         return 0;
1259 }
1260 void rtllib_rx_extract_addr(struct rtllib_device *ieee, struct rtllib_hdr_4addr *hdr, u8 *dst, u8 *src, u8 *bssid)
1261 {
1262         u16 fc = le16_to_cpu(hdr->frame_ctl);
1263
1264         switch (fc & (RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS)) {
1265                 case RTLLIB_FCTL_FROMDS:
1266                         memcpy(dst, hdr->addr1, ETH_ALEN);
1267                         memcpy(src, hdr->addr3, ETH_ALEN);
1268                         memcpy(bssid, hdr->addr2, ETH_ALEN);
1269                         break;
1270                 case RTLLIB_FCTL_TODS:
1271                         memcpy(dst, hdr->addr3, ETH_ALEN);
1272                         memcpy(src, hdr->addr2, ETH_ALEN);
1273                         memcpy(bssid, hdr->addr1, ETH_ALEN);
1274                         break;
1275                 case RTLLIB_FCTL_FROMDS | RTLLIB_FCTL_TODS:
1276                         memcpy(dst, hdr->addr3, ETH_ALEN);
1277                         memcpy(src, hdr->addr4, ETH_ALEN);
1278                         memcpy(bssid, ieee->current_network.bssid, ETH_ALEN);
1279                         break;
1280                 case 0:
1281                         memcpy(dst, hdr->addr1, ETH_ALEN);
1282                         memcpy(src, hdr->addr2, ETH_ALEN);
1283                         memcpy(bssid, hdr->addr3, ETH_ALEN);
1284                         break;
1285         }
1286 }
1287 int rtllib_rx_data_filter(struct rtllib_device *ieee, u16 fc, u8 *dst, u8 *src, u8 *bssid, u8 *addr2)
1288 {
1289         u8 zero_addr[ETH_ALEN] = {0};
1290         u8 type, stype;
1291
1292         type = WLAN_FC_GET_TYPE(fc);
1293         stype = WLAN_FC_GET_STYPE(fc);
1294
1295         /* Filter frames from different BSS */
1296         if (((fc & RTLLIB_FCTL_DSTODS) != RTLLIB_FCTL_DSTODS)
1297                 && (compare_ether_addr(ieee->current_network.bssid, bssid) != 0)
1298                 && memcmp(ieee->current_network.bssid, zero_addr, ETH_ALEN)) {
1299                 return -1;
1300         }
1301
1302         /* Filter packets sent by an STA that will be forwarded by AP */
1303         if ( ieee->IntelPromiscuousModeInfo.bPromiscuousOn  &&
1304                 ieee->IntelPromiscuousModeInfo.bFilterSourceStationFrame ) {
1305                 if ((fc & RTLLIB_FCTL_TODS) && !(fc & RTLLIB_FCTL_FROMDS) &&
1306                         (compare_ether_addr(dst, ieee->current_network.bssid) != 0) &&
1307                         (compare_ether_addr(bssid, ieee->current_network.bssid) == 0)) {
1308                         return -1;
1309                 }
1310         }
1311
1312         /* Nullfunc frames may have PS-bit set, so they must be passed to
1313          * hostap_handle_sta_rx() before being dropped here. */
1314         if (!ieee->IntelPromiscuousModeInfo.bPromiscuousOn){
1315                 if (stype != RTLLIB_STYPE_DATA &&
1316                     stype != RTLLIB_STYPE_DATA_CFACK &&
1317                     stype != RTLLIB_STYPE_DATA_CFPOLL &&
1318                     stype != RTLLIB_STYPE_DATA_CFACKPOLL&&
1319                     stype != RTLLIB_STYPE_QOS_DATA
1320                     ) {
1321                         if (stype != RTLLIB_STYPE_NULLFUNC)
1322                                 RTLLIB_DEBUG_DROP(
1323                                         "RX: dropped data frame "
1324                                         "with no data (type=0x%02x, "
1325                                         "subtype=0x%02x)\n",
1326                                         type, stype);
1327                         return -1;
1328                 }
1329         }
1330
1331         if (ieee->iw_mode != IW_MODE_MESH) {
1332                 /* packets from our adapter are dropped (echo) */
1333                 if (!memcmp(src, ieee->dev->dev_addr, ETH_ALEN))
1334                         return -1;
1335
1336                 /* {broad,multi}cast packets to our BSS go through */
1337                 if (is_multicast_ether_addr(dst) || is_broadcast_ether_addr(dst)) {
1338                         if (memcmp(bssid, ieee->current_network.bssid, ETH_ALEN)) {
1339                                 return -1;
1340                         }
1341                 }
1342         }
1343         return 0;
1344 }
1345 int rtllib_rx_get_crypt(
1346                 struct rtllib_device *ieee,
1347                 struct sk_buff *skb,
1348                 struct rtllib_crypt_data **crypt,
1349                 size_t hdrlen)
1350 {
1351         struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1352         u16 fc = le16_to_cpu(hdr->frame_ctl);
1353         int idx = 0;
1354
1355         if (ieee->host_decrypt) {
1356                 if (skb->len >= hdrlen + 3)
1357                         idx = skb->data[hdrlen + 3] >> 6;
1358
1359                 *crypt = ieee->crypt[idx];
1360                 /* allow NULL decrypt to indicate an station specific override
1361                  * for default encryption */
1362                 if (*crypt && ((*crypt)->ops == NULL ||
1363                               (*crypt)->ops->decrypt_mpdu == NULL))
1364                         *crypt = NULL;
1365
1366                 if (!*crypt && (fc & RTLLIB_FCTL_WEP)) {
1367                         /* This seems to be triggered by some (multicast?)
1368                          * frames from other than current BSS, so just drop the
1369                          * frames silently instead of filling system log with
1370                          * these reports. */
1371                         RTLLIB_DEBUG_DROP("Decryption failed (not set)"
1372                                              " (SA=" MAC_FMT ")\n",
1373                                              MAC_ARG(hdr->addr2));
1374                         ieee->ieee_stats.rx_discards_undecryptable++;
1375                         return -1;
1376                 }
1377         }
1378
1379         return 0;
1380 }
1381 int rtllib_rx_decrypt(
1382                 struct rtllib_device *ieee,
1383                 struct sk_buff *skb,
1384                 struct rtllib_rx_stats *rx_stats,
1385                 struct rtllib_crypt_data *crypt,
1386                 size_t hdrlen)
1387 {
1388         struct rtllib_hdr_4addr *hdr;
1389         int keyidx = 0;
1390         u16 fc, sc;
1391         u8 frag;
1392
1393         hdr = (struct rtllib_hdr_4addr *)skb->data;
1394         fc = le16_to_cpu(hdr->frame_ctl);
1395         sc = le16_to_cpu(hdr->seq_ctl);
1396         frag = WLAN_GET_SEQ_FRAG(sc);
1397
1398         if ((!rx_stats->Decrypted)){
1399                 ieee->need_sw_enc = 1;
1400         }else{
1401                 ieee->need_sw_enc = 0;
1402         }
1403
1404         if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) &&
1405             ((keyidx = rtllib_rx_frame_decrypt(ieee, skb, crypt)) < 0)) {
1406                 printk("%s: decrypt frame error\n", __func__);
1407                 return -1;
1408         }
1409
1410         hdr = (struct rtllib_hdr_4addr *) skb->data;
1411         if ((frag != 0 || (fc & RTLLIB_FCTL_MOREFRAGS))) {
1412                 int flen;
1413                 struct sk_buff *frag_skb = rtllib_frag_cache_get(ieee, hdr);
1414                 RTLLIB_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
1415
1416                 if (!frag_skb) {
1417                         RTLLIB_DEBUG(RTLLIB_DL_RX | RTLLIB_DL_FRAG,
1418                                         "Rx cannot get skb from fragment "
1419                                         "cache (morefrag=%d seq=%u frag=%u)\n",
1420                                         (fc & RTLLIB_FCTL_MOREFRAGS) != 0,
1421                                         WLAN_GET_SEQ_SEQ(sc), frag);
1422                         return -1;
1423                 }
1424                 flen = skb->len;
1425                 if (frag != 0)
1426                         flen -= hdrlen;
1427
1428                 if (frag_skb->tail + flen > frag_skb->end) {
1429                         printk(KERN_WARNING "%s: host decrypted and "
1430                                "reassembled frame did not fit skb\n",
1431                                __func__);
1432                         rtllib_frag_cache_invalidate(ieee, hdr);
1433                         return -1;
1434                 }
1435
1436                 if (frag == 0) {
1437                         /* copy first fragment (including full headers) into
1438                          * beginning of the fragment cache skb */
1439                         memcpy(skb_put(frag_skb, flen), skb->data, flen);
1440                 } else {
1441                         /* append frame payload to the end of the fragment
1442                          * cache skb */
1443                         memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
1444                                flen);
1445                 }
1446                 dev_kfree_skb_any(skb);
1447                 skb = NULL;
1448
1449                 if (fc & RTLLIB_FCTL_MOREFRAGS) {
1450                         /* more fragments expected - leave the skb in fragment
1451                          * cache for now; it will be delivered to upper layers
1452                          * after all fragments have been received */
1453                         return -2;
1454                 }
1455
1456                 /* this was the last fragment and the frame will be
1457                  * delivered, so remove skb from fragment cache */
1458                 skb = frag_skb;
1459                 hdr = (struct rtllib_hdr_4addr *) skb->data;
1460                 rtllib_frag_cache_invalidate(ieee, hdr);
1461         }
1462
1463         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
1464          * encrypted/authenticated */
1465         if (ieee->host_decrypt && (fc & RTLLIB_FCTL_WEP) &&
1466                 rtllib_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
1467         {
1468                 printk("%s: ==>decrypt msdu error\n", __func__);
1469                 return -1;
1470         }
1471
1472         hdr = (struct rtllib_hdr_4addr *) skb->data;
1473         if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep) {
1474                 if (/*ieee->ieee802_1x &&*/
1475                     rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1476
1477 #ifdef CONFIG_RTLLIB_DEBUG
1478                         /* pass unencrypted EAPOL frames even if encryption is
1479                          * configured */
1480                         struct eapol *eap = (struct eapol *)(skb->data +
1481                                 24);
1482                         RTLLIB_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
1483                                                 eap_get_type(eap->type));
1484 #endif
1485                 } else {
1486                         RTLLIB_DEBUG_DROP(
1487                                 "encryption configured, but RX "
1488                                 "frame not encrypted (SA=" MAC_FMT ")\n",
1489                                 MAC_ARG(hdr->addr2));
1490                         return -1;
1491                 }
1492         }
1493
1494 #ifdef CONFIG_RTLLIB_DEBUG
1495         if (crypt && !(fc & RTLLIB_FCTL_WEP) &&
1496             rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1497                         struct eapol *eap = (struct eapol *)(skb->data +
1498                                 24);
1499                         RTLLIB_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
1500                                                 eap_get_type(eap->type));
1501         }
1502 #endif
1503
1504         if (crypt && !(fc & RTLLIB_FCTL_WEP) && !ieee->open_wep &&
1505             !rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1506                 RTLLIB_DEBUG_DROP(
1507                         "dropped unencrypted RX data "
1508                         "frame from " MAC_FMT
1509                         " (drop_unencrypted=1)\n",
1510                         MAC_ARG(hdr->addr2));
1511                 return -1;
1512         }
1513
1514         if (rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1515                 printk(KERN_WARNING "RX: IEEE802.1X EAPOL frame!\n");
1516         }
1517
1518         return 0;
1519 }
1520 void rtllib_rx_check_leave_lps(struct rtllib_device *ieee, u8 unicast, u8 nr_subframes)
1521 {
1522         if (unicast){
1523
1524                 if ((ieee->state == RTLLIB_LINKED) /*&& !MgntInitAdapterInProgress(pMgntInfo)*/)
1525                 {
1526                         if (    ((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +ieee->LinkDetectInfo.NumTxOkInPeriod) > 8 ) ||
1527                                 (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2) )
1528                         {
1529                                 if (ieee->LeisurePSLeave)
1530                                         ieee->LeisurePSLeave(ieee->dev);
1531                         }
1532                 }
1533         }
1534         ieee->last_rx_ps_time = jiffies;
1535 }
1536 void rtllib_rx_indicate_pkt_legacy(
1537                 struct rtllib_device *ieee,
1538                 struct rtllib_rx_stats *rx_stats,
1539                 struct rtllib_rxb* rxb,
1540                 u8 *dst,
1541                 u8 *src)
1542 {
1543         struct net_device *dev = ieee->dev;
1544         u16 ethertype;
1545         int i = 0;
1546
1547         if (rxb == NULL){
1548                 printk("%s: rxb is NULL!!\n", __func__);
1549                 return ;
1550         }
1551
1552         for (i = 0; i<rxb->nr_subframes; i++) {
1553                 struct sk_buff *sub_skb = rxb->subframes[i];
1554
1555                 if (sub_skb) {
1556                         /* convert hdr + possible LLC headers into Ethernet header */
1557                         ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
1558                         if (sub_skb->len >= 8 &&
1559                                         ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
1560                                           ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1561                                          memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1562                                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1563                                  * replace EtherType */
1564                                 skb_pull(sub_skb, SNAP_SIZE);
1565                                 memcpy(skb_push(sub_skb, ETH_ALEN), src, ETH_ALEN);
1566                                 memcpy(skb_push(sub_skb, ETH_ALEN), dst, ETH_ALEN);
1567                         } else {
1568                                 u16 len;
1569                                 /* Leave Ethernet header part of hdr and full payload */
1570                                 len = htons(sub_skb->len);
1571                                 memcpy(skb_push(sub_skb, 2), &len, 2);
1572                                 memcpy(skb_push(sub_skb, ETH_ALEN), src, ETH_ALEN);
1573                                 memcpy(skb_push(sub_skb, ETH_ALEN), dst, ETH_ALEN);
1574                         }
1575
1576                         ieee->stats.rx_packets++;
1577                         ieee->stats.rx_bytes += sub_skb->len;
1578
1579                         if (is_multicast_ether_addr(dst)) {
1580                                 ieee->stats.multicast++;
1581                         }
1582
1583                         /* Indicat the packets to upper layer */
1584                         memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
1585                         sub_skb->protocol = eth_type_trans(sub_skb, dev);
1586                         sub_skb->dev = dev;
1587                         sub_skb->dev->stats.rx_packets++;
1588                         sub_skb->dev->stats.rx_bytes += sub_skb->len;
1589 #ifdef TCP_CSUM_OFFLOAD_RX
1590                         if ( rx_stats->tcp_csum_valid)
1591                                 sub_skb->ip_summed = CHECKSUM_UNNECESSARY;
1592                         else
1593                                 sub_skb->ip_summed = CHECKSUM_NONE;
1594 #else
1595                         sub_skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
1596 #endif
1597                         netif_rx(sub_skb);
1598                 }
1599         }
1600         kfree(rxb);
1601         rxb = NULL;
1602 }
1603 int rtllib_rx_InfraAdhoc(struct rtllib_device *ieee, struct sk_buff *skb,
1604                  struct rtllib_rx_stats *rx_stats)
1605 {
1606         struct net_device *dev = ieee->dev;
1607         struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1608         struct rtllib_crypt_data *crypt = NULL;
1609         struct rtllib_rxb* rxb = NULL;
1610         PRX_TS_RECORD pTS = NULL;
1611         u16 fc, sc, SeqNum = 0;
1612         u8 type, stype, multicast = 0, unicast = 0, nr_subframes = 0, TID = 0;
1613         u8 dst[ETH_ALEN], src[ETH_ALEN], bssid[ETH_ALEN] = {0}, *payload;
1614         size_t hdrlen = 0;
1615         bool bToOtherSTA = false;
1616         int ret = 0, i = 0;
1617
1618         hdr = (struct rtllib_hdr_4addr *)skb->data;
1619         fc = le16_to_cpu(hdr->frame_ctl);
1620         type = WLAN_FC_GET_TYPE(fc);
1621         stype = WLAN_FC_GET_STYPE(fc);
1622         sc = le16_to_cpu(hdr->seq_ctl);
1623
1624         /*Filter pkt not to me*/
1625         multicast = is_multicast_ether_addr(hdr->addr1)|is_broadcast_ether_addr(hdr->addr1);
1626         unicast = !multicast;
1627         if (unicast && (compare_ether_addr(dev->dev_addr, hdr->addr1) != 0)) {
1628                 if (ieee->bNetPromiscuousMode)
1629                         bToOtherSTA = true;
1630                 else
1631                         goto rx_dropped;
1632         }
1633
1634         /*Filter pkt has too small length */
1635         hdrlen = rtllib_rx_get_hdrlen(ieee, skb, rx_stats);
1636         if (skb->len < hdrlen){
1637                 printk("%s():ERR!!! skb->len is smaller than hdrlen\n",__func__);
1638                 goto rx_dropped;
1639         }
1640
1641         /* Filter Duplicate pkt */
1642         ret = rtllib_rx_check_duplicate(ieee, skb, multicast);
1643         if (ret < 0)
1644                 goto rx_dropped;
1645
1646         /* Filter CTRL Frame */
1647         if (type == RTLLIB_FTYPE_CTL) {
1648                 goto rx_dropped;
1649         }
1650
1651         /* Filter MGNT Frame */
1652         if (type == RTLLIB_FTYPE_MGMT) {
1653                 if (bToOtherSTA)
1654                         goto rx_dropped;
1655                 if (rtllib_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
1656                         goto rx_dropped;
1657                 else
1658                         goto rx_exit;
1659         }
1660
1661         /* Filter WAPI DATA Frame */
1662
1663         /* Update statstics for AP roaming */
1664         if (!bToOtherSTA){
1665                 ieee->LinkDetectInfo.NumRecvDataInPeriod++;
1666                 ieee->LinkDetectInfo.NumRxOkInPeriod++;
1667         }
1668         dev->last_rx = jiffies;
1669
1670         /* Data frame - extract src/dst addresses */
1671         rtllib_rx_extract_addr(ieee, hdr, dst, src, bssid);
1672
1673         /* Filter Data frames */
1674         ret = rtllib_rx_data_filter(ieee, fc, dst, src, bssid, hdr->addr2);
1675         if (ret < 0)
1676                 goto rx_dropped;
1677
1678         if (skb->len == hdrlen){
1679                 goto rx_dropped;
1680         }
1681
1682         /* Send pspoll based on moredata */
1683         if ((ieee->iw_mode == IW_MODE_INFRA)  && (ieee->sta_sleep == LPS_IS_SLEEP)
1684                 && (ieee->polling) && (!bToOtherSTA)) {
1685                 if (WLAN_FC_MORE_DATA(fc)) {
1686                         /* more data bit is set, let's request a new frame from the AP */
1687                         rtllib_sta_ps_send_pspoll_frame(ieee);
1688                 } else {
1689                         ieee->polling =  false;
1690                 }
1691         }
1692
1693         /* Get crypt if encrypted */
1694         ret = rtllib_rx_get_crypt(ieee, skb, &crypt, hdrlen);
1695         if (ret == -1)
1696                 goto rx_dropped;
1697
1698         /* Decrypt data frame (including reassemble) */
1699         ret = rtllib_rx_decrypt(ieee, skb, rx_stats, crypt, hdrlen);
1700         if (ret == -1)
1701                 goto rx_dropped;
1702         else if (ret == -2)
1703                 goto rx_exit;
1704
1705         /* Get TS for Rx Reorder  */
1706         hdr = (struct rtllib_hdr_4addr *) skb->data;
1707         if (ieee->current_network.qos_data.active && IsQoSDataFrame(skb->data)
1708                 && !is_multicast_ether_addr(hdr->addr1) && !is_broadcast_ether_addr(hdr->addr1)
1709                 && (!bToOtherSTA))
1710         {
1711                 TID = Frame_QoSTID(skb->data);
1712                 SeqNum = WLAN_GET_SEQ_SEQ(sc);
1713                 GetTs(ieee,(PTS_COMMON_INFO*) &pTS,hdr->addr2,TID,RX_DIR,true);
1714                 if (TID !=0 && TID !=3){
1715                         ieee->bis_any_nonbepkts = true;
1716                 }
1717         }
1718
1719         /* Parse rx data frame (For AMSDU) */
1720         /* skb: hdr + (possible reassembled) full plaintext payload */
1721         payload = skb->data + hdrlen;
1722         rxb = (struct rtllib_rxb*)kmalloc(sizeof(struct rtllib_rxb),GFP_ATOMIC);
1723         if (rxb == NULL)
1724         {
1725                 RTLLIB_DEBUG(RTLLIB_DL_ERR,"%s(): kmalloc rxb error\n",__func__);
1726                 goto rx_dropped;
1727         }
1728         /* to parse amsdu packets */
1729         /* qos data packets & reserved bit is 1 */
1730         if (parse_subframe(ieee,skb,rx_stats,rxb,src,dst) == 0) {
1731                 /* only to free rxb, and not submit the packets to upper layer */
1732                 for (i =0; i < rxb->nr_subframes; i++) {
1733                         dev_kfree_skb(rxb->subframes[i]);
1734                 }
1735                 kfree(rxb);
1736                 rxb = NULL;
1737                 goto rx_dropped;
1738         }
1739
1740         /* Update WAPI PN */
1741
1742         /* Check if leave LPS */
1743         if (!bToOtherSTA){
1744                 if (ieee->bIsAggregateFrame)
1745                         nr_subframes = rxb->nr_subframes;
1746                 else
1747                         nr_subframes = 1;
1748                 if (unicast)
1749                         ieee->LinkDetectInfo.NumRxUnicastOkInPeriod += nr_subframes;
1750                 rtllib_rx_check_leave_lps(ieee, unicast, nr_subframes);
1751         }
1752
1753         /* Indicate packets to upper layer or Rx Reorder */
1754         if (ieee->pHTInfo->bCurRxReorderEnable == false ||pTS == NULL || bToOtherSTA){
1755                 rtllib_rx_indicate_pkt_legacy(ieee, rx_stats, rxb, dst, src);
1756         }else{
1757 #ifdef TCP_CSUM_OFFLOAD_RX
1758                 rxb->tcp_csum_valid = rx_stats->tcp_csum_valid;
1759 #endif
1760                 RxReorderIndicatePacket(ieee, rxb, pTS, SeqNum);
1761         }
1762
1763         dev_kfree_skb(skb);
1764
1765  rx_exit:
1766         return 1;
1767
1768  rx_dropped:
1769         if (rxb != NULL)
1770         {
1771                 kfree(rxb);
1772                 rxb = NULL;
1773         }
1774         ieee->stats.rx_dropped++;
1775
1776         /* Returning 0 indicates to caller that we have not handled the SKB--
1777          * so it is still allocated and can be used again by underlying
1778          * hardware as a DMA target */
1779         return 0;
1780 }
1781
1782 int rtllib_rx_Master(struct rtllib_device *ieee, struct sk_buff *skb,
1783                  struct rtllib_rx_stats *rx_stats)
1784 {
1785         return 0;
1786 }
1787 int rtllib_rx_Monitor(struct rtllib_device *ieee, struct sk_buff *skb,
1788                  struct rtllib_rx_stats *rx_stats)
1789 {
1790         struct rtllib_hdr_4addr *hdr = (struct rtllib_hdr_4addr *)skb->data;
1791         u16 fc = le16_to_cpu(hdr->frame_ctl);
1792         size_t hdrlen = rtllib_get_hdrlen(fc);
1793
1794         if (skb->len < hdrlen){
1795                 printk("%s():ERR!!! skb->len is smaller than hdrlen\n", __func__);
1796                 return 0;
1797         }
1798
1799         if (HTCCheck(ieee, skb->data)) {
1800                 if (net_ratelimit())
1801                         printk("%s: Find HTCControl!\n", __func__);
1802                 hdrlen += 4;
1803         }
1804
1805         rtllib_monitor_rx(ieee, skb, rx_stats, hdrlen);
1806         ieee->stats.rx_packets++;
1807         ieee->stats.rx_bytes += skb->len;
1808
1809         return 1;
1810 }
1811
1812 int rtllib_rx_Mesh(struct rtllib_device *ieee, struct sk_buff *skb,
1813                  struct rtllib_rx_stats *rx_stats)
1814 {
1815         return 0;
1816 }
1817
1818 /* All received frames are sent to this function. @skb contains the frame in
1819  * IEEE 802.11 format, i.e., in the format it was sent over air.
1820  * This function is called only as a tasklet (software IRQ). */
1821 int rtllib_rx(struct rtllib_device *ieee, struct sk_buff *skb,
1822                  struct rtllib_rx_stats *rx_stats)
1823 {
1824         int ret = 0;
1825
1826         if ((NULL==ieee) || (NULL==skb) || (NULL==rx_stats)){
1827                 printk(KERN_INFO "%s: Input parameters NULL!\n", __func__);
1828                 goto rx_dropped;
1829         }
1830         if (skb->len < 10) {
1831                 printk(KERN_INFO "%s: SKB length < 10 \n", __func__);
1832                 goto rx_dropped;
1833         }
1834
1835         switch (ieee->iw_mode) {
1836         case IW_MODE_ADHOC:
1837         case IW_MODE_INFRA:
1838                 ret = rtllib_rx_InfraAdhoc(ieee, skb, rx_stats);
1839                 break;
1840         case IW_MODE_MASTER:
1841         case IW_MODE_REPEAT:
1842                 ret = rtllib_rx_Master(ieee, skb, rx_stats);
1843                 break;
1844         case IW_MODE_MONITOR:
1845                 ret = rtllib_rx_Monitor(ieee, skb, rx_stats);
1846                 break;
1847         case IW_MODE_MESH:
1848                 ret = rtllib_rx_Mesh(ieee, skb, rx_stats);
1849                 break;
1850         default:
1851                 printk(KERN_INFO"%s: ERR iw mode!!!\n", __func__);
1852                 break;
1853         }
1854
1855         return ret;
1856
1857  rx_dropped:
1858         ieee->stats.rx_dropped++;
1859         return 0;
1860 }
1861
1862 static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
1863
1864 /*
1865 * Make ther structure we read from the beacon packet has
1866 * the right values
1867 */
1868 static int rtllib_verify_qos_info(struct rtllib_qos_information_element
1869                                      *info_element, int sub_type)
1870 {
1871
1872         if (info_element->qui_subtype != sub_type)
1873                 return -1;
1874         if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
1875                 return -1;
1876         if (info_element->qui_type != QOS_OUI_TYPE)
1877                 return -1;
1878         if (info_element->version != QOS_VERSION_1)
1879                 return -1;
1880
1881         return 0;
1882 }
1883
1884
1885 /*
1886  * Parse a QoS parameter element
1887  */
1888 static int rtllib_read_qos_param_element(struct rtllib_qos_parameter_info
1889                                             *element_param, struct rtllib_info_element
1890                                             *info_element)
1891 {
1892         int ret = 0;
1893         u16 size = sizeof(struct rtllib_qos_parameter_info) - 2;
1894
1895         if ((info_element == NULL) || (element_param == NULL))
1896                 return -1;
1897
1898         if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
1899                 memcpy(element_param->info_element.qui, info_element->data,
1900                        info_element->len);
1901                 element_param->info_element.elementID = info_element->id;
1902                 element_param->info_element.length = info_element->len;
1903         } else
1904                 ret = -1;
1905         if (ret == 0)
1906                 ret = rtllib_verify_qos_info(&element_param->info_element,
1907                                                 QOS_OUI_PARAM_SUB_TYPE);
1908         return ret;
1909 }
1910
1911 /*
1912  * Parse a QoS information element
1913  */
1914 static int rtllib_read_qos_info_element(struct
1915                                            rtllib_qos_information_element
1916                                            *element_info, struct rtllib_info_element
1917                                            *info_element)
1918 {
1919         int ret = 0;
1920         u16 size = sizeof(struct rtllib_qos_information_element) - 2;
1921
1922         if (element_info == NULL)
1923                 return -1;
1924         if (info_element == NULL)
1925                 return -1;
1926
1927         if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
1928                 memcpy(element_info->qui, info_element->data,
1929                        info_element->len);
1930                 element_info->elementID = info_element->id;
1931                 element_info->length = info_element->len;
1932         } else
1933                 ret = -1;
1934
1935         if (ret == 0)
1936                 ret = rtllib_verify_qos_info(element_info,
1937                                                 QOS_OUI_INFO_SUB_TYPE);
1938         return ret;
1939 }
1940
1941
1942 /*
1943  * Write QoS parameters from the ac parameters.
1944  */
1945 static int rtllib_qos_convert_ac_to_parameters(struct rtllib_qos_parameter_info *param_elm,
1946                 struct rtllib_qos_data *qos_data)
1947 {
1948         struct rtllib_qos_ac_parameter *ac_params;
1949         struct rtllib_qos_parameters *qos_param = &(qos_data->parameters);
1950         int rc = 0;
1951         int i;
1952         u8 aci;
1953         u8 acm;
1954
1955         qos_data->wmm_acm = 0;
1956         for (i = 0; i < QOS_QUEUE_NUM; i++) {
1957                 ac_params = &(param_elm->ac_params_record[i]);
1958
1959                 aci = (ac_params->aci_aifsn & 0x60) >> 5;
1960                 acm = (ac_params->aci_aifsn & 0x10) >> 4;
1961
1962                 if (aci >= QOS_QUEUE_NUM)
1963                         continue;
1964                 switch (aci) {
1965                         case 1:
1966                                 /* BIT(0) | BIT(3) */
1967                                 if (acm)
1968                                         qos_data->wmm_acm |= (0x01<<0)|(0x01<<3);
1969                                 break;
1970                         case 2:
1971                                 /* BIT(4) | BIT(5) */
1972                                 if (acm)
1973                                         qos_data->wmm_acm |= (0x01<<4)|(0x01<<5);
1974                                 break;
1975                         case 3:
1976                                 /* BIT(6) | BIT(7) */
1977                                 if (acm)
1978                                         qos_data->wmm_acm |= (0x01<<6)|(0x01<<7);
1979                                 break;
1980                         case 0:
1981                         default:
1982                                 /* BIT(1) | BIT(2) */
1983                                 if (acm)
1984                                         qos_data->wmm_acm |= (0x01<<1)|(0x01<<2);
1985                                 break;
1986                 }
1987
1988                 qos_param->aifs[aci] = (ac_params->aci_aifsn) & 0x0f;
1989
1990                 /* WMM spec P.11: The minimum value for AIFSN shall be 2 */
1991                 qos_param->aifs[aci] = (qos_param->aifs[aci] < 2) ? 2:qos_param->aifs[aci];
1992
1993                 qos_param->cw_min[aci] = ac_params->ecw_min_max & 0x0F;
1994
1995                 qos_param->cw_max[aci] = (ac_params->ecw_min_max & 0xF0) >> 4;
1996
1997                 qos_param->flag[aci] =
1998                     (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
1999                 qos_param->tx_op_limit[aci] = le16_to_cpu(ac_params->tx_op_limit);
2000         }
2001         return rc;
2002 }
2003
2004 /*
2005  * we have a generic data element which it may contain QoS information or
2006  * parameters element. check the information element length to decide
2007  * which type to read
2008  */
2009 static int rtllib_parse_qos_info_param_IE(struct rtllib_info_element
2010                                              *info_element,
2011                                              struct rtllib_network *network)
2012 {
2013         int rc = 0;
2014         struct rtllib_qos_information_element qos_info_element;
2015
2016         rc = rtllib_read_qos_info_element(&qos_info_element, info_element);
2017
2018         if (rc == 0) {
2019                 network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
2020                 network->flags |= NETWORK_HAS_QOS_INFORMATION;
2021         } else {
2022                 struct rtllib_qos_parameter_info param_element;
2023
2024                 rc = rtllib_read_qos_param_element(&param_element,
2025                                                       info_element);
2026                 if (rc == 0) {
2027                         rtllib_qos_convert_ac_to_parameters(&param_element,
2028                                                                &(network->qos_data));
2029                         network->flags |= NETWORK_HAS_QOS_PARAMETERS;
2030                         network->qos_data.param_count =
2031                             param_element.info_element.ac_info & 0x0F;
2032                 }
2033         }
2034
2035         if (rc == 0) {
2036                 RTLLIB_DEBUG_QOS("QoS is supported\n");
2037                 network->qos_data.supported = 1;
2038         }
2039         return rc;
2040 }
2041
2042 #ifdef CONFIG_RTLLIB_DEBUG
2043 #define MFIE_STRING(x) case MFIE_TYPE_ ##x: return #x
2044
2045 static const char *get_info_element_string(u16 id)
2046 {
2047         switch (id) {
2048                 MFIE_STRING(SSID);
2049                 MFIE_STRING(RATES);
2050                 MFIE_STRING(FH_SET);
2051                 MFIE_STRING(DS_SET);
2052                 MFIE_STRING(CF_SET);
2053                 MFIE_STRING(TIM);
2054                 MFIE_STRING(IBSS_SET);
2055                 MFIE_STRING(COUNTRY);
2056                 MFIE_STRING(HOP_PARAMS);
2057                 MFIE_STRING(HOP_TABLE);
2058                 MFIE_STRING(REQUEST);
2059                 MFIE_STRING(CHALLENGE);
2060                 MFIE_STRING(POWER_CONSTRAINT);
2061                 MFIE_STRING(POWER_CAPABILITY);
2062                 MFIE_STRING(TPC_REQUEST);
2063                 MFIE_STRING(TPC_REPORT);
2064                 MFIE_STRING(SUPP_CHANNELS);
2065                 MFIE_STRING(CSA);
2066                 MFIE_STRING(MEASURE_REQUEST);
2067                 MFIE_STRING(MEASURE_REPORT);
2068                 MFIE_STRING(QUIET);
2069                 MFIE_STRING(IBSS_DFS);
2070                 MFIE_STRING(RSN);
2071                 MFIE_STRING(RATES_EX);
2072                 MFIE_STRING(GENERIC);
2073                 MFIE_STRING(QOS_PARAMETER);
2074         default:
2075                 return "UNKNOWN";
2076         }
2077 }
2078 #endif
2079
2080 #ifdef ENABLE_DOT11D
2081 static inline void rtllib_extract_country_ie(
2082         struct rtllib_device *ieee,
2083         struct rtllib_info_element *info_element,
2084         struct rtllib_network *network,
2085         u8 * addr2)
2086 {
2087         if (IS_DOT11D_ENABLE(ieee)) {
2088                 if (info_element->len!= 0) {
2089                         memcpy(network->CountryIeBuf, info_element->data, info_element->len);
2090                         network->CountryIeLen = info_element->len;
2091
2092                         if (!IS_COUNTRY_IE_VALID(ieee))
2093                         {
2094                                 if ((rtllib_act_scanning(ieee,false) == true) && (ieee->FirstIe_InScan == 1))
2095                                         printk("Received beacon ContryIE, SSID: <%s>\n",network->ssid);
2096                                 Dot11d_UpdateCountryIe(ieee, addr2, info_element->len, info_element->data);
2097                         }
2098                 }
2099
2100                 if (IS_EQUAL_CIE_SRC(ieee, addr2)) {
2101                         UPDATE_CIE_WATCHDOG(ieee);
2102                 }
2103         }
2104
2105 }
2106 #endif
2107
2108 int rtllib_parse_info_param(struct rtllib_device *ieee,
2109                 struct rtllib_info_element *info_element,
2110                 u16 length,
2111                 struct rtllib_network *network,
2112                 struct rtllib_rx_stats *stats)
2113 {
2114         u8 i;
2115         short offset;
2116         u16     tmp_htcap_len=0;
2117         u16     tmp_htinfo_len=0;
2118         u16 ht_realtek_agg_len=0;
2119         u8  ht_realtek_agg_buf[MAX_IE_LEN];
2120 #ifdef CONFIG_RTLLIB_DEBUG
2121         char rates_str[64];
2122         char *p;
2123 #endif
2124         while (length >= sizeof(*info_element)) {
2125                 if (sizeof(*info_element) + info_element->len > length) {
2126                         RTLLIB_DEBUG_MGMT("Info elem: parse failed: "
2127                                              "info_element->len + 2 > left : "
2128                                              "info_element->len+2=%zd left=%d, id=%d.\n",
2129                                              info_element->len +
2130                                              sizeof(*info_element),
2131                                              length, info_element->id);
2132                         /* We stop processing but don't return an error here
2133                          * because some misbehaviour APs break this rule. ie.
2134                          * Orinoco AP1000. */
2135                         break;
2136                 }
2137
2138                 switch (info_element->id) {
2139                 case MFIE_TYPE_SSID:
2140                         if (rtllib_is_empty_essid(info_element->data,
2141                                                      info_element->len)) {
2142                                 network->flags |= NETWORK_EMPTY_ESSID;
2143                                 break;
2144                         }
2145
2146                         network->ssid_len = min(info_element->len,
2147                                                 (u8) IW_ESSID_MAX_SIZE);
2148                         memcpy(network->ssid, info_element->data, network->ssid_len);
2149                         if (network->ssid_len < IW_ESSID_MAX_SIZE)
2150                                 memset(network->ssid + network->ssid_len, 0,
2151                                        IW_ESSID_MAX_SIZE - network->ssid_len);
2152
2153                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_SSID: '%s' len=%d.\n",
2154                                              network->ssid, network->ssid_len);
2155                         break;
2156
2157                 case MFIE_TYPE_RATES:
2158 #ifdef CONFIG_RTLLIB_DEBUG
2159                         p = rates_str;
2160 #endif
2161                         network->rates_len = min(info_element->len,
2162                                                  MAX_RATES_LENGTH);
2163                         for (i = 0; i < network->rates_len; i++) {
2164                                 network->rates[i] = info_element->data[i];
2165 #ifdef CONFIG_RTLLIB_DEBUG
2166                                 p += snprintf(p, sizeof(rates_str) -
2167                                               (p - rates_str), "%02X ",
2168                                               network->rates[i]);
2169 #endif
2170                                 if (rtllib_is_ofdm_rate
2171                                     (info_element->data[i])) {
2172                                         network->flags |= NETWORK_HAS_OFDM;
2173                                         if (info_element->data[i] &
2174                                             RTLLIB_BASIC_RATE_MASK)
2175                                                 network->flags &=
2176                                                     ~NETWORK_HAS_CCK;
2177                                 }
2178
2179                                 if (rtllib_is_cck_rate
2180                                     (info_element->data[i])) {
2181                                         network->flags |= NETWORK_HAS_CCK;
2182                                 }
2183                         }
2184
2185                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_RATES: '%s' (%d)\n",
2186                                              rates_str, network->rates_len);
2187                         break;
2188
2189                 case MFIE_TYPE_RATES_EX:
2190 #ifdef CONFIG_RTLLIB_DEBUG
2191                         p = rates_str;
2192 #endif
2193                         network->rates_ex_len = min(info_element->len,
2194                                                     MAX_RATES_EX_LENGTH);
2195                         for (i = 0; i < network->rates_ex_len; i++) {
2196                                 network->rates_ex[i] = info_element->data[i];
2197 #ifdef CONFIG_RTLLIB_DEBUG
2198                                 p += snprintf(p, sizeof(rates_str) -
2199                                               (p - rates_str), "%02X ",
2200                                               network->rates[i]);
2201 #endif
2202                                 if (rtllib_is_ofdm_rate
2203                                     (info_element->data[i])) {
2204                                         network->flags |= NETWORK_HAS_OFDM;
2205                                         if (info_element->data[i] &
2206                                             RTLLIB_BASIC_RATE_MASK)
2207                                                 network->flags &=
2208                                                     ~NETWORK_HAS_CCK;
2209                                 }
2210                         }
2211
2212                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
2213                                              rates_str, network->rates_ex_len);
2214                         break;
2215
2216                 case MFIE_TYPE_DS_SET:
2217                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_DS_SET: %d\n",
2218                                              info_element->data[0]);
2219                         network->channel = info_element->data[0];
2220                         break;
2221
2222                 case MFIE_TYPE_FH_SET:
2223                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_FH_SET: ignored\n");
2224                         break;
2225
2226                 case MFIE_TYPE_CF_SET:
2227                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_CF_SET: ignored\n");
2228                         break;
2229
2230                 case MFIE_TYPE_TIM:
2231                         if (info_element->len < 4)
2232                                 break;
2233
2234                         network->tim.tim_count = info_element->data[0];
2235                         network->tim.tim_period = info_element->data[1];
2236
2237                         network->dtim_period = info_element->data[1];
2238                         if (ieee->state != RTLLIB_LINKED)
2239                                 break;
2240                         network->last_dtim_sta_time[0] = jiffies;
2241                         network->last_dtim_sta_time[1] = stats->mac_time[1];
2242
2243                         network->dtim_data = RTLLIB_DTIM_VALID;
2244
2245
2246                         if (info_element->data[2] & 1)
2247                                 network->dtim_data |= RTLLIB_DTIM_MBCAST;
2248
2249                         offset = (info_element->data[2] >> 1)*2;
2250
2251
2252                         if (ieee->assoc_id < 8*offset ||
2253                                 ieee->assoc_id > 8*(offset + info_element->len -3))
2254
2255                                 break;
2256
2257                         offset = (ieee->assoc_id / 8) - offset;
2258                         if (info_element->data[3+offset] & (1<<(ieee->assoc_id%8)))
2259                                 network->dtim_data |= RTLLIB_DTIM_UCAST;
2260
2261                         network->listen_interval = network->dtim_period;
2262                         break;
2263
2264                 case MFIE_TYPE_ERP:
2265                         network->erp_value = info_element->data[0];
2266                         network->flags |= NETWORK_HAS_ERP_VALUE;
2267                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
2268                                              network->erp_value);
2269                         break;
2270                 case MFIE_TYPE_IBSS_SET:
2271                         network->atim_window = info_element->data[0];
2272                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_IBSS_SET: %d\n",
2273                                              network->atim_window);
2274                         break;
2275
2276                 case MFIE_TYPE_CHALLENGE:
2277                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_CHALLENGE: ignored\n");
2278                         break;
2279
2280                 case MFIE_TYPE_GENERIC:
2281                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_GENERIC: %d bytes\n",
2282                                              info_element->len);
2283                         if (!rtllib_parse_qos_info_param_IE(info_element,
2284                                                                network))
2285                                 break;
2286                         if (info_element->len >= 4 &&
2287                             info_element->data[0] == 0x00 &&
2288                             info_element->data[1] == 0x50 &&
2289                             info_element->data[2] == 0xf2 &&
2290                             info_element->data[3] == 0x01) {
2291                                 network->wpa_ie_len = min(info_element->len + 2,
2292                                                           MAX_WPA_IE_LEN);
2293                                 memcpy(network->wpa_ie, info_element,
2294                                        network->wpa_ie_len);
2295                                 break;
2296                         }
2297                         if (info_element->len == 7 &&
2298                             info_element->data[0] == 0x00 &&
2299                             info_element->data[1] == 0xe0 &&
2300                             info_element->data[2] == 0x4c &&
2301                             info_element->data[3] == 0x01 &&
2302                             info_element->data[4] == 0x02)
2303                                 network->Turbo_Enable = 1;
2304
2305                         if (tmp_htcap_len == 0) {
2306                                 if (info_element->len >= 4 &&
2307                                    info_element->data[0] == 0x00 &&
2308                                    info_element->data[1] == 0x90 &&
2309                                    info_element->data[2] == 0x4c &&
2310                                    info_element->data[3] == 0x033) {
2311
2312                                                 tmp_htcap_len = min(info_element->len,(u8)MAX_IE_LEN);
2313                                                 if (tmp_htcap_len != 0){
2314                                                         network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
2315                                                         network->bssht.bdHTCapLen = tmp_htcap_len > sizeof(network->bssht.bdHTCapBuf)?\
2316                                                                 sizeof(network->bssht.bdHTCapBuf):tmp_htcap_len;
2317                                                         memcpy(network->bssht.bdHTCapBuf,info_element->data,network->bssht.bdHTCapLen);
2318                                                 }
2319                                 }
2320                                 if (tmp_htcap_len != 0){
2321                                         network->bssht.bdSupportHT = true;
2322                                         network->bssht.bdHT1R = ((((PHT_CAPABILITY_ELE)(network->bssht.bdHTCapBuf))->MCS[1]) == 0);
2323                                 }else{
2324                                         network->bssht.bdSupportHT = false;
2325                                         network->bssht.bdHT1R = false;
2326                                 }
2327                         }
2328
2329
2330                         if (tmp_htinfo_len == 0){
2331                                 if (info_element->len >= 4 &&
2332                                         info_element->data[0] == 0x00 &&
2333                                         info_element->data[1] == 0x90 &&
2334                                         info_element->data[2] == 0x4c &&
2335                                         info_element->data[3] == 0x034){
2336
2337                                                 tmp_htinfo_len = min(info_element->len,(u8)MAX_IE_LEN);
2338                                                 if (tmp_htinfo_len != 0){
2339                                                         network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
2340                                                         if (tmp_htinfo_len){
2341                                                                 network->bssht.bdHTInfoLen = tmp_htinfo_len > sizeof(network->bssht.bdHTInfoBuf)?\
2342                                                                         sizeof(network->bssht.bdHTInfoBuf):tmp_htinfo_len;
2343                                                                 memcpy(network->bssht.bdHTInfoBuf,info_element->data,network->bssht.bdHTInfoLen);
2344                                                         }
2345
2346                                                 }
2347
2348                                 }
2349                         }
2350
2351                         if (ieee->aggregation){
2352                                 if (network->bssht.bdSupportHT){
2353                                         if (info_element->len >= 4 &&
2354                                                 info_element->data[0] == 0x00 &&
2355                                                 info_element->data[1] == 0xe0 &&
2356                                                 info_element->data[2] == 0x4c &&
2357                                                 info_element->data[3] == 0x02){
2358
2359                                                 ht_realtek_agg_len = min(info_element->len,(u8)MAX_IE_LEN);
2360                                                 memcpy(ht_realtek_agg_buf,info_element->data,info_element->len);
2361
2362                                         }
2363                                         if (ht_realtek_agg_len >= 5){
2364                                                 network->realtek_cap_exit = true;
2365                                                 network->bssht.bdRT2RTAggregation = true;
2366
2367                                                 if ((ht_realtek_agg_buf[4] == 1) && (ht_realtek_agg_buf[5] & 0x02))
2368                                                 network->bssht.bdRT2RTLongSlotTime = true;
2369
2370                                                 if ((ht_realtek_agg_buf[4]==1) && (ht_realtek_agg_buf[5] & RT_HT_CAP_USE_92SE))
2371                                                 {
2372                                                         network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_92SE;
2373                                                 }
2374                                         }
2375                                 }
2376                                 if (ht_realtek_agg_len >= 5){
2377                                         if ((ht_realtek_agg_buf[5] & RT_HT_CAP_USE_SOFTAP))
2378                                                 network->bssht.RT2RT_HT_Mode |= RT_HT_CAP_USE_SOFTAP;
2379                                 }
2380                         }
2381
2382                         {
2383                                 if ((info_element->len >= 3 &&
2384                                          info_element->data[0] == 0x00 &&
2385                                          info_element->data[1] == 0x05 &&
2386                                          info_element->data[2] == 0xb5) ||
2387                                          (info_element->len >= 3 &&
2388                                          info_element->data[0] == 0x00 &&
2389                                          info_element->data[1] == 0x0a &&
2390                                          info_element->data[2] == 0xf7) ||
2391                                          (info_element->len >= 3 &&
2392                                          info_element->data[0] == 0x00 &&
2393                                          info_element->data[1] == 0x10 &&
2394                                          info_element->data[2] == 0x18)){
2395
2396                                                 network->broadcom_cap_exist = true;
2397
2398                                 }
2399                         }
2400                         if (info_element->len >= 3 &&
2401                                 info_element->data[0] == 0x00 &&
2402                                 info_element->data[1] == 0x0c &&
2403                                 info_element->data[2] == 0x43)
2404                         {
2405                                 network->ralink_cap_exist = true;
2406                         }
2407                         if ((info_element->len >= 3 &&
2408                                 info_element->data[0] == 0x00 &&
2409                                 info_element->data[1] == 0x03 &&
2410                                 info_element->data[2] == 0x7f) ||
2411                                 (info_element->len >= 3 &&
2412                                 info_element->data[0] == 0x00 &&
2413                                 info_element->data[1] == 0x13 &&
2414                                 info_element->data[2] == 0x74))
2415                         {
2416                                 network->atheros_cap_exist = true;
2417                         }
2418
2419                         if ((info_element->len >= 3 &&
2420                                 info_element->data[0] == 0x00 &&
2421                                 info_element->data[1] == 0x50 &&
2422                                 info_element->data[2] == 0x43) )
2423                                 {
2424                                         network->marvell_cap_exist = true;
2425                                 }
2426                         if (info_element->len >= 3 &&
2427                                 info_element->data[0] == 0x00 &&
2428                                 info_element->data[1] == 0x40 &&
2429                                 info_element->data[2] == 0x96)
2430                         {
2431                                 network->cisco_cap_exist = true;
2432                         }
2433
2434
2435                         if (info_element->len >= 3 &&
2436                                 info_element->data[0] == 0x00 &&
2437                                 info_element->data[1] == 0x0a &&
2438                                 info_element->data[2] == 0xf5)
2439                         {
2440                                 network->airgo_cap_exist = true;
2441                         }
2442
2443                         if (info_element->len > 4 &&
2444                                 info_element->data[0] == 0x00 &&
2445                                 info_element->data[1] == 0x40 &&
2446                                 info_element->data[2] == 0x96 &&
2447                                 info_element->data[3] == 0x01)
2448                         {
2449                                 if (info_element->len == 6)
2450                                 {
2451                                         memcpy(network->CcxRmState, &info_element[4], 2);
2452                                         if (network->CcxRmState[0] != 0)
2453                                         {
2454                                                 network->bCcxRmEnable = true;
2455                                         }
2456                                         else
2457                                                 network->bCcxRmEnable = false;
2458                                         network->MBssidMask = network->CcxRmState[1] & 0x07;
2459                                         if (network->MBssidMask != 0)
2460                                         {
2461                                                 network->bMBssidValid = true;
2462                                                 network->MBssidMask = 0xff << (network->MBssidMask);
2463                                                 memcpy(network->MBssid, network->bssid, ETH_ALEN);
2464                                                 network->MBssid[5] &= network->MBssidMask;
2465                                         }
2466                                         else
2467                                         {
2468                                                 network->bMBssidValid = false;
2469                                         }
2470                                 }
2471                                 else
2472                                 {
2473                                         network->bCcxRmEnable = false;
2474                                 }
2475                         }
2476                         if (info_element->len > 4  &&
2477                                 info_element->data[0] == 0x00 &&
2478                                 info_element->data[1] == 0x40 &&
2479                                 info_element->data[2] == 0x96 &&
2480                                 info_element->data[3] == 0x03)
2481                         {
2482                                 if (info_element->len == 5)
2483                                 {
2484                                         network->bWithCcxVerNum = true;
2485                                         network->BssCcxVerNumber = info_element->data[4];
2486                                 }
2487                                 else
2488                                 {
2489                                         network->bWithCcxVerNum = false;
2490                                         network->BssCcxVerNumber = 0;
2491                                 }
2492                         }
2493                         if (info_element->len > 4  &&
2494                                 info_element->data[0] == 0x00 &&
2495                                 info_element->data[1] == 0x50 &&
2496                                 info_element->data[2] == 0xf2 &&
2497                                 info_element->data[3] == 0x04)
2498                         {
2499                                 RTLLIB_DEBUG_MGMT("MFIE_TYPE_WZC: %d bytes\n",
2500                                                      info_element->len);
2501                                 network->wzc_ie_len = min(info_element->len+2,
2502                                                           MAX_WZC_IE_LEN);
2503                                 memcpy(network->wzc_ie, info_element,
2504                                                 network->wzc_ie_len);
2505                         }
2506                         break;
2507
2508                 case MFIE_TYPE_RSN:
2509                         RTLLIB_DEBUG_MGMT("MFIE_TYPE_RSN: %d bytes\n",
2510                                              info_element->len);
2511                         network->rsn_ie_len = min(info_element->len + 2,
2512                                                   MAX_WPA_IE_LEN);
2513                         memcpy(network->rsn_ie, info_element,
2514                                network->rsn_ie_len);
2515                         break;
2516
2517                 case MFIE_TYPE_HT_CAP:
2518                         RTLLIB_DEBUG_SCAN("MFIE_TYPE_HT_CAP: %d bytes\n",
2519                                              info_element->len);
2520                         tmp_htcap_len = min(info_element->len,(u8)MAX_IE_LEN);
2521                         if (tmp_htcap_len != 0){
2522                                 network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
2523                                 network->bssht.bdHTCapLen = tmp_htcap_len > sizeof(network->bssht.bdHTCapBuf)?\
2524                                         sizeof(network->bssht.bdHTCapBuf):tmp_htcap_len;
2525                                 memcpy(network->bssht.bdHTCapBuf,info_element->data,network->bssht.bdHTCapLen);
2526
2527                                 network->bssht.bdSupportHT = true;
2528                                 network->bssht.bdHT1R = ((((PHT_CAPABILITY_ELE)(network->bssht.bdHTCapBuf))->MCS[1]) == 0);
2529
2530                                 network->bssht.bdBandWidth = (HT_CHANNEL_WIDTH)(((PHT_CAPABILITY_ELE)(network->bssht.bdHTCapBuf))->ChlWidth);
2531                         }
2532                         else{
2533                                 network->bssht.bdSupportHT = false;
2534                                 network->bssht.bdHT1R = false;
2535                                 network->bssht.bdBandWidth = HT_CHANNEL_WIDTH_20 ;
2536                         }
2537                         break;
2538
2539
2540                 case MFIE_TYPE_HT_INFO:
2541                         RTLLIB_DEBUG_SCAN("MFIE_TYPE_HT_INFO: %d bytes\n",
2542                                              info_element->len);
2543                         tmp_htinfo_len = min(info_element->len,(u8)MAX_IE_LEN);
2544                         if (tmp_htinfo_len){
2545                                 network->bssht.bdHTSpecVer = HT_SPEC_VER_IEEE;
2546                                 network->bssht.bdHTInfoLen = tmp_htinfo_len > sizeof(network->bssht.bdHTInfoBuf)?\
2547                                         sizeof(network->bssht.bdHTInfoBuf):tmp_htinfo_len;
2548                                 memcpy(network->bssht.bdHTInfoBuf,info_element->data,network->bssht.bdHTInfoLen);
2549                         }
2550                         break;
2551
2552                 case MFIE_TYPE_AIRONET:
2553                         RTLLIB_DEBUG_SCAN("MFIE_TYPE_AIRONET: %d bytes\n",
2554                                              info_element->len);
2555                         if (info_element->len >IE_CISCO_FLAG_POSITION)
2556                         {
2557                                 network->bWithAironetIE = true;
2558
2559                                 if (    (info_element->data[IE_CISCO_FLAG_POSITION]&SUPPORT_CKIP_MIC)   ||
2560                                         (info_element->data[IE_CISCO_FLAG_POSITION]&SUPPORT_CKIP_PK)    )
2561                                 {
2562                                         network->bCkipSupported = true;
2563                                 }
2564                                 else
2565                                 {
2566                                         network->bCkipSupported = false;
2567                                 }
2568                         }
2569                         else
2570                         {
2571                                 network->bWithAironetIE = false;
2572                                 network->bCkipSupported = false;
2573                         }
2574                         break;
2575                 case MFIE_TYPE_QOS_PARAMETER:
2576                         printk(KERN_ERR
2577                                "QoS Error need to parse QOS_PARAMETER IE\n");
2578                         break;
2579
2580 #ifdef ENABLE_DOT11D
2581                 case MFIE_TYPE_COUNTRY:
2582                         RTLLIB_DEBUG_SCAN("MFIE_TYPE_COUNTRY: %d bytes\n",
2583                                              info_element->len);
2584                         rtllib_extract_country_ie(ieee, info_element, network, network->bssid);
2585                         break;
2586 #endif
2587 /* TODO */
2588                 default:
2589                         RTLLIB_DEBUG_MGMT
2590                             ("Unsupported info element: %s (%d)\n",
2591                              get_info_element_string(info_element->id),
2592                              info_element->id);
2593                         break;
2594                 }
2595
2596                 length -= sizeof(*info_element) + info_element->len;
2597                 info_element =
2598                     (struct rtllib_info_element *)&info_element->
2599                     data[info_element->len];
2600         }
2601
2602         if (!network->atheros_cap_exist && !network->broadcom_cap_exist &&
2603                 !network->cisco_cap_exist && !network->ralink_cap_exist && !network->bssht.bdRT2RTAggregation)
2604         {
2605                 network->unknown_cap_exist = true;
2606         }
2607         else
2608         {
2609                 network->unknown_cap_exist = false;
2610         }
2611         return 0;
2612 }
2613
2614 static inline u8 rtllib_SignalStrengthTranslate(
2615         u8  CurrSS
2616         )
2617 {
2618         u8 RetSS;
2619
2620         if (CurrSS >= 71 && CurrSS <= 100)
2621         {
2622                 RetSS = 90 + ((CurrSS - 70) / 3);
2623         }
2624         else if (CurrSS >= 41 && CurrSS <= 70)
2625         {
2626                 RetSS = 78 + ((CurrSS - 40) / 3);
2627         }
2628         else if (CurrSS >= 31 && CurrSS <= 40)
2629         {
2630                 RetSS = 66 + (CurrSS - 30);
2631         }
2632         else if (CurrSS >= 21 && CurrSS <= 30)
2633         {
2634                 RetSS = 54 + (CurrSS - 20);
2635         }
2636         else if (CurrSS >= 5 && CurrSS <= 20)
2637         {
2638                 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
2639         }
2640         else if (CurrSS == 4)
2641         {
2642                 RetSS = 36;
2643         }
2644         else if (CurrSS == 3)
2645         {
2646                 RetSS = 27;
2647         }
2648         else if (CurrSS == 2)
2649         {
2650                 RetSS = 18;
2651         }
2652         else if (CurrSS == 1)
2653         {
2654                 RetSS = 9;
2655         }
2656         else
2657         {
2658                 RetSS = CurrSS;
2659         }
2660
2661
2662
2663         return RetSS;
2664 }
2665
2666 long rtllib_translate_todbm(u8 signal_strength_index    )
2667 {
2668         long    signal_power;
2669
2670         signal_power = (long)((signal_strength_index + 1) >> 1);
2671         signal_power -= 95;
2672
2673         return signal_power;
2674 }
2675
2676 static inline int rtllib_network_init(
2677         struct rtllib_device *ieee,
2678         struct rtllib_probe_response *beacon,
2679         struct rtllib_network *network,
2680         struct rtllib_rx_stats *stats)
2681 {
2682 #ifdef CONFIG_RTLLIB_DEBUG
2683 #endif
2684
2685         /*
2686         network->qos_data.active = 0;
2687         network->qos_data.supported = 0;
2688         network->qos_data.param_count = 0;
2689         network->qos_data.old_param_count = 0;
2690         */
2691         memset(&network->qos_data, 0, sizeof(struct rtllib_qos_data));
2692
2693         /* Pull out fixed field data */
2694         memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
2695         network->capability = le16_to_cpu(beacon->capability);
2696         network->last_scanned = jiffies;
2697         network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
2698         network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
2699         network->beacon_interval = le32_to_cpu(beacon->beacon_interval);
2700         /* Where to pull this? beacon->listen_interval;*/
2701         network->listen_interval = 0x0A;
2702         network->rates_len = network->rates_ex_len = 0;
2703         network->last_associate = 0;
2704         network->ssid_len = 0;
2705         network->hidden_ssid_len = 0;
2706         memset(network->hidden_ssid, 0, sizeof(network->hidden_ssid));
2707         network->flags = 0;
2708         network->atim_window = 0;
2709         network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
2710             0x3 : 0x0;
2711         network->berp_info_valid = false;
2712         network->broadcom_cap_exist = false;
2713         network->ralink_cap_exist = false;
2714         network->atheros_cap_exist = false;
2715         network->cisco_cap_exist = false;
2716         network->unknown_cap_exist = false;
2717         network->realtek_cap_exit = false;
2718         network->marvell_cap_exist = false;
2719         network->airgo_cap_exist = false;
2720         network->Turbo_Enable = 0;
2721         network->SignalStrength = stats->SignalStrength;
2722         network->RSSI = stats->SignalStrength;
2723 #ifdef ENABLE_DOT11D
2724         network->CountryIeLen = 0;
2725         memset(network->CountryIeBuf, 0, MAX_IE_LEN);
2726 #endif
2727         HTInitializeBssDesc(&network->bssht);
2728         if (stats->freq == RTLLIB_52GHZ_BAND) {
2729                 /* for A band (No DS info) */
2730                 network->channel = stats->received_channel;
2731         } else
2732                 network->flags |= NETWORK_HAS_CCK;
2733
2734         network->wpa_ie_len = 0;
2735         network->rsn_ie_len = 0;
2736         network->wzc_ie_len = 0;
2737
2738         if (rtllib_parse_info_param(ieee,
2739                         beacon->info_element,
2740                         (stats->len - sizeof(*beacon)),
2741                         network,
2742                         stats))
2743                 return 1;
2744
2745         network->mode = 0;
2746         if (stats->freq == RTLLIB_52GHZ_BAND)
2747                 network->mode = IEEE_A;
2748         else {
2749                 if (network->flags & NETWORK_HAS_OFDM)
2750                         network->mode |= IEEE_G;
2751                 if (network->flags & NETWORK_HAS_CCK)
2752                         network->mode |= IEEE_B;
2753         }
2754
2755         if (network->mode == 0) {
2756                 RTLLIB_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
2757                                      "network.\n",
2758                                      escape_essid(network->ssid,
2759                                                   network->ssid_len),
2760                                      MAC_ARG(network->bssid));
2761                 return 1;
2762         }
2763
2764         if (network->bssht.bdSupportHT){
2765                 if (network->mode == IEEE_A)
2766                         network->mode = IEEE_N_5G;
2767                 else if (network->mode & (IEEE_G | IEEE_B))
2768                         network->mode = IEEE_N_24G;
2769         }
2770         if (rtllib_is_empty_essid(network->ssid, network->ssid_len))
2771                 network->flags |= NETWORK_EMPTY_ESSID;
2772         stats->signal = 30 + (stats->SignalStrength * 70) / 100;
2773         stats->noise = rtllib_translate_todbm((u8)(100-stats->signal)) -25;
2774
2775         memcpy(&network->stats, stats, sizeof(network->stats));
2776
2777         return 0;
2778 }
2779
2780 static inline int is_same_network(struct rtllib_network *src,
2781                                   struct rtllib_network *dst, u8 ssidbroad)
2782 {
2783         /* A network is only a duplicate if the channel, BSSID, ESSID
2784          * and the capability field (in particular IBSS and BSS) all match.
2785          * We treat all <hidden> with the same BSSID and channel
2786          * as one network */
2787         return
2788                 (((src->ssid_len == dst->ssid_len) || (!ssidbroad)) &&
2789                 (src->channel == dst->channel) &&
2790                 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
2791                 (!memcmp(src->ssid, dst->ssid, src->ssid_len) || (!ssidbroad)) &&
2792                 ((src->capability & WLAN_CAPABILITY_IBSS) ==
2793                 (dst->capability & WLAN_CAPABILITY_IBSS)) &&
2794                 ((src->capability & WLAN_CAPABILITY_ESS) ==
2795                 (dst->capability & WLAN_CAPABILITY_ESS)));
2796 }
2797
2798 static inline void update_ibss_network(struct rtllib_network *dst,
2799                                   struct rtllib_network *src)
2800 {
2801         memcpy(&dst->stats, &src->stats, sizeof(struct rtllib_rx_stats));
2802         dst->last_scanned = jiffies;
2803 }
2804
2805
2806 static inline void update_network(struct rtllib_network *dst,
2807                                   struct rtllib_network *src)
2808 {
2809         int qos_active;
2810         u8 old_param;
2811
2812         memcpy(&dst->stats, &src->stats, sizeof(struct rtllib_rx_stats));
2813         dst->capability = src->capability;
2814         memcpy(dst->rates, src->rates, src->rates_len);
2815         dst->rates_len = src->rates_len;
2816         memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
2817         dst->rates_ex_len = src->rates_ex_len;
2818         if (src->ssid_len > 0)
2819         {
2820                 if (dst->ssid_len == 0)
2821                 {
2822                         memset(dst->hidden_ssid, 0, sizeof(dst->hidden_ssid));
2823                         dst->hidden_ssid_len = src->ssid_len;
2824                         memcpy(dst->hidden_ssid, src->ssid, src->ssid_len);
2825                 }else{
2826                         memset(dst->ssid, 0, dst->ssid_len);
2827                         dst->ssid_len = src->ssid_len;
2828                         memcpy(dst->ssid, src->ssid, src->ssid_len);
2829                 }
2830         }
2831         dst->mode = src->mode;
2832         dst->flags = src->flags;
2833         dst->time_stamp[0] = src->time_stamp[0];
2834         dst->time_stamp[1] = src->time_stamp[1];
2835         if (src->flags & NETWORK_HAS_ERP_VALUE)
2836         {
2837                 dst->erp_value = src->erp_value;
2838                 dst->berp_info_valid = src->berp_info_valid = true;
2839         }
2840         dst->beacon_interval = src->beacon_interval;
2841         dst->listen_interval = src->listen_interval;
2842         dst->atim_window = src->atim_window;
2843         dst->dtim_period = src->dtim_period;
2844         dst->dtim_data = src->dtim_data;
2845         dst->last_dtim_sta_time[0] = src->last_dtim_sta_time[0];
2846         dst->last_dtim_sta_time[1] = src->last_dtim_sta_time[1];
2847         memcpy(&dst->tim, &src->tim, sizeof(struct rtllib_tim_parameters));
2848
2849         dst->bssht.bdSupportHT = src->bssht.bdSupportHT;
2850         dst->bssht.bdRT2RTAggregation = src->bssht.bdRT2RTAggregation;
2851         dst->bssht.bdHTCapLen= src->bssht.bdHTCapLen;
2852         memcpy(dst->bssht.bdHTCapBuf,src->bssht.bdHTCapBuf,src->bssht.bdHTCapLen);
2853         dst->bssht.bdHTInfoLen= src->bssht.bdHTInfoLen;
2854         memcpy(dst->bssht.bdHTInfoBuf,src->bssht.bdHTInfoBuf,src->bssht.bdHTInfoLen);
2855         dst->bssht.bdHTSpecVer = src->bssht.bdHTSpecVer;
2856         dst->bssht.bdRT2RTLongSlotTime = src->bssht.bdRT2RTLongSlotTime;
2857         dst->broadcom_cap_exist = src->broadcom_cap_exist;
2858         dst->ralink_cap_exist = src->ralink_cap_exist;
2859         dst->atheros_cap_exist = src->atheros_cap_exist;
2860         dst->realtek_cap_exit = src->realtek_cap_exit;
2861         dst->marvell_cap_exist = src->marvell_cap_exist;
2862         dst->cisco_cap_exist = src->cisco_cap_exist;
2863         dst->airgo_cap_exist = src->airgo_cap_exist;
2864         dst->unknown_cap_exist = src->unknown_cap_exist;
2865         memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
2866         dst->wpa_ie_len = src->wpa_ie_len;
2867         memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
2868         dst->rsn_ie_len = src->rsn_ie_len;
2869         memcpy(dst->wzc_ie, src->wzc_ie, src->wzc_ie_len);
2870         dst->wzc_ie_len = src->wzc_ie_len;
2871
2872         dst->last_scanned = jiffies;
2873         /* qos related parameters */
2874         qos_active = dst->qos_data.active;
2875         old_param = dst->qos_data.param_count;
2876         dst->qos_data.supported = src->qos_data.supported;
2877         if (dst->flags & NETWORK_HAS_QOS_PARAMETERS)
2878                 memcpy(&dst->qos_data, &src->qos_data, sizeof(struct rtllib_qos_data));
2879         if (dst->qos_data.supported == 1) {
2880                 if (dst->ssid_len)
2881                         RTLLIB_DEBUG_QOS
2882                                 ("QoS the network %s is QoS supported\n",
2883                                 dst->ssid);
2884                 else
2885                         RTLLIB_DEBUG_QOS
2886                                 ("QoS the network is QoS supported\n");
2887         }
2888         dst->qos_data.active = qos_active;
2889         dst->qos_data.old_param_count = old_param;
2890
2891         /* dst->last_associate is not overwritten */
2892         dst->wmm_info = src->wmm_info;
2893         if (src->wmm_param[0].ac_aci_acm_aifsn|| \
2894            src->wmm_param[1].ac_aci_acm_aifsn|| \
2895            src->wmm_param[2].ac_aci_acm_aifsn|| \
2896            src->wmm_param[1].ac_aci_acm_aifsn) {
2897           memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
2898         }
2899
2900         dst->SignalStrength = src->SignalStrength;
2901         dst->RSSI = src->RSSI;
2902         dst->Turbo_Enable = src->Turbo_Enable;
2903
2904 #ifdef ENABLE_DOT11D
2905         dst->CountryIeLen = src->CountryIeLen;
2906         memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
2907 #endif
2908
2909         dst->bWithAironetIE = src->bWithAironetIE;
2910         dst->bCkipSupported = src->bCkipSupported;
2911         memcpy(dst->CcxRmState,src->CcxRmState,2);
2912         dst->bCcxRmEnable = src->bCcxRmEnable;
2913         dst->MBssidMask = src->MBssidMask;
2914         dst->bMBssidValid = src->bMBssidValid;
2915         memcpy(dst->MBssid,src->MBssid,6);
2916         dst->bWithCcxVerNum = src->bWithCcxVerNum;
2917         dst->BssCcxVerNumber = src->BssCcxVerNumber;
2918
2919 }
2920 static inline int is_beacon(__le16 fc)
2921 {
2922         return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == RTLLIB_STYPE_BEACON);
2923 }
2924
2925 static int IsPassiveChannel( struct rtllib_device *rtllib, u8 channel)
2926 {
2927         if (MAX_CHANNEL_NUMBER < channel) {
2928                 printk("%s(): Invalid Channel\n", __func__);
2929                 return 0;
2930         }
2931
2932         if (rtllib->active_channel_map[channel] == 2)
2933                 return 1;
2934
2935         return 0;
2936 }
2937
2938 int IsLegalChannel( struct rtllib_device *rtllib, u8 channel)
2939 {
2940         if (MAX_CHANNEL_NUMBER < channel) {
2941                 printk("%s(): Invalid Channel\n", __func__);
2942                 return 0;
2943         }
2944         if (rtllib->active_channel_map[channel] > 0)
2945                 return 1;
2946
2947         return 0;
2948 }
2949
2950
2951 static inline void rtllib_process_probe_response(
2952         struct rtllib_device *ieee,
2953         struct rtllib_probe_response *beacon,
2954         struct rtllib_rx_stats *stats)
2955 {
2956         struct rtllib_network *target;
2957         struct rtllib_network *oldest = NULL;
2958 #ifdef CONFIG_RTLLIB_DEBUG
2959         struct rtllib_info_element *info_element = &beacon->info_element[0];
2960 #endif
2961         unsigned long flags;
2962         short renew;
2963         struct rtllib_network *network = kzalloc(sizeof(struct rtllib_network), GFP_ATOMIC);
2964
2965         if (!network)
2966                 return;
2967
2968         RTLLIB_DEBUG_SCAN(
2969                 "'%s' (" MAC_FMT "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
2970                 escape_essid(info_element->data, info_element->len),
2971                 MAC_ARG(beacon->header.addr3),
2972                 (beacon->capability & (1<<0xf)) ? '1' : '0',
2973                 (beacon->capability & (1<<0xe)) ? '1' : '0',
2974                 (beacon->capability & (1<<0xd)) ? '1' : '0',
2975                 (beacon->capability & (1<<0xc)) ? '1' : '0',
2976                 (beacon->capability & (1<<0xb)) ? '1' : '0',
2977                 (beacon->capability & (1<<0xa)) ? '1' : '0',
2978                 (beacon->capability & (1<<0x9)) ? '1' : '0',
2979                 (beacon->capability & (1<<0x8)) ? '1' : '0',
2980                 (beacon->capability & (1<<0x7)) ? '1' : '0',
2981                 (beacon->capability & (1<<0x6)) ? '1' : '0',
2982                 (beacon->capability & (1<<0x5)) ? '1' : '0',
2983                 (beacon->capability & (1<<0x4)) ? '1' : '0',
2984                 (beacon->capability & (1<<0x3)) ? '1' : '0',
2985                 (beacon->capability & (1<<0x2)) ? '1' : '0',
2986                 (beacon->capability & (1<<0x1)) ? '1' : '0',
2987                 (beacon->capability & (1<<0x0)) ? '1' : '0');
2988
2989         if (rtllib_network_init(ieee, beacon, network, stats)) {
2990                 RTLLIB_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
2991                                      escape_essid(info_element->data,
2992                                                   info_element->len),
2993                                      MAC_ARG(beacon->header.addr3),
2994                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
2995                                      RTLLIB_STYPE_PROBE_RESP ?
2996                                      "PROBE RESPONSE" : "BEACON");
2997                 goto free_network;
2998         }
2999
3000
3001         if (!IsLegalChannel(ieee, network->channel))
3002                 goto free_network;
3003
3004         if (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == RTLLIB_STYPE_PROBE_RESP) {
3005                 if (IsPassiveChannel(ieee, network->channel)) {
3006                         printk("GetScanInfo(): For Global Domain, "
3007                                "filter probe response at channel(%d).\n", network->channel);
3008                         goto free_network;
3009                 }
3010         }
3011
3012         /* The network parsed correctly -- so now we scan our known networks
3013          * to see if we can find it in our list.
3014          *
3015          * NOTE:  This search is definitely not optimized.  Once its doing
3016          *        the "right thing" we'll optimize it for efficiency if
3017          *        necessary */
3018
3019         /* Search for this entry in the list and update it if it is
3020          * already there. */
3021
3022         spin_lock_irqsave(&ieee->lock, flags);
3023         {
3024                 if (is_same_network(&ieee->current_network, network, (network->ssid_len?1:0))) {
3025                         update_network(&ieee->current_network, network);
3026                         if ((ieee->current_network.mode == IEEE_N_24G || ieee->current_network.mode == IEEE_G)
3027                         && ieee->current_network.berp_info_valid){
3028                         if (ieee->current_network.erp_value& ERP_UseProtection)
3029                                 ieee->current_network.buseprotection = true;
3030                 else
3031                         ieee->current_network.buseprotection = false;
3032                 }
3033                 if (is_beacon(beacon->header.frame_ctl))
3034                 {
3035                                 if (ieee->state >= RTLLIB_LINKED)
3036                                         ieee->LinkDetectInfo.NumRecvBcnInPeriod++;
3037                         }
3038                 }
3039         }
3040         list_for_each_entry(target, &ieee->network_list, list) {
3041                 if (is_same_network(target, network,(target->ssid_len?1:0)))
3042                         break;
3043                 if ((oldest == NULL) ||
3044                     (target->last_scanned < oldest->last_scanned))
3045                         oldest = target;
3046         }
3047
3048         /* If we didn't find a match, then get a new network slot to initialize
3049          * with this beacon's information */
3050         if (&target->list == &ieee->network_list) {
3051                 if (list_empty(&ieee->network_free_list)) {
3052                         /* If there are no more slots, expire the oldest */
3053                         list_del(&oldest->list);
3054                         target = oldest;
3055                         RTLLIB_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
3056                                              "network list.\n",
3057                                              escape_essid(target->ssid,
3058                                                           target->ssid_len),
3059                                              MAC_ARG(target->bssid));
3060                 } else {
3061                         /* Otherwise just pull from the free list */
3062                         target = list_entry(ieee->network_free_list.next,
3063                                             struct rtllib_network, list);
3064                         list_del(ieee->network_free_list.next);
3065                 }
3066
3067
3068 #ifdef CONFIG_RTLLIB_DEBUG
3069                 RTLLIB_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
3070                                      escape_essid(network->ssid,
3071                                                   network->ssid_len),
3072                                      MAC_ARG(network->bssid),
3073                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
3074                                      RTLLIB_STYPE_PROBE_RESP ?
3075                                      "PROBE RESPONSE" : "BEACON");
3076 #endif
3077                 memcpy(target, network, sizeof(*target));
3078                 list_add_tail(&target->list, &ieee->network_list);
3079                 if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
3080                         rtllib_softmac_new_net(ieee, network);
3081         } else {
3082                 RTLLIB_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
3083                                      escape_essid(target->ssid,
3084                                                   target->ssid_len),
3085                                      MAC_ARG(target->bssid),
3086                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
3087                                      RTLLIB_STYPE_PROBE_RESP ?
3088                                      "PROBE RESPONSE" : "BEACON");
3089
3090                 /* we have an entry and we are going to update it. But this entry may
3091                  * be already expired. In this case we do the same as we found a new
3092                  * net and call the new_net handler
3093                  */
3094                 renew = !time_after(target->last_scanned + ieee->scan_age, jiffies);
3095                 if ((!target->ssid_len) &&
3096                         (((network->ssid_len > 0) && (target->hidden_ssid_len == 0))
3097                         || ((ieee->current_network.ssid_len == network->ssid_len) &&
3098                            (strncmp(ieee->current_network.ssid, network->ssid, network->ssid_len) == 0) &&
3099                            (ieee->state == RTLLIB_NOLINK)))
3100                         ) {
3101                         renew = 1;
3102                 }
3103                 update_network(target, network);
3104                 if (renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
3105                         rtllib_softmac_new_net(ieee, network);
3106         }
3107
3108         spin_unlock_irqrestore(&ieee->lock, flags);
3109         if (is_beacon(beacon->header.frame_ctl)&&is_same_network(&ieee->current_network, network, (network->ssid_len?1:0))&&\
3110                 (ieee->state == RTLLIB_LINKED)) {
3111                 if (ieee->handle_beacon != NULL) {
3112                         ieee->handle_beacon(ieee->dev,beacon,&ieee->current_network);
3113                 }
3114         }
3115 free_network:
3116         kfree(network);
3117         return;
3118 }
3119
3120 void rtllib_rx_mgt(struct rtllib_device *ieee,
3121                       struct sk_buff *skb,
3122                       struct rtllib_rx_stats *stats)
3123 {
3124     struct rtllib_hdr_4addr *header = (struct rtllib_hdr_4addr *)skb->data ;
3125     if (WLAN_FC_GET_STYPE(header->frame_ctl) != RTLLIB_STYPE_PROBE_RESP &&
3126             WLAN_FC_GET_STYPE(header->frame_ctl) != RTLLIB_STYPE_BEACON)
3127         ieee->last_rx_ps_time = jiffies;
3128
3129     switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
3130
3131         case RTLLIB_STYPE_BEACON:
3132             RTLLIB_DEBUG_MGMT("received BEACON (%d)\n",
3133                     WLAN_FC_GET_STYPE(header->frame_ctl));
3134             RTLLIB_DEBUG_SCAN("Beacon\n");
3135             rtllib_process_probe_response(
3136                     ieee, (struct rtllib_probe_response *)header, stats);
3137
3138             if (ieee->sta_sleep || (ieee->ps != RTLLIB_PS_DISABLED &&
3139                         ieee->iw_mode == IW_MODE_INFRA &&
3140                         ieee->state == RTLLIB_LINKED))
3141                 tasklet_schedule(&ieee->ps_task);
3142
3143             break;
3144
3145         case RTLLIB_STYPE_PROBE_RESP:
3146             RTLLIB_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
3147                     WLAN_FC_GET_STYPE(header->frame_ctl));
3148             RTLLIB_DEBUG_SCAN("Probe response\n");
3149             rtllib_process_probe_response(
3150                     ieee, (struct rtllib_probe_response *)header, stats);
3151             break;
3152         case RTLLIB_STYPE_PROBE_REQ:
3153             RTLLIB_DEBUG_MGMT("received PROBE RESQUEST (%d)\n",
3154                     WLAN_FC_GET_STYPE(header->frame_ctl));
3155             RTLLIB_DEBUG_SCAN("Probe request\n");
3156             if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
3157                     ((ieee->iw_mode == IW_MODE_ADHOC ||
3158                       ieee->iw_mode == IW_MODE_MASTER) &&
3159                      ieee->state == RTLLIB_LINKED)){
3160                 rtllib_rx_probe_rq(ieee, skb);
3161             }
3162             break;
3163     }
3164
3165 }