• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/net/wireless/ipw2x00/
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 * <j@w1.fi>
7 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.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/errno.h>
18#include <linux/if_arp.h>
19#include <linux/in6.h>
20#include <linux/gfp.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/tcp.h>
29#include <linux/types.h>
30#include <linux/wireless.h>
31#include <linux/etherdevice.h>
32#include <asm/uaccess.h>
33#include <linux/ctype.h>
34
35#include <net/lib80211.h>
36
37#include "libipw.h"
38
39static void libipw_monitor_rx(struct libipw_device *ieee,
40					struct sk_buff *skb,
41					struct libipw_rx_stats *rx_stats)
42{
43	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
44	u16 fc = le16_to_cpu(hdr->frame_control);
45
46	skb->dev = ieee->dev;
47	skb_reset_mac_header(skb);
48	skb_pull(skb, libipw_get_hdrlen(fc));
49	skb->pkt_type = PACKET_OTHERHOST;
50	skb->protocol = 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) */
56static struct libipw_frag_entry *libipw_frag_cache_find(struct
57							      libipw_device
58							      *ieee,
59							      unsigned int seq,
60							      unsigned int frag,
61							      u8 * src,
62							      u8 * dst)
63{
64	struct libipw_frag_entry *entry;
65	int i;
66
67	for (i = 0; i < LIBIPW_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			LIBIPW_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		    !compare_ether_addr(entry->src_addr, src) &&
81		    !compare_ether_addr(entry->dst_addr, dst))
82			return entry;
83	}
84
85	return NULL;
86}
87
88/* Called only as a tasklet (software IRQ) */
89static struct sk_buff *libipw_frag_cache_get(struct libipw_device *ieee,
90						struct libipw_hdr_4addr *hdr)
91{
92	struct sk_buff *skb = NULL;
93	u16 sc;
94	unsigned int frag, seq;
95	struct libipw_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 libipw_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 >= LIBIPW_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 = libipw_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) */
140static int libipw_frag_cache_invalidate(struct libipw_device *ieee,
141					   struct libipw_hdr_4addr *hdr)
142{
143	u16 sc;
144	unsigned int seq;
145	struct libipw_frag_entry *entry;
146
147	sc = le16_to_cpu(hdr->seq_ctl);
148	seq = WLAN_GET_SEQ_SEQ(sc);
149
150	entry = libipw_frag_cache_find(ieee, seq, -1, hdr->addr2,
151					  hdr->addr1);
152
153	if (entry == NULL) {
154		LIBIPW_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/* libipw_rx_frame_mgtmt
165 *
166 * Responsible for handling management control frames
167 *
168 * Called by libipw_rx */
169static int
170libipw_rx_frame_mgmt(struct libipw_device *ieee, struct sk_buff *skb,
171			struct libipw_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_libipw_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) */
222static unsigned char libipw_rfc1042_header[] =
223    { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
224
225/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
226static unsigned char libipw_bridge_tunnel_header[] =
227    { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
228/* No encapsulation header if EtherType < 0x600 (=length) */
229
230/* Called by libipw_rx_frame_decrypt */
231static int libipw_is_eapol_frame(struct libipw_device *ieee,
232				    struct sk_buff *skb)
233{
234	struct net_device *dev = ieee->dev;
235	u16 fc, ethertype;
236	struct libipw_hdr_3addr *hdr;
237	u8 *pos;
238
239	if (skb->len < 24)
240		return 0;
241
242	hdr = (struct libipw_hdr_3addr *)skb->data;
243	fc = le16_to_cpu(hdr->frame_ctl);
244
245	/* check that the frame is unicast frame to us */
246	if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
247	    IEEE80211_FCTL_TODS &&
248	    !compare_ether_addr(hdr->addr1, dev->dev_addr) &&
249	    !compare_ether_addr(hdr->addr3, dev->dev_addr)) {
250		/* ToDS frame with own addr BSSID and DA */
251	} else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
252		   IEEE80211_FCTL_FROMDS &&
253		   !compare_ether_addr(hdr->addr1, dev->dev_addr)) {
254		/* FromDS frame with own addr as DA */
255	} else
256		return 0;
257
258	if (skb->len < 24 + 8)
259		return 0;
260
261	/* check for port access entity Ethernet type */
262	pos = skb->data + 24;
263	ethertype = (pos[6] << 8) | pos[7];
264	if (ethertype == ETH_P_PAE)
265		return 1;
266
267	return 0;
268}
269
270/* Called only as a tasklet (software IRQ), by libipw_rx */
271static int
272libipw_rx_frame_decrypt(struct libipw_device *ieee, struct sk_buff *skb,
273			   struct lib80211_crypt_data *crypt)
274{
275	struct libipw_hdr_3addr *hdr;
276	int res, hdrlen;
277
278	if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
279		return 0;
280
281	hdr = (struct libipw_hdr_3addr *)skb->data;
282	hdrlen = libipw_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
283
284	atomic_inc(&crypt->refcnt);
285	res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
286	atomic_dec(&crypt->refcnt);
287	if (res < 0) {
288		LIBIPW_DEBUG_DROP("decryption failed (SA=%pM) res=%d\n",
289				     hdr->addr2, res);
290		if (res == -2)
291			LIBIPW_DEBUG_DROP("Decryption failed ICV "
292					     "mismatch (key %d)\n",
293					     skb->data[hdrlen + 3] >> 6);
294		ieee->ieee_stats.rx_discards_undecryptable++;
295		return -1;
296	}
297
298	return res;
299}
300
301/* Called only as a tasklet (software IRQ), by libipw_rx */
302static int
303libipw_rx_frame_decrypt_msdu(struct libipw_device *ieee,
304				struct sk_buff *skb, int keyidx,
305				struct lib80211_crypt_data *crypt)
306{
307	struct libipw_hdr_3addr *hdr;
308	int res, hdrlen;
309
310	if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
311		return 0;
312
313	hdr = (struct libipw_hdr_3addr *)skb->data;
314	hdrlen = libipw_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
315
316	atomic_inc(&crypt->refcnt);
317	res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
318	atomic_dec(&crypt->refcnt);
319	if (res < 0) {
320		printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
321		       " (SA=%pM keyidx=%d)\n", ieee->dev->name, hdr->addr2,
322		       keyidx);
323		return -1;
324	}
325
326	return 0;
327}
328
329/* All received frames are sent to this function. @skb contains the frame in
330 * IEEE 802.11 format, i.e., in the format it was sent over air.
331 * This function is called only as a tasklet (software IRQ). */
332int libipw_rx(struct libipw_device *ieee, struct sk_buff *skb,
333		 struct libipw_rx_stats *rx_stats)
334{
335	struct net_device *dev = ieee->dev;
336	struct libipw_hdr_4addr *hdr;
337	size_t hdrlen;
338	u16 fc, type, stype, sc;
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 lib80211_crypt_data *crypt = NULL;
353	int keyidx = 0;
354	int can_be_decrypted = 0;
355
356	hdr = (struct libipw_hdr_4addr *)skb->data;
357	if (skb->len < 10) {
358		printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
359		goto rx_dropped;
360	}
361
362	fc = le16_to_cpu(hdr->frame_ctl);
363	type = WLAN_FC_GET_TYPE(fc);
364	stype = WLAN_FC_GET_STYPE(fc);
365	sc = le16_to_cpu(hdr->seq_ctl);
366	frag = WLAN_GET_SEQ_FRAG(sc);
367	hdrlen = libipw_get_hdrlen(fc);
368
369	if (skb->len < hdrlen) {
370		printk(KERN_INFO "%s: invalid SKB length %d\n",
371			dev->name, skb->len);
372		goto rx_dropped;
373	}
374
375	/* Put this code here so that we avoid duplicating it in all
376	 * Rx paths. - Jean II */
377#ifdef CONFIG_WIRELESS_EXT
378#ifdef IW_WIRELESS_SPY		/* defined in iw_handler.h */
379	/* If spy monitoring on */
380	if (ieee->spy_data.spy_number > 0) {
381		struct iw_quality wstats;
382
383		wstats.updated = 0;
384		if (rx_stats->mask & LIBIPW_STATMASK_RSSI) {
385			wstats.level = rx_stats->signal;
386			wstats.updated |= IW_QUAL_LEVEL_UPDATED;
387		} else
388			wstats.updated |= IW_QUAL_LEVEL_INVALID;
389
390		if (rx_stats->mask & LIBIPW_STATMASK_NOISE) {
391			wstats.noise = rx_stats->noise;
392			wstats.updated |= IW_QUAL_NOISE_UPDATED;
393		} else
394			wstats.updated |= IW_QUAL_NOISE_INVALID;
395
396		if (rx_stats->mask & LIBIPW_STATMASK_SIGNAL) {
397			wstats.qual = rx_stats->signal;
398			wstats.updated |= IW_QUAL_QUAL_UPDATED;
399		} else
400			wstats.updated |= IW_QUAL_QUAL_INVALID;
401
402		/* Update spy records */
403		wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
404	}
405#endif				/* IW_WIRELESS_SPY */
406#endif				/* CONFIG_WIRELESS_EXT */
407
408#ifdef NOT_YET
409	hostap_update_rx_stats(local->ap, hdr, rx_stats);
410#endif
411
412	if (ieee->iw_mode == IW_MODE_MONITOR) {
413		dev->stats.rx_packets++;
414		dev->stats.rx_bytes += skb->len;
415		libipw_monitor_rx(ieee, skb, rx_stats);
416		return 1;
417	}
418
419	can_be_decrypted = (is_multicast_ether_addr(hdr->addr1) ||
420			    is_broadcast_ether_addr(hdr->addr2)) ?
421	    ieee->host_mc_decrypt : ieee->host_decrypt;
422
423	if (can_be_decrypted) {
424		if (skb->len >= hdrlen + 3) {
425			/* Top two-bits of byte 3 are the key index */
426			keyidx = skb->data[hdrlen + 3] >> 6;
427		}
428
429		/* ieee->crypt[] is WEP_KEY (4) in length.  Given that keyidx
430		 * is only allowed 2-bits of storage, no value of keyidx can
431		 * be provided via above code that would result in keyidx
432		 * being out of range */
433		crypt = ieee->crypt_info.crypt[keyidx];
434
435#ifdef NOT_YET
436		sta = NULL;
437
438		/* Use station specific key to override default keys if the
439		 * receiver address is a unicast address ("individual RA"). If
440		 * bcrx_sta_key parameter is set, station specific key is used
441		 * even with broad/multicast targets (this is against IEEE
442		 * 802.11, but makes it easier to use different keys with
443		 * stations that do not support WEP key mapping). */
444
445		if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
446			(void)hostap_handle_sta_crypto(local, hdr, &crypt,
447						       &sta);
448#endif
449
450		/* allow NULL decrypt to indicate an station specific override
451		 * for default encryption */
452		if (crypt && (crypt->ops == NULL ||
453			      crypt->ops->decrypt_mpdu == NULL))
454			crypt = NULL;
455
456		if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
457			/* This seems to be triggered by some (multicast?)
458			 * frames from other than current BSS, so just drop the
459			 * frames silently instead of filling system log with
460			 * these reports. */
461			LIBIPW_DEBUG_DROP("Decryption failed (not set)"
462					     " (SA=%pM)\n", hdr->addr2);
463			ieee->ieee_stats.rx_discards_undecryptable++;
464			goto rx_dropped;
465		}
466	}
467#ifdef NOT_YET
468	if (type != WLAN_FC_TYPE_DATA) {
469		if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
470		    fc & IEEE80211_FCTL_PROTECTED && ieee->host_decrypt &&
471		    (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0) {
472			printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
473			       "from %pM\n", dev->name, hdr->addr2);
474			/* TODO: could inform hostapd about this so that it
475			 * could send auth failure report */
476			goto rx_dropped;
477		}
478
479		if (libipw_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
480			goto rx_dropped;
481		else
482			goto rx_exit;
483	}
484#endif
485	/* drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.29) */
486	if (sc == ieee->prev_seq_ctl)
487		goto rx_dropped;
488	else
489		ieee->prev_seq_ctl = sc;
490
491	/* Data frame - extract src/dst addresses */
492	if (skb->len < LIBIPW_3ADDR_LEN)
493		goto rx_dropped;
494
495	switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
496	case IEEE80211_FCTL_FROMDS:
497		memcpy(dst, hdr->addr1, ETH_ALEN);
498		memcpy(src, hdr->addr3, ETH_ALEN);
499		break;
500	case IEEE80211_FCTL_TODS:
501		memcpy(dst, hdr->addr3, ETH_ALEN);
502		memcpy(src, hdr->addr2, ETH_ALEN);
503		break;
504	case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
505		if (skb->len < LIBIPW_4ADDR_LEN)
506			goto rx_dropped;
507		memcpy(dst, hdr->addr3, ETH_ALEN);
508		memcpy(src, hdr->addr4, ETH_ALEN);
509		break;
510	case 0:
511		memcpy(dst, hdr->addr1, ETH_ALEN);
512		memcpy(src, hdr->addr2, ETH_ALEN);
513		break;
514	}
515
516#ifdef NOT_YET
517	if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
518		goto rx_dropped;
519	if (wds) {
520		skb->dev = dev = wds;
521		stats = hostap_get_stats(dev);
522	}
523
524	if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
525	    (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
526	    IEEE80211_FCTL_FROMDS && ieee->stadev
527	    && !compare_ether_addr(hdr->addr2, ieee->assoc_ap_addr)) {
528		/* Frame from BSSID of the AP for which we are a client */
529		skb->dev = dev = ieee->stadev;
530		stats = hostap_get_stats(dev);
531		from_assoc_ap = 1;
532	}
533#endif
534
535#ifdef NOT_YET
536	if ((ieee->iw_mode == IW_MODE_MASTER ||
537	     ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
538		switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
539					     wds != NULL)) {
540		case AP_RX_CONTINUE_NOT_AUTHORIZED:
541			frame_authorized = 0;
542			break;
543		case AP_RX_CONTINUE:
544			frame_authorized = 1;
545			break;
546		case AP_RX_DROP:
547			goto rx_dropped;
548		case AP_RX_EXIT:
549			goto rx_exit;
550		}
551	}
552#endif
553
554	/* Nullfunc frames may have PS-bit set, so they must be passed to
555	 * hostap_handle_sta_rx() before being dropped here. */
556
557	stype &= ~IEEE80211_STYPE_QOS_DATA;
558
559	if (stype != IEEE80211_STYPE_DATA &&
560	    stype != IEEE80211_STYPE_DATA_CFACK &&
561	    stype != IEEE80211_STYPE_DATA_CFPOLL &&
562	    stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
563		if (stype != IEEE80211_STYPE_NULLFUNC)
564			LIBIPW_DEBUG_DROP("RX: dropped data frame "
565					     "with no data (type=0x%02x, "
566					     "subtype=0x%02x, len=%d)\n",
567					     type, stype, skb->len);
568		goto rx_dropped;
569	}
570
571	/* skb: hdr + (possibly fragmented, possibly encrypted) payload */
572
573	if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
574	    (keyidx = libipw_rx_frame_decrypt(ieee, skb, crypt)) < 0)
575		goto rx_dropped;
576
577	hdr = (struct libipw_hdr_4addr *)skb->data;
578
579	/* skb: hdr + (possibly fragmented) plaintext payload */
580	// ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
581	if ((frag != 0) || (fc & IEEE80211_FCTL_MOREFRAGS)) {
582		int flen;
583		struct sk_buff *frag_skb = libipw_frag_cache_get(ieee, hdr);
584		LIBIPW_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
585
586		if (!frag_skb) {
587			LIBIPW_DEBUG(LIBIPW_DL_RX | LIBIPW_DL_FRAG,
588					"Rx cannot get skb from fragment "
589					"cache (morefrag=%d seq=%u frag=%u)\n",
590					(fc & IEEE80211_FCTL_MOREFRAGS) != 0,
591					WLAN_GET_SEQ_SEQ(sc), frag);
592			goto rx_dropped;
593		}
594
595		flen = skb->len;
596		if (frag != 0)
597			flen -= hdrlen;
598
599		if (frag_skb->tail + flen > frag_skb->end) {
600			printk(KERN_WARNING "%s: host decrypted and "
601			       "reassembled frame did not fit skb\n",
602			       dev->name);
603			libipw_frag_cache_invalidate(ieee, hdr);
604			goto rx_dropped;
605		}
606
607		if (frag == 0) {
608			/* copy first fragment (including full headers) into
609			 * beginning of the fragment cache skb */
610			skb_copy_from_linear_data(skb, skb_put(frag_skb, flen), flen);
611		} else {
612			/* append frame payload to the end of the fragment
613			 * cache skb */
614			skb_copy_from_linear_data_offset(skb, hdrlen,
615				      skb_put(frag_skb, flen), flen);
616		}
617		dev_kfree_skb_any(skb);
618		skb = NULL;
619
620		if (fc & IEEE80211_FCTL_MOREFRAGS) {
621			/* more fragments expected - leave the skb in fragment
622			 * cache for now; it will be delivered to upper layers
623			 * after all fragments have been received */
624			goto rx_exit;
625		}
626
627		/* this was the last fragment and the frame will be
628		 * delivered, so remove skb from fragment cache */
629		skb = frag_skb;
630		hdr = (struct libipw_hdr_4addr *)skb->data;
631		libipw_frag_cache_invalidate(ieee, hdr);
632	}
633
634	/* skb: hdr + (possible reassembled) full MSDU payload; possibly still
635	 * encrypted/authenticated */
636	if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
637	    libipw_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
638		goto rx_dropped;
639
640	hdr = (struct libipw_hdr_4addr *)skb->data;
641	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
642		if (		/*ieee->ieee802_1x && */
643			   libipw_is_eapol_frame(ieee, skb)) {
644			/* pass unencrypted EAPOL frames even if encryption is
645			 * configured */
646		} else {
647			LIBIPW_DEBUG_DROP("encryption configured, but RX "
648					     "frame not encrypted (SA=%pM)\n",
649					     hdr->addr2);
650			goto rx_dropped;
651		}
652	}
653
654	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
655	    !libipw_is_eapol_frame(ieee, skb)) {
656		LIBIPW_DEBUG_DROP("dropped unencrypted RX data "
657				     "frame from %pM (drop_unencrypted=1)\n",
658				     hdr->addr2);
659		goto rx_dropped;
660	}
661
662	/* If the frame was decrypted in hardware, we may need to strip off
663	 * any security data (IV, ICV, etc) that was left behind */
664	if (!can_be_decrypted && (fc & IEEE80211_FCTL_PROTECTED) &&
665	    ieee->host_strip_iv_icv) {
666		int trimlen = 0;
667
668		/* Top two-bits of byte 3 are the key index */
669		if (skb->len >= hdrlen + 3)
670			keyidx = skb->data[hdrlen + 3] >> 6;
671
672		/* To strip off any security data which appears before the
673		 * payload, we simply increase hdrlen (as the header gets
674		 * chopped off immediately below). For the security data which
675		 * appears after the payload, we use skb_trim. */
676
677		switch (ieee->sec.encode_alg[keyidx]) {
678		case SEC_ALG_WEP:
679			/* 4 byte IV */
680			hdrlen += 4;
681			/* 4 byte ICV */
682			trimlen = 4;
683			break;
684		case SEC_ALG_TKIP:
685			/* 4 byte IV, 4 byte ExtIV */
686			hdrlen += 8;
687			/* 8 byte MIC, 4 byte ICV */
688			trimlen = 12;
689			break;
690		case SEC_ALG_CCMP:
691			/* 8 byte CCMP header */
692			hdrlen += 8;
693			/* 8 byte MIC */
694			trimlen = 8;
695			break;
696		}
697
698		if (skb->len < trimlen)
699			goto rx_dropped;
700
701		__skb_trim(skb, skb->len - trimlen);
702
703		if (skb->len < hdrlen)
704			goto rx_dropped;
705	}
706
707	/* skb: hdr + (possible reassembled) full plaintext payload */
708
709	payload = skb->data + hdrlen;
710	ethertype = (payload[6] << 8) | payload[7];
711
712#ifdef NOT_YET
713	/* If IEEE 802.1X is used, check whether the port is authorized to send
714	 * the received frame. */
715	if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
716		if (ethertype == ETH_P_PAE) {
717			printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
718			       dev->name);
719			if (ieee->hostapd && ieee->apdev) {
720				/* Send IEEE 802.1X frames to the user
721				 * space daemon for processing */
722				prism2_rx_80211(ieee->apdev, skb, rx_stats,
723						PRISM2_RX_MGMT);
724				ieee->apdevstats.rx_packets++;
725				ieee->apdevstats.rx_bytes += skb->len;
726				goto rx_exit;
727			}
728		} else if (!frame_authorized) {
729			printk(KERN_DEBUG "%s: dropped frame from "
730			       "unauthorized port (IEEE 802.1X): "
731			       "ethertype=0x%04x\n", dev->name, ethertype);
732			goto rx_dropped;
733		}
734	}
735#endif
736
737	/* convert hdr + possible LLC headers into Ethernet header */
738	if (skb->len - hdrlen >= 8 &&
739	    ((memcmp(payload, libipw_rfc1042_header, SNAP_SIZE) == 0 &&
740	      ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
741	     memcmp(payload, libipw_bridge_tunnel_header, SNAP_SIZE) == 0)) {
742		/* remove RFC1042 or Bridge-Tunnel encapsulation and
743		 * replace EtherType */
744		skb_pull(skb, hdrlen + SNAP_SIZE);
745		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
746		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
747	} else {
748		__be16 len;
749		/* Leave Ethernet header part of hdr and full payload */
750		skb_pull(skb, hdrlen);
751		len = htons(skb->len);
752		memcpy(skb_push(skb, 2), &len, 2);
753		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
754		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
755	}
756
757#ifdef NOT_YET
758	if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
759		    IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
760		/* Non-standard frame: get addr4 from its bogus location after
761		 * the payload */
762		skb_copy_to_linear_data_offset(skb, ETH_ALEN,
763					       skb->data + skb->len - ETH_ALEN,
764					       ETH_ALEN);
765		skb_trim(skb, skb->len - ETH_ALEN);
766	}
767#endif
768
769	dev->stats.rx_packets++;
770	dev->stats.rx_bytes += skb->len;
771
772#ifdef NOT_YET
773	if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
774		if (dst[0] & 0x01) {
775			/* copy multicast frame both to the higher layers and
776			 * to the wireless media */
777			ieee->ap->bridged_multicast++;
778			skb2 = skb_clone(skb, GFP_ATOMIC);
779			if (skb2 == NULL)
780				printk(KERN_DEBUG "%s: skb_clone failed for "
781				       "multicast frame\n", dev->name);
782		} else if (hostap_is_sta_assoc(ieee->ap, dst)) {
783			/* send frame directly to the associated STA using
784			 * wireless media and not passing to higher layers */
785			ieee->ap->bridged_unicast++;
786			skb2 = skb;
787			skb = NULL;
788		}
789	}
790
791	if (skb2 != NULL) {
792		/* send to wireless media */
793		skb2->dev = dev;
794		skb2->protocol = htons(ETH_P_802_3);
795		skb_reset_mac_header(skb2);
796		skb_reset_network_header(skb2);
797		/* skb2->network_header += ETH_HLEN; */
798		dev_queue_xmit(skb2);
799	}
800#endif
801
802	if (skb) {
803		skb->protocol = eth_type_trans(skb, dev);
804		memset(skb->cb, 0, sizeof(skb->cb));
805		skb->ip_summed = CHECKSUM_NONE;	/* 802.11 crc not sufficient */
806		if (netif_rx(skb) == NET_RX_DROP) {
807			/* netif_rx always succeeds, but it might drop
808			 * the packet.  If it drops the packet, we log that
809			 * in our stats. */
810			LIBIPW_DEBUG_DROP
811			    ("RX: netif_rx dropped the packet\n");
812			dev->stats.rx_dropped++;
813		}
814	}
815
816      rx_exit:
817#ifdef NOT_YET
818	if (sta)
819		hostap_handle_sta_release(sta);
820#endif
821	return 1;
822
823      rx_dropped:
824	dev->stats.rx_dropped++;
825
826	/* Returning 0 indicates to caller that we have not handled the SKB--
827	 * so it is still allocated and can be used again by underlying
828	 * hardware as a DMA target */
829	return 0;
830}
831
832/* Filter out unrelated packets, call libipw_rx[_mgt]
833 * This function takes over the skb, it should not be used again after calling
834 * this function. */
835void libipw_rx_any(struct libipw_device *ieee,
836		     struct sk_buff *skb, struct libipw_rx_stats *stats)
837{
838	struct libipw_hdr_4addr *hdr;
839	int is_packet_for_us;
840	u16 fc;
841
842	if (ieee->iw_mode == IW_MODE_MONITOR) {
843		if (!libipw_rx(ieee, skb, stats))
844			dev_kfree_skb_irq(skb);
845		return;
846	}
847
848	if (skb->len < sizeof(struct ieee80211_hdr))
849		goto drop_free;
850
851	hdr = (struct libipw_hdr_4addr *)skb->data;
852	fc = le16_to_cpu(hdr->frame_ctl);
853
854	if ((fc & IEEE80211_FCTL_VERS) != 0)
855		goto drop_free;
856
857	switch (fc & IEEE80211_FCTL_FTYPE) {
858	case IEEE80211_FTYPE_MGMT:
859		if (skb->len < sizeof(struct libipw_hdr_3addr))
860			goto drop_free;
861		libipw_rx_mgt(ieee, hdr, stats);
862		dev_kfree_skb_irq(skb);
863		return;
864	case IEEE80211_FTYPE_DATA:
865		break;
866	case IEEE80211_FTYPE_CTL:
867		return;
868	default:
869		return;
870	}
871
872	is_packet_for_us = 0;
873	switch (ieee->iw_mode) {
874	case IW_MODE_ADHOC:
875		/* our BSS and not from/to DS */
876		if (memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) == 0)
877		if ((fc & (IEEE80211_FCTL_TODS+IEEE80211_FCTL_FROMDS)) == 0) {
878			/* promisc: get all */
879			if (ieee->dev->flags & IFF_PROMISC)
880				is_packet_for_us = 1;
881			/* to us */
882			else if (memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN) == 0)
883				is_packet_for_us = 1;
884			/* mcast */
885			else if (is_multicast_ether_addr(hdr->addr1))
886				is_packet_for_us = 1;
887		}
888		break;
889	case IW_MODE_INFRA:
890		/* our BSS (== from our AP) and from DS */
891		if (memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) == 0)
892		if ((fc & (IEEE80211_FCTL_TODS+IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS) {
893			/* promisc: get all */
894			if (ieee->dev->flags & IFF_PROMISC)
895				is_packet_for_us = 1;
896			/* to us */
897			else if (memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN) == 0)
898				is_packet_for_us = 1;
899			/* mcast */
900			else if (is_multicast_ether_addr(hdr->addr1)) {
901				/* not our own packet bcasted from AP */
902				if (memcmp(hdr->addr3, ieee->dev->dev_addr, ETH_ALEN))
903					is_packet_for_us = 1;
904			}
905		}
906		break;
907	default:
908		/* ? */
909		break;
910	}
911
912	if (is_packet_for_us)
913		if (!libipw_rx(ieee, skb, stats))
914			dev_kfree_skb_irq(skb);
915	return;
916
917drop_free:
918	dev_kfree_skb_irq(skb);
919	ieee->dev->stats.rx_dropped++;
920}
921
922#define MGMT_FRAME_FIXED_PART_LENGTH		0x24
923
924static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
925
926/*
927* Make ther structure we read from the beacon packet has
928* the right values
929*/
930static int libipw_verify_qos_info(struct libipw_qos_information_element
931				     *info_element, int sub_type)
932{
933
934	if (info_element->qui_subtype != sub_type)
935		return -1;
936	if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
937		return -1;
938	if (info_element->qui_type != QOS_OUI_TYPE)
939		return -1;
940	if (info_element->version != QOS_VERSION_1)
941		return -1;
942
943	return 0;
944}
945
946/*
947 * Parse a QoS parameter element
948 */
949static int libipw_read_qos_param_element(struct libipw_qos_parameter_info
950					    *element_param, struct libipw_info_element
951					    *info_element)
952{
953	int ret = 0;
954	u16 size = sizeof(struct libipw_qos_parameter_info) - 2;
955
956	if ((info_element == NULL) || (element_param == NULL))
957		return -1;
958
959	if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
960		memcpy(element_param->info_element.qui, info_element->data,
961		       info_element->len);
962		element_param->info_element.elementID = info_element->id;
963		element_param->info_element.length = info_element->len;
964	} else
965		ret = -1;
966	if (ret == 0)
967		ret = libipw_verify_qos_info(&element_param->info_element,
968						QOS_OUI_PARAM_SUB_TYPE);
969	return ret;
970}
971
972/*
973 * Parse a QoS information element
974 */
975static int libipw_read_qos_info_element(struct
976					   libipw_qos_information_element
977					   *element_info, struct libipw_info_element
978					   *info_element)
979{
980	int ret = 0;
981	u16 size = sizeof(struct libipw_qos_information_element) - 2;
982
983	if (element_info == NULL)
984		return -1;
985	if (info_element == NULL)
986		return -1;
987
988	if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
989		memcpy(element_info->qui, info_element->data,
990		       info_element->len);
991		element_info->elementID = info_element->id;
992		element_info->length = info_element->len;
993	} else
994		ret = -1;
995
996	if (ret == 0)
997		ret = libipw_verify_qos_info(element_info,
998						QOS_OUI_INFO_SUB_TYPE);
999	return ret;
1000}
1001
1002/*
1003 * Write QoS parameters from the ac parameters.
1004 */
1005static int libipw_qos_convert_ac_to_parameters(struct
1006						  libipw_qos_parameter_info
1007						  *param_elm, struct
1008						  libipw_qos_parameters
1009						  *qos_param)
1010{
1011	int rc = 0;
1012	int i;
1013	struct libipw_qos_ac_parameter *ac_params;
1014	u32 txop;
1015	u8 cw_min;
1016	u8 cw_max;
1017
1018	for (i = 0; i < QOS_QUEUE_NUM; i++) {
1019		ac_params = &(param_elm->ac_params_record[i]);
1020
1021		qos_param->aifs[i] = (ac_params->aci_aifsn) & 0x0F;
1022		qos_param->aifs[i] -= (qos_param->aifs[i] < 2) ? 0 : 2;
1023
1024		cw_min = ac_params->ecw_min_max & 0x0F;
1025		qos_param->cw_min[i] = cpu_to_le16((1 << cw_min) - 1);
1026
1027		cw_max = (ac_params->ecw_min_max & 0xF0) >> 4;
1028		qos_param->cw_max[i] = cpu_to_le16((1 << cw_max) - 1);
1029
1030		qos_param->flag[i] =
1031		    (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
1032
1033		txop = le16_to_cpu(ac_params->tx_op_limit) * 32;
1034		qos_param->tx_op_limit[i] = cpu_to_le16(txop);
1035	}
1036	return rc;
1037}
1038
1039/*
1040 * we have a generic data element which it may contain QoS information or
1041 * parameters element. check the information element length to decide
1042 * which type to read
1043 */
1044static int libipw_parse_qos_info_param_IE(struct libipw_info_element
1045					     *info_element,
1046					     struct libipw_network *network)
1047{
1048	int rc = 0;
1049	struct libipw_qos_parameters *qos_param = NULL;
1050	struct libipw_qos_information_element qos_info_element;
1051
1052	rc = libipw_read_qos_info_element(&qos_info_element, info_element);
1053
1054	if (rc == 0) {
1055		network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
1056		network->flags |= NETWORK_HAS_QOS_INFORMATION;
1057	} else {
1058		struct libipw_qos_parameter_info param_element;
1059
1060		rc = libipw_read_qos_param_element(&param_element,
1061						      info_element);
1062		if (rc == 0) {
1063			qos_param = &(network->qos_data.parameters);
1064			libipw_qos_convert_ac_to_parameters(&param_element,
1065							       qos_param);
1066			network->flags |= NETWORK_HAS_QOS_PARAMETERS;
1067			network->qos_data.param_count =
1068			    param_element.info_element.ac_info & 0x0F;
1069		}
1070	}
1071
1072	if (rc == 0) {
1073		LIBIPW_DEBUG_QOS("QoS is supported\n");
1074		network->qos_data.supported = 1;
1075	}
1076	return rc;
1077}
1078
1079#ifdef CONFIG_LIBIPW_DEBUG
1080#define MFIE_STRING(x) case WLAN_EID_ ##x: return #x
1081
1082static const char *get_info_element_string(u16 id)
1083{
1084	switch (id) {
1085		MFIE_STRING(SSID);
1086		MFIE_STRING(SUPP_RATES);
1087		MFIE_STRING(FH_PARAMS);
1088		MFIE_STRING(DS_PARAMS);
1089		MFIE_STRING(CF_PARAMS);
1090		MFIE_STRING(TIM);
1091		MFIE_STRING(IBSS_PARAMS);
1092		MFIE_STRING(COUNTRY);
1093		MFIE_STRING(HP_PARAMS);
1094		MFIE_STRING(HP_TABLE);
1095		MFIE_STRING(REQUEST);
1096		MFIE_STRING(CHALLENGE);
1097		MFIE_STRING(PWR_CONSTRAINT);
1098		MFIE_STRING(PWR_CAPABILITY);
1099		MFIE_STRING(TPC_REQUEST);
1100		MFIE_STRING(TPC_REPORT);
1101		MFIE_STRING(SUPPORTED_CHANNELS);
1102		MFIE_STRING(CHANNEL_SWITCH);
1103		MFIE_STRING(MEASURE_REQUEST);
1104		MFIE_STRING(MEASURE_REPORT);
1105		MFIE_STRING(QUIET);
1106		MFIE_STRING(IBSS_DFS);
1107		MFIE_STRING(ERP_INFO);
1108		MFIE_STRING(RSN);
1109		MFIE_STRING(EXT_SUPP_RATES);
1110		MFIE_STRING(GENERIC);
1111		MFIE_STRING(QOS_PARAMETER);
1112	default:
1113		return "UNKNOWN";
1114	}
1115}
1116#endif
1117
1118static int libipw_parse_info_param(struct libipw_info_element
1119				      *info_element, u16 length,
1120				      struct libipw_network *network)
1121{
1122	DECLARE_SSID_BUF(ssid);
1123	u8 i;
1124#ifdef CONFIG_LIBIPW_DEBUG
1125	char rates_str[64];
1126	char *p;
1127#endif
1128
1129	while (length >= sizeof(*info_element)) {
1130		if (sizeof(*info_element) + info_element->len > length) {
1131			LIBIPW_DEBUG_MGMT("Info elem: parse failed: "
1132					     "info_element->len + 2 > left : "
1133					     "info_element->len+2=%zd left=%d, id=%d.\n",
1134					     info_element->len +
1135					     sizeof(*info_element),
1136					     length, info_element->id);
1137			/* We stop processing but don't return an error here
1138			 * because some misbehaviour APs break this rule. ie.
1139			 * Orinoco AP1000. */
1140			break;
1141		}
1142
1143		switch (info_element->id) {
1144		case WLAN_EID_SSID:
1145			network->ssid_len = min(info_element->len,
1146						(u8) IW_ESSID_MAX_SIZE);
1147			memcpy(network->ssid, info_element->data,
1148			       network->ssid_len);
1149			if (network->ssid_len < IW_ESSID_MAX_SIZE)
1150				memset(network->ssid + network->ssid_len, 0,
1151				       IW_ESSID_MAX_SIZE - network->ssid_len);
1152
1153			LIBIPW_DEBUG_MGMT("WLAN_EID_SSID: '%s' len=%d.\n",
1154					     print_ssid(ssid, network->ssid,
1155							network->ssid_len),
1156					     network->ssid_len);
1157			break;
1158
1159		case WLAN_EID_SUPP_RATES:
1160#ifdef CONFIG_LIBIPW_DEBUG
1161			p = rates_str;
1162#endif
1163			network->rates_len = min(info_element->len,
1164						 MAX_RATES_LENGTH);
1165			for (i = 0; i < network->rates_len; i++) {
1166				network->rates[i] = info_element->data[i];
1167#ifdef CONFIG_LIBIPW_DEBUG
1168				p += snprintf(p, sizeof(rates_str) -
1169					      (p - rates_str), "%02X ",
1170					      network->rates[i]);
1171#endif
1172				if (libipw_is_ofdm_rate
1173				    (info_element->data[i])) {
1174					network->flags |= NETWORK_HAS_OFDM;
1175					if (info_element->data[i] &
1176					    LIBIPW_BASIC_RATE_MASK)
1177						network->flags &=
1178						    ~NETWORK_HAS_CCK;
1179				}
1180			}
1181
1182			LIBIPW_DEBUG_MGMT("WLAN_EID_SUPP_RATES: '%s' (%d)\n",
1183					     rates_str, network->rates_len);
1184			break;
1185
1186		case WLAN_EID_EXT_SUPP_RATES:
1187#ifdef CONFIG_LIBIPW_DEBUG
1188			p = rates_str;
1189#endif
1190			network->rates_ex_len = min(info_element->len,
1191						    MAX_RATES_EX_LENGTH);
1192			for (i = 0; i < network->rates_ex_len; i++) {
1193				network->rates_ex[i] = info_element->data[i];
1194#ifdef CONFIG_LIBIPW_DEBUG
1195				p += snprintf(p, sizeof(rates_str) -
1196					      (p - rates_str), "%02X ",
1197					      network->rates[i]);
1198#endif
1199				if (libipw_is_ofdm_rate
1200				    (info_element->data[i])) {
1201					network->flags |= NETWORK_HAS_OFDM;
1202					if (info_element->data[i] &
1203					    LIBIPW_BASIC_RATE_MASK)
1204						network->flags &=
1205						    ~NETWORK_HAS_CCK;
1206				}
1207			}
1208
1209			LIBIPW_DEBUG_MGMT("WLAN_EID_EXT_SUPP_RATES: '%s' (%d)\n",
1210					     rates_str, network->rates_ex_len);
1211			break;
1212
1213		case WLAN_EID_DS_PARAMS:
1214			LIBIPW_DEBUG_MGMT("WLAN_EID_DS_PARAMS: %d\n",
1215					     info_element->data[0]);
1216			network->channel = info_element->data[0];
1217			break;
1218
1219		case WLAN_EID_FH_PARAMS:
1220			LIBIPW_DEBUG_MGMT("WLAN_EID_FH_PARAMS: ignored\n");
1221			break;
1222
1223		case WLAN_EID_CF_PARAMS:
1224			LIBIPW_DEBUG_MGMT("WLAN_EID_CF_PARAMS: ignored\n");
1225			break;
1226
1227		case WLAN_EID_TIM:
1228			network->tim.tim_count = info_element->data[0];
1229			network->tim.tim_period = info_element->data[1];
1230			LIBIPW_DEBUG_MGMT("WLAN_EID_TIM: partially ignored\n");
1231			break;
1232
1233		case WLAN_EID_ERP_INFO:
1234			network->erp_value = info_element->data[0];
1235			network->flags |= NETWORK_HAS_ERP_VALUE;
1236			LIBIPW_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
1237					     network->erp_value);
1238			break;
1239
1240		case WLAN_EID_IBSS_PARAMS:
1241			network->atim_window = info_element->data[0];
1242			LIBIPW_DEBUG_MGMT("WLAN_EID_IBSS_PARAMS: %d\n",
1243					     network->atim_window);
1244			break;
1245
1246		case WLAN_EID_CHALLENGE:
1247			LIBIPW_DEBUG_MGMT("WLAN_EID_CHALLENGE: ignored\n");
1248			break;
1249
1250		case WLAN_EID_GENERIC:
1251			LIBIPW_DEBUG_MGMT("WLAN_EID_GENERIC: %d bytes\n",
1252					     info_element->len);
1253			if (!libipw_parse_qos_info_param_IE(info_element,
1254							       network))
1255				break;
1256
1257			if (info_element->len >= 4 &&
1258			    info_element->data[0] == 0x00 &&
1259			    info_element->data[1] == 0x50 &&
1260			    info_element->data[2] == 0xf2 &&
1261			    info_element->data[3] == 0x01) {
1262				network->wpa_ie_len = min(info_element->len + 2,
1263							  MAX_WPA_IE_LEN);
1264				memcpy(network->wpa_ie, info_element,
1265				       network->wpa_ie_len);
1266			}
1267			break;
1268
1269		case WLAN_EID_RSN:
1270			LIBIPW_DEBUG_MGMT("WLAN_EID_RSN: %d bytes\n",
1271					     info_element->len);
1272			network->rsn_ie_len = min(info_element->len + 2,
1273						  MAX_WPA_IE_LEN);
1274			memcpy(network->rsn_ie, info_element,
1275			       network->rsn_ie_len);
1276			break;
1277
1278		case WLAN_EID_QOS_PARAMETER:
1279			printk(KERN_ERR
1280			       "QoS Error need to parse QOS_PARAMETER IE\n");
1281			break;
1282			/* 802.11h */
1283		case WLAN_EID_PWR_CONSTRAINT:
1284			network->power_constraint = info_element->data[0];
1285			network->flags |= NETWORK_HAS_POWER_CONSTRAINT;
1286			break;
1287
1288		case WLAN_EID_CHANNEL_SWITCH:
1289			network->power_constraint = info_element->data[0];
1290			network->flags |= NETWORK_HAS_CSA;
1291			break;
1292
1293		case WLAN_EID_QUIET:
1294			network->quiet.count = info_element->data[0];
1295			network->quiet.period = info_element->data[1];
1296			network->quiet.duration = info_element->data[2];
1297			network->quiet.offset = info_element->data[3];
1298			network->flags |= NETWORK_HAS_QUIET;
1299			break;
1300
1301		case WLAN_EID_IBSS_DFS:
1302			if (network->ibss_dfs)
1303				break;
1304			network->ibss_dfs = kmemdup(info_element->data,
1305						    info_element->len,
1306						    GFP_ATOMIC);
1307			if (!network->ibss_dfs)
1308				return 1;
1309			network->flags |= NETWORK_HAS_IBSS_DFS;
1310			break;
1311
1312		case WLAN_EID_TPC_REPORT:
1313			network->tpc_report.transmit_power =
1314			    info_element->data[0];
1315			network->tpc_report.link_margin = info_element->data[1];
1316			network->flags |= NETWORK_HAS_TPC_REPORT;
1317			break;
1318
1319		default:
1320			LIBIPW_DEBUG_MGMT
1321			    ("Unsupported info element: %s (%d)\n",
1322			     get_info_element_string(info_element->id),
1323			     info_element->id);
1324			break;
1325		}
1326
1327		length -= sizeof(*info_element) + info_element->len;
1328		info_element =
1329		    (struct libipw_info_element *)&info_element->
1330		    data[info_element->len];
1331	}
1332
1333	return 0;
1334}
1335
1336static int libipw_handle_assoc_resp(struct libipw_device *ieee, struct libipw_assoc_response
1337				       *frame, struct libipw_rx_stats *stats)
1338{
1339	struct libipw_network network_resp = {
1340		.ibss_dfs = NULL,
1341	};
1342	struct libipw_network *network = &network_resp;
1343	struct net_device *dev = ieee->dev;
1344
1345	network->flags = 0;
1346	network->qos_data.active = 0;
1347	network->qos_data.supported = 0;
1348	network->qos_data.param_count = 0;
1349	network->qos_data.old_param_count = 0;
1350
1351	//network->atim_window = le16_to_cpu(frame->aid) & (0x3FFF);
1352	network->atim_window = le16_to_cpu(frame->aid);
1353	network->listen_interval = le16_to_cpu(frame->status);
1354	memcpy(network->bssid, frame->header.addr3, ETH_ALEN);
1355	network->capability = le16_to_cpu(frame->capability);
1356	network->last_scanned = jiffies;
1357	network->rates_len = network->rates_ex_len = 0;
1358	network->last_associate = 0;
1359	network->ssid_len = 0;
1360	network->erp_value =
1361	    (network->capability & WLAN_CAPABILITY_IBSS) ? 0x3 : 0x0;
1362
1363	if (stats->freq == LIBIPW_52GHZ_BAND) {
1364		/* for A band (No DS info) */
1365		network->channel = stats->received_channel;
1366	} else
1367		network->flags |= NETWORK_HAS_CCK;
1368
1369	network->wpa_ie_len = 0;
1370	network->rsn_ie_len = 0;
1371
1372	if (libipw_parse_info_param
1373	    (frame->info_element, stats->len - sizeof(*frame), network))
1374		return 1;
1375
1376	network->mode = 0;
1377	if (stats->freq == LIBIPW_52GHZ_BAND)
1378		network->mode = IEEE_A;
1379	else {
1380		if (network->flags & NETWORK_HAS_OFDM)
1381			network->mode |= IEEE_G;
1382		if (network->flags & NETWORK_HAS_CCK)
1383			network->mode |= IEEE_B;
1384	}
1385
1386	memcpy(&network->stats, stats, sizeof(network->stats));
1387
1388	if (ieee->handle_assoc_response != NULL)
1389		ieee->handle_assoc_response(dev, frame, network);
1390
1391	return 0;
1392}
1393
1394/***************************************************/
1395
1396static int libipw_network_init(struct libipw_device *ieee, struct libipw_probe_response
1397					 *beacon,
1398					 struct libipw_network *network,
1399					 struct libipw_rx_stats *stats)
1400{
1401	DECLARE_SSID_BUF(ssid);
1402
1403	network->qos_data.active = 0;
1404	network->qos_data.supported = 0;
1405	network->qos_data.param_count = 0;
1406	network->qos_data.old_param_count = 0;
1407
1408	/* Pull out fixed field data */
1409	memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
1410	network->capability = le16_to_cpu(beacon->capability);
1411	network->last_scanned = jiffies;
1412	network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
1413	network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
1414	network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
1415	/* Where to pull this? beacon->listen_interval; */
1416	network->listen_interval = 0x0A;
1417	network->rates_len = network->rates_ex_len = 0;
1418	network->last_associate = 0;
1419	network->ssid_len = 0;
1420	network->flags = 0;
1421	network->atim_window = 0;
1422	network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
1423	    0x3 : 0x0;
1424
1425	if (stats->freq == LIBIPW_52GHZ_BAND) {
1426		/* for A band (No DS info) */
1427		network->channel = stats->received_channel;
1428	} else
1429		network->flags |= NETWORK_HAS_CCK;
1430
1431	network->wpa_ie_len = 0;
1432	network->rsn_ie_len = 0;
1433
1434	if (libipw_parse_info_param
1435	    (beacon->info_element, stats->len - sizeof(*beacon), network))
1436		return 1;
1437
1438	network->mode = 0;
1439	if (stats->freq == LIBIPW_52GHZ_BAND)
1440		network->mode = IEEE_A;
1441	else {
1442		if (network->flags & NETWORK_HAS_OFDM)
1443			network->mode |= IEEE_G;
1444		if (network->flags & NETWORK_HAS_CCK)
1445			network->mode |= IEEE_B;
1446	}
1447
1448	if (network->mode == 0) {
1449		LIBIPW_DEBUG_SCAN("Filtered out '%s (%pM)' "
1450				     "network.\n",
1451				     print_ssid(ssid, network->ssid,
1452						 network->ssid_len),
1453				     network->bssid);
1454		return 1;
1455	}
1456
1457	memcpy(&network->stats, stats, sizeof(network->stats));
1458
1459	return 0;
1460}
1461
1462static inline int is_same_network(struct libipw_network *src,
1463				  struct libipw_network *dst)
1464{
1465	/* A network is only a duplicate if the channel, BSSID, and ESSID
1466	 * all match.  We treat all <hidden> with the same BSSID and channel
1467	 * as one network */
1468	return ((src->ssid_len == dst->ssid_len) &&
1469		(src->channel == dst->channel) &&
1470		!compare_ether_addr(src->bssid, dst->bssid) &&
1471		!memcmp(src->ssid, dst->ssid, src->ssid_len));
1472}
1473
1474static void update_network(struct libipw_network *dst,
1475				  struct libipw_network *src)
1476{
1477	int qos_active;
1478	u8 old_param;
1479
1480	libipw_network_reset(dst);
1481	dst->ibss_dfs = src->ibss_dfs;
1482
1483	/* We only update the statistics if they were created by receiving
1484	 * the network information on the actual channel the network is on.
1485	 *
1486	 * This keeps beacons received on neighbor channels from bringing
1487	 * down the signal level of an AP. */
1488	if (dst->channel == src->stats.received_channel)
1489		memcpy(&dst->stats, &src->stats,
1490		       sizeof(struct libipw_rx_stats));
1491	else
1492		LIBIPW_DEBUG_SCAN("Network %pM info received "
1493			"off channel (%d vs. %d)\n", src->bssid,
1494			dst->channel, src->stats.received_channel);
1495
1496	dst->capability = src->capability;
1497	memcpy(dst->rates, src->rates, src->rates_len);
1498	dst->rates_len = src->rates_len;
1499	memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1500	dst->rates_ex_len = src->rates_ex_len;
1501
1502	dst->mode = src->mode;
1503	dst->flags = src->flags;
1504	dst->time_stamp[0] = src->time_stamp[0];
1505	dst->time_stamp[1] = src->time_stamp[1];
1506
1507	dst->beacon_interval = src->beacon_interval;
1508	dst->listen_interval = src->listen_interval;
1509	dst->atim_window = src->atim_window;
1510	dst->erp_value = src->erp_value;
1511	dst->tim = src->tim;
1512
1513	memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1514	dst->wpa_ie_len = src->wpa_ie_len;
1515	memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1516	dst->rsn_ie_len = src->rsn_ie_len;
1517
1518	dst->last_scanned = jiffies;
1519	qos_active = src->qos_data.active;
1520	old_param = dst->qos_data.old_param_count;
1521	if (dst->flags & NETWORK_HAS_QOS_MASK)
1522		memcpy(&dst->qos_data, &src->qos_data,
1523		       sizeof(struct libipw_qos_data));
1524	else {
1525		dst->qos_data.supported = src->qos_data.supported;
1526		dst->qos_data.param_count = src->qos_data.param_count;
1527	}
1528
1529	if (dst->qos_data.supported == 1) {
1530		if (dst->ssid_len)
1531			LIBIPW_DEBUG_QOS
1532			    ("QoS the network %s is QoS supported\n",
1533			     dst->ssid);
1534		else
1535			LIBIPW_DEBUG_QOS
1536			    ("QoS the network is QoS supported\n");
1537	}
1538	dst->qos_data.active = qos_active;
1539	dst->qos_data.old_param_count = old_param;
1540
1541	/* dst->last_associate is not overwritten */
1542}
1543
1544static inline int is_beacon(__le16 fc)
1545{
1546	return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
1547}
1548
1549static void libipw_process_probe_response(struct libipw_device
1550						    *ieee, struct
1551						    libipw_probe_response
1552						    *beacon, struct libipw_rx_stats
1553						    *stats)
1554{
1555	struct net_device *dev = ieee->dev;
1556	struct libipw_network network = {
1557		.ibss_dfs = NULL,
1558	};
1559	struct libipw_network *target;
1560	struct libipw_network *oldest = NULL;
1561#ifdef CONFIG_LIBIPW_DEBUG
1562	struct libipw_info_element *info_element = beacon->info_element;
1563#endif
1564	unsigned long flags;
1565	DECLARE_SSID_BUF(ssid);
1566
1567	LIBIPW_DEBUG_SCAN("'%s' (%pM"
1568		     "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1569		     print_ssid(ssid, info_element->data, info_element->len),
1570		     beacon->header.addr3,
1571		     (beacon->capability & cpu_to_le16(1 << 0xf)) ? '1' : '0',
1572		     (beacon->capability & cpu_to_le16(1 << 0xe)) ? '1' : '0',
1573		     (beacon->capability & cpu_to_le16(1 << 0xd)) ? '1' : '0',
1574		     (beacon->capability & cpu_to_le16(1 << 0xc)) ? '1' : '0',
1575		     (beacon->capability & cpu_to_le16(1 << 0xb)) ? '1' : '0',
1576		     (beacon->capability & cpu_to_le16(1 << 0xa)) ? '1' : '0',
1577		     (beacon->capability & cpu_to_le16(1 << 0x9)) ? '1' : '0',
1578		     (beacon->capability & cpu_to_le16(1 << 0x8)) ? '1' : '0',
1579		     (beacon->capability & cpu_to_le16(1 << 0x7)) ? '1' : '0',
1580		     (beacon->capability & cpu_to_le16(1 << 0x6)) ? '1' : '0',
1581		     (beacon->capability & cpu_to_le16(1 << 0x5)) ? '1' : '0',
1582		     (beacon->capability & cpu_to_le16(1 << 0x4)) ? '1' : '0',
1583		     (beacon->capability & cpu_to_le16(1 << 0x3)) ? '1' : '0',
1584		     (beacon->capability & cpu_to_le16(1 << 0x2)) ? '1' : '0',
1585		     (beacon->capability & cpu_to_le16(1 << 0x1)) ? '1' : '0',
1586		     (beacon->capability & cpu_to_le16(1 << 0x0)) ? '1' : '0');
1587
1588	if (libipw_network_init(ieee, beacon, &network, stats)) {
1589		LIBIPW_DEBUG_SCAN("Dropped '%s' (%pM) via %s.\n",
1590				     print_ssid(ssid, info_element->data,
1591						 info_element->len),
1592				     beacon->header.addr3,
1593				     is_beacon(beacon->header.frame_ctl) ?
1594				     "BEACON" : "PROBE RESPONSE");
1595		return;
1596	}
1597
1598	/* The network parsed correctly -- so now we scan our known networks
1599	 * to see if we can find it in our list.
1600	 *
1601	 * NOTE:  This search is definitely not optimized.  Once its doing
1602	 *        the "right thing" we'll optimize it for efficiency if
1603	 *        necessary */
1604
1605	/* Search for this entry in the list and update it if it is
1606	 * already there. */
1607
1608	spin_lock_irqsave(&ieee->lock, flags);
1609
1610	list_for_each_entry(target, &ieee->network_list, list) {
1611		if (is_same_network(target, &network))
1612			break;
1613
1614		if ((oldest == NULL) ||
1615		    time_before(target->last_scanned, oldest->last_scanned))
1616			oldest = target;
1617	}
1618
1619	/* If we didn't find a match, then get a new network slot to initialize
1620	 * with this beacon's information */
1621	if (&target->list == &ieee->network_list) {
1622		if (list_empty(&ieee->network_free_list)) {
1623			/* If there are no more slots, expire the oldest */
1624			list_del(&oldest->list);
1625			target = oldest;
1626			LIBIPW_DEBUG_SCAN("Expired '%s' (%pM) from "
1627					     "network list.\n",
1628					     print_ssid(ssid, target->ssid,
1629							 target->ssid_len),
1630					     target->bssid);
1631			libipw_network_reset(target);
1632		} else {
1633			/* Otherwise just pull from the free list */
1634			target = list_entry(ieee->network_free_list.next,
1635					    struct libipw_network, list);
1636			list_del(ieee->network_free_list.next);
1637		}
1638
1639#ifdef CONFIG_LIBIPW_DEBUG
1640		LIBIPW_DEBUG_SCAN("Adding '%s' (%pM) via %s.\n",
1641				     print_ssid(ssid, network.ssid,
1642						 network.ssid_len),
1643				     network.bssid,
1644				     is_beacon(beacon->header.frame_ctl) ?
1645				     "BEACON" : "PROBE RESPONSE");
1646#endif
1647		memcpy(target, &network, sizeof(*target));
1648		network.ibss_dfs = NULL;
1649		list_add_tail(&target->list, &ieee->network_list);
1650	} else {
1651		LIBIPW_DEBUG_SCAN("Updating '%s' (%pM) via %s.\n",
1652				     print_ssid(ssid, target->ssid,
1653						 target->ssid_len),
1654				     target->bssid,
1655				     is_beacon(beacon->header.frame_ctl) ?
1656				     "BEACON" : "PROBE RESPONSE");
1657		update_network(target, &network);
1658		network.ibss_dfs = NULL;
1659	}
1660
1661	spin_unlock_irqrestore(&ieee->lock, flags);
1662
1663	if (is_beacon(beacon->header.frame_ctl)) {
1664		if (ieee->handle_beacon != NULL)
1665			ieee->handle_beacon(dev, beacon, target);
1666	} else {
1667		if (ieee->handle_probe_response != NULL)
1668			ieee->handle_probe_response(dev, beacon, target);
1669	}
1670}
1671
1672void libipw_rx_mgt(struct libipw_device *ieee,
1673		      struct libipw_hdr_4addr *header,
1674		      struct libipw_rx_stats *stats)
1675{
1676	switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
1677	case IEEE80211_STYPE_ASSOC_RESP:
1678		LIBIPW_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1679				     WLAN_FC_GET_STYPE(le16_to_cpu
1680						       (header->frame_ctl)));
1681		libipw_handle_assoc_resp(ieee,
1682					    (struct libipw_assoc_response *)
1683					    header, stats);
1684		break;
1685
1686	case IEEE80211_STYPE_REASSOC_RESP:
1687		LIBIPW_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1688				     WLAN_FC_GET_STYPE(le16_to_cpu
1689						       (header->frame_ctl)));
1690		break;
1691
1692	case IEEE80211_STYPE_PROBE_REQ:
1693		LIBIPW_DEBUG_MGMT("received auth (%d)\n",
1694				     WLAN_FC_GET_STYPE(le16_to_cpu
1695						       (header->frame_ctl)));
1696
1697		if (ieee->handle_probe_request != NULL)
1698			ieee->handle_probe_request(ieee->dev,
1699						   (struct
1700						    libipw_probe_request *)
1701						   header, stats);
1702		break;
1703
1704	case IEEE80211_STYPE_PROBE_RESP:
1705		LIBIPW_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1706				     WLAN_FC_GET_STYPE(le16_to_cpu
1707						       (header->frame_ctl)));
1708		LIBIPW_DEBUG_SCAN("Probe response\n");
1709		libipw_process_probe_response(ieee,
1710						 (struct
1711						  libipw_probe_response *)
1712						 header, stats);
1713		break;
1714
1715	case IEEE80211_STYPE_BEACON:
1716		LIBIPW_DEBUG_MGMT("received BEACON (%d)\n",
1717				     WLAN_FC_GET_STYPE(le16_to_cpu
1718						       (header->frame_ctl)));
1719		LIBIPW_DEBUG_SCAN("Beacon\n");
1720		libipw_process_probe_response(ieee,
1721						 (struct
1722						  libipw_probe_response *)
1723						 header, stats);
1724		break;
1725	case IEEE80211_STYPE_AUTH:
1726
1727		LIBIPW_DEBUG_MGMT("received auth (%d)\n",
1728				     WLAN_FC_GET_STYPE(le16_to_cpu
1729						       (header->frame_ctl)));
1730
1731		if (ieee->handle_auth != NULL)
1732			ieee->handle_auth(ieee->dev,
1733					  (struct libipw_auth *)header);
1734		break;
1735
1736	case IEEE80211_STYPE_DISASSOC:
1737		if (ieee->handle_disassoc != NULL)
1738			ieee->handle_disassoc(ieee->dev,
1739					      (struct libipw_disassoc *)
1740					      header);
1741		break;
1742
1743	case IEEE80211_STYPE_ACTION:
1744		LIBIPW_DEBUG_MGMT("ACTION\n");
1745		if (ieee->handle_action)
1746			ieee->handle_action(ieee->dev,
1747					    (struct libipw_action *)
1748					    header, stats);
1749		break;
1750
1751	case IEEE80211_STYPE_REASSOC_REQ:
1752		LIBIPW_DEBUG_MGMT("received reassoc (%d)\n",
1753				     WLAN_FC_GET_STYPE(le16_to_cpu
1754						       (header->frame_ctl)));
1755
1756		LIBIPW_DEBUG_MGMT("%s: LIBIPW_REASSOC_REQ received\n",
1757				     ieee->dev->name);
1758		if (ieee->handle_reassoc_request != NULL)
1759			ieee->handle_reassoc_request(ieee->dev,
1760						    (struct libipw_reassoc_request *)
1761						     header);
1762		break;
1763
1764	case IEEE80211_STYPE_ASSOC_REQ:
1765		LIBIPW_DEBUG_MGMT("received assoc (%d)\n",
1766				     WLAN_FC_GET_STYPE(le16_to_cpu
1767						       (header->frame_ctl)));
1768
1769		LIBIPW_DEBUG_MGMT("%s: LIBIPW_ASSOC_REQ received\n",
1770				     ieee->dev->name);
1771		if (ieee->handle_assoc_request != NULL)
1772			ieee->handle_assoc_request(ieee->dev);
1773		break;
1774
1775	case IEEE80211_STYPE_DEAUTH:
1776		LIBIPW_DEBUG_MGMT("DEAUTH\n");
1777		if (ieee->handle_deauth != NULL)
1778			ieee->handle_deauth(ieee->dev,
1779					    (struct libipw_deauth *)
1780					    header);
1781		break;
1782	default:
1783		LIBIPW_DEBUG_MGMT("received UNKNOWN (%d)\n",
1784				     WLAN_FC_GET_STYPE(le16_to_cpu
1785						       (header->frame_ctl)));
1786		LIBIPW_DEBUG_MGMT("%s: Unknown management packet: %d\n",
1787				     ieee->dev->name,
1788				     WLAN_FC_GET_STYPE(le16_to_cpu
1789						       (header->frame_ctl)));
1790		break;
1791	}
1792}
1793
1794EXPORT_SYMBOL_GPL(libipw_rx_any);
1795EXPORT_SYMBOL(libipw_rx_mgt);
1796EXPORT_SYMBOL(libipw_rx);
1797