libipw_rx.c 50.4 KB
Newer Older
J
Jeff Garzik 已提交
1 2 3 4 5
/*
 * Original code based Host AP (software wireless LAN access point) driver
 * for Intersil Prism2/2.5/3 - hostap.o module, common routines
 *
 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6 7
 * <j@w1.fi>
 * Copyright (c) 2002-2003, Jouni Malinen <j@w1.fi>
8
 * Copyright (c) 2004-2005, Intel Corporation
J
Jeff Garzik 已提交
9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation. See README and COPYING for
 * more details.
 */

#include <linux/compiler.h>
#include <linux/errno.h>
#include <linux/if_arp.h>
#include <linux/in6.h>
20
#include <linux/gfp.h>
J
Jeff Garzik 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/proc_fs.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/types.h>
#include <linux/wireless.h>
#include <linux/etherdevice.h>
#include <asm/uaccess.h>
#include <linux/ctype.h>

35
#include <net/lib80211.h>
36

37
#include "libipw.h"
J
Jeff Garzik 已提交
38

39
static void libipw_monitor_rx(struct libipw_device *ieee,
J
Jeff Garzik 已提交
40
					struct sk_buff *skb,
41
					struct libipw_rx_stats *rx_stats)
J
Jeff Garzik 已提交
42 43
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
44
	u16 fc = le16_to_cpu(hdr->frame_control);
J
Jeff Garzik 已提交
45 46

	skb->dev = ieee->dev;
47
	skb_reset_mac_header(skb);
48
	skb_pull(skb, libipw_get_hdrlen(fc));
J
Jeff Garzik 已提交
49
	skb->pkt_type = PACKET_OTHERHOST;
50
	skb->protocol = htons(ETH_P_80211_RAW);
J
Jeff Garzik 已提交
51 52 53 54 55
	memset(skb->cb, 0, sizeof(skb->cb));
	netif_rx(skb);
}

/* Called only as a tasklet (software IRQ) */
56 57
static struct libipw_frag_entry *libipw_frag_cache_find(struct
							      libipw_device
58 59 60 61 62
							      *ieee,
							      unsigned int seq,
							      unsigned int frag,
							      u8 * src,
							      u8 * dst)
J
Jeff Garzik 已提交
63
{
64
	struct libipw_frag_entry *entry;
J
Jeff Garzik 已提交
65 66
	int i;

67
	for (i = 0; i < LIBIPW_FRAG_CACHE_LEN; i++) {
J
Jeff Garzik 已提交
68 69 70
		entry = &ieee->frag_cache[i];
		if (entry->skb != NULL &&
		    time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
71
			LIBIPW_DEBUG_FRAG("expiring fragment cache entry "
72 73
					     "seq=%u last_frag=%u\n",
					     entry->seq, entry->last_frag);
J
Jeff Garzik 已提交
74 75 76 77 78 79
			dev_kfree_skb_any(entry->skb);
			entry->skb = NULL;
		}

		if (entry->skb != NULL && entry->seq == seq &&
		    (entry->last_frag + 1 == frag || frag == -1) &&
80 81
		    !compare_ether_addr(entry->src_addr, src) &&
		    !compare_ether_addr(entry->dst_addr, dst))
J
Jeff Garzik 已提交
82 83 84 85 86 87 88
			return entry;
	}

	return NULL;
}

/* Called only as a tasklet (software IRQ) */
89 90
static struct sk_buff *libipw_frag_cache_get(struct libipw_device *ieee,
						struct libipw_hdr_4addr *hdr)
J
Jeff Garzik 已提交
91 92 93 94
{
	struct sk_buff *skb = NULL;
	u16 sc;
	unsigned int frag, seq;
95
	struct libipw_frag_entry *entry;
J
Jeff Garzik 已提交
96 97 98 99 100 101 102 103

	sc = le16_to_cpu(hdr->seq_ctl);
	frag = WLAN_GET_SEQ_FRAG(sc);
	seq = WLAN_GET_SEQ_SEQ(sc);

	if (frag == 0) {
		/* Reserve enough space to fit maximum frame length */
		skb = dev_alloc_skb(ieee->dev->mtu +
104
				    sizeof(struct libipw_hdr_4addr) +
105 106 107
				    8 /* LLC */  +
				    2 /* alignment */  +
				    8 /* WEP */  + ETH_ALEN /* WDS */ );
J
Jeff Garzik 已提交
108 109 110 111 112
		if (skb == NULL)
			return NULL;

		entry = &ieee->frag_cache[ieee->frag_next_idx];
		ieee->frag_next_idx++;
113
		if (ieee->frag_next_idx >= LIBIPW_FRAG_CACHE_LEN)
J
Jeff Garzik 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
			ieee->frag_next_idx = 0;

		if (entry->skb != NULL)
			dev_kfree_skb_any(entry->skb);

		entry->first_frag_time = jiffies;
		entry->seq = seq;
		entry->last_frag = frag;
		entry->skb = skb;
		memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
		memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
	} else {
		/* received a fragment of a frame for which the head fragment
		 * should have already been received */
128
		entry = libipw_frag_cache_find(ieee, seq, frag, hdr->addr2,
J
Jeff Garzik 已提交
129 130 131 132 133 134 135 136 137 138 139
						  hdr->addr1);
		if (entry != NULL) {
			entry->last_frag = frag;
			skb = entry->skb;
		}
	}

	return skb;
}

/* Called only as a tasklet (software IRQ) */
140 141
static int libipw_frag_cache_invalidate(struct libipw_device *ieee,
					   struct libipw_hdr_4addr *hdr)
J
Jeff Garzik 已提交
142 143 144
{
	u16 sc;
	unsigned int seq;
145
	struct libipw_frag_entry *entry;
J
Jeff Garzik 已提交
146 147 148 149

	sc = le16_to_cpu(hdr->seq_ctl);
	seq = WLAN_GET_SEQ_SEQ(sc);

150
	entry = libipw_frag_cache_find(ieee, seq, -1, hdr->addr2,
J
Jeff Garzik 已提交
151 152 153
					  hdr->addr1);

	if (entry == NULL) {
154
		LIBIPW_DEBUG_FRAG("could not invalidate fragment cache "
155
				     "entry (seq=%u)\n", seq);
J
Jeff Garzik 已提交
156 157 158 159 160 161 162 163
		return -1;
	}

	entry->skb = NULL;
	return 0;
}

#ifdef NOT_YET
164
/* libipw_rx_frame_mgtmt
J
Jeff Garzik 已提交
165 166 167
 *
 * Responsible for handling management control frames
 *
168
 * Called by libipw_rx */
169
static int
170 171
libipw_rx_frame_mgmt(struct libipw_device *ieee, struct sk_buff *skb,
			struct libipw_rx_stats *rx_stats, u16 type,
J
Jeff Garzik 已提交
172 173 174
			u16 stype)
{
	if (ieee->iw_mode == IW_MODE_MASTER) {
175
		printk(KERN_DEBUG "%s: Master mode not yet supported.\n",
J
Jeff Garzik 已提交
176 177 178
		       ieee->dev->name);
		return 0;
/*
179
  hostap_update_sta_ps(ieee, (struct hostap_libipw_hdr_4addr *)
J
Jeff Garzik 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  skb->data);*/
	}

	if (ieee->hostapd && type == WLAN_FC_TYPE_MGMT) {
		if (stype == WLAN_FC_STYPE_BEACON &&
		    ieee->iw_mode == IW_MODE_MASTER) {
			struct sk_buff *skb2;
			/* Process beacon frames also in kernel driver to
			 * update STA(AP) table statistics */
			skb2 = skb_clone(skb, GFP_ATOMIC);
			if (skb2)
				hostap_rx(skb2->dev, skb2, rx_stats);
		}

		/* send management frames to the user space daemon for
		 * processing */
		ieee->apdevstats.rx_packets++;
		ieee->apdevstats.rx_bytes += skb->len;
		prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
		return 0;
	}

202
	if (ieee->iw_mode == IW_MODE_MASTER) {
J
Jeff Garzik 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
			printk(KERN_DEBUG "%s: unknown management frame "
			       "(type=0x%02x, stype=0x%02x) dropped\n",
			       skb->dev->name, type, stype);
			return -1;
		}

		hostap_rx(skb->dev, skb, rx_stats);
		return 0;
	}

	printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
	       "received in non-Host AP mode\n", skb->dev->name);
	return -1;
}
#endif

/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
222 223
static unsigned char libipw_rfc1042_header[] =
    { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
224

J
Jeff Garzik 已提交
225
/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
226
static unsigned char libipw_bridge_tunnel_header[] =
227
    { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
J
Jeff Garzik 已提交
228 229
/* No encapsulation header if EtherType < 0x600 (=length) */

230 231
/* Called by libipw_rx_frame_decrypt */
static int libipw_is_eapol_frame(struct libipw_device *ieee,
J
Jeff Garzik 已提交
232 233 234 235
				    struct sk_buff *skb)
{
	struct net_device *dev = ieee->dev;
	u16 fc, ethertype;
236
	struct libipw_hdr_3addr *hdr;
J
Jeff Garzik 已提交
237 238 239 240 241
	u8 *pos;

	if (skb->len < 24)
		return 0;

242
	hdr = (struct libipw_hdr_3addr *)skb->data;
J
Jeff Garzik 已提交
243 244 245 246 247
	fc = le16_to_cpu(hdr->frame_ctl);

	/* check that the frame is unicast frame to us */
	if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
	    IEEE80211_FCTL_TODS &&
248 249
	    !compare_ether_addr(hdr->addr1, dev->dev_addr) &&
	    !compare_ether_addr(hdr->addr3, dev->dev_addr)) {
J
Jeff Garzik 已提交
250 251 252
		/* ToDS frame with own addr BSSID and DA */
	} else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
		   IEEE80211_FCTL_FROMDS &&
253
		   !compare_ether_addr(hdr->addr1, dev->dev_addr)) {
J
Jeff Garzik 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		/* FromDS frame with own addr as DA */
	} else
		return 0;

	if (skb->len < 24 + 8)
		return 0;

	/* check for port access entity Ethernet type */
	pos = skb->data + 24;
	ethertype = (pos[6] << 8) | pos[7];
	if (ethertype == ETH_P_PAE)
		return 1;

	return 0;
}

270
/* Called only as a tasklet (software IRQ), by libipw_rx */
271
static int
272
libipw_rx_frame_decrypt(struct libipw_device *ieee, struct sk_buff *skb,
273
			   struct lib80211_crypt_data *crypt)
J
Jeff Garzik 已提交
274
{
275
	struct libipw_hdr_3addr *hdr;
J
Jeff Garzik 已提交
276 277 278 279 280
	int res, hdrlen;

	if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
		return 0;

281 282
	hdr = (struct libipw_hdr_3addr *)skb->data;
	hdrlen = libipw_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
J
Jeff Garzik 已提交
283 284 285 286 287

	atomic_inc(&crypt->refcnt);
	res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
	atomic_dec(&crypt->refcnt);
	if (res < 0) {
288
		LIBIPW_DEBUG_DROP("decryption failed (SA=%pM) res=%d\n",
J
Johannes Berg 已提交
289
				     hdr->addr2, res);
J
Jeff Garzik 已提交
290
		if (res == -2)
291
			LIBIPW_DEBUG_DROP("Decryption failed ICV "
J
Jeff Garzik 已提交
292 293 294 295 296 297 298 299 300
					     "mismatch (key %d)\n",
					     skb->data[hdrlen + 3] >> 6);
		ieee->ieee_stats.rx_discards_undecryptable++;
		return -1;
	}

	return res;
}

301
/* Called only as a tasklet (software IRQ), by libipw_rx */
302
static int
303
libipw_rx_frame_decrypt_msdu(struct libipw_device *ieee,
304
				struct sk_buff *skb, int keyidx,
305
				struct lib80211_crypt_data *crypt)
J
Jeff Garzik 已提交
306
{
307
	struct libipw_hdr_3addr *hdr;
J
Jeff Garzik 已提交
308 309 310 311 312
	int res, hdrlen;

	if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
		return 0;

313 314
	hdr = (struct libipw_hdr_3addr *)skb->data;
	hdrlen = libipw_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
J
Jeff Garzik 已提交
315 316 317 318 319 320

	atomic_inc(&crypt->refcnt);
	res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
	atomic_dec(&crypt->refcnt);
	if (res < 0) {
		printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
J
Johannes Berg 已提交
321
		       " (SA=%pM keyidx=%d)\n", ieee->dev->name, hdr->addr2,
322
		       keyidx);
J
Jeff Garzik 已提交
323 324 325 326 327 328 329 330 331
		return -1;
	}

	return 0;
}

/* All received frames are sent to this function. @skb contains the frame in
 * IEEE 802.11 format, i.e., in the format it was sent over air.
 * This function is called only as a tasklet (software IRQ). */
332 333
int libipw_rx(struct libipw_device *ieee, struct sk_buff *skb,
		 struct libipw_rx_stats *rx_stats)
J
Jeff Garzik 已提交
334 335
{
	struct net_device *dev = ieee->dev;
336
	struct libipw_hdr_4addr *hdr;
J
Jeff Garzik 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	size_t hdrlen;
	u16 fc, type, stype, sc;
	unsigned int frag;
	u8 *payload;
	u16 ethertype;
#ifdef NOT_YET
	struct net_device *wds = NULL;
	struct sk_buff *skb2 = NULL;
	struct net_device *wds = NULL;
	int frame_authorized = 0;
	int from_assoc_ap = 0;
	void *sta = NULL;
#endif
	u8 dst[ETH_ALEN];
	u8 src[ETH_ALEN];
352
	struct lib80211_crypt_data *crypt = NULL;
J
Jeff Garzik 已提交
353
	int keyidx = 0;
354
	int can_be_decrypted = 0;
J
Jeff Garzik 已提交
355

356
	hdr = (struct libipw_hdr_4addr *)skb->data;
J
Jeff Garzik 已提交
357
	if (skb->len < 10) {
358
		printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
J
Jeff Garzik 已提交
359 360 361 362 363 364 365 366
		goto rx_dropped;
	}

	fc = le16_to_cpu(hdr->frame_ctl);
	type = WLAN_FC_GET_TYPE(fc);
	stype = WLAN_FC_GET_STYPE(fc);
	sc = le16_to_cpu(hdr->seq_ctl);
	frag = WLAN_GET_SEQ_FRAG(sc);
367
	hdrlen = libipw_get_hdrlen(fc);
J
Jeff Garzik 已提交
368

369 370 371 372 373 374
	if (skb->len < hdrlen) {
		printk(KERN_INFO "%s: invalid SKB length %d\n",
			dev->name, skb->len);
		goto rx_dropped;
	}

J
Jeff Garzik 已提交
375 376
	/* Put this code here so that we avoid duplicating it in all
	 * Rx paths. - Jean II */
377
#ifdef CONFIG_WIRELESS_EXT
J
Jeff Garzik 已提交
378 379
#ifdef IW_WIRELESS_SPY		/* defined in iw_handler.h */
	/* If spy monitoring on */
380
	if (ieee->spy_data.spy_number > 0) {
J
Jeff Garzik 已提交
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 387 388 389
			wstats.updated |= IW_QUAL_LEVEL_UPDATED;
		} else
			wstats.updated |= IW_QUAL_LEVEL_INVALID;

390
		if (rx_stats->mask & LIBIPW_STATMASK_NOISE) {
391 392 393 394 395
			wstats.noise = rx_stats->noise;
			wstats.updated |= IW_QUAL_NOISE_UPDATED;
		} else
			wstats.updated |= IW_QUAL_NOISE_INVALID;

396
		if (rx_stats->mask & LIBIPW_STATMASK_SIGNAL) {
397 398 399 400 401
			wstats.qual = rx_stats->signal;
			wstats.updated |= IW_QUAL_QUAL_UPDATED;
		} else
			wstats.updated |= IW_QUAL_QUAL_INVALID;

J
Jeff Garzik 已提交
402
		/* Update spy records */
403
		wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
J
Jeff Garzik 已提交
404
	}
405
#endif				/* IW_WIRELESS_SPY */
406
#endif				/* CONFIG_WIRELESS_EXT */
407 408

#ifdef NOT_YET
J
Jeff Garzik 已提交
409 410 411 412
	hostap_update_rx_stats(local->ap, hdr, rx_stats);
#endif

	if (ieee->iw_mode == IW_MODE_MONITOR) {
413 414
		dev->stats.rx_packets++;
		dev->stats.rx_bytes += skb->len;
415
		libipw_monitor_rx(ieee, skb, rx_stats);
J
Jeff Garzik 已提交
416 417 418
		return 1;
	}

419 420 421 422 423 424 425
	can_be_decrypted = (is_multicast_ether_addr(hdr->addr1) ||
			    is_broadcast_ether_addr(hdr->addr2)) ?
	    ieee->host_mc_decrypt : ieee->host_decrypt;

	if (can_be_decrypted) {
		if (skb->len >= hdrlen + 3) {
			/* Top two-bits of byte 3 are the key index */
426
			keyidx = skb->data[hdrlen + 3] >> 6;
427 428
		}

429 430 431
		/* ieee->crypt[] is WEP_KEY (4) in length.  Given that keyidx
		 * is only allowed 2-bits of storage, no value of keyidx can
		 * be provided via above code that would result in keyidx
432
		 * being out of range */
433
		crypt = ieee->crypt_info.crypt[keyidx];
434

J
Jeff Garzik 已提交
435 436 437 438 439 440 441 442 443 444
#ifdef NOT_YET
		sta = NULL;

		/* Use station specific key to override default keys if the
		 * receiver address is a unicast address ("individual RA"). If
		 * bcrx_sta_key parameter is set, station specific key is used
		 * even with broad/multicast targets (this is against IEEE
		 * 802.11, but makes it easier to use different keys with
		 * stations that do not support WEP key mapping). */

445
		if (is_unicast_ether_addr(hdr->addr1) || local->bcrx_sta_key)
446 447
			(void)hostap_handle_sta_crypto(local, hdr, &crypt,
						       &sta);
J
Jeff Garzik 已提交
448 449 450 451 452 453 454 455
#endif

		/* allow NULL decrypt to indicate an station specific override
		 * for default encryption */
		if (crypt && (crypt->ops == NULL ||
			      crypt->ops->decrypt_mpdu == NULL))
			crypt = NULL;

456
		if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
J
Jeff Garzik 已提交
457 458 459 460
			/* This seems to be triggered by some (multicast?)
			 * frames from other than current BSS, so just drop the
			 * frames silently instead of filling system log with
			 * these reports. */
461
			LIBIPW_DEBUG_DROP("Decryption failed (not set)"
J
Johannes Berg 已提交
462
					     " (SA=%pM)\n", hdr->addr2);
J
Jeff Garzik 已提交
463 464 465 466 467 468 469
			ieee->ieee_stats.rx_discards_undecryptable++;
			goto rx_dropped;
		}
	}
#ifdef NOT_YET
	if (type != WLAN_FC_TYPE_DATA) {
		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) {
J
Jeff Garzik 已提交
472
			printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
J
Johannes Berg 已提交
473
			       "from %pM\n", dev->name, hdr->addr2);
J
Jeff Garzik 已提交
474 475 476 477 478
			/* TODO: could inform hostapd about this so that it
			 * could send auth failure report */
			goto rx_dropped;
		}

479
		if (libipw_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
J
Jeff Garzik 已提交
480 481 482 483 484
			goto rx_dropped;
		else
			goto rx_exit;
	}
#endif
485 486 487 488 489
	/* drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.29) */
	if (sc == ieee->prev_seq_ctl)
		goto rx_dropped;
	else
		ieee->prev_seq_ctl = sc;
J
Jeff Garzik 已提交
490 491

	/* Data frame - extract src/dst addresses */
492
	if (skb->len < LIBIPW_3ADDR_LEN)
J
Jeff Garzik 已提交
493 494 495 496 497 498 499 500 501 502 503 504
		goto rx_dropped;

	switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
	case IEEE80211_FCTL_FROMDS:
		memcpy(dst, hdr->addr1, ETH_ALEN);
		memcpy(src, hdr->addr3, ETH_ALEN);
		break;
	case IEEE80211_FCTL_TODS:
		memcpy(dst, hdr->addr3, ETH_ALEN);
		memcpy(src, hdr->addr2, ETH_ALEN);
		break;
	case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
505
		if (skb->len < LIBIPW_4ADDR_LEN)
J
Jeff Garzik 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
			goto rx_dropped;
		memcpy(dst, hdr->addr3, ETH_ALEN);
		memcpy(src, hdr->addr4, ETH_ALEN);
		break;
	case 0:
		memcpy(dst, hdr->addr1, ETH_ALEN);
		memcpy(src, hdr->addr2, ETH_ALEN);
		break;
	}

#ifdef NOT_YET
	if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
		goto rx_dropped;
	if (wds) {
		skb->dev = dev = wds;
		stats = hostap_get_stats(dev);
	}

	if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
525 526
	    (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
	    IEEE80211_FCTL_FROMDS && ieee->stadev
527
	    && !compare_ether_addr(hdr->addr2, ieee->assoc_ap_addr)) {
J
Jeff Garzik 已提交
528 529 530 531 532 533 534 535 536
		/* Frame from BSSID of the AP for which we are a client */
		skb->dev = dev = ieee->stadev;
		stats = hostap_get_stats(dev);
		from_assoc_ap = 1;
	}
#endif

#ifdef NOT_YET
	if ((ieee->iw_mode == IW_MODE_MASTER ||
537
	     ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
J
Jeff Garzik 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
					     wds != NULL)) {
		case AP_RX_CONTINUE_NOT_AUTHORIZED:
			frame_authorized = 0;
			break;
		case AP_RX_CONTINUE:
			frame_authorized = 1;
			break;
		case AP_RX_DROP:
			goto rx_dropped;
		case AP_RX_EXIT:
			goto rx_exit;
		}
	}
#endif

	/* Nullfunc frames may have PS-bit set, so they must be passed to
	 * hostap_handle_sta_rx() before being dropped here. */
556 557 558

	stype &= ~IEEE80211_STYPE_QOS_DATA;

J
Jeff Garzik 已提交
559 560 561 562 563
	if (stype != IEEE80211_STYPE_DATA &&
	    stype != IEEE80211_STYPE_DATA_CFACK &&
	    stype != IEEE80211_STYPE_DATA_CFPOLL &&
	    stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
		if (stype != IEEE80211_STYPE_NULLFUNC)
564
			LIBIPW_DEBUG_DROP("RX: dropped data frame "
565 566 567
					     "with no data (type=0x%02x, "
					     "subtype=0x%02x, len=%d)\n",
					     type, stype, skb->len);
J
Jeff Garzik 已提交
568 569 570 571 572
		goto rx_dropped;
	}

	/* skb: hdr + (possibly fragmented, possibly encrypted) payload */

573
	if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
574
	    (keyidx = libipw_rx_frame_decrypt(ieee, skb, crypt)) < 0)
J
Jeff Garzik 已提交
575 576
		goto rx_dropped;

577
	hdr = (struct libipw_hdr_4addr *)skb->data;
J
Jeff Garzik 已提交
578 579 580

	/* skb: hdr + (possibly fragmented) plaintext payload */
	// PR: FIXME: hostap has additional conditions in the "if" below:
581
	// ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
582
	if ((frag != 0) || (fc & IEEE80211_FCTL_MOREFRAGS)) {
J
Jeff Garzik 已提交
583
		int flen;
584 585
		struct sk_buff *frag_skb = libipw_frag_cache_get(ieee, hdr);
		LIBIPW_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
J
Jeff Garzik 已提交
586 587

		if (!frag_skb) {
588
			LIBIPW_DEBUG(LIBIPW_DL_RX | LIBIPW_DL_FRAG,
J
Jeff Garzik 已提交
589 590 591 592 593 594 595 596 597 598 599
					"Rx cannot get skb from fragment "
					"cache (morefrag=%d seq=%u frag=%u)\n",
					(fc & IEEE80211_FCTL_MOREFRAGS) != 0,
					WLAN_GET_SEQ_SEQ(sc), frag);
			goto rx_dropped;
		}

		flen = skb->len;
		if (frag != 0)
			flen -= hdrlen;

600
		if (frag_skb->tail + flen > frag_skb->end) {
J
Jeff Garzik 已提交
601 602 603
			printk(KERN_WARNING "%s: host decrypted and "
			       "reassembled frame did not fit skb\n",
			       dev->name);
604
			libipw_frag_cache_invalidate(ieee, hdr);
J
Jeff Garzik 已提交
605 606 607 608 609 610
			goto rx_dropped;
		}

		if (frag == 0) {
			/* copy first fragment (including full headers) into
			 * beginning of the fragment cache skb */
611
			skb_copy_from_linear_data(skb, skb_put(frag_skb, flen), flen);
J
Jeff Garzik 已提交
612 613 614
		} else {
			/* append frame payload to the end of the fragment
			 * cache skb */
615 616
			skb_copy_from_linear_data_offset(skb, hdrlen,
				      skb_put(frag_skb, flen), flen);
J
Jeff Garzik 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630
		}
		dev_kfree_skb_any(skb);
		skb = NULL;

		if (fc & IEEE80211_FCTL_MOREFRAGS) {
			/* more fragments expected - leave the skb in fragment
			 * cache for now; it will be delivered to upper layers
			 * after all fragments have been received */
			goto rx_exit;
		}

		/* this was the last fragment and the frame will be
		 * delivered, so remove skb from fragment cache */
		skb = frag_skb;
631 632
		hdr = (struct libipw_hdr_4addr *)skb->data;
		libipw_frag_cache_invalidate(ieee, hdr);
J
Jeff Garzik 已提交
633 634 635 636
	}

	/* skb: hdr + (possible reassembled) full MSDU payload; possibly still
	 * encrypted/authenticated */
637
	if ((fc & IEEE80211_FCTL_PROTECTED) && can_be_decrypted &&
638
	    libipw_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
J
Jeff Garzik 已提交
639 640
		goto rx_dropped;

641
	hdr = (struct libipw_hdr_4addr *)skb->data;
642
	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
643
		if (		/*ieee->ieee802_1x && */
644
			   libipw_is_eapol_frame(ieee, skb)) {
J
Jeff Garzik 已提交
645 646 647
			/* pass unencrypted EAPOL frames even if encryption is
			 * configured */
		} else {
648
			LIBIPW_DEBUG_DROP("encryption configured, but RX "
J
Johannes Berg 已提交
649 650
					     "frame not encrypted (SA=%pM)\n",
					     hdr->addr2);
J
Jeff Garzik 已提交
651 652 653 654
			goto rx_dropped;
		}
	}

655
	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
656 657
	    !libipw_is_eapol_frame(ieee, skb)) {
		LIBIPW_DEBUG_DROP("dropped unencrypted RX data "
J
Johannes Berg 已提交
658 659
				     "frame from %pM (drop_unencrypted=1)\n",
				     hdr->addr2);
J
Jeff Garzik 已提交
660 661 662
		goto rx_dropped;
	}

663 664 665 666
	/* If the frame was decrypted in hardware, we may need to strip off
	 * any security data (IV, ICV, etc) that was left behind */
	if (!can_be_decrypted && (fc & IEEE80211_FCTL_PROTECTED) &&
	    ieee->host_strip_iv_icv) {
667
		int trimlen = 0;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

		/* Top two-bits of byte 3 are the key index */
		if (skb->len >= hdrlen + 3)
			keyidx = skb->data[hdrlen + 3] >> 6;

		/* To strip off any security data which appears before the
		 * payload, we simply increase hdrlen (as the header gets
		 * chopped off immediately below). For the security data which
		 * appears after the payload, we use skb_trim. */

		switch (ieee->sec.encode_alg[keyidx]) {
		case SEC_ALG_WEP:
			/* 4 byte IV */
			hdrlen += 4;
			/* 4 byte ICV */
			trimlen = 4;
			break;
		case SEC_ALG_TKIP:
			/* 4 byte IV, 4 byte ExtIV */
			hdrlen += 8;
			/* 8 byte MIC, 4 byte ICV */
			trimlen = 12;
			break;
		case SEC_ALG_CCMP:
			/* 8 byte CCMP header */
			hdrlen += 8;
			/* 8 byte MIC */
			trimlen = 8;
			break;
		}

		if (skb->len < trimlen)
			goto rx_dropped;

		__skb_trim(skb, skb->len - trimlen);

		if (skb->len < hdrlen)
			goto rx_dropped;
	}

J
Jeff Garzik 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	/* skb: hdr + (possible reassembled) full plaintext payload */

	payload = skb->data + hdrlen;
	ethertype = (payload[6] << 8) | payload[7];

#ifdef NOT_YET
	/* If IEEE 802.1X is used, check whether the port is authorized to send
	 * the received frame. */
	if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
		if (ethertype == ETH_P_PAE) {
			printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
			       dev->name);
			if (ieee->hostapd && ieee->apdev) {
				/* Send IEEE 802.1X frames to the user
				 * space daemon for processing */
				prism2_rx_80211(ieee->apdev, skb, rx_stats,
						PRISM2_RX_MGMT);
				ieee->apdevstats.rx_packets++;
				ieee->apdevstats.rx_bytes += skb->len;
				goto rx_exit;
			}
		} else if (!frame_authorized) {
			printk(KERN_DEBUG "%s: dropped frame from "
			       "unauthorized port (IEEE 802.1X): "
732
			       "ethertype=0x%04x\n", dev->name, ethertype);
J
Jeff Garzik 已提交
733 734 735 736 737 738 739
			goto rx_dropped;
		}
	}
#endif

	/* convert hdr + possible LLC headers into Ethernet header */
	if (skb->len - hdrlen >= 8 &&
740
	    ((memcmp(payload, libipw_rfc1042_header, SNAP_SIZE) == 0 &&
J
Jeff Garzik 已提交
741
	      ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
742
	     memcmp(payload, libipw_bridge_tunnel_header, SNAP_SIZE) == 0)) {
J
Jeff Garzik 已提交
743 744 745 746 747 748
		/* remove RFC1042 or Bridge-Tunnel encapsulation and
		 * replace EtherType */
		skb_pull(skb, hdrlen + SNAP_SIZE);
		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
	} else {
A
Al Viro 已提交
749
		__be16 len;
J
Jeff Garzik 已提交
750 751 752 753 754 755 756 757 758 759
		/* Leave Ethernet header part of hdr and full payload */
		skb_pull(skb, hdrlen);
		len = htons(skb->len);
		memcpy(skb_push(skb, 2), &len, 2);
		memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
		memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
	}

#ifdef NOT_YET
	if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
760
		    IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
J
Jeff Garzik 已提交
761 762
		/* Non-standard frame: get addr4 from its bogus location after
		 * the payload */
763 764 765
		skb_copy_to_linear_data_offset(skb, ETH_ALEN,
					       skb->data + skb->len - ETH_ALEN,
					       ETH_ALEN);
J
Jeff Garzik 已提交
766 767 768 769
		skb_trim(skb, skb->len - ETH_ALEN);
	}
#endif

770 771
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += skb->len;
J
Jeff Garzik 已提交
772 773

#ifdef NOT_YET
774
	if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
775
		if (is_multicast_ether_addr(dst)) {
J
Jeff Garzik 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
			/* copy multicast frame both to the higher layers and
			 * to the wireless media */
			ieee->ap->bridged_multicast++;
			skb2 = skb_clone(skb, GFP_ATOMIC);
			if (skb2 == NULL)
				printk(KERN_DEBUG "%s: skb_clone failed for "
				       "multicast frame\n", dev->name);
		} else if (hostap_is_sta_assoc(ieee->ap, dst)) {
			/* send frame directly to the associated STA using
			 * wireless media and not passing to higher layers */
			ieee->ap->bridged_unicast++;
			skb2 = skb;
			skb = NULL;
		}
	}

	if (skb2 != NULL) {
		/* send to wireless media */
794
		skb2->dev = dev;
795
		skb2->protocol = htons(ETH_P_802_3);
796
		skb_reset_mac_header(skb2);
797
		skb_reset_network_header(skb2);
798
		/* skb2->network_header += ETH_HLEN; */
J
Jeff Garzik 已提交
799 800 801 802 803 804 805
		dev_queue_xmit(skb2);
	}
#endif

	if (skb) {
		skb->protocol = eth_type_trans(skb, dev);
		memset(skb->cb, 0, sizeof(skb->cb));
806
		skb->ip_summed = CHECKSUM_NONE;	/* 802.11 crc not sufficient */
807 808 809 810
		if (netif_rx(skb) == NET_RX_DROP) {
			/* netif_rx always succeeds, but it might drop
			 * the packet.  If it drops the packet, we log that
			 * in our stats. */
811
			LIBIPW_DEBUG_DROP
812
			    ("RX: netif_rx dropped the packet\n");
813
			dev->stats.rx_dropped++;
814
		}
J
Jeff Garzik 已提交
815 816
	}

817
      rx_exit:
J
Jeff Garzik 已提交
818 819 820 821 822 823
#ifdef NOT_YET
	if (sta)
		hostap_handle_sta_release(sta);
#endif
	return 1;

824
      rx_dropped:
825
	dev->stats.rx_dropped++;
J
Jeff Garzik 已提交
826 827 828 829 830 831 832

	/* Returning 0 indicates to caller that we have not handled the SKB--
	 * so it is still allocated and can be used again by underlying
	 * hardware as a DMA target */
	return 0;
}

833
/* Filter out unrelated packets, call libipw_rx[_mgt]
834 835
 * This function takes over the skb, it should not be used again after calling
 * this function. */
836 837
void libipw_rx_any(struct libipw_device *ieee,
		     struct sk_buff *skb, struct libipw_rx_stats *stats)
838
{
839
	struct libipw_hdr_4addr *hdr;
840 841 842
	int is_packet_for_us;
	u16 fc;

843
	if (ieee->iw_mode == IW_MODE_MONITOR) {
844
		if (!libipw_rx(ieee, skb, stats))
845 846 847 848 849 850
			dev_kfree_skb_irq(skb);
		return;
	}

	if (skb->len < sizeof(struct ieee80211_hdr))
		goto drop_free;
851

852
	hdr = (struct libipw_hdr_4addr *)skb->data;
853 854 855
	fc = le16_to_cpu(hdr->frame_ctl);

	if ((fc & IEEE80211_FCTL_VERS) != 0)
856
		goto drop_free;
857

858 859
	switch (fc & IEEE80211_FCTL_FTYPE) {
	case IEEE80211_FTYPE_MGMT:
860
		if (skb->len < sizeof(struct libipw_hdr_3addr))
861
			goto drop_free;
862
		libipw_rx_mgt(ieee, hdr, stats);
863 864
		dev_kfree_skb_irq(skb);
		return;
865 866 867
	case IEEE80211_FTYPE_DATA:
		break;
	case IEEE80211_FTYPE_CTL:
868
		return;
869
	default:
870
		return;
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
	}

	is_packet_for_us = 0;
	switch (ieee->iw_mode) {
	case IW_MODE_ADHOC:
		/* our BSS and not from/to DS */
		if (memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) == 0)
		if ((fc & (IEEE80211_FCTL_TODS+IEEE80211_FCTL_FROMDS)) == 0) {
			/* promisc: get all */
			if (ieee->dev->flags & IFF_PROMISC)
				is_packet_for_us = 1;
			/* to us */
			else if (memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN) == 0)
				is_packet_for_us = 1;
			/* mcast */
			else if (is_multicast_ether_addr(hdr->addr1))
				is_packet_for_us = 1;
		}
		break;
	case IW_MODE_INFRA:
		/* our BSS (== from our AP) and from DS */
		if (memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) == 0)
		if ((fc & (IEEE80211_FCTL_TODS+IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS) {
			/* promisc: get all */
			if (ieee->dev->flags & IFF_PROMISC)
				is_packet_for_us = 1;
			/* to us */
			else if (memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN) == 0)
				is_packet_for_us = 1;
			/* mcast */
			else if (is_multicast_ether_addr(hdr->addr1)) {
				/* not our own packet bcasted from AP */
				if (memcmp(hdr->addr3, ieee->dev->dev_addr, ETH_ALEN))
					is_packet_for_us = 1;
			}
		}
		break;
	default:
		/* ? */
		break;
	}

	if (is_packet_for_us)
914
		if (!libipw_rx(ieee, skb, stats))
915 916 917 918 919
			dev_kfree_skb_irq(skb);
	return;

drop_free:
	dev_kfree_skb_irq(skb);
920
	ieee->dev->stats.rx_dropped++;
921 922
}

J
Jeff Garzik 已提交
923 924
#define MGMT_FRAME_FIXED_PART_LENGTH		0x24

925 926 927
static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };

/*
L
Lucas De Marchi 已提交
928
* Make the structure we read from the beacon packet to have
929 930
* the right values
*/
931
static int libipw_verify_qos_info(struct libipw_qos_information_element
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
				     *info_element, int sub_type)
{

	if (info_element->qui_subtype != sub_type)
		return -1;
	if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
		return -1;
	if (info_element->qui_type != QOS_OUI_TYPE)
		return -1;
	if (info_element->version != QOS_VERSION_1)
		return -1;

	return 0;
}

/*
 * Parse a QoS parameter element
 */
950 951
static int libipw_read_qos_param_element(struct libipw_qos_parameter_info
					    *element_param, struct libipw_info_element
952 953 954
					    *info_element)
{
	int ret = 0;
955
	u16 size = sizeof(struct libipw_qos_parameter_info) - 2;
956 957 958 959 960 961 962 963 964 965 966 967

	if ((info_element == NULL) || (element_param == NULL))
		return -1;

	if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
		memcpy(element_param->info_element.qui, info_element->data,
		       info_element->len);
		element_param->info_element.elementID = info_element->id;
		element_param->info_element.length = info_element->len;
	} else
		ret = -1;
	if (ret == 0)
968
		ret = libipw_verify_qos_info(&element_param->info_element,
969 970 971 972 973 974 975
						QOS_OUI_PARAM_SUB_TYPE);
	return ret;
}

/*
 * Parse a QoS information element
 */
976 977 978
static int libipw_read_qos_info_element(struct
					   libipw_qos_information_element
					   *element_info, struct libipw_info_element
979 980 981
					   *info_element)
{
	int ret = 0;
982
	u16 size = sizeof(struct libipw_qos_information_element) - 2;
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997

	if (element_info == NULL)
		return -1;
	if (info_element == NULL)
		return -1;

	if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
		memcpy(element_info->qui, info_element->data,
		       info_element->len);
		element_info->elementID = info_element->id;
		element_info->length = info_element->len;
	} else
		ret = -1;

	if (ret == 0)
998
		ret = libipw_verify_qos_info(element_info,
999 1000 1001 1002 1003 1004 1005
						QOS_OUI_INFO_SUB_TYPE);
	return ret;
}

/*
 * Write QoS parameters from the ac parameters.
 */
1006 1007
static int libipw_qos_convert_ac_to_parameters(struct
						  libipw_qos_parameter_info
1008
						  *param_elm, struct
1009
						  libipw_qos_parameters
1010 1011 1012 1013
						  *qos_param)
{
	int rc = 0;
	int i;
1014
	struct libipw_qos_ac_parameter *ac_params;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	u32 txop;
	u8 cw_min;
	u8 cw_max;

	for (i = 0; i < QOS_QUEUE_NUM; i++) {
		ac_params = &(param_elm->ac_params_record[i]);

		qos_param->aifs[i] = (ac_params->aci_aifsn) & 0x0F;
		qos_param->aifs[i] -= (qos_param->aifs[i] < 2) ? 0 : 2;

		cw_min = ac_params->ecw_min_max & 0x0F;
1026
		qos_param->cw_min[i] = cpu_to_le16((1 << cw_min) - 1);
1027 1028

		cw_max = (ac_params->ecw_min_max & 0xF0) >> 4;
1029
		qos_param->cw_max[i] = cpu_to_le16((1 << cw_max) - 1);
1030 1031 1032 1033 1034

		qos_param->flag[i] =
		    (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;

		txop = le16_to_cpu(ac_params->tx_op_limit) * 32;
1035
		qos_param->tx_op_limit[i] = cpu_to_le16(txop);
1036 1037 1038 1039 1040 1041 1042 1043 1044
	}
	return rc;
}

/*
 * we have a generic data element which it may contain QoS information or
 * parameters element. check the information element length to decide
 * which type to read
 */
1045
static int libipw_parse_qos_info_param_IE(struct libipw_info_element
1046
					     *info_element,
1047
					     struct libipw_network *network)
1048 1049
{
	int rc = 0;
1050 1051
	struct libipw_qos_parameters *qos_param = NULL;
	struct libipw_qos_information_element qos_info_element;
1052

1053
	rc = libipw_read_qos_info_element(&qos_info_element, info_element);
1054 1055 1056 1057 1058

	if (rc == 0) {
		network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
		network->flags |= NETWORK_HAS_QOS_INFORMATION;
	} else {
1059
		struct libipw_qos_parameter_info param_element;
1060

1061
		rc = libipw_read_qos_param_element(&param_element,
1062 1063 1064
						      info_element);
		if (rc == 0) {
			qos_param = &(network->qos_data.parameters);
1065
			libipw_qos_convert_ac_to_parameters(&param_element,
1066 1067 1068 1069 1070 1071 1072 1073
							       qos_param);
			network->flags |= NETWORK_HAS_QOS_PARAMETERS;
			network->qos_data.param_count =
			    param_element.info_element.ac_info & 0x0F;
		}
	}

	if (rc == 0) {
1074
		LIBIPW_DEBUG_QOS("QoS is supported\n");
1075 1076 1077 1078 1079
		network->qos_data.supported = 1;
	}
	return rc;
}

H
Helmut Schaa 已提交
1080
#ifdef CONFIG_LIBIPW_DEBUG
1081
#define MFIE_STRING(x) case WLAN_EID_ ##x: return #x
1082 1083 1084 1085 1086

static const char *get_info_element_string(u16 id)
{
	switch (id) {
		MFIE_STRING(SSID);
1087 1088 1089 1090
		MFIE_STRING(SUPP_RATES);
		MFIE_STRING(FH_PARAMS);
		MFIE_STRING(DS_PARAMS);
		MFIE_STRING(CF_PARAMS);
1091
		MFIE_STRING(TIM);
1092
		MFIE_STRING(IBSS_PARAMS);
1093
		MFIE_STRING(COUNTRY);
1094 1095
		MFIE_STRING(HP_PARAMS);
		MFIE_STRING(HP_TABLE);
1096 1097
		MFIE_STRING(REQUEST);
		MFIE_STRING(CHALLENGE);
1098 1099
		MFIE_STRING(PWR_CONSTRAINT);
		MFIE_STRING(PWR_CAPABILITY);
1100 1101
		MFIE_STRING(TPC_REQUEST);
		MFIE_STRING(TPC_REPORT);
1102 1103
		MFIE_STRING(SUPPORTED_CHANNELS);
		MFIE_STRING(CHANNEL_SWITCH);
1104 1105 1106 1107 1108 1109
		MFIE_STRING(MEASURE_REQUEST);
		MFIE_STRING(MEASURE_REPORT);
		MFIE_STRING(QUIET);
		MFIE_STRING(IBSS_DFS);
		MFIE_STRING(ERP_INFO);
		MFIE_STRING(RSN);
1110
		MFIE_STRING(EXT_SUPP_RATES);
1111 1112 1113 1114 1115 1116 1117 1118
		MFIE_STRING(GENERIC);
		MFIE_STRING(QOS_PARAMETER);
	default:
		return "UNKNOWN";
	}
}
#endif

1119
static int libipw_parse_info_param(struct libipw_info_element
1120
				      *info_element, u16 length,
1121
				      struct libipw_network *network)
J
Jeff Garzik 已提交
1122
{
1123
	DECLARE_SSID_BUF(ssid);
1124
	u8 i;
H
Helmut Schaa 已提交
1125
#ifdef CONFIG_LIBIPW_DEBUG
J
Jeff Garzik 已提交
1126 1127 1128 1129
	char rates_str[64];
	char *p;
#endif

1130 1131
	while (length >= sizeof(*info_element)) {
		if (sizeof(*info_element) + info_element->len > length) {
1132
			LIBIPW_DEBUG_MGMT("Info elem: parse failed: "
1133 1134 1135 1136 1137
					     "info_element->len + 2 > left : "
					     "info_element->len+2=%zd left=%d, id=%d.\n",
					     info_element->len +
					     sizeof(*info_element),
					     length, info_element->id);
1138 1139 1140 1141
			/* We stop processing but don't return an error here
			 * because some misbehaviour APs break this rule. ie.
			 * Orinoco AP1000. */
			break;
1142
		}
J
Jeff Garzik 已提交
1143 1144

		switch (info_element->id) {
1145
		case WLAN_EID_SSID:
J
Jeff Garzik 已提交
1146
			network->ssid_len = min(info_element->len,
1147 1148 1149 1150 1151
						(u8) IW_ESSID_MAX_SIZE);
			memcpy(network->ssid, info_element->data,
			       network->ssid_len);
			if (network->ssid_len < IW_ESSID_MAX_SIZE)
				memset(network->ssid + network->ssid_len, 0,
J
Jeff Garzik 已提交
1152 1153
				       IW_ESSID_MAX_SIZE - network->ssid_len);

1154
			LIBIPW_DEBUG_MGMT("WLAN_EID_SSID: '%s' len=%d.\n",
1155 1156
					     print_ssid(ssid, network->ssid,
							network->ssid_len),
1157
					     network->ssid_len);
J
Jeff Garzik 已提交
1158 1159
			break;

1160
		case WLAN_EID_SUPP_RATES:
H
Helmut Schaa 已提交
1161
#ifdef CONFIG_LIBIPW_DEBUG
J
Jeff Garzik 已提交
1162 1163
			p = rates_str;
#endif
1164 1165
			network->rates_len = min(info_element->len,
						 MAX_RATES_LENGTH);
J
Jeff Garzik 已提交
1166 1167
			for (i = 0; i < network->rates_len; i++) {
				network->rates[i] = info_element->data[i];
H
Helmut Schaa 已提交
1168
#ifdef CONFIG_LIBIPW_DEBUG
1169 1170 1171
				p += snprintf(p, sizeof(rates_str) -
					      (p - rates_str), "%02X ",
					      network->rates[i]);
J
Jeff Garzik 已提交
1172
#endif
1173
				if (libipw_is_ofdm_rate
1174
				    (info_element->data[i])) {
J
Jeff Garzik 已提交
1175 1176
					network->flags |= NETWORK_HAS_OFDM;
					if (info_element->data[i] &
1177
					    LIBIPW_BASIC_RATE_MASK)
J
Jeff Garzik 已提交
1178
						network->flags &=
1179
						    ~NETWORK_HAS_CCK;
J
Jeff Garzik 已提交
1180 1181 1182
				}
			}

1183
			LIBIPW_DEBUG_MGMT("WLAN_EID_SUPP_RATES: '%s' (%d)\n",
J
Jeff Garzik 已提交
1184 1185 1186
					     rates_str, network->rates_len);
			break;

1187
		case WLAN_EID_EXT_SUPP_RATES:
H
Helmut Schaa 已提交
1188
#ifdef CONFIG_LIBIPW_DEBUG
J
Jeff Garzik 已提交
1189 1190
			p = rates_str;
#endif
1191 1192
			network->rates_ex_len = min(info_element->len,
						    MAX_RATES_EX_LENGTH);
J
Jeff Garzik 已提交
1193 1194
			for (i = 0; i < network->rates_ex_len; i++) {
				network->rates_ex[i] = info_element->data[i];
H
Helmut Schaa 已提交
1195
#ifdef CONFIG_LIBIPW_DEBUG
1196 1197 1198
				p += snprintf(p, sizeof(rates_str) -
					      (p - rates_str), "%02X ",
					      network->rates[i]);
J
Jeff Garzik 已提交
1199
#endif
1200
				if (libipw_is_ofdm_rate
1201
				    (info_element->data[i])) {
J
Jeff Garzik 已提交
1202 1203
					network->flags |= NETWORK_HAS_OFDM;
					if (info_element->data[i] &
1204
					    LIBIPW_BASIC_RATE_MASK)
J
Jeff Garzik 已提交
1205
						network->flags &=
1206
						    ~NETWORK_HAS_CCK;
J
Jeff Garzik 已提交
1207 1208 1209
				}
			}

1210
			LIBIPW_DEBUG_MGMT("WLAN_EID_EXT_SUPP_RATES: '%s' (%d)\n",
J
Jeff Garzik 已提交
1211 1212 1213
					     rates_str, network->rates_ex_len);
			break;

1214
		case WLAN_EID_DS_PARAMS:
1215
			LIBIPW_DEBUG_MGMT("WLAN_EID_DS_PARAMS: %d\n",
J
Jeff Garzik 已提交
1216
					     info_element->data[0]);
1217
			network->channel = info_element->data[0];
J
Jeff Garzik 已提交
1218 1219
			break;

1220
		case WLAN_EID_FH_PARAMS:
1221
			LIBIPW_DEBUG_MGMT("WLAN_EID_FH_PARAMS: ignored\n");
J
Jeff Garzik 已提交
1222 1223
			break;

1224
		case WLAN_EID_CF_PARAMS:
1225
			LIBIPW_DEBUG_MGMT("WLAN_EID_CF_PARAMS: ignored\n");
J
Jeff Garzik 已提交
1226 1227
			break;

1228
		case WLAN_EID_TIM:
1229 1230
			network->tim.tim_count = info_element->data[0];
			network->tim.tim_period = info_element->data[1];
1231
			LIBIPW_DEBUG_MGMT("WLAN_EID_TIM: partially ignored\n");
J
Jeff Garzik 已提交
1232 1233
			break;

1234
		case WLAN_EID_ERP_INFO:
1235
			network->erp_value = info_element->data[0];
1236
			network->flags |= NETWORK_HAS_ERP_VALUE;
1237
			LIBIPW_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
1238 1239 1240
					     network->erp_value);
			break;

1241
		case WLAN_EID_IBSS_PARAMS:
1242
			network->atim_window = info_element->data[0];
1243
			LIBIPW_DEBUG_MGMT("WLAN_EID_IBSS_PARAMS: %d\n",
1244
					     network->atim_window);
J
Jeff Garzik 已提交
1245 1246
			break;

1247
		case WLAN_EID_CHALLENGE:
1248
			LIBIPW_DEBUG_MGMT("WLAN_EID_CHALLENGE: ignored\n");
J
Jeff Garzik 已提交
1249 1250
			break;

1251
		case WLAN_EID_GENERIC:
1252
			LIBIPW_DEBUG_MGMT("WLAN_EID_GENERIC: %d bytes\n",
J
Jeff Garzik 已提交
1253
					     info_element->len);
1254
			if (!libipw_parse_qos_info_param_IE(info_element,
1255 1256 1257
							       network))
				break;

1258
			if (info_element->len >= 4 &&
J
Jeff Garzik 已提交
1259 1260 1261 1262 1263
			    info_element->data[0] == 0x00 &&
			    info_element->data[1] == 0x50 &&
			    info_element->data[2] == 0xf2 &&
			    info_element->data[3] == 0x01) {
				network->wpa_ie_len = min(info_element->len + 2,
1264
							  MAX_WPA_IE_LEN);
J
Jeff Garzik 已提交
1265 1266 1267 1268 1269
				memcpy(network->wpa_ie, info_element,
				       network->wpa_ie_len);
			}
			break;

1270
		case WLAN_EID_RSN:
1271
			LIBIPW_DEBUG_MGMT("WLAN_EID_RSN: %d bytes\n",
J
Jeff Garzik 已提交
1272 1273
					     info_element->len);
			network->rsn_ie_len = min(info_element->len + 2,
1274
						  MAX_WPA_IE_LEN);
J
Jeff Garzik 已提交
1275 1276 1277 1278
			memcpy(network->rsn_ie, info_element,
			       network->rsn_ie_len);
			break;

1279
		case WLAN_EID_QOS_PARAMETER:
1280 1281
			printk(KERN_ERR
			       "QoS Error need to parse QOS_PARAMETER IE\n");
1282
			break;
1283
			/* 802.11h */
1284
		case WLAN_EID_PWR_CONSTRAINT:
1285 1286 1287 1288
			network->power_constraint = info_element->data[0];
			network->flags |= NETWORK_HAS_POWER_CONSTRAINT;
			break;

1289
		case WLAN_EID_CHANNEL_SWITCH:
1290 1291 1292 1293
			network->power_constraint = info_element->data[0];
			network->flags |= NETWORK_HAS_CSA;
			break;

1294
		case WLAN_EID_QUIET:
1295 1296 1297 1298 1299 1300 1301
			network->quiet.count = info_element->data[0];
			network->quiet.period = info_element->data[1];
			network->quiet.duration = info_element->data[2];
			network->quiet.offset = info_element->data[3];
			network->flags |= NETWORK_HAS_QUIET;
			break;

1302
		case WLAN_EID_IBSS_DFS:
1303 1304
			if (network->ibss_dfs)
				break;
1305 1306 1307
			network->ibss_dfs = kmemdup(info_element->data,
						    info_element->len,
						    GFP_ATOMIC);
1308 1309 1310 1311 1312
			if (!network->ibss_dfs)
				return 1;
			network->flags |= NETWORK_HAS_IBSS_DFS;
			break;

1313
		case WLAN_EID_TPC_REPORT:
1314 1315 1316 1317 1318
			network->tpc_report.transmit_power =
			    info_element->data[0];
			network->tpc_report.link_margin = info_element->data[1];
			network->flags |= NETWORK_HAS_TPC_REPORT;
			break;
1319

J
Jeff Garzik 已提交
1320
		default:
1321
			LIBIPW_DEBUG_MGMT
1322 1323 1324
			    ("Unsupported info element: %s (%d)\n",
			     get_info_element_string(info_element->id),
			     info_element->id);
1325 1326
			break;
		}
J
Jeff Garzik 已提交
1327

1328
		length -= sizeof(*info_element) + info_element->len;
1329
		info_element =
1330
		    (struct libipw_info_element *)&info_element->
1331
		    data[info_element->len];
1332
	}
J
Jeff Garzik 已提交
1333

1334 1335 1336
	return 0;
}

1337 1338
static int libipw_handle_assoc_resp(struct libipw_device *ieee, struct libipw_assoc_response
				       *frame, struct libipw_rx_stats *stats)
1339
{
1340
	struct libipw_network network_resp = {
1341 1342
		.ibss_dfs = NULL,
	};
1343
	struct libipw_network *network = &network_resp;
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
	struct net_device *dev = ieee->dev;

	network->flags = 0;
	network->qos_data.active = 0;
	network->qos_data.supported = 0;
	network->qos_data.param_count = 0;
	network->qos_data.old_param_count = 0;

	//network->atim_window = le16_to_cpu(frame->aid) & (0x3FFF);
	network->atim_window = le16_to_cpu(frame->aid);
	network->listen_interval = le16_to_cpu(frame->status);
1355 1356 1357 1358 1359 1360
	memcpy(network->bssid, frame->header.addr3, ETH_ALEN);
	network->capability = le16_to_cpu(frame->capability);
	network->last_scanned = jiffies;
	network->rates_len = network->rates_ex_len = 0;
	network->last_associate = 0;
	network->ssid_len = 0;
1361 1362
	network->erp_value =
	    (network->capability & WLAN_CAPABILITY_IBSS) ? 0x3 : 0x0;
1363

1364
	if (stats->freq == LIBIPW_52GHZ_BAND) {
1365 1366 1367 1368 1369 1370 1371
		/* for A band (No DS info) */
		network->channel = stats->received_channel;
	} else
		network->flags |= NETWORK_HAS_CCK;

	network->wpa_ie_len = 0;
	network->rsn_ie_len = 0;
1372

1373
	if (libipw_parse_info_param
1374
	    (frame->info_element, stats->len - sizeof(*frame), network))
1375 1376
		return 1;

1377
	network->mode = 0;
1378
	if (stats->freq == LIBIPW_52GHZ_BAND)
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
		network->mode = IEEE_A;
	else {
		if (network->flags & NETWORK_HAS_OFDM)
			network->mode |= IEEE_G;
		if (network->flags & NETWORK_HAS_CCK)
			network->mode |= IEEE_B;
	}

	memcpy(&network->stats, stats, sizeof(network->stats));

1389 1390 1391 1392 1393 1394 1395 1396
	if (ieee->handle_assoc_response != NULL)
		ieee->handle_assoc_response(dev, frame, network);

	return 0;
}

/***************************************************/

1397
static int libipw_network_init(struct libipw_device *ieee, struct libipw_probe_response
1398
					 *beacon,
1399 1400
					 struct libipw_network *network,
					 struct libipw_rx_stats *stats)
1401
{
1402 1403
	DECLARE_SSID_BUF(ssid);

1404 1405 1406
	network->qos_data.active = 0;
	network->qos_data.supported = 0;
	network->qos_data.param_count = 0;
1407
	network->qos_data.old_param_count = 0;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

	/* Pull out fixed field data */
	memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
	network->capability = le16_to_cpu(beacon->capability);
	network->last_scanned = jiffies;
	network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
	network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
	network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
	/* Where to pull this? beacon->listen_interval; */
	network->listen_interval = 0x0A;
	network->rates_len = network->rates_ex_len = 0;
	network->last_associate = 0;
	network->ssid_len = 0;
	network->flags = 0;
	network->atim_window = 0;
	network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
	    0x3 : 0x0;

1426
	if (stats->freq == LIBIPW_52GHZ_BAND) {
1427 1428 1429 1430 1431 1432 1433 1434
		/* for A band (No DS info) */
		network->channel = stats->received_channel;
	} else
		network->flags |= NETWORK_HAS_CCK;

	network->wpa_ie_len = 0;
	network->rsn_ie_len = 0;

1435
	if (libipw_parse_info_param
1436
	    (beacon->info_element, stats->len - sizeof(*beacon), network))
1437 1438
		return 1;

J
Jeff Garzik 已提交
1439
	network->mode = 0;
1440
	if (stats->freq == LIBIPW_52GHZ_BAND)
J
Jeff Garzik 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449
		network->mode = IEEE_A;
	else {
		if (network->flags & NETWORK_HAS_OFDM)
			network->mode |= IEEE_G;
		if (network->flags & NETWORK_HAS_CCK)
			network->mode |= IEEE_B;
	}

	if (network->mode == 0) {
1450
		LIBIPW_DEBUG_SCAN("Filtered out '%s (%pM)' "
J
Jeff Garzik 已提交
1451
				     "network.\n",
1452
				     print_ssid(ssid, network->ssid,
1453
						 network->ssid_len),
J
Johannes Berg 已提交
1454
				     network->bssid);
J
Jeff Garzik 已提交
1455 1456 1457 1458 1459 1460 1461 1462
		return 1;
	}

	memcpy(&network->stats, stats, sizeof(network->stats));

	return 0;
}

1463 1464
static inline int is_same_network(struct libipw_network *src,
				  struct libipw_network *dst)
J
Jeff Garzik 已提交
1465 1466 1467 1468 1469 1470
{
	/* A network is only a duplicate if the channel, BSSID, and ESSID
	 * all match.  We treat all <hidden> with the same BSSID and channel
	 * as one network */
	return ((src->ssid_len == dst->ssid_len) &&
		(src->channel == dst->channel) &&
1471
		!compare_ether_addr(src->bssid, dst->bssid) &&
J
Jeff Garzik 已提交
1472 1473 1474
		!memcmp(src->ssid, dst->ssid, src->ssid_len));
}

1475 1476
static void update_network(struct libipw_network *dst,
				  struct libipw_network *src)
J
Jeff Garzik 已提交
1477
{
1478 1479 1480
	int qos_active;
	u8 old_param;

1481
	libipw_network_reset(dst);
1482 1483
	dst->ibss_dfs = src->ibss_dfs;

1484 1485
	/* We only update the statistics if they were created by receiving
	 * the network information on the actual channel the network is on.
1486
	 *
1487 1488 1489 1490
	 * This keeps beacons received on neighbor channels from bringing
	 * down the signal level of an AP. */
	if (dst->channel == src->stats.received_channel)
		memcpy(&dst->stats, &src->stats,
1491
		       sizeof(struct libipw_rx_stats));
1492
	else
1493
		LIBIPW_DEBUG_SCAN("Network %pM info received "
J
Johannes Berg 已提交
1494
			"off channel (%d vs. %d)\n", src->bssid,
1495 1496
			dst->channel, src->stats.received_channel);

J
Jeff Garzik 已提交
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	dst->capability = src->capability;
	memcpy(dst->rates, src->rates, src->rates_len);
	dst->rates_len = src->rates_len;
	memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
	dst->rates_ex_len = src->rates_ex_len;

	dst->mode = src->mode;
	dst->flags = src->flags;
	dst->time_stamp[0] = src->time_stamp[0];
	dst->time_stamp[1] = src->time_stamp[1];

	dst->beacon_interval = src->beacon_interval;
	dst->listen_interval = src->listen_interval;
	dst->atim_window = src->atim_window;
1511
	dst->erp_value = src->erp_value;
1512
	dst->tim = src->tim;
J
Jeff Garzik 已提交
1513 1514 1515 1516 1517 1518 1519

	memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
	dst->wpa_ie_len = src->wpa_ie_len;
	memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
	dst->rsn_ie_len = src->rsn_ie_len;

	dst->last_scanned = jiffies;
1520 1521 1522 1523
	qos_active = src->qos_data.active;
	old_param = dst->qos_data.old_param_count;
	if (dst->flags & NETWORK_HAS_QOS_MASK)
		memcpy(&dst->qos_data, &src->qos_data,
1524
		       sizeof(struct libipw_qos_data));
1525 1526 1527 1528 1529 1530 1531
	else {
		dst->qos_data.supported = src->qos_data.supported;
		dst->qos_data.param_count = src->qos_data.param_count;
	}

	if (dst->qos_data.supported == 1) {
		if (dst->ssid_len)
1532
			LIBIPW_DEBUG_QOS
1533 1534 1535
			    ("QoS the network %s is QoS supported\n",
			     dst->ssid);
		else
1536
			LIBIPW_DEBUG_QOS
1537 1538 1539 1540 1541
			    ("QoS the network is QoS supported\n");
	}
	dst->qos_data.active = qos_active;
	dst->qos_data.old_param_count = old_param;

J
Jeff Garzik 已提交
1542 1543 1544
	/* dst->last_associate is not overwritten */
}

P
Pete Zaitcev 已提交
1545
static inline int is_beacon(__le16 fc)
1546 1547 1548 1549
{
	return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
}

1550
static void libipw_process_probe_response(struct libipw_device
1551
						    *ieee, struct
1552 1553
						    libipw_probe_response
						    *beacon, struct libipw_rx_stats
1554
						    *stats)
J
Jeff Garzik 已提交
1555
{
1556
	struct net_device *dev = ieee->dev;
1557
	struct libipw_network network = {
1558 1559
		.ibss_dfs = NULL,
	};
1560 1561
	struct libipw_network *target;
	struct libipw_network *oldest = NULL;
H
Helmut Schaa 已提交
1562
#ifdef CONFIG_LIBIPW_DEBUG
1563
	struct libipw_info_element *info_element = beacon->info_element;
J
Jeff Garzik 已提交
1564 1565
#endif
	unsigned long flags;
1566
	DECLARE_SSID_BUF(ssid);
J
Jeff Garzik 已提交
1567

1568
	LIBIPW_DEBUG_SCAN("'%s' (%pM"
1569
		     "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1570
		     print_ssid(ssid, info_element->data, info_element->len),
J
Johannes Berg 已提交
1571
		     beacon->header.addr3,
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
		     (beacon->capability & cpu_to_le16(1 << 0xf)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0xe)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0xd)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0xc)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0xb)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0xa)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x9)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x8)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x7)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x6)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x5)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x4)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x3)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x2)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x1)) ? '1' : '0',
		     (beacon->capability & cpu_to_le16(1 << 0x0)) ? '1' : '0');
J
Jeff Garzik 已提交
1588

1589 1590
	if (libipw_network_init(ieee, beacon, &network, stats)) {
		LIBIPW_DEBUG_SCAN("Dropped '%s' (%pM) via %s.\n",
1591
				     print_ssid(ssid, info_element->data,
1592
						 info_element->len),
J
Johannes Berg 已提交
1593
				     beacon->header.addr3,
P
Pete Zaitcev 已提交
1594
				     is_beacon(beacon->header.frame_ctl) ?
1595
				     "BEACON" : "PROBE RESPONSE");
J
Jeff Garzik 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
		return;
	}

	/* The network parsed correctly -- so now we scan our known networks
	 * to see if we can find it in our list.
	 *
	 * NOTE:  This search is definitely not optimized.  Once its doing
	 *        the "right thing" we'll optimize it for efficiency if
	 *        necessary */

	/* Search for this entry in the list and update it if it is
	 * already there. */

	spin_lock_irqsave(&ieee->lock, flags);

	list_for_each_entry(target, &ieee->network_list, list) {
		if (is_same_network(target, &network))
			break;

		if ((oldest == NULL) ||
1616
		    time_before(target->last_scanned, oldest->last_scanned))
J
Jeff Garzik 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
			oldest = target;
	}

	/* If we didn't find a match, then get a new network slot to initialize
	 * with this beacon's information */
	if (&target->list == &ieee->network_list) {
		if (list_empty(&ieee->network_free_list)) {
			/* If there are no more slots, expire the oldest */
			list_del(&oldest->list);
			target = oldest;
1627
			LIBIPW_DEBUG_SCAN("Expired '%s' (%pM) from "
J
Jeff Garzik 已提交
1628
					     "network list.\n",
1629
					     print_ssid(ssid, target->ssid,
1630
							 target->ssid_len),
J
Johannes Berg 已提交
1631
					     target->bssid);
1632
			libipw_network_reset(target);
J
Jeff Garzik 已提交
1633 1634 1635
		} else {
			/* Otherwise just pull from the free list */
			target = list_entry(ieee->network_free_list.next,
1636
					    struct libipw_network, list);
J
Jeff Garzik 已提交
1637 1638 1639
			list_del(ieee->network_free_list.next);
		}

H
Helmut Schaa 已提交
1640
#ifdef CONFIG_LIBIPW_DEBUG
1641
		LIBIPW_DEBUG_SCAN("Adding '%s' (%pM) via %s.\n",
1642
				     print_ssid(ssid, network.ssid,
1643
						 network.ssid_len),
J
Johannes Berg 已提交
1644
				     network.bssid,
P
Pete Zaitcev 已提交
1645
				     is_beacon(beacon->header.frame_ctl) ?
1646
				     "BEACON" : "PROBE RESPONSE");
J
Jeff Garzik 已提交
1647 1648
#endif
		memcpy(target, &network, sizeof(*target));
1649
		network.ibss_dfs = NULL;
J
Jeff Garzik 已提交
1650 1651
		list_add_tail(&target->list, &ieee->network_list);
	} else {
1652
		LIBIPW_DEBUG_SCAN("Updating '%s' (%pM) via %s.\n",
1653
				     print_ssid(ssid, target->ssid,
1654
						 target->ssid_len),
J
Johannes Berg 已提交
1655
				     target->bssid,
P
Pete Zaitcev 已提交
1656
				     is_beacon(beacon->header.frame_ctl) ?
1657
				     "BEACON" : "PROBE RESPONSE");
J
Jeff Garzik 已提交
1658
		update_network(target, &network);
1659
		network.ibss_dfs = NULL;
J
Jeff Garzik 已提交
1660 1661 1662
	}

	spin_unlock_irqrestore(&ieee->lock, flags);
1663

P
Pete Zaitcev 已提交
1664
	if (is_beacon(beacon->header.frame_ctl)) {
1665
		if (ieee->handle_beacon != NULL)
1666
			ieee->handle_beacon(dev, beacon, target);
1667 1668
	} else {
		if (ieee->handle_probe_response != NULL)
1669
			ieee->handle_probe_response(dev, beacon, target);
1670
	}
J
Jeff Garzik 已提交
1671 1672
}

1673 1674 1675
void libipw_rx_mgt(struct libipw_device *ieee,
		      struct libipw_hdr_4addr *header,
		      struct libipw_rx_stats *stats)
J
Jeff Garzik 已提交
1676
{
1677
	switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
J
Jeff Garzik 已提交
1678
	case IEEE80211_STYPE_ASSOC_RESP:
1679
		LIBIPW_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
1680 1681
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
1682 1683
		libipw_handle_assoc_resp(ieee,
					    (struct libipw_assoc_response *)
1684
					    header, stats);
J
Jeff Garzik 已提交
1685 1686 1687
		break;

	case IEEE80211_STYPE_REASSOC_RESP:
1688
		LIBIPW_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
1689 1690
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
J
Jeff Garzik 已提交
1691 1692
		break;

1693
	case IEEE80211_STYPE_PROBE_REQ:
1694
		LIBIPW_DEBUG_MGMT("received auth (%d)\n",
1695 1696 1697 1698 1699 1700
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));

		if (ieee->handle_probe_request != NULL)
			ieee->handle_probe_request(ieee->dev,
						   (struct
1701
						    libipw_probe_request *)
1702 1703 1704
						   header, stats);
		break;

J
Jeff Garzik 已提交
1705
	case IEEE80211_STYPE_PROBE_RESP:
1706
		LIBIPW_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1707 1708
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
1709 1710
		LIBIPW_DEBUG_SCAN("Probe response\n");
		libipw_process_probe_response(ieee,
1711
						 (struct
1712
						  libipw_probe_response *)
1713
						 header, stats);
J
Jeff Garzik 已提交
1714 1715 1716
		break;

	case IEEE80211_STYPE_BEACON:
1717
		LIBIPW_DEBUG_MGMT("received BEACON (%d)\n",
1718 1719
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
1720 1721
		LIBIPW_DEBUG_SCAN("Beacon\n");
		libipw_process_probe_response(ieee,
1722
						 (struct
1723
						  libipw_probe_response *)
1724
						 header, stats);
J
Jeff Garzik 已提交
1725
		break;
1726 1727
	case IEEE80211_STYPE_AUTH:

1728
		LIBIPW_DEBUG_MGMT("received auth (%d)\n",
1729 1730 1731 1732 1733
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));

		if (ieee->handle_auth != NULL)
			ieee->handle_auth(ieee->dev,
1734
					  (struct libipw_auth *)header);
1735 1736 1737 1738 1739
		break;

	case IEEE80211_STYPE_DISASSOC:
		if (ieee->handle_disassoc != NULL)
			ieee->handle_disassoc(ieee->dev,
1740
					      (struct libipw_disassoc *)
1741 1742
					      header);
		break;
J
Jeff Garzik 已提交
1743

1744
	case IEEE80211_STYPE_ACTION:
1745
		LIBIPW_DEBUG_MGMT("ACTION\n");
1746 1747
		if (ieee->handle_action)
			ieee->handle_action(ieee->dev,
1748
					    (struct libipw_action *)
1749 1750 1751
					    header, stats);
		break;

1752
	case IEEE80211_STYPE_REASSOC_REQ:
1753
		LIBIPW_DEBUG_MGMT("received reassoc (%d)\n",
1754 1755 1756
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));

1757
		LIBIPW_DEBUG_MGMT("%s: LIBIPW_REASSOC_REQ received\n",
1758
				     ieee->dev->name);
1759 1760
		if (ieee->handle_reassoc_request != NULL)
			ieee->handle_reassoc_request(ieee->dev,
1761
						    (struct libipw_reassoc_request *)
1762 1763 1764 1765
						     header);
		break;

	case IEEE80211_STYPE_ASSOC_REQ:
1766
		LIBIPW_DEBUG_MGMT("received assoc (%d)\n",
1767 1768 1769
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));

1770
		LIBIPW_DEBUG_MGMT("%s: LIBIPW_ASSOC_REQ received\n",
1771
				     ieee->dev->name);
1772 1773 1774 1775
		if (ieee->handle_assoc_request != NULL)
			ieee->handle_assoc_request(ieee->dev);
		break;

1776
	case IEEE80211_STYPE_DEAUTH:
1777
		LIBIPW_DEBUG_MGMT("DEAUTH\n");
1778
		if (ieee->handle_deauth != NULL)
1779
			ieee->handle_deauth(ieee->dev,
1780
					    (struct libipw_deauth *)
1781 1782
					    header);
		break;
J
Jeff Garzik 已提交
1783
	default:
1784
		LIBIPW_DEBUG_MGMT("received UNKNOWN (%d)\n",
1785 1786
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
1787
		LIBIPW_DEBUG_MGMT("%s: Unknown management packet: %d\n",
1788 1789 1790
				     ieee->dev->name,
				     WLAN_FC_GET_STYPE(le16_to_cpu
						       (header->frame_ctl)));
J
Jeff Garzik 已提交
1791 1792 1793 1794
		break;
	}
}

1795 1796 1797
EXPORT_SYMBOL_GPL(libipw_rx_any);
EXPORT_SYMBOL(libipw_rx_mgt);
EXPORT_SYMBOL(libipw_rx);