3bf04d6d2b13479ca3112f2abf5450830a37f564
[sfrench/cifs-2.6.git] / net / ieee80211 / ieee80211_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-2005, 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 #include <linux/compiler.h>
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/if_arp.h>
20 #include <linux/in6.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/proc_fs.h>
27 #include <linux/skbuff.h>
28 #include <linux/slab.h>
29 #include <linux/tcp.h>
30 #include <linux/types.h>
31 #include <linux/version.h>
32 #include <linux/wireless.h>
33 #include <linux/etherdevice.h>
34 #include <asm/uaccess.h>
35 #include <linux/ctype.h>
36
37 #include <net/ieee80211.h>
38
39 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
40                                         struct sk_buff *skb,
41                                         struct ieee80211_rx_stats *rx_stats)
42 {
43         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
44         u16 fc = le16_to_cpu(hdr->frame_ctl);
45
46         skb->dev = ieee->dev;
47         skb->mac.raw = skb->data;
48         skb_pull(skb, ieee80211_get_hdrlen(fc));
49         skb->pkt_type = PACKET_OTHERHOST;
50         skb->protocol = __constant_htons(ETH_P_80211_RAW);
51         memset(skb->cb, 0, sizeof(skb->cb));
52         netif_rx(skb);
53 }
54
55 /* Called only as a tasklet (software IRQ) */
56 static struct ieee80211_frag_entry *ieee80211_frag_cache_find(struct
57                                                               ieee80211_device
58                                                               *ieee,
59                                                               unsigned int seq,
60                                                               unsigned int frag,
61                                                               u8 * src,
62                                                               u8 * dst)
63 {
64         struct ieee80211_frag_entry *entry;
65         int i;
66
67         for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
68                 entry = &ieee->frag_cache[i];
69                 if (entry->skb != NULL &&
70                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
71                         IEEE80211_DEBUG_FRAG("expiring fragment cache entry "
72                                              "seq=%u last_frag=%u\n",
73                                              entry->seq, entry->last_frag);
74                         dev_kfree_skb_any(entry->skb);
75                         entry->skb = NULL;
76                 }
77
78                 if (entry->skb != NULL && entry->seq == seq &&
79                     (entry->last_frag + 1 == frag || frag == -1) &&
80                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
81                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
82                         return entry;
83         }
84
85         return NULL;
86 }
87
88 /* Called only as a tasklet (software IRQ) */
89 static struct sk_buff *ieee80211_frag_cache_get(struct ieee80211_device *ieee,
90                                                 struct ieee80211_hdr_4addr *hdr)
91 {
92         struct sk_buff *skb = NULL;
93         u16 sc;
94         unsigned int frag, seq;
95         struct ieee80211_frag_entry *entry;
96
97         sc = le16_to_cpu(hdr->seq_ctl);
98         frag = WLAN_GET_SEQ_FRAG(sc);
99         seq = WLAN_GET_SEQ_SEQ(sc);
100
101         if (frag == 0) {
102                 /* Reserve enough space to fit maximum frame length */
103                 skb = dev_alloc_skb(ieee->dev->mtu +
104                                     sizeof(struct ieee80211_hdr_4addr) +
105                                     8 /* LLC */  +
106                                     2 /* alignment */  +
107                                     8 /* WEP */  + ETH_ALEN /* WDS */ );
108                 if (skb == NULL)
109                         return NULL;
110
111                 entry = &ieee->frag_cache[ieee->frag_next_idx];
112                 ieee->frag_next_idx++;
113                 if (ieee->frag_next_idx >= IEEE80211_FRAG_CACHE_LEN)
114                         ieee->frag_next_idx = 0;
115
116                 if (entry->skb != NULL)
117                         dev_kfree_skb_any(entry->skb);
118
119                 entry->first_frag_time = jiffies;
120                 entry->seq = seq;
121                 entry->last_frag = frag;
122                 entry->skb = skb;
123                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
124                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
125         } else {
126                 /* received a fragment of a frame for which the head fragment
127                  * should have already been received */
128                 entry = ieee80211_frag_cache_find(ieee, seq, frag, hdr->addr2,
129                                                   hdr->addr1);
130                 if (entry != NULL) {
131                         entry->last_frag = frag;
132                         skb = entry->skb;
133                 }
134         }
135
136         return skb;
137 }
138
139 /* Called only as a tasklet (software IRQ) */
140 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
141                                            struct ieee80211_hdr_4addr *hdr)
142 {
143         u16 sc;
144         unsigned int seq;
145         struct ieee80211_frag_entry *entry;
146
147         sc = le16_to_cpu(hdr->seq_ctl);
148         seq = WLAN_GET_SEQ_SEQ(sc);
149
150         entry = ieee80211_frag_cache_find(ieee, seq, -1, hdr->addr2,
151                                           hdr->addr1);
152
153         if (entry == NULL) {
154                 IEEE80211_DEBUG_FRAG("could not invalidate fragment cache "
155                                      "entry (seq=%u)\n", seq);
156                 return -1;
157         }
158
159         entry->skb = NULL;
160         return 0;
161 }
162
163 #ifdef NOT_YET
164 /* ieee80211_rx_frame_mgtmt
165  *
166  * Responsible for handling management control frames
167  *
168  * Called by ieee80211_rx */
169 static inline int
170 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
171                         struct ieee80211_rx_stats *rx_stats, u16 type,
172                         u16 stype)
173 {
174         if (ieee->iw_mode == IW_MODE_MASTER) {
175                 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
176                        ieee->dev->name);
177                 return 0;
178 /*
179   hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr_4addr *)
180   skb->data);*/
181         }
182
183         if (ieee->hostapd && type == WLAN_FC_TYPE_MGMT) {
184                 if (stype == WLAN_FC_STYPE_BEACON &&
185                     ieee->iw_mode == IW_MODE_MASTER) {
186                         struct sk_buff *skb2;
187                         /* Process beacon frames also in kernel driver to
188                          * update STA(AP) table statistics */
189                         skb2 = skb_clone(skb, GFP_ATOMIC);
190                         if (skb2)
191                                 hostap_rx(skb2->dev, skb2, rx_stats);
192                 }
193
194                 /* send management frames to the user space daemon for
195                  * processing */
196                 ieee->apdevstats.rx_packets++;
197                 ieee->apdevstats.rx_bytes += skb->len;
198                 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
199                 return 0;
200         }
201
202         if (ieee->iw_mode == IW_MODE_MASTER) {
203                 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
204                         printk(KERN_DEBUG "%s: unknown management frame "
205                                "(type=0x%02x, stype=0x%02x) dropped\n",
206                                skb->dev->name, type, stype);
207                         return -1;
208                 }
209
210                 hostap_rx(skb->dev, skb, rx_stats);
211                 return 0;
212         }
213
214         printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
215                "received in non-Host AP mode\n", skb->dev->name);
216         return -1;
217 }
218 #endif
219
220 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
221 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
222 static unsigned char rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
223
224 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
225 static unsigned char bridge_tunnel_header[] =
226     { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
227 /* No encapsulation header if EtherType < 0x600 (=length) */
228
229 /* Called by ieee80211_rx_frame_decrypt */
230 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
231                                     struct sk_buff *skb)
232 {
233         struct net_device *dev = ieee->dev;
234         u16 fc, ethertype;
235         struct ieee80211_hdr_3addr *hdr;
236         u8 *pos;
237
238         if (skb->len < 24)
239                 return 0;
240
241         hdr = (struct ieee80211_hdr_3addr *)skb->data;
242         fc = le16_to_cpu(hdr->frame_ctl);
243
244         /* check that the frame is unicast frame to us */
245         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
246             IEEE80211_FCTL_TODS &&
247             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
248             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
249                 /* ToDS frame with own addr BSSID and DA */
250         } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
251                    IEEE80211_FCTL_FROMDS &&
252                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
253                 /* FromDS frame with own addr as DA */
254         } else
255                 return 0;
256
257         if (skb->len < 24 + 8)
258                 return 0;
259
260         /* check for port access entity Ethernet type */
261         pos = skb->data + 24;
262         ethertype = (pos[6] << 8) | pos[7];
263         if (ethertype == ETH_P_PAE)
264                 return 1;
265
266         return 0;
267 }
268
269 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
270 static inline int
271 ieee80211_rx_frame_decrypt(struct ieee80211_device *ieee, struct sk_buff *skb,
272                            struct ieee80211_crypt_data *crypt)
273 {
274         struct ieee80211_hdr_3addr *hdr;
275         int res, hdrlen;
276
277         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
278                 return 0;
279
280         hdr = (struct ieee80211_hdr_3addr *)skb->data;
281         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
282
283         atomic_inc(&crypt->refcnt);
284         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
285         atomic_dec(&crypt->refcnt);
286         if (res < 0) {
287                 IEEE80211_DEBUG_DROP("decryption failed (SA=" MAC_FMT
288                                      ") res=%d\n", MAC_ARG(hdr->addr2), res);
289                 if (res == -2)
290                         IEEE80211_DEBUG_DROP("Decryption failed ICV "
291                                              "mismatch (key %d)\n",
292                                              skb->data[hdrlen + 3] >> 6);
293                 ieee->ieee_stats.rx_discards_undecryptable++;
294                 return -1;
295         }
296
297         return res;
298 }
299
300 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
301 static inline int
302 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device *ieee,
303                                 struct sk_buff *skb, int keyidx,
304                                 struct ieee80211_crypt_data *crypt)
305 {
306         struct ieee80211_hdr_3addr *hdr;
307         int res, hdrlen;
308
309         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
310                 return 0;
311
312         hdr = (struct ieee80211_hdr_3addr *)skb->data;
313         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
314
315         atomic_inc(&crypt->refcnt);
316         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
317         atomic_dec(&crypt->refcnt);
318         if (res < 0) {
319                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
320                        " (SA=" MAC_FMT " keyidx=%d)\n",
321                        ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
322                 return -1;
323         }
324
325         return 0;
326 }
327
328 /* All received frames are sent to this function. @skb contains the frame in
329  * IEEE 802.11 format, i.e., in the format it was sent over air.
330  * This function is called only as a tasklet (software IRQ). */
331 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
332                  struct ieee80211_rx_stats *rx_stats)
333 {
334         struct net_device *dev = ieee->dev;
335         struct ieee80211_hdr_4addr *hdr;
336         size_t hdrlen;
337         u16 fc, type, stype, sc;
338         struct net_device_stats *stats;
339         unsigned int frag;
340         u8 *payload;
341         u16 ethertype;
342 #ifdef NOT_YET
343         struct net_device *wds = NULL;
344         struct sk_buff *skb2 = NULL;
345         struct net_device *wds = NULL;
346         int frame_authorized = 0;
347         int from_assoc_ap = 0;
348         void *sta = NULL;
349 #endif
350         u8 dst[ETH_ALEN];
351         u8 src[ETH_ALEN];
352         struct ieee80211_crypt_data *crypt = NULL;
353         int keyidx = 0;
354
355         hdr = (struct ieee80211_hdr_4addr *)skb->data;
356         stats = &ieee->stats;
357
358         if (skb->len < 10) {
359                 printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
360                 goto rx_dropped;
361         }
362
363         fc = le16_to_cpu(hdr->frame_ctl);
364         type = WLAN_FC_GET_TYPE(fc);
365         stype = WLAN_FC_GET_STYPE(fc);
366         sc = le16_to_cpu(hdr->seq_ctl);
367         frag = WLAN_GET_SEQ_FRAG(sc);
368         hdrlen = ieee80211_get_hdrlen(fc);
369
370         /* Put this code here so that we avoid duplicating it in all
371          * Rx paths. - Jean II */
372 #ifdef IW_WIRELESS_SPY          /* defined in iw_handler.h */
373         /* If spy monitoring on */
374         if (ieee->spy_data.spy_number > 0) {
375                 struct iw_quality wstats;
376
377                 wstats.updated = 0;
378                 if (rx_stats->mask & IEEE80211_STATMASK_RSSI) {
379                         wstats.level = rx_stats->rssi;
380                         wstats.updated |= IW_QUAL_LEVEL_UPDATED;
381                 } else
382                         wstats.updated |= IW_QUAL_LEVEL_INVALID;
383
384                 if (rx_stats->mask & IEEE80211_STATMASK_NOISE) {
385                         wstats.noise = rx_stats->noise;
386                         wstats.updated |= IW_QUAL_NOISE_UPDATED;
387                 } else
388                         wstats.updated |= IW_QUAL_NOISE_INVALID;
389
390                 if (rx_stats->mask & IEEE80211_STATMASK_SIGNAL) {
391                         wstats.qual = rx_stats->signal;
392                         wstats.updated |= IW_QUAL_QUAL_UPDATED;
393                 } else
394                         wstats.updated |= IW_QUAL_QUAL_INVALID;
395
396                 /* Update spy records */
397                 wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
398         }
399 #endif                          /* IW_WIRELESS_SPY */
400
401 #ifdef NOT_YET
402         hostap_update_rx_stats(local->ap, hdr, rx_stats);
403 #endif
404
405         if (ieee->iw_mode == IW_MODE_MONITOR) {
406                 ieee80211_monitor_rx(ieee, skb, rx_stats);
407                 stats->rx_packets++;
408                 stats->rx_bytes += skb->len;
409                 return 1;
410         }
411
412         if (is_multicast_ether_addr(hdr->addr1) ? ieee->host_mc_decrypt :
413             ieee->host_decrypt) {
414                 int idx = 0;
415                 if (skb->len >= hdrlen + 3)
416                         idx = skb->data[hdrlen + 3] >> 6;
417                 crypt = ieee->crypt[idx];
418 #ifdef NOT_YET
419                 sta = NULL;
420
421                 /* Use station specific key to override default keys if the
422                  * receiver address is a unicast address ("individual RA"). If
423                  * bcrx_sta_key parameter is set, station specific key is used
424                  * even with broad/multicast targets (this is against IEEE
425                  * 802.11, but makes it easier to use different keys with
426                  * stations that do not support WEP key mapping). */
427
428                 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
429                         (void)hostap_handle_sta_crypto(local, hdr, &crypt,
430                                                        &sta);
431 #endif
432
433                 /* allow NULL decrypt to indicate an station specific override
434                  * for default encryption */
435                 if (crypt && (crypt->ops == NULL ||
436                               crypt->ops->decrypt_mpdu == NULL))
437                         crypt = NULL;
438
439                 if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
440                         /* This seems to be triggered by some (multicast?)
441                          * frames from other than current BSS, so just drop the
442                          * frames silently instead of filling system log with
443                          * these reports. */
444                         IEEE80211_DEBUG_DROP("Decryption failed (not set)"
445                                              " (SA=" MAC_FMT ")\n",
446                                              MAC_ARG(hdr->addr2));
447                         ieee->ieee_stats.rx_discards_undecryptable++;
448                         goto rx_dropped;
449                 }
450         }
451 #ifdef NOT_YET
452         if (type != WLAN_FC_TYPE_DATA) {
453                 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
454                     fc & IEEE80211_FCTL_PROTECTED && ieee->host_decrypt &&
455                     (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0) {
456                         printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
457                                "from " MAC_FMT "\n", dev->name,
458                                MAC_ARG(hdr->addr2));
459                         /* TODO: could inform hostapd about this so that it
460                          * could send auth failure report */
461                         goto rx_dropped;
462                 }
463
464                 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
465                         goto rx_dropped;
466                 else
467                         goto rx_exit;
468         }
469 #endif
470
471         /* Data frame - extract src/dst addresses */
472         if (skb->len < IEEE80211_3ADDR_LEN)
473                 goto rx_dropped;
474
475         switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
476         case IEEE80211_FCTL_FROMDS:
477                 memcpy(dst, hdr->addr1, ETH_ALEN);
478                 memcpy(src, hdr->addr3, ETH_ALEN);
479                 break;
480         case IEEE80211_FCTL_TODS:
481                 memcpy(dst, hdr->addr3, ETH_ALEN);
482                 memcpy(src, hdr->addr2, ETH_ALEN);
483                 break;
484         case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
485                 if (skb->len < IEEE80211_4ADDR_LEN)
486                         goto rx_dropped;
487                 memcpy(dst, hdr->addr3, ETH_ALEN);
488                 memcpy(src, hdr->addr4, ETH_ALEN);
489                 break;
490         case 0:
491                 memcpy(dst, hdr->addr1, ETH_ALEN);
492                 memcpy(src, hdr->addr2, ETH_ALEN);
493                 break;
494         }
495
496 #ifdef NOT_YET
497         if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
498                 goto rx_dropped;
499         if (wds) {
500                 skb->dev = dev = wds;
501                 stats = hostap_get_stats(dev);
502         }
503
504         if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
505             (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
506             IEEE80211_FCTL_FROMDS && ieee->stadev
507             && memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
508                 /* Frame from BSSID of the AP for which we are a client */
509                 skb->dev = dev = ieee->stadev;
510                 stats = hostap_get_stats(dev);
511                 from_assoc_ap = 1;
512         }
513 #endif
514
515         dev->last_rx = jiffies;
516
517 #ifdef NOT_YET
518         if ((ieee->iw_mode == IW_MODE_MASTER ||
519              ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
520                 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
521                                              wds != NULL)) {
522                 case AP_RX_CONTINUE_NOT_AUTHORIZED:
523                         frame_authorized = 0;
524                         break;
525                 case AP_RX_CONTINUE:
526                         frame_authorized = 1;
527                         break;
528                 case AP_RX_DROP:
529                         goto rx_dropped;
530                 case AP_RX_EXIT:
531                         goto rx_exit;
532                 }
533         }
534 #endif
535
536         /* Nullfunc frames may have PS-bit set, so they must be passed to
537          * hostap_handle_sta_rx() before being dropped here. */
538
539         stype &= ~IEEE80211_STYPE_QOS_DATA;
540
541         if (stype != IEEE80211_STYPE_DATA &&
542             stype != IEEE80211_STYPE_DATA_CFACK &&
543             stype != IEEE80211_STYPE_DATA_CFPOLL &&
544             stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
545                 if (stype != IEEE80211_STYPE_NULLFUNC)
546                         IEEE80211_DEBUG_DROP("RX: dropped data frame "
547                                              "with no data (type=0x%02x, "
548                                              "subtype=0x%02x, len=%d)\n",
549                                              type, stype, skb->len);
550                 goto rx_dropped;
551         }
552
553         /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
554
555         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
556             (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
557                 goto rx_dropped;
558
559         hdr = (struct ieee80211_hdr_4addr *)skb->data;
560
561         /* skb: hdr + (possibly fragmented) plaintext payload */
562         // PR: FIXME: hostap has additional conditions in the "if" below:
563         // ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
564         if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
565                 int flen;
566                 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
567                 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
568
569                 if (!frag_skb) {
570                         IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
571                                         "Rx cannot get skb from fragment "
572                                         "cache (morefrag=%d seq=%u frag=%u)\n",
573                                         (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
574                                         WLAN_GET_SEQ_SEQ(sc), frag);
575                         goto rx_dropped;
576                 }
577
578                 flen = skb->len;
579                 if (frag != 0)
580                         flen -= hdrlen;
581
582                 if (frag_skb->tail + flen > frag_skb->end) {
583                         printk(KERN_WARNING "%s: host decrypted and "
584                                "reassembled frame did not fit skb\n",
585                                dev->name);
586                         ieee80211_frag_cache_invalidate(ieee, hdr);
587                         goto rx_dropped;
588                 }
589
590                 if (frag == 0) {
591                         /* copy first fragment (including full headers) into
592                          * beginning of the fragment cache skb */
593                         memcpy(skb_put(frag_skb, flen), skb->data, flen);
594                 } else {
595                         /* append frame payload to the end of the fragment
596                          * cache skb */
597                         memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
598                                flen);
599                 }
600                 dev_kfree_skb_any(skb);
601                 skb = NULL;
602
603                 if (fc & IEEE80211_FCTL_MOREFRAGS) {
604                         /* more fragments expected - leave the skb in fragment
605                          * cache for now; it will be delivered to upper layers
606                          * after all fragments have been received */
607                         goto rx_exit;
608                 }
609
610                 /* this was the last fragment and the frame will be
611                  * delivered, so remove skb from fragment cache */
612                 skb = frag_skb;
613                 hdr = (struct ieee80211_hdr_4addr *)skb->data;
614                 ieee80211_frag_cache_invalidate(ieee, hdr);
615         }
616
617         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
618          * encrypted/authenticated */
619         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
620             ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
621                 goto rx_dropped;
622
623         hdr = (struct ieee80211_hdr_4addr *)skb->data;
624         if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
625                 if (            /*ieee->ieee802_1x && */
626                            ieee80211_is_eapol_frame(ieee, skb)) {
627                         /* pass unencrypted EAPOL frames even if encryption is
628                          * configured */
629                 } else {
630                         IEEE80211_DEBUG_DROP("encryption configured, but RX "
631                                              "frame not encrypted (SA=" MAC_FMT
632                                              ")\n", MAC_ARG(hdr->addr2));
633                         goto rx_dropped;
634                 }
635         }
636
637         if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
638             !ieee80211_is_eapol_frame(ieee, skb)) {
639                 IEEE80211_DEBUG_DROP("dropped unencrypted RX data "
640                                      "frame from " MAC_FMT
641                                      " (drop_unencrypted=1)\n",
642                                      MAC_ARG(hdr->addr2));
643                 goto rx_dropped;
644         }
645
646         /* skb: hdr + (possible reassembled) full plaintext payload */
647
648         payload = skb->data + hdrlen;
649         ethertype = (payload[6] << 8) | payload[7];
650
651 #ifdef NOT_YET
652         /* If IEEE 802.1X is used, check whether the port is authorized to send
653          * the received frame. */
654         if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
655                 if (ethertype == ETH_P_PAE) {
656                         printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
657                                dev->name);
658                         if (ieee->hostapd && ieee->apdev) {
659                                 /* Send IEEE 802.1X frames to the user
660                                  * space daemon for processing */
661                                 prism2_rx_80211(ieee->apdev, skb, rx_stats,
662                                                 PRISM2_RX_MGMT);
663                                 ieee->apdevstats.rx_packets++;
664                                 ieee->apdevstats.rx_bytes += skb->len;
665                                 goto rx_exit;
666                         }
667                 } else if (!frame_authorized) {
668                         printk(KERN_DEBUG "%s: dropped frame from "
669                                "unauthorized port (IEEE 802.1X): "
670                                "ethertype=0x%04x\n", dev->name, ethertype);
671                         goto rx_dropped;
672                 }
673         }
674 #endif
675
676         /* convert hdr + possible LLC headers into Ethernet header */
677         if (skb->len - hdrlen >= 8 &&
678             ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
679               ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
680              memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
681                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
682                  * replace EtherType */
683                 skb_pull(skb, hdrlen + SNAP_SIZE);
684                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
685                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
686         } else {
687                 u16 len;
688                 /* Leave Ethernet header part of hdr and full payload */
689                 skb_pull(skb, hdrlen);
690                 len = htons(skb->len);
691                 memcpy(skb_push(skb, 2), &len, 2);
692                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
693                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
694         }
695
696 #ifdef NOT_YET
697         if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
698                     IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
699                 /* Non-standard frame: get addr4 from its bogus location after
700                  * the payload */
701                 memcpy(skb->data + ETH_ALEN,
702                        skb->data + skb->len - ETH_ALEN, ETH_ALEN);
703                 skb_trim(skb, skb->len - ETH_ALEN);
704         }
705 #endif
706
707         stats->rx_packets++;
708         stats->rx_bytes += skb->len;
709
710 #ifdef NOT_YET
711         if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
712                 if (dst[0] & 0x01) {
713                         /* copy multicast frame both to the higher layers and
714                          * to the wireless media */
715                         ieee->ap->bridged_multicast++;
716                         skb2 = skb_clone(skb, GFP_ATOMIC);
717                         if (skb2 == NULL)
718                                 printk(KERN_DEBUG "%s: skb_clone failed for "
719                                        "multicast frame\n", dev->name);
720                 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
721                         /* send frame directly to the associated STA using
722                          * wireless media and not passing to higher layers */
723                         ieee->ap->bridged_unicast++;
724                         skb2 = skb;
725                         skb = NULL;
726                 }
727         }
728
729         if (skb2 != NULL) {
730                 /* send to wireless media */
731                 skb2->protocol = __constant_htons(ETH_P_802_3);
732                 skb2->mac.raw = skb2->nh.raw = skb2->data;
733                 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
734                 skb2->dev = dev;
735                 dev_queue_xmit(skb2);
736         }
737 #endif
738
739         if (skb) {
740                 skb->protocol = eth_type_trans(skb, dev);
741                 memset(skb->cb, 0, sizeof(skb->cb));
742                 skb->dev = dev;
743                 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
744                 netif_rx(skb);
745         }
746
747       rx_exit:
748 #ifdef NOT_YET
749         if (sta)
750                 hostap_handle_sta_release(sta);
751 #endif
752         return 1;
753
754       rx_dropped:
755         stats->rx_dropped++;
756
757         /* Returning 0 indicates to caller that we have not handled the SKB--
758          * so it is still allocated and can be used again by underlying
759          * hardware as a DMA target */
760         return 0;
761 }
762
763 #define MGMT_FRAME_FIXED_PART_LENGTH            0x24
764
765 static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
766
767 /*
768 * Make ther structure we read from the beacon packet has
769 * the right values
770 */
771 static int ieee80211_verify_qos_info(struct ieee80211_qos_information_element
772                                      *info_element, int sub_type)
773 {
774
775         if (info_element->qui_subtype != sub_type)
776                 return -1;
777         if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
778                 return -1;
779         if (info_element->qui_type != QOS_OUI_TYPE)
780                 return -1;
781         if (info_element->version != QOS_VERSION_1)
782                 return -1;
783
784         return 0;
785 }
786
787 /*
788  * Parse a QoS parameter element
789  */
790 static int ieee80211_read_qos_param_element(struct ieee80211_qos_parameter_info
791                                             *element_param, struct ieee80211_info_element
792                                             *info_element)
793 {
794         int ret = 0;
795         u16 size = sizeof(struct ieee80211_qos_parameter_info) - 2;
796
797         if ((info_element == NULL) || (element_param == NULL))
798                 return -1;
799
800         if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
801                 memcpy(element_param->info_element.qui, info_element->data,
802                        info_element->len);
803                 element_param->info_element.elementID = info_element->id;
804                 element_param->info_element.length = info_element->len;
805         } else
806                 ret = -1;
807         if (ret == 0)
808                 ret = ieee80211_verify_qos_info(&element_param->info_element,
809                                                 QOS_OUI_PARAM_SUB_TYPE);
810         return ret;
811 }
812
813 /*
814  * Parse a QoS information element
815  */
816 static int ieee80211_read_qos_info_element(struct
817                                            ieee80211_qos_information_element
818                                            *element_info, struct ieee80211_info_element
819                                            *info_element)
820 {
821         int ret = 0;
822         u16 size = sizeof(struct ieee80211_qos_information_element) - 2;
823
824         if (element_info == NULL)
825                 return -1;
826         if (info_element == NULL)
827                 return -1;
828
829         if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
830                 memcpy(element_info->qui, info_element->data,
831                        info_element->len);
832                 element_info->elementID = info_element->id;
833                 element_info->length = info_element->len;
834         } else
835                 ret = -1;
836
837         if (ret == 0)
838                 ret = ieee80211_verify_qos_info(element_info,
839                                                 QOS_OUI_INFO_SUB_TYPE);
840         return ret;
841 }
842
843 /*
844  * Write QoS parameters from the ac parameters.
845  */
846 static int ieee80211_qos_convert_ac_to_parameters(struct
847                                                   ieee80211_qos_parameter_info
848                                                   *param_elm, struct
849                                                   ieee80211_qos_parameters
850                                                   *qos_param)
851 {
852         int rc = 0;
853         int i;
854         struct ieee80211_qos_ac_parameter *ac_params;
855         u32 txop;
856         u8 cw_min;
857         u8 cw_max;
858
859         for (i = 0; i < QOS_QUEUE_NUM; i++) {
860                 ac_params = &(param_elm->ac_params_record[i]);
861
862                 qos_param->aifs[i] = (ac_params->aci_aifsn) & 0x0F;
863                 qos_param->aifs[i] -= (qos_param->aifs[i] < 2) ? 0 : 2;
864
865                 cw_min = ac_params->ecw_min_max & 0x0F;
866                 qos_param->cw_min[i] = (u16) ((1 << cw_min) - 1);
867
868                 cw_max = (ac_params->ecw_min_max & 0xF0) >> 4;
869                 qos_param->cw_max[i] = (u16) ((1 << cw_max) - 1);
870
871                 qos_param->flag[i] =
872                     (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
873
874                 txop = le16_to_cpu(ac_params->tx_op_limit) * 32;
875                 qos_param->tx_op_limit[i] = (u16) txop;
876         }
877         return rc;
878 }
879
880 /*
881  * we have a generic data element which it may contain QoS information or
882  * parameters element. check the information element length to decide
883  * which type to read
884  */
885 static int ieee80211_parse_qos_info_param_IE(struct ieee80211_info_element
886                                              *info_element,
887                                              struct ieee80211_network *network)
888 {
889         int rc = 0;
890         struct ieee80211_qos_parameters *qos_param = NULL;
891         struct ieee80211_qos_information_element qos_info_element;
892
893         rc = ieee80211_read_qos_info_element(&qos_info_element, info_element);
894
895         if (rc == 0) {
896                 network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
897                 network->flags |= NETWORK_HAS_QOS_INFORMATION;
898         } else {
899                 struct ieee80211_qos_parameter_info param_element;
900
901                 rc = ieee80211_read_qos_param_element(&param_element,
902                                                       info_element);
903                 if (rc == 0) {
904                         qos_param = &(network->qos_data.parameters);
905                         ieee80211_qos_convert_ac_to_parameters(&param_element,
906                                                                qos_param);
907                         network->flags |= NETWORK_HAS_QOS_PARAMETERS;
908                         network->qos_data.param_count =
909                             param_element.info_element.ac_info & 0x0F;
910                 }
911         }
912
913         if (rc == 0) {
914                 IEEE80211_DEBUG_QOS("QoS is supported\n");
915                 network->qos_data.supported = 1;
916         }
917         return rc;
918 }
919
920 static int ieee80211_parse_info_param(struct ieee80211_info_element *info_element,
921                                         u16 length, struct ieee80211_network *network)
922 {
923         u8 i;
924 #ifdef CONFIG_IEEE80211_DEBUG
925         char rates_str[64];
926         char *p;
927 #endif
928
929         while (length >= sizeof(*info_element)) {
930                 if (sizeof(*info_element) + info_element->len > length) {
931                         IEEE80211_DEBUG_MGMT("Info elem: parse failed: "
932                                             "info_element->len + 2 > left : "
933                                             "info_element->len+2=%zd left=%d, id=%d.\n",
934                                             info_element->len +
935                                             sizeof(*info_element),
936                                             length, info_element->id);
937                         return 1;
938                 }
939
940                 switch (info_element->id) {
941                 case MFIE_TYPE_SSID:
942                         if (ieee80211_is_empty_essid(info_element->data,
943                                                      info_element->len)) {
944                                 network->flags |= NETWORK_EMPTY_ESSID;
945                                 break;
946                         }
947
948                         network->ssid_len = min(info_element->len,
949                                                 (u8) IW_ESSID_MAX_SIZE);
950                         memcpy(network->ssid, info_element->data,
951                                network->ssid_len);
952                         if (network->ssid_len < IW_ESSID_MAX_SIZE)
953                                 memset(network->ssid + network->ssid_len, 0,
954                                        IW_ESSID_MAX_SIZE - network->ssid_len);
955
956                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_SSID: '%s' len=%d.\n",
957                                             network->ssid, network->ssid_len);
958                         break;
959
960                 case MFIE_TYPE_RATES:
961 #ifdef CONFIG_IEEE80211_DEBUG
962                         p = rates_str;
963 #endif
964                         network->rates_len = min(info_element->len,
965                                                  MAX_RATES_LENGTH);
966                         for (i = 0; i < network->rates_len; i++) {
967                                 network->rates[i] = info_element->data[i];
968 #ifdef CONFIG_IEEE80211_DEBUG
969                                 p += snprintf(p, sizeof(rates_str) -
970                                               (p - rates_str), "%02X ",
971                                               network->rates[i]);
972 #endif
973                                 if (ieee80211_is_ofdm_rate
974                                     (info_element->data[i])) {
975                                         network->flags |= NETWORK_HAS_OFDM;
976                                         if (info_element->data[i] &
977                                             IEEE80211_BASIC_RATE_MASK)
978                                                 network->flags &=
979                                                     ~NETWORK_HAS_CCK;
980                                 }
981                         }
982
983                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES: '%s' (%d)\n",
984                                              rates_str, network->rates_len);
985                         break;
986
987                 case MFIE_TYPE_RATES_EX:
988 #ifdef CONFIG_IEEE80211_DEBUG
989                         p = rates_str;
990 #endif
991                         network->rates_ex_len = min(info_element->len,
992                                                     MAX_RATES_EX_LENGTH);
993                         for (i = 0; i < network->rates_ex_len; i++) {
994                                 network->rates_ex[i] = info_element->data[i];
995 #ifdef CONFIG_IEEE80211_DEBUG
996                                 p += snprintf(p, sizeof(rates_str) -
997                                               (p - rates_str), "%02X ",
998                                               network->rates[i]);
999 #endif
1000                                 if (ieee80211_is_ofdm_rate
1001                                     (info_element->data[i])) {
1002                                         network->flags |= NETWORK_HAS_OFDM;
1003                                         if (info_element->data[i] &
1004                                             IEEE80211_BASIC_RATE_MASK)
1005                                                 network->flags &=
1006                                                     ~NETWORK_HAS_CCK;
1007                                 }
1008                         }
1009
1010                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
1011                                              rates_str, network->rates_ex_len);
1012                         break;
1013
1014                 case MFIE_TYPE_DS_SET:
1015                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_DS_SET: %d\n",
1016                                              info_element->data[0]);
1017                         network->channel = info_element->data[0];
1018                         break;
1019
1020                 case MFIE_TYPE_FH_SET:
1021                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_FH_SET: ignored\n");
1022                         break;
1023
1024                 case MFIE_TYPE_CF_SET:
1025                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_CF_SET: ignored\n");
1026                         break;
1027
1028                 case MFIE_TYPE_TIM:
1029                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_TIM: ignored\n");
1030                         break;
1031
1032                 case MFIE_TYPE_ERP_INFO:
1033                         network->erp_value = info_element->data[0];
1034                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
1035                                              network->erp_value);
1036                         break;
1037
1038                 case MFIE_TYPE_IBSS_SET:
1039                         network->atim_window = info_element->data[0];
1040                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_IBSS_SET: %d\n",
1041                                              network->atim_window);
1042                         break;
1043
1044                 case MFIE_TYPE_CHALLENGE:
1045                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_CHALLENGE: ignored\n");
1046                         break;
1047
1048                 case MFIE_TYPE_GENERIC:
1049                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_GENERIC: %d bytes\n",
1050                                              info_element->len);
1051                         if (!ieee80211_parse_qos_info_param_IE(info_element,
1052                                                                network))
1053                                 break;
1054
1055                         if (info_element->len >= 4 &&
1056                             info_element->data[0] == 0x00 &&
1057                             info_element->data[1] == 0x50 &&
1058                             info_element->data[2] == 0xf2 &&
1059                             info_element->data[3] == 0x01) {
1060                                 network->wpa_ie_len = min(info_element->len + 2,
1061                                                           MAX_WPA_IE_LEN);
1062                                 memcpy(network->wpa_ie, info_element,
1063                                        network->wpa_ie_len);
1064                         }
1065                         break;
1066
1067                 case MFIE_TYPE_RSN:
1068                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RSN: %d bytes\n",
1069                                              info_element->len);
1070                         network->rsn_ie_len = min(info_element->len + 2,
1071                                                   MAX_WPA_IE_LEN);
1072                         memcpy(network->rsn_ie, info_element,
1073                                network->rsn_ie_len);
1074                         break;
1075
1076                 case MFIE_TYPE_QOS_PARAMETER:
1077                         printk(KERN_ERR "QoS Error need to parse QOS_PARAMETER IE\n");
1078                         break;
1079
1080                 default:
1081                         IEEE80211_DEBUG_MGMT("unsupported IE %d\n",
1082                                             info_element->id);
1083                         break;
1084                 }
1085
1086                 length -= sizeof(*info_element) + info_element->len;
1087                 info_element = (struct ieee80211_info_element *) &info_element->data[info_element->len];
1088         }
1089
1090         return 0;
1091 }
1092
1093 static int ieee80211_handle_assoc_resp(struct ieee80211_device *ieee, struct ieee80211_assoc_response
1094                                        *frame, struct ieee80211_rx_stats *stats)
1095 {
1096         struct ieee80211_network network_resp;
1097         struct ieee80211_network *network = &network_resp;
1098         struct net_device *dev = ieee->dev;
1099
1100         network->flags = 0;
1101         network->qos_data.active = 0;
1102         network->qos_data.supported = 0;
1103         network->qos_data.param_count = 0;
1104         network->qos_data.old_param_count = 0;
1105
1106         //network->atim_window = le16_to_cpu(frame->aid) & (0x3FFF);
1107         network->atim_window = le16_to_cpu(frame->aid);
1108         network->listen_interval = le16_to_cpu(frame->status);
1109
1110         if(ieee80211_parse_info_param(frame->info_element, stats->len - sizeof(*frame), network))
1111                 return 1;
1112
1113         if (ieee->handle_assoc_response != NULL)
1114                 ieee->handle_assoc_response(dev, frame, network);
1115
1116         return 0;
1117 }
1118
1119 /***************************************************/
1120
1121 static inline int ieee80211_network_init(struct ieee80211_device *ieee, struct ieee80211_probe_response
1122                                          *beacon,
1123                                          struct ieee80211_network *network,
1124                                          struct ieee80211_rx_stats *stats)
1125 {
1126         network->qos_data.active = 0;
1127         network->qos_data.supported = 0;
1128         network->qos_data.param_count = 0;
1129
1130         /* Pull out fixed field data */
1131         memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
1132         network->capability = le16_to_cpu(beacon->capability);
1133         network->last_scanned = jiffies;
1134         network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
1135         network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
1136         network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
1137         /* Where to pull this? beacon->listen_interval; */
1138         network->listen_interval = 0x0A;
1139         network->rates_len = network->rates_ex_len = 0;
1140         network->last_associate = 0;
1141         network->ssid_len = 0;
1142         network->flags = 0;
1143         network->atim_window = 0;
1144         network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
1145             0x3 : 0x0;
1146
1147         if (stats->freq == IEEE80211_52GHZ_BAND) {
1148                 /* for A band (No DS info) */
1149                 network->channel = stats->received_channel;
1150         } else
1151                 network->flags |= NETWORK_HAS_CCK;
1152
1153         network->wpa_ie_len = 0;
1154         network->rsn_ie_len = 0;
1155
1156         if(ieee80211_parse_info_param(beacon->info_element, stats->len - sizeof(*beacon), network))
1157                 return 1;
1158
1159         network->mode = 0;
1160         if (stats->freq == IEEE80211_52GHZ_BAND)
1161                 network->mode = IEEE_A;
1162         else {
1163                 if (network->flags & NETWORK_HAS_OFDM)
1164                         network->mode |= IEEE_G;
1165                 if (network->flags & NETWORK_HAS_CCK)
1166                         network->mode |= IEEE_B;
1167         }
1168
1169         if (network->mode == 0) {
1170                 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
1171                                      "network.\n",
1172                                      escape_essid(network->ssid,
1173                                                   network->ssid_len),
1174                                      MAC_ARG(network->bssid));
1175                 return 1;
1176         }
1177
1178         if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
1179                 network->flags |= NETWORK_EMPTY_ESSID;
1180
1181         memcpy(&network->stats, stats, sizeof(network->stats));
1182
1183         return 0;
1184 }
1185
1186 static inline int is_same_network(struct ieee80211_network *src,
1187                                   struct ieee80211_network *dst)
1188 {
1189         /* A network is only a duplicate if the channel, BSSID, and ESSID
1190          * all match.  We treat all <hidden> with the same BSSID and channel
1191          * as one network */
1192         return ((src->ssid_len == dst->ssid_len) &&
1193                 (src->channel == dst->channel) &&
1194                 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1195                 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1196 }
1197
1198 static inline void update_network(struct ieee80211_network *dst,
1199                                   struct ieee80211_network *src)
1200 {
1201         int qos_active;
1202         u8 old_param;
1203
1204         memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1205         dst->capability = src->capability;
1206         memcpy(dst->rates, src->rates, src->rates_len);
1207         dst->rates_len = src->rates_len;
1208         memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1209         dst->rates_ex_len = src->rates_ex_len;
1210
1211         dst->mode = src->mode;
1212         dst->flags = src->flags;
1213         dst->time_stamp[0] = src->time_stamp[0];
1214         dst->time_stamp[1] = src->time_stamp[1];
1215
1216         dst->beacon_interval = src->beacon_interval;
1217         dst->listen_interval = src->listen_interval;
1218         dst->atim_window = src->atim_window;
1219         dst->erp_value = src->erp_value;
1220
1221         memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1222         dst->wpa_ie_len = src->wpa_ie_len;
1223         memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1224         dst->rsn_ie_len = src->rsn_ie_len;
1225
1226         dst->last_scanned = jiffies;
1227         qos_active = src->qos_data.active;
1228         old_param = dst->qos_data.old_param_count;
1229         if (dst->flags & NETWORK_HAS_QOS_MASK)
1230                 memcpy(&dst->qos_data, &src->qos_data,
1231                        sizeof(struct ieee80211_qos_data));
1232         else {
1233                 dst->qos_data.supported = src->qos_data.supported;
1234                 dst->qos_data.param_count = src->qos_data.param_count;
1235         }
1236
1237         if (dst->qos_data.supported == 1) {
1238                 if (dst->ssid_len)
1239                         IEEE80211_DEBUG_QOS
1240                             ("QoS the network %s is QoS supported\n",
1241                              dst->ssid);
1242                 else
1243                         IEEE80211_DEBUG_QOS
1244                             ("QoS the network is QoS supported\n");
1245         }
1246         dst->qos_data.active = qos_active;
1247         dst->qos_data.old_param_count = old_param;
1248
1249         /* dst->last_associate is not overwritten */
1250 }
1251
1252 static inline int is_beacon(int fc)
1253 {
1254         return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
1255 }
1256
1257 static inline void ieee80211_process_probe_response(struct ieee80211_device
1258                                                     *ieee, struct
1259                                                     ieee80211_probe_response
1260                                                     *beacon, struct ieee80211_rx_stats
1261                                                     *stats)
1262 {
1263         struct net_device *dev = ieee->dev;
1264         struct ieee80211_network network;
1265         struct ieee80211_network *target;
1266         struct ieee80211_network *oldest = NULL;
1267 #ifdef CONFIG_IEEE80211_DEBUG
1268         struct ieee80211_info_element *info_element = beacon->info_element;
1269 #endif
1270         unsigned long flags;
1271
1272         IEEE80211_DEBUG_SCAN("'%s' (" MAC_FMT
1273                              "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1274                              escape_essid(info_element->data,
1275                                           info_element->len),
1276                              MAC_ARG(beacon->header.addr3),
1277                              (beacon->capability & (1 << 0xf)) ? '1' : '0',
1278                              (beacon->capability & (1 << 0xe)) ? '1' : '0',
1279                              (beacon->capability & (1 << 0xd)) ? '1' : '0',
1280                              (beacon->capability & (1 << 0xc)) ? '1' : '0',
1281                              (beacon->capability & (1 << 0xb)) ? '1' : '0',
1282                              (beacon->capability & (1 << 0xa)) ? '1' : '0',
1283                              (beacon->capability & (1 << 0x9)) ? '1' : '0',
1284                              (beacon->capability & (1 << 0x8)) ? '1' : '0',
1285                              (beacon->capability & (1 << 0x7)) ? '1' : '0',
1286                              (beacon->capability & (1 << 0x6)) ? '1' : '0',
1287                              (beacon->capability & (1 << 0x5)) ? '1' : '0',
1288                              (beacon->capability & (1 << 0x4)) ? '1' : '0',
1289                              (beacon->capability & (1 << 0x3)) ? '1' : '0',
1290                              (beacon->capability & (1 << 0x2)) ? '1' : '0',
1291                              (beacon->capability & (1 << 0x1)) ? '1' : '0',
1292                              (beacon->capability & (1 << 0x0)) ? '1' : '0');
1293
1294         if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1295                 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1296                                      escape_essid(info_element->data,
1297                                                   info_element->len),
1298                                      MAC_ARG(beacon->header.addr3),
1299                                      is_beacon(le16_to_cpu
1300                                                (beacon->header.
1301                                                 frame_ctl)) ?
1302                                      "BEACON" : "PROBE RESPONSE");
1303                 return;
1304         }
1305
1306         /* The network parsed correctly -- so now we scan our known networks
1307          * to see if we can find it in our list.
1308          *
1309          * NOTE:  This search is definitely not optimized.  Once its doing
1310          *        the "right thing" we'll optimize it for efficiency if
1311          *        necessary */
1312
1313         /* Search for this entry in the list and update it if it is
1314          * already there. */
1315
1316         spin_lock_irqsave(&ieee->lock, flags);
1317
1318         list_for_each_entry(target, &ieee->network_list, list) {
1319                 if (is_same_network(target, &network))
1320                         break;
1321
1322                 if ((oldest == NULL) ||
1323                     (target->last_scanned < oldest->last_scanned))
1324                         oldest = target;
1325         }
1326
1327         /* If we didn't find a match, then get a new network slot to initialize
1328          * with this beacon's information */
1329         if (&target->list == &ieee->network_list) {
1330                 if (list_empty(&ieee->network_free_list)) {
1331                         /* If there are no more slots, expire the oldest */
1332                         list_del(&oldest->list);
1333                         target = oldest;
1334                         IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1335                                              "network list.\n",
1336                                              escape_essid(target->ssid,
1337                                                           target->ssid_len),
1338                                              MAC_ARG(target->bssid));
1339                 } else {
1340                         /* Otherwise just pull from the free list */
1341                         target = list_entry(ieee->network_free_list.next,
1342                                             struct ieee80211_network, list);
1343                         list_del(ieee->network_free_list.next);
1344                 }
1345
1346 #ifdef CONFIG_IEEE80211_DEBUG
1347                 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1348                                      escape_essid(network.ssid,
1349                                                   network.ssid_len),
1350                                      MAC_ARG(network.bssid),
1351                                      is_beacon(le16_to_cpu
1352                                                (beacon->header.
1353                                                 frame_ctl)) ?
1354                                      "BEACON" : "PROBE RESPONSE");
1355 #endif
1356                 memcpy(target, &network, sizeof(*target));
1357                 list_add_tail(&target->list, &ieee->network_list);
1358         } else {
1359                 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1360                                      escape_essid(target->ssid,
1361                                                   target->ssid_len),
1362                                      MAC_ARG(target->bssid),
1363                                      is_beacon(le16_to_cpu
1364                                                (beacon->header.
1365                                                 frame_ctl)) ?
1366                                      "BEACON" : "PROBE RESPONSE");
1367                 update_network(target, &network);
1368         }
1369
1370         spin_unlock_irqrestore(&ieee->lock, flags);
1371
1372         if (is_beacon(le16_to_cpu(beacon->header.frame_ctl))) {
1373                 if (ieee->handle_beacon != NULL)
1374                         ieee->handle_beacon(dev, beacon, &network);
1375         } else {
1376                 if (ieee->handle_probe_response != NULL)
1377                         ieee->handle_probe_response(dev, beacon, &network);
1378         }
1379 }
1380
1381 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1382                       struct ieee80211_hdr_4addr *header,
1383                       struct ieee80211_rx_stats *stats)
1384 {
1385         switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
1386         case IEEE80211_STYPE_ASSOC_RESP:
1387                 IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1388                                      WLAN_FC_GET_STYPE(le16_to_cpu
1389                                                        (header->frame_ctl)));
1390                 ieee80211_handle_assoc_resp(ieee,
1391                                             (struct ieee80211_assoc_response *)
1392                                             header, stats);
1393                 break;
1394
1395         case IEEE80211_STYPE_REASSOC_RESP:
1396                 IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1397                                      WLAN_FC_GET_STYPE(le16_to_cpu
1398                                                        (header->frame_ctl)));
1399                 break;
1400
1401         case IEEE80211_STYPE_PROBE_REQ:
1402                 IEEE80211_DEBUG_MGMT("recieved auth (%d)\n",
1403                                      WLAN_FC_GET_STYPE(le16_to_cpu
1404                                                        (header->frame_ctl)));
1405
1406                 if (ieee->handle_probe_request != NULL)
1407                         ieee->handle_probe_request(ieee->dev,
1408                                                    (struct
1409                                                     ieee80211_probe_request *)
1410                                                    header, stats);
1411                 break;
1412
1413         case IEEE80211_STYPE_PROBE_RESP:
1414                 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1415                                      WLAN_FC_GET_STYPE(le16_to_cpu
1416                                                        (header->frame_ctl)));
1417                 IEEE80211_DEBUG_SCAN("Probe response\n");
1418                 ieee80211_process_probe_response(ieee,
1419                                                  (struct
1420                                                   ieee80211_probe_response *)
1421                                                  header, stats);
1422                 break;
1423
1424         case IEEE80211_STYPE_BEACON:
1425                 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1426                                      WLAN_FC_GET_STYPE(le16_to_cpu
1427                                                        (header->frame_ctl)));
1428                 IEEE80211_DEBUG_SCAN("Beacon\n");
1429                 ieee80211_process_probe_response(ieee,
1430                                                  (struct
1431                                                   ieee80211_probe_response *)
1432                                                  header, stats);
1433                 break;
1434         case IEEE80211_STYPE_AUTH:
1435
1436                 IEEE80211_DEBUG_MGMT("recieved auth (%d)\n",
1437                                      WLAN_FC_GET_STYPE(le16_to_cpu
1438                                                        (header->frame_ctl)));
1439
1440                 if (ieee->handle_auth != NULL)
1441                         ieee->handle_auth(ieee->dev,
1442                                           (struct ieee80211_auth *)header);
1443                 break;
1444
1445         case IEEE80211_STYPE_DISASSOC:
1446                 if (ieee->handle_disassoc != NULL)
1447                         ieee->handle_disassoc(ieee->dev,
1448                                               (struct ieee80211_disassoc *)
1449                                               header);
1450                 break;
1451
1452         case IEEE80211_STYPE_DEAUTH:
1453                 printk("DEAUTH from AP\n");
1454                 if (ieee->handle_deauth != NULL)
1455                         ieee->handle_deauth(ieee->dev, (struct ieee80211_auth *)
1456                                             header);
1457                 break;
1458         default:
1459                 IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
1460                                      WLAN_FC_GET_STYPE(le16_to_cpu
1461                                                        (header->frame_ctl)));
1462                 IEEE80211_WARNING("%s: Unknown management packet: %d\n",
1463                                   ieee->dev->name,
1464                                   WLAN_FC_GET_STYPE(le16_to_cpu
1465                                                     (header->frame_ctl)));
1466                 break;
1467         }
1468 }
1469
1470 EXPORT_SYMBOL(ieee80211_rx_mgt);
1471 EXPORT_SYMBOL(ieee80211_rx);