blob: 9a125d45289a36434ba9a69193026b27215e87b5 [file] [log] [blame]
Jeff Garzikb4538722005-05-12 22:48:20 -04001/*
2 * Original code based Host AP (software wireless LAN access point) driver
3 * for Intersil Prism2/2.5/3 - hostap.o module, common routines
4 *
5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6 * <jkmaline@cc.hut.fi>
7 * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
8 * Copyright (c) 2004, Intel Corporation
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation. See README and COPYING for
13 * more details.
14 */
15
16#include <linux/compiler.h>
17#include <linux/config.h>
18#include <linux/errno.h>
19#include <linux/if_arp.h>
20#include <linux/in6.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/netdevice.h>
Jeff Garzikb4538722005-05-12 22:48:20 -040026#include <linux/proc_fs.h>
27#include <linux/skbuff.h>
28#include <linux/slab.h>
29#include <linux/tcp.h>
30#include <linux/types.h>
31#include <linux/version.h>
32#include <linux/wireless.h>
33#include <linux/etherdevice.h>
34#include <asm/uaccess.h>
35#include <linux/ctype.h>
36
37#include <net/ieee80211.h>
38
39static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
40 struct sk_buff *skb,
41 struct ieee80211_rx_stats *rx_stats)
42{
43 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
44 u16 fc = le16_to_cpu(hdr->frame_ctl);
45
46 skb->dev = ieee->dev;
47 skb->mac.raw = skb->data;
48 skb_pull(skb, ieee80211_get_hdrlen(fc));
49 skb->pkt_type = PACKET_OTHERHOST;
50 skb->protocol = __constant_htons(ETH_P_80211_RAW);
51 memset(skb->cb, 0, sizeof(skb->cb));
52 netif_rx(skb);
53}
54
Jeff Garzikb4538722005-05-12 22:48:20 -040055/* Called only as a tasklet (software IRQ) */
Jeff Garzik0edd5b42005-09-07 00:48:31 -040056static struct ieee80211_frag_entry *ieee80211_frag_cache_find(struct
57 ieee80211_device
58 *ieee,
59 unsigned int seq,
60 unsigned int frag,
61 u8 * src,
62 u8 * dst)
Jeff Garzikb4538722005-05-12 22:48:20 -040063{
64 struct ieee80211_frag_entry *entry;
65 int i;
66
67 for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
68 entry = &ieee->frag_cache[i];
69 if (entry->skb != NULL &&
70 time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -040071 IEEE80211_DEBUG_FRAG("expiring fragment cache entry "
72 "seq=%u last_frag=%u\n",
73 entry->seq, entry->last_frag);
Jeff Garzikb4538722005-05-12 22:48:20 -040074 dev_kfree_skb_any(entry->skb);
75 entry->skb = NULL;
76 }
77
78 if (entry->skb != NULL && entry->seq == seq &&
79 (entry->last_frag + 1 == frag || frag == -1) &&
80 memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
81 memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
82 return entry;
83 }
84
85 return NULL;
86}
87
88/* Called only as a tasklet (software IRQ) */
Jeff Garzik0edd5b42005-09-07 00:48:31 -040089static struct sk_buff *ieee80211_frag_cache_get(struct ieee80211_device *ieee,
90 struct ieee80211_hdr *hdr)
Jeff Garzikb4538722005-05-12 22:48:20 -040091{
92 struct sk_buff *skb = NULL;
93 u16 sc;
94 unsigned int frag, seq;
95 struct ieee80211_frag_entry *entry;
96
97 sc = le16_to_cpu(hdr->seq_ctl);
98 frag = WLAN_GET_SEQ_FRAG(sc);
99 seq = WLAN_GET_SEQ_SEQ(sc);
100
101 if (frag == 0) {
102 /* Reserve enough space to fit maximum frame length */
103 skb = dev_alloc_skb(ieee->dev->mtu +
104 sizeof(struct ieee80211_hdr) +
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400105 8 /* LLC */ +
106 2 /* alignment */ +
107 8 /* WEP */ + ETH_ALEN /* WDS */ );
Jeff Garzikb4538722005-05-12 22:48:20 -0400108 if (skb == NULL)
109 return NULL;
110
111 entry = &ieee->frag_cache[ieee->frag_next_idx];
112 ieee->frag_next_idx++;
113 if (ieee->frag_next_idx >= IEEE80211_FRAG_CACHE_LEN)
114 ieee->frag_next_idx = 0;
115
116 if (entry->skb != NULL)
117 dev_kfree_skb_any(entry->skb);
118
119 entry->first_frag_time = jiffies;
120 entry->seq = seq;
121 entry->last_frag = frag;
122 entry->skb = skb;
123 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
124 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
125 } else {
126 /* received a fragment of a frame for which the head fragment
127 * should have already been received */
128 entry = ieee80211_frag_cache_find(ieee, seq, frag, hdr->addr2,
129 hdr->addr1);
130 if (entry != NULL) {
131 entry->last_frag = frag;
132 skb = entry->skb;
133 }
134 }
135
136 return skb;
137}
138
Jeff Garzikb4538722005-05-12 22:48:20 -0400139/* Called only as a tasklet (software IRQ) */
140static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
141 struct ieee80211_hdr *hdr)
142{
143 u16 sc;
144 unsigned int seq;
145 struct ieee80211_frag_entry *entry;
146
147 sc = le16_to_cpu(hdr->seq_ctl);
148 seq = WLAN_GET_SEQ_SEQ(sc);
149
150 entry = ieee80211_frag_cache_find(ieee, seq, -1, hdr->addr2,
151 hdr->addr1);
152
153 if (entry == NULL) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400154 IEEE80211_DEBUG_FRAG("could not invalidate fragment cache "
155 "entry (seq=%u)\n", seq);
Jeff Garzikb4538722005-05-12 22:48:20 -0400156 return -1;
157 }
158
159 entry->skb = NULL;
160 return 0;
161}
162
Jeff Garzikb4538722005-05-12 22:48:20 -0400163#ifdef NOT_YET
164/* ieee80211_rx_frame_mgtmt
165 *
166 * Responsible for handling management control frames
167 *
168 * Called by ieee80211_rx */
169static inline int
170ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
171 struct ieee80211_rx_stats *rx_stats, u16 type,
172 u16 stype)
173{
174 if (ieee->iw_mode == IW_MODE_MASTER) {
175 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
176 ieee->dev->name);
177 return 0;
178/*
179 hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr *)
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
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400202 if (ieee->iw_mode == IW_MODE_MASTER) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400203 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
Jeff Garzikb4538722005-05-12 22:48:20 -0400220/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
221/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400222static unsigned char rfc1042_header[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
223
Jeff Garzikb4538722005-05-12 22:48:20 -0400224/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
225static unsigned char bridge_tunnel_header[] =
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400226 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
Jeff Garzikb4538722005-05-12 22:48:20 -0400227/* No encapsulation header if EtherType < 0x600 (=length) */
228
229/* Called by ieee80211_rx_frame_decrypt */
230static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
231 struct sk_buff *skb)
232{
233 struct net_device *dev = ieee->dev;
234 u16 fc, ethertype;
235 struct ieee80211_hdr *hdr;
236 u8 *pos;
237
238 if (skb->len < 24)
239 return 0;
240
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400241 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400242 fc = le16_to_cpu(hdr->frame_ctl);
243
244 /* check that the frame is unicast frame to us */
245 if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
246 IEEE80211_FCTL_TODS &&
247 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
248 memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
249 /* ToDS frame with own addr BSSID and DA */
250 } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
251 IEEE80211_FCTL_FROMDS &&
252 memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
253 /* FromDS frame with own addr as DA */
254 } else
255 return 0;
256
257 if (skb->len < 24 + 8)
258 return 0;
259
260 /* check for port access entity Ethernet type */
261 pos = skb->data + 24;
262 ethertype = (pos[6] << 8) | pos[7];
263 if (ethertype == ETH_P_PAE)
264 return 1;
265
266 return 0;
267}
268
269/* Called only as a tasklet (software IRQ), by ieee80211_rx */
270static inline int
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400271ieee80211_rx_frame_decrypt(struct ieee80211_device *ieee, struct sk_buff *skb,
Jeff Garzikb4538722005-05-12 22:48:20 -0400272 struct ieee80211_crypt_data *crypt)
273{
274 struct ieee80211_hdr *hdr;
275 int res, hdrlen;
276
277 if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
278 return 0;
279
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400280 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400281 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
282
Jeff Garzikb4538722005-05-12 22:48:20 -0400283 atomic_inc(&crypt->refcnt);
284 res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
285 atomic_dec(&crypt->refcnt);
286 if (res < 0) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400287 IEEE80211_DEBUG_DROP("decryption failed (SA=" MAC_FMT
288 ") res=%d\n", MAC_ARG(hdr->addr2), res);
Jeff Garzikb4538722005-05-12 22:48:20 -0400289 if (res == -2)
290 IEEE80211_DEBUG_DROP("Decryption failed ICV "
291 "mismatch (key %d)\n",
292 skb->data[hdrlen + 3] >> 6);
293 ieee->ieee_stats.rx_discards_undecryptable++;
294 return -1;
295 }
296
297 return res;
298}
299
Jeff Garzikb4538722005-05-12 22:48:20 -0400300/* Called only as a tasklet (software IRQ), by ieee80211_rx */
301static inline int
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400302ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device *ieee,
303 struct sk_buff *skb, int keyidx,
304 struct ieee80211_crypt_data *crypt)
Jeff Garzikb4538722005-05-12 22:48:20 -0400305{
306 struct ieee80211_hdr *hdr;
307 int res, hdrlen;
308
309 if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
310 return 0;
311
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400312 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400313 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
314
315 atomic_inc(&crypt->refcnt);
316 res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
317 atomic_dec(&crypt->refcnt);
318 if (res < 0) {
319 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
320 " (SA=" MAC_FMT " keyidx=%d)\n",
321 ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
322 return -1;
323 }
324
325 return 0;
326}
327
Jeff Garzikb4538722005-05-12 22:48:20 -0400328/* All received frames are sent to this function. @skb contains the frame in
329 * IEEE 802.11 format, i.e., in the format it was sent over air.
330 * This function is called only as a tasklet (software IRQ). */
331int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
332 struct ieee80211_rx_stats *rx_stats)
333{
334 struct net_device *dev = ieee->dev;
335 struct ieee80211_hdr *hdr;
336 size_t hdrlen;
337 u16 fc, type, stype, sc;
338 struct net_device_stats *stats;
339 unsigned int frag;
340 u8 *payload;
341 u16 ethertype;
342#ifdef NOT_YET
343 struct net_device *wds = NULL;
344 struct sk_buff *skb2 = NULL;
345 struct net_device *wds = NULL;
346 int frame_authorized = 0;
347 int from_assoc_ap = 0;
348 void *sta = NULL;
349#endif
350 u8 dst[ETH_ALEN];
351 u8 src[ETH_ALEN];
352 struct ieee80211_crypt_data *crypt = NULL;
353 int keyidx = 0;
354
355 hdr = (struct ieee80211_hdr *)skb->data;
356 stats = &ieee->stats;
357
358 if (skb->len < 10) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400359 printk(KERN_INFO "%s: SKB length < 10\n", dev->name);
Jeff Garzikb4538722005-05-12 22:48:20 -0400360 goto rx_dropped;
361 }
362
363 fc = le16_to_cpu(hdr->frame_ctl);
364 type = WLAN_FC_GET_TYPE(fc);
365 stype = WLAN_FC_GET_STYPE(fc);
366 sc = le16_to_cpu(hdr->seq_ctl);
367 frag = WLAN_GET_SEQ_FRAG(sc);
368 hdrlen = ieee80211_get_hdrlen(fc);
369
Jeff Garzikb4538722005-05-12 22:48:20 -0400370 /* Put this code here so that we avoid duplicating it in all
371 * Rx paths. - Jean II */
372#ifdef IW_WIRELESS_SPY /* defined in iw_handler.h */
373 /* If spy monitoring on */
James Ketrenos74079fd2005-09-13 17:35:21 -0500374 if (ieee->spy_data.spy_number > 0) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400375 struct iw_quality wstats;
James Ketrenos74079fd2005-09-13 17:35:21 -0500376
377 wstats.updated = 0;
378 if (rx_stats->mask & IEEE80211_STATMASK_RSSI) {
379 wstats.level = rx_stats->rssi;
380 wstats.updated |= IW_QUAL_LEVEL_UPDATED;
381 } else
382 wstats.updated |= IW_QUAL_LEVEL_INVALID;
383
384 if (rx_stats->mask & IEEE80211_STATMASK_NOISE) {
385 wstats.noise = rx_stats->noise;
386 wstats.updated |= IW_QUAL_NOISE_UPDATED;
387 } else
388 wstats.updated |= IW_QUAL_NOISE_INVALID;
389
390 if (rx_stats->mask & IEEE80211_STATMASK_SIGNAL) {
391 wstats.qual = rx_stats->signal;
392 wstats.updated |= IW_QUAL_QUAL_UPDATED;
393 } else
394 wstats.updated |= IW_QUAL_QUAL_INVALID;
395
Jeff Garzikb4538722005-05-12 22:48:20 -0400396 /* Update spy records */
James Ketrenos74079fd2005-09-13 17:35:21 -0500397 wireless_spy_update(ieee->dev, hdr->addr2, &wstats);
Jeff Garzikb4538722005-05-12 22:48:20 -0400398 }
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400399#endif /* IW_WIRELESS_SPY */
James Ketrenos74079fd2005-09-13 17:35:21 -0500400
401#ifdef NOT_YET
Jeff Garzikb4538722005-05-12 22:48:20 -0400402 hostap_update_rx_stats(local->ap, hdr, rx_stats);
403#endif
404
Jeff Garzikb4538722005-05-12 22:48:20 -0400405 if (ieee->iw_mode == IW_MODE_MONITOR) {
406 ieee80211_monitor_rx(ieee, skb, rx_stats);
407 stats->rx_packets++;
408 stats->rx_bytes += skb->len;
409 return 1;
410 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400411
412 if (ieee->host_decrypt) {
413 int idx = 0;
414 if (skb->len >= hdrlen + 3)
415 idx = skb->data[hdrlen + 3] >> 6;
416 crypt = ieee->crypt[idx];
417#ifdef NOT_YET
418 sta = NULL;
419
420 /* Use station specific key to override default keys if the
421 * receiver address is a unicast address ("individual RA"). If
422 * bcrx_sta_key parameter is set, station specific key is used
423 * even with broad/multicast targets (this is against IEEE
424 * 802.11, but makes it easier to use different keys with
425 * stations that do not support WEP key mapping). */
426
427 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400428 (void)hostap_handle_sta_crypto(local, hdr, &crypt,
429 &sta);
Jeff Garzikb4538722005-05-12 22:48:20 -0400430#endif
431
432 /* allow NULL decrypt to indicate an station specific override
433 * for default encryption */
434 if (crypt && (crypt->ops == NULL ||
435 crypt->ops->decrypt_mpdu == NULL))
436 crypt = NULL;
437
Jiri Bencf13baae2005-08-25 20:11:46 -0400438 if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400439 /* This seems to be triggered by some (multicast?)
440 * frames from other than current BSS, so just drop the
441 * frames silently instead of filling system log with
442 * these reports. */
443 IEEE80211_DEBUG_DROP("Decryption failed (not set)"
444 " (SA=" MAC_FMT ")\n",
445 MAC_ARG(hdr->addr2));
446 ieee->ieee_stats.rx_discards_undecryptable++;
447 goto rx_dropped;
448 }
449 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400450#ifdef NOT_YET
451 if (type != WLAN_FC_TYPE_DATA) {
452 if (type == WLAN_FC_TYPE_MGMT && stype == WLAN_FC_STYPE_AUTH &&
Jiri Bencf13baae2005-08-25 20:11:46 -0400453 fc & IEEE80211_FCTL_PROTECTED && ieee->host_decrypt &&
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400454 (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400455 printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
456 "from " MAC_FMT "\n", dev->name,
457 MAC_ARG(hdr->addr2));
458 /* TODO: could inform hostapd about this so that it
459 * could send auth failure report */
460 goto rx_dropped;
461 }
462
463 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
464 goto rx_dropped;
465 else
466 goto rx_exit;
467 }
468#endif
469
470 /* Data frame - extract src/dst addresses */
Jiri Benc286d9742005-05-24 15:10:18 +0200471 if (skb->len < IEEE80211_3ADDR_LEN)
Jeff Garzikb4538722005-05-12 22:48:20 -0400472 goto rx_dropped;
473
474 switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
475 case IEEE80211_FCTL_FROMDS:
476 memcpy(dst, hdr->addr1, ETH_ALEN);
477 memcpy(src, hdr->addr3, ETH_ALEN);
478 break;
479 case IEEE80211_FCTL_TODS:
480 memcpy(dst, hdr->addr3, ETH_ALEN);
481 memcpy(src, hdr->addr2, ETH_ALEN);
482 break;
483 case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
Jiri Benc286d9742005-05-24 15:10:18 +0200484 if (skb->len < IEEE80211_4ADDR_LEN)
Jeff Garzikb4538722005-05-12 22:48:20 -0400485 goto rx_dropped;
486 memcpy(dst, hdr->addr3, ETH_ALEN);
487 memcpy(src, hdr->addr4, ETH_ALEN);
488 break;
489 case 0:
490 memcpy(dst, hdr->addr1, ETH_ALEN);
491 memcpy(src, hdr->addr2, ETH_ALEN);
492 break;
493 }
494
495#ifdef NOT_YET
496 if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
497 goto rx_dropped;
498 if (wds) {
499 skb->dev = dev = wds;
500 stats = hostap_get_stats(dev);
501 }
502
503 if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400504 (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
505 IEEE80211_FCTL_FROMDS && ieee->stadev
506 && memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400507 /* Frame from BSSID of the AP for which we are a client */
508 skb->dev = dev = ieee->stadev;
509 stats = hostap_get_stats(dev);
510 from_assoc_ap = 1;
511 }
512#endif
513
514 dev->last_rx = jiffies;
515
516#ifdef NOT_YET
517 if ((ieee->iw_mode == IW_MODE_MASTER ||
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400518 ieee->iw_mode == IW_MODE_REPEAT) && !from_assoc_ap) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400519 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
520 wds != NULL)) {
521 case AP_RX_CONTINUE_NOT_AUTHORIZED:
522 frame_authorized = 0;
523 break;
524 case AP_RX_CONTINUE:
525 frame_authorized = 1;
526 break;
527 case AP_RX_DROP:
528 goto rx_dropped;
529 case AP_RX_EXIT:
530 goto rx_exit;
531 }
532 }
533#endif
534
535 /* Nullfunc frames may have PS-bit set, so they must be passed to
536 * hostap_handle_sta_rx() before being dropped here. */
537 if (stype != IEEE80211_STYPE_DATA &&
538 stype != IEEE80211_STYPE_DATA_CFACK &&
539 stype != IEEE80211_STYPE_DATA_CFPOLL &&
540 stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
541 if (stype != IEEE80211_STYPE_NULLFUNC)
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400542 IEEE80211_DEBUG_DROP("RX: dropped data frame "
543 "with no data (type=0x%02x, "
544 "subtype=0x%02x, len=%d)\n",
545 type, stype, skb->len);
Jeff Garzikb4538722005-05-12 22:48:20 -0400546 goto rx_dropped;
547 }
548
549 /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
550
Jiri Bencf13baae2005-08-25 20:11:46 -0400551 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
Jeff Garzikb4538722005-05-12 22:48:20 -0400552 (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
553 goto rx_dropped;
554
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400555 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400556
557 /* skb: hdr + (possibly fragmented) plaintext payload */
558 // PR: FIXME: hostap has additional conditions in the "if" below:
Jiri Bencf13baae2005-08-25 20:11:46 -0400559 // ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
Jeff Garzikb4538722005-05-12 22:48:20 -0400560 if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
561 int flen;
562 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
563 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
564
565 if (!frag_skb) {
566 IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
567 "Rx cannot get skb from fragment "
568 "cache (morefrag=%d seq=%u frag=%u)\n",
569 (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
570 WLAN_GET_SEQ_SEQ(sc), frag);
571 goto rx_dropped;
572 }
573
574 flen = skb->len;
575 if (frag != 0)
576 flen -= hdrlen;
577
578 if (frag_skb->tail + flen > frag_skb->end) {
579 printk(KERN_WARNING "%s: host decrypted and "
580 "reassembled frame did not fit skb\n",
581 dev->name);
582 ieee80211_frag_cache_invalidate(ieee, hdr);
583 goto rx_dropped;
584 }
585
586 if (frag == 0) {
587 /* copy first fragment (including full headers) into
588 * beginning of the fragment cache skb */
589 memcpy(skb_put(frag_skb, flen), skb->data, flen);
590 } else {
591 /* append frame payload to the end of the fragment
592 * cache skb */
593 memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
594 flen);
595 }
596 dev_kfree_skb_any(skb);
597 skb = NULL;
598
599 if (fc & IEEE80211_FCTL_MOREFRAGS) {
600 /* more fragments expected - leave the skb in fragment
601 * cache for now; it will be delivered to upper layers
602 * after all fragments have been received */
603 goto rx_exit;
604 }
605
606 /* this was the last fragment and the frame will be
607 * delivered, so remove skb from fragment cache */
608 skb = frag_skb;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400609 hdr = (struct ieee80211_hdr *)skb->data;
Jeff Garzikb4538722005-05-12 22:48:20 -0400610 ieee80211_frag_cache_invalidate(ieee, hdr);
611 }
612
613 /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
614 * encrypted/authenticated */
Jiri Bencf13baae2005-08-25 20:11:46 -0400615 if (ieee->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
Jeff Garzikb4538722005-05-12 22:48:20 -0400616 ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
617 goto rx_dropped;
618
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400619 hdr = (struct ieee80211_hdr *)skb->data;
Jiri Bencf13baae2005-08-25 20:11:46 -0400620 if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400621 if ( /*ieee->ieee802_1x && */
622 ieee80211_is_eapol_frame(ieee, skb)) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400623 /* pass unencrypted EAPOL frames even if encryption is
624 * configured */
Jeff Garzikb4538722005-05-12 22:48:20 -0400625 } else {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400626 IEEE80211_DEBUG_DROP("encryption configured, but RX "
627 "frame not encrypted (SA=" MAC_FMT
628 ")\n", MAC_ARG(hdr->addr2));
Jeff Garzikb4538722005-05-12 22:48:20 -0400629 goto rx_dropped;
630 }
631 }
632
Jiri Bencf13baae2005-08-25 20:11:46 -0400633 if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
Jeff Garzikb4538722005-05-12 22:48:20 -0400634 !ieee80211_is_eapol_frame(ieee, skb)) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400635 IEEE80211_DEBUG_DROP("dropped unencrypted RX data "
636 "frame from " MAC_FMT
637 " (drop_unencrypted=1)\n",
638 MAC_ARG(hdr->addr2));
Jeff Garzikb4538722005-05-12 22:48:20 -0400639 goto rx_dropped;
640 }
641
642 /* skb: hdr + (possible reassembled) full plaintext payload */
643
644 payload = skb->data + hdrlen;
645 ethertype = (payload[6] << 8) | payload[7];
646
647#ifdef NOT_YET
648 /* If IEEE 802.1X is used, check whether the port is authorized to send
649 * the received frame. */
650 if (ieee->ieee802_1x && ieee->iw_mode == IW_MODE_MASTER) {
651 if (ethertype == ETH_P_PAE) {
652 printk(KERN_DEBUG "%s: RX: IEEE 802.1X frame\n",
653 dev->name);
654 if (ieee->hostapd && ieee->apdev) {
655 /* Send IEEE 802.1X frames to the user
656 * space daemon for processing */
657 prism2_rx_80211(ieee->apdev, skb, rx_stats,
658 PRISM2_RX_MGMT);
659 ieee->apdevstats.rx_packets++;
660 ieee->apdevstats.rx_bytes += skb->len;
661 goto rx_exit;
662 }
663 } else if (!frame_authorized) {
664 printk(KERN_DEBUG "%s: dropped frame from "
665 "unauthorized port (IEEE 802.1X): "
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400666 "ethertype=0x%04x\n", dev->name, ethertype);
Jeff Garzikb4538722005-05-12 22:48:20 -0400667 goto rx_dropped;
668 }
669 }
670#endif
671
672 /* convert hdr + possible LLC headers into Ethernet header */
673 if (skb->len - hdrlen >= 8 &&
674 ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
675 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
676 memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
677 /* remove RFC1042 or Bridge-Tunnel encapsulation and
678 * replace EtherType */
679 skb_pull(skb, hdrlen + SNAP_SIZE);
680 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
681 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
682 } else {
683 u16 len;
684 /* Leave Ethernet header part of hdr and full payload */
685 skb_pull(skb, hdrlen);
686 len = htons(skb->len);
687 memcpy(skb_push(skb, 2), &len, 2);
688 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
689 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
690 }
691
692#ifdef NOT_YET
693 if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400694 IEEE80211_FCTL_TODS) && skb->len >= ETH_HLEN + ETH_ALEN) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400695 /* Non-standard frame: get addr4 from its bogus location after
696 * the payload */
697 memcpy(skb->data + ETH_ALEN,
698 skb->data + skb->len - ETH_ALEN, ETH_ALEN);
699 skb_trim(skb, skb->len - ETH_ALEN);
700 }
701#endif
702
703 stats->rx_packets++;
704 stats->rx_bytes += skb->len;
705
706#ifdef NOT_YET
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400707 if (ieee->iw_mode == IW_MODE_MASTER && !wds && ieee->ap->bridge_packets) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400708 if (dst[0] & 0x01) {
709 /* copy multicast frame both to the higher layers and
710 * to the wireless media */
711 ieee->ap->bridged_multicast++;
712 skb2 = skb_clone(skb, GFP_ATOMIC);
713 if (skb2 == NULL)
714 printk(KERN_DEBUG "%s: skb_clone failed for "
715 "multicast frame\n", dev->name);
716 } else if (hostap_is_sta_assoc(ieee->ap, dst)) {
717 /* send frame directly to the associated STA using
718 * wireless media and not passing to higher layers */
719 ieee->ap->bridged_unicast++;
720 skb2 = skb;
721 skb = NULL;
722 }
723 }
724
725 if (skb2 != NULL) {
726 /* send to wireless media */
727 skb2->protocol = __constant_htons(ETH_P_802_3);
728 skb2->mac.raw = skb2->nh.raw = skb2->data;
729 /* skb2->nh.raw = skb2->data + ETH_HLEN; */
730 skb2->dev = dev;
731 dev_queue_xmit(skb2);
732 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400733#endif
734
735 if (skb) {
736 skb->protocol = eth_type_trans(skb, dev);
737 memset(skb->cb, 0, sizeof(skb->cb));
738 skb->dev = dev;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400739 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
Jeff Garzikb4538722005-05-12 22:48:20 -0400740 netif_rx(skb);
741 }
742
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400743 rx_exit:
Jeff Garzikb4538722005-05-12 22:48:20 -0400744#ifdef NOT_YET
745 if (sta)
746 hostap_handle_sta_release(sta);
747#endif
748 return 1;
749
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400750 rx_dropped:
Jeff Garzikb4538722005-05-12 22:48:20 -0400751 stats->rx_dropped++;
752
753 /* Returning 0 indicates to caller that we have not handled the SKB--
754 * so it is still allocated and can be used again by underlying
755 * hardware as a DMA target */
756 return 0;
757}
758
759#define MGMT_FRAME_FIXED_PART_LENGTH 0x24
760
761static inline int ieee80211_is_ofdm_rate(u8 rate)
762{
763 switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
764 case IEEE80211_OFDM_RATE_6MB:
765 case IEEE80211_OFDM_RATE_9MB:
766 case IEEE80211_OFDM_RATE_12MB:
767 case IEEE80211_OFDM_RATE_18MB:
768 case IEEE80211_OFDM_RATE_24MB:
769 case IEEE80211_OFDM_RATE_36MB:
770 case IEEE80211_OFDM_RATE_48MB:
771 case IEEE80211_OFDM_RATE_54MB:
772 return 1;
773 }
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400774 return 0;
Jeff Garzikb4538722005-05-12 22:48:20 -0400775}
776
James Ketrenos74079fd2005-09-13 17:35:21 -0500777static inline int ieee80211_network_init(struct ieee80211_device *ieee, struct ieee80211_probe_response
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400778 *beacon,
779 struct ieee80211_network *network,
780 struct ieee80211_rx_stats *stats)
Jeff Garzikb4538722005-05-12 22:48:20 -0400781{
782#ifdef CONFIG_IEEE80211_DEBUG
783 char rates_str[64];
784 char *p;
785#endif
786 struct ieee80211_info_element *info_element;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400787 u16 left;
Jeff Garzikb4538722005-05-12 22:48:20 -0400788 u8 i;
789
790 /* Pull out fixed field data */
791 memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
James Ketrenosfd278172005-09-13 17:25:51 -0500792 network->capability = le16_to_cpu(beacon->capability);
Jeff Garzikb4538722005-05-12 22:48:20 -0400793 network->last_scanned = jiffies;
James Ketrenosfd278172005-09-13 17:25:51 -0500794 network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
795 network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
796 network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400797 /* Where to pull this? beacon->listen_interval; */
Jeff Garzikb4538722005-05-12 22:48:20 -0400798 network->listen_interval = 0x0A;
799 network->rates_len = network->rates_ex_len = 0;
800 network->last_associate = 0;
801 network->ssid_len = 0;
802 network->flags = 0;
803 network->atim_window = 0;
804
805 if (stats->freq == IEEE80211_52GHZ_BAND) {
806 /* for A band (No DS info) */
807 network->channel = stats->received_channel;
808 } else
809 network->flags |= NETWORK_HAS_CCK;
810
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400811 network->wpa_ie_len = 0;
812 network->rsn_ie_len = 0;
Jeff Garzikb4538722005-05-12 22:48:20 -0400813
James Ketrenos68e4e032005-09-13 17:37:22 -0500814 info_element = beacon->info_element;
James Ketrenos7b1fa542005-09-13 17:38:13 -0500815 left = stats->len - sizeof(*beacon);
816 while (left >= sizeof(struct ieee80211_info_element)) {
817 if (sizeof(struct ieee80211_info_element) + info_element->len >
818 left) {
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400819 IEEE80211_DEBUG_SCAN
820 ("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%Zd left=%d.\n",
821 info_element->len +
822 sizeof(struct ieee80211_info_element), left);
Jeff Garzikb4538722005-05-12 22:48:20 -0400823 return 1;
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400824 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400825
826 switch (info_element->id) {
827 case MFIE_TYPE_SSID:
828 if (ieee80211_is_empty_essid(info_element->data,
829 info_element->len)) {
830 network->flags |= NETWORK_EMPTY_ESSID;
831 break;
832 }
833
834 network->ssid_len = min(info_element->len,
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400835 (u8) IW_ESSID_MAX_SIZE);
836 memcpy(network->ssid, info_element->data,
837 network->ssid_len);
838 if (network->ssid_len < IW_ESSID_MAX_SIZE)
839 memset(network->ssid + network->ssid_len, 0,
Jeff Garzikb4538722005-05-12 22:48:20 -0400840 IW_ESSID_MAX_SIZE - network->ssid_len);
841
842 IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
843 network->ssid, network->ssid_len);
844 break;
845
846 case MFIE_TYPE_RATES:
847#ifdef CONFIG_IEEE80211_DEBUG
848 p = rates_str;
849#endif
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400850 network->rates_len =
851 min(info_element->len, MAX_RATES_LENGTH);
Jeff Garzikb4538722005-05-12 22:48:20 -0400852 for (i = 0; i < network->rates_len; i++) {
853 network->rates[i] = info_element->data[i];
854#ifdef CONFIG_IEEE80211_DEBUG
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400855 p += snprintf(p,
856 sizeof(rates_str) - (p -
857 rates_str),
858 "%02X ", network->rates[i]);
Jeff Garzikb4538722005-05-12 22:48:20 -0400859#endif
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400860 if (ieee80211_is_ofdm_rate
861 (info_element->data[i])) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400862 network->flags |= NETWORK_HAS_OFDM;
863 if (info_element->data[i] &
864 IEEE80211_BASIC_RATE_MASK)
865 network->flags &=
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400866 ~NETWORK_HAS_CCK;
Jeff Garzikb4538722005-05-12 22:48:20 -0400867 }
868 }
869
870 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES: '%s' (%d)\n",
871 rates_str, network->rates_len);
872 break;
873
874 case MFIE_TYPE_RATES_EX:
875#ifdef CONFIG_IEEE80211_DEBUG
876 p = rates_str;
877#endif
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400878 network->rates_ex_len =
879 min(info_element->len, MAX_RATES_EX_LENGTH);
Jeff Garzikb4538722005-05-12 22:48:20 -0400880 for (i = 0; i < network->rates_ex_len; i++) {
881 network->rates_ex[i] = info_element->data[i];
882#ifdef CONFIG_IEEE80211_DEBUG
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400883 p += snprintf(p,
884 sizeof(rates_str) - (p -
885 rates_str),
886 "%02X ", network->rates[i]);
Jeff Garzikb4538722005-05-12 22:48:20 -0400887#endif
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400888 if (ieee80211_is_ofdm_rate
889 (info_element->data[i])) {
Jeff Garzikb4538722005-05-12 22:48:20 -0400890 network->flags |= NETWORK_HAS_OFDM;
891 if (info_element->data[i] &
892 IEEE80211_BASIC_RATE_MASK)
893 network->flags &=
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400894 ~NETWORK_HAS_CCK;
Jeff Garzikb4538722005-05-12 22:48:20 -0400895 }
896 }
897
898 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
899 rates_str, network->rates_ex_len);
900 break;
901
902 case MFIE_TYPE_DS_SET:
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400903 IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
Jeff Garzikb4538722005-05-12 22:48:20 -0400904 info_element->data[0]);
905 if (stats->freq == IEEE80211_24GHZ_BAND)
906 network->channel = info_element->data[0];
907 break;
908
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400909 case MFIE_TYPE_FH_SET:
910 IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
Jeff Garzikb4538722005-05-12 22:48:20 -0400911 break;
912
913 case MFIE_TYPE_CF_SET:
914 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
915 break;
916
917 case MFIE_TYPE_TIM:
918 IEEE80211_DEBUG_SCAN("MFIE_TYPE_TIM: ignored\n");
919 break;
920
921 case MFIE_TYPE_IBSS_SET:
922 IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
923 break;
924
925 case MFIE_TYPE_CHALLENGE:
926 IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
927 break;
928
929 case MFIE_TYPE_GENERIC:
930 IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
931 info_element->len);
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400932 if (info_element->len >= 4 &&
Jeff Garzikb4538722005-05-12 22:48:20 -0400933 info_element->data[0] == 0x00 &&
934 info_element->data[1] == 0x50 &&
935 info_element->data[2] == 0xf2 &&
936 info_element->data[3] == 0x01) {
937 network->wpa_ie_len = min(info_element->len + 2,
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400938 MAX_WPA_IE_LEN);
Jeff Garzikb4538722005-05-12 22:48:20 -0400939 memcpy(network->wpa_ie, info_element,
940 network->wpa_ie_len);
941 }
942 break;
943
944 case MFIE_TYPE_RSN:
945 IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
946 info_element->len);
947 network->rsn_ie_len = min(info_element->len + 2,
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400948 MAX_WPA_IE_LEN);
Jeff Garzikb4538722005-05-12 22:48:20 -0400949 memcpy(network->rsn_ie, info_element,
950 network->rsn_ie_len);
951 break;
952
953 default:
954 IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
955 info_element->id);
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400956 break;
957 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400958
James Ketrenos7b1fa542005-09-13 17:38:13 -0500959 left -= sizeof(struct ieee80211_info_element) +
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400960 info_element->len;
Jeff Garzikb4538722005-05-12 22:48:20 -0400961 info_element = (struct ieee80211_info_element *)
Jeff Garzik0edd5b42005-09-07 00:48:31 -0400962 &info_element->data[info_element->len];
963 }
Jeff Garzikb4538722005-05-12 22:48:20 -0400964
965 network->mode = 0;
966 if (stats->freq == IEEE80211_52GHZ_BAND)
967 network->mode = IEEE_A;
968 else {
969 if (network->flags & NETWORK_HAS_OFDM)
970 network->mode |= IEEE_G;
971 if (network->flags & NETWORK_HAS_CCK)
972 network->mode |= IEEE_B;
973 }
974
975 if (network->mode == 0) {
976 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
977 "network.\n",
978 escape_essid(network->ssid,
979 network->ssid_len),
980 MAC_ARG(network->bssid));
981 return 1;
982 }
983
984 if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
985 network->flags |= NETWORK_EMPTY_ESSID;
986
987 memcpy(&network->stats, stats, sizeof(network->stats));
988
989 return 0;
990}
991
992static inline int is_same_network(struct ieee80211_network *src,
993 struct ieee80211_network *dst)
994{
995 /* A network is only a duplicate if the channel, BSSID, and ESSID
996 * all match. We treat all <hidden> with the same BSSID and channel
997 * as one network */
998 return ((src->ssid_len == dst->ssid_len) &&
999 (src->channel == dst->channel) &&
1000 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1001 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1002}
1003
1004static inline void update_network(struct ieee80211_network *dst,
1005 struct ieee80211_network *src)
1006{
1007 memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1008 dst->capability = src->capability;
1009 memcpy(dst->rates, src->rates, src->rates_len);
1010 dst->rates_len = src->rates_len;
1011 memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1012 dst->rates_ex_len = src->rates_ex_len;
1013
1014 dst->mode = src->mode;
1015 dst->flags = src->flags;
1016 dst->time_stamp[0] = src->time_stamp[0];
1017 dst->time_stamp[1] = src->time_stamp[1];
1018
1019 dst->beacon_interval = src->beacon_interval;
1020 dst->listen_interval = src->listen_interval;
1021 dst->atim_window = src->atim_window;
1022
1023 memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1024 dst->wpa_ie_len = src->wpa_ie_len;
1025 memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1026 dst->rsn_ie_len = src->rsn_ie_len;
1027
1028 dst->last_scanned = jiffies;
1029 /* dst->last_associate is not overwritten */
1030}
1031
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001032static inline void ieee80211_process_probe_response(struct ieee80211_device
James Ketrenos74079fd2005-09-13 17:35:21 -05001033 *ieee, struct
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001034 ieee80211_probe_response
James Ketrenos74079fd2005-09-13 17:35:21 -05001035 *beacon, struct ieee80211_rx_stats
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001036 *stats)
Jeff Garzikb4538722005-05-12 22:48:20 -04001037{
1038 struct ieee80211_network network;
1039 struct ieee80211_network *target;
1040 struct ieee80211_network *oldest = NULL;
1041#ifdef CONFIG_IEEE80211_DEBUG
James Ketrenos68e4e032005-09-13 17:37:22 -05001042 struct ieee80211_info_element *info_element = beacon->info_element;
Jeff Garzikb4538722005-05-12 22:48:20 -04001043#endif
1044 unsigned long flags;
1045
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001046 IEEE80211_DEBUG_SCAN("'%s' (" MAC_FMT
1047 "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1048 escape_essid(info_element->data,
1049 info_element->len),
1050 MAC_ARG(beacon->header.addr3),
1051 (beacon->capability & (1 << 0xf)) ? '1' : '0',
1052 (beacon->capability & (1 << 0xe)) ? '1' : '0',
1053 (beacon->capability & (1 << 0xd)) ? '1' : '0',
1054 (beacon->capability & (1 << 0xc)) ? '1' : '0',
1055 (beacon->capability & (1 << 0xb)) ? '1' : '0',
1056 (beacon->capability & (1 << 0xa)) ? '1' : '0',
1057 (beacon->capability & (1 << 0x9)) ? '1' : '0',
1058 (beacon->capability & (1 << 0x8)) ? '1' : '0',
1059 (beacon->capability & (1 << 0x7)) ? '1' : '0',
1060 (beacon->capability & (1 << 0x6)) ? '1' : '0',
1061 (beacon->capability & (1 << 0x5)) ? '1' : '0',
1062 (beacon->capability & (1 << 0x4)) ? '1' : '0',
1063 (beacon->capability & (1 << 0x3)) ? '1' : '0',
1064 (beacon->capability & (1 << 0x2)) ? '1' : '0',
1065 (beacon->capability & (1 << 0x1)) ? '1' : '0',
1066 (beacon->capability & (1 << 0x0)) ? '1' : '0');
Jeff Garzikb4538722005-05-12 22:48:20 -04001067
1068 if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1069 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1070 escape_essid(info_element->data,
1071 info_element->len),
1072 MAC_ARG(beacon->header.addr3),
James Ketrenosfd278172005-09-13 17:25:51 -05001073 WLAN_FC_GET_STYPE(le16_to_cpu
1074 (beacon->header.
1075 frame_ctl)) ==
Jeff Garzikb4538722005-05-12 22:48:20 -04001076 IEEE80211_STYPE_PROBE_RESP ?
1077 "PROBE RESPONSE" : "BEACON");
1078 return;
1079 }
1080
1081 /* The network parsed correctly -- so now we scan our known networks
1082 * to see if we can find it in our list.
1083 *
1084 * NOTE: This search is definitely not optimized. Once its doing
1085 * the "right thing" we'll optimize it for efficiency if
1086 * necessary */
1087
1088 /* Search for this entry in the list and update it if it is
1089 * already there. */
1090
1091 spin_lock_irqsave(&ieee->lock, flags);
1092
1093 list_for_each_entry(target, &ieee->network_list, list) {
1094 if (is_same_network(target, &network))
1095 break;
1096
1097 if ((oldest == NULL) ||
1098 (target->last_scanned < oldest->last_scanned))
1099 oldest = target;
1100 }
1101
1102 /* If we didn't find a match, then get a new network slot to initialize
1103 * with this beacon's information */
1104 if (&target->list == &ieee->network_list) {
1105 if (list_empty(&ieee->network_free_list)) {
1106 /* If there are no more slots, expire the oldest */
1107 list_del(&oldest->list);
1108 target = oldest;
1109 IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1110 "network list.\n",
1111 escape_essid(target->ssid,
1112 target->ssid_len),
1113 MAC_ARG(target->bssid));
1114 } else {
1115 /* Otherwise just pull from the free list */
1116 target = list_entry(ieee->network_free_list.next,
1117 struct ieee80211_network, list);
1118 list_del(ieee->network_free_list.next);
1119 }
1120
Jeff Garzikb4538722005-05-12 22:48:20 -04001121#ifdef CONFIG_IEEE80211_DEBUG
1122 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1123 escape_essid(network.ssid,
1124 network.ssid_len),
1125 MAC_ARG(network.bssid),
James Ketrenosfd278172005-09-13 17:25:51 -05001126 WLAN_FC_GET_STYPE(le16_to_cpu
1127 (beacon->header.
1128 frame_ctl)) ==
Jeff Garzikb4538722005-05-12 22:48:20 -04001129 IEEE80211_STYPE_PROBE_RESP ?
1130 "PROBE RESPONSE" : "BEACON");
1131#endif
1132 memcpy(target, &network, sizeof(*target));
1133 list_add_tail(&target->list, &ieee->network_list);
1134 } else {
1135 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1136 escape_essid(target->ssid,
1137 target->ssid_len),
1138 MAC_ARG(target->bssid),
James Ketrenosfd278172005-09-13 17:25:51 -05001139 WLAN_FC_GET_STYPE(le16_to_cpu
1140 (beacon->header.
1141 frame_ctl)) ==
Jeff Garzikb4538722005-05-12 22:48:20 -04001142 IEEE80211_STYPE_PROBE_RESP ?
1143 "PROBE RESPONSE" : "BEACON");
1144 update_network(target, &network);
1145 }
1146
1147 spin_unlock_irqrestore(&ieee->lock, flags);
1148}
1149
1150void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1151 struct ieee80211_hdr *header,
1152 struct ieee80211_rx_stats *stats)
1153{
James Ketrenosfd278172005-09-13 17:25:51 -05001154 switch (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl))) {
Jeff Garzikb4538722005-05-12 22:48:20 -04001155 case IEEE80211_STYPE_ASSOC_RESP:
1156 IEEE80211_DEBUG_MGMT("received ASSOCIATION RESPONSE (%d)\n",
James Ketrenosfd278172005-09-13 17:25:51 -05001157 WLAN_FC_GET_STYPE(le16_to_cpu
1158 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001159 break;
1160
1161 case IEEE80211_STYPE_REASSOC_RESP:
1162 IEEE80211_DEBUG_MGMT("received REASSOCIATION RESPONSE (%d)\n",
James Ketrenosfd278172005-09-13 17:25:51 -05001163 WLAN_FC_GET_STYPE(le16_to_cpu
1164 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001165 break;
1166
1167 case IEEE80211_STYPE_PROBE_RESP:
1168 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
James Ketrenosfd278172005-09-13 17:25:51 -05001169 WLAN_FC_GET_STYPE(le16_to_cpu
1170 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001171 IEEE80211_DEBUG_SCAN("Probe response\n");
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001172 ieee80211_process_probe_response(ieee,
1173 (struct
1174 ieee80211_probe_response *)
1175 header, stats);
Jeff Garzikb4538722005-05-12 22:48:20 -04001176 break;
1177
1178 case IEEE80211_STYPE_BEACON:
1179 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
James Ketrenosfd278172005-09-13 17:25:51 -05001180 WLAN_FC_GET_STYPE(le16_to_cpu
1181 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001182 IEEE80211_DEBUG_SCAN("Beacon\n");
Jeff Garzik0edd5b42005-09-07 00:48:31 -04001183 ieee80211_process_probe_response(ieee,
1184 (struct
1185 ieee80211_probe_response *)
1186 header, stats);
Jeff Garzikb4538722005-05-12 22:48:20 -04001187 break;
1188
1189 default:
1190 IEEE80211_DEBUG_MGMT("received UNKNOWN (%d)\n",
James Ketrenosfd278172005-09-13 17:25:51 -05001191 WLAN_FC_GET_STYPE(le16_to_cpu
1192 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001193 IEEE80211_WARNING("%s: Unknown management packet: %d\n",
1194 ieee->dev->name,
James Ketrenosfd278172005-09-13 17:25:51 -05001195 WLAN_FC_GET_STYPE(le16_to_cpu
1196 (header->frame_ctl)));
Jeff Garzikb4538722005-05-12 22:48:20 -04001197 break;
1198 }
1199}
1200
Jeff Garzikb4538722005-05-12 22:48:20 -04001201EXPORT_SYMBOL(ieee80211_rx_mgt);
1202EXPORT_SYMBOL(ieee80211_rx);