Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / wireless / hostap / hostap_80211_rx.c
1 #include <linux/etherdevice.h>
2 #include <net/lib80211.h>
3
4 #include "hostap_80211.h"
5 #include "hostap.h"
6 #include "hostap_ap.h"
7
8 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
9 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
10 static unsigned char rfc1042_header[] =
11 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
12 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
13 static unsigned char bridge_tunnel_header[] =
14 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
15 /* No encapsulation header if EtherType < 0x600 (=length) */
16
17 void hostap_dump_rx_80211(const char *name, struct sk_buff *skb,
18                           struct hostap_80211_rx_status *rx_stats)
19 {
20         struct ieee80211_hdr_4addr *hdr;
21         u16 fc;
22
23         hdr = (struct ieee80211_hdr_4addr *) skb->data;
24
25         printk(KERN_DEBUG "%s: RX signal=%d noise=%d rate=%d len=%d "
26                "jiffies=%ld\n",
27                name, rx_stats->signal, rx_stats->noise, rx_stats->rate,
28                skb->len, jiffies);
29
30         if (skb->len < 2)
31                 return;
32
33         fc = le16_to_cpu(hdr->frame_ctl);
34         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d)%s%s",
35                fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
36                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
37                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
38
39         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
40                 printk("\n");
41                 return;
42         }
43
44         printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
45                le16_to_cpu(hdr->seq_ctl));
46
47         printk(KERN_DEBUG "   A1=%pM", hdr->addr1);
48         printk(" A2=%pM", hdr->addr2);
49         printk(" A3=%pM", hdr->addr3);
50         if (skb->len >= 30)
51                 printk(" A4=%pM", hdr->addr4);
52         printk("\n");
53 }
54
55
56 /* Send RX frame to netif with 802.11 (and possible prism) header.
57  * Called from hardware or software IRQ context. */
58 int prism2_rx_80211(struct net_device *dev, struct sk_buff *skb,
59                     struct hostap_80211_rx_status *rx_stats, int type)
60 {
61         struct hostap_interface *iface;
62         local_info_t *local;
63         int hdrlen, phdrlen, head_need, tail_need;
64         u16 fc;
65         int prism_header, ret;
66         struct ieee80211_hdr_4addr *fhdr;
67
68         iface = netdev_priv(dev);
69         local = iface->local;
70
71         if (dev->type == ARPHRD_IEEE80211_PRISM) {
72                 if (local->monitor_type == PRISM2_MONITOR_PRISM) {
73                         prism_header = 1;
74                         phdrlen = sizeof(struct linux_wlan_ng_prism_hdr);
75                 } else { /* local->monitor_type == PRISM2_MONITOR_CAPHDR */
76                         prism_header = 2;
77                         phdrlen = sizeof(struct linux_wlan_ng_cap_hdr);
78                 }
79         } else if (dev->type == ARPHRD_IEEE80211_RADIOTAP) {
80                 prism_header = 3;
81                 phdrlen = sizeof(struct hostap_radiotap_rx);
82         } else {
83                 prism_header = 0;
84                 phdrlen = 0;
85         }
86
87         fhdr = (struct ieee80211_hdr_4addr *) skb->data;
88         fc = le16_to_cpu(fhdr->frame_ctl);
89
90         if (type == PRISM2_RX_MGMT && (fc & IEEE80211_FCTL_VERS)) {
91                 printk(KERN_DEBUG "%s: dropped management frame with header "
92                        "version %d\n", dev->name, fc & IEEE80211_FCTL_VERS);
93                 dev_kfree_skb_any(skb);
94                 return 0;
95         }
96
97         hdrlen = hostap_80211_get_hdrlen(fc);
98
99         /* check if there is enough room for extra data; if not, expand skb
100          * buffer to be large enough for the changes */
101         head_need = phdrlen;
102         tail_need = 0;
103 #ifdef PRISM2_ADD_BOGUS_CRC
104         tail_need += 4;
105 #endif /* PRISM2_ADD_BOGUS_CRC */
106
107         head_need -= skb_headroom(skb);
108         tail_need -= skb_tailroom(skb);
109
110         if (head_need > 0 || tail_need > 0) {
111                 if (pskb_expand_head(skb, head_need > 0 ? head_need : 0,
112                                      tail_need > 0 ? tail_need : 0,
113                                      GFP_ATOMIC)) {
114                         printk(KERN_DEBUG "%s: prism2_rx_80211 failed to "
115                                "reallocate skb buffer\n", dev->name);
116                         dev_kfree_skb_any(skb);
117                         return 0;
118                 }
119         }
120
121         /* We now have an skb with enough head and tail room, so just insert
122          * the extra data */
123
124 #ifdef PRISM2_ADD_BOGUS_CRC
125         memset(skb_put(skb, 4), 0xff, 4); /* Prism2 strips CRC */
126 #endif /* PRISM2_ADD_BOGUS_CRC */
127
128         if (prism_header == 1) {
129                 struct linux_wlan_ng_prism_hdr *hdr;
130                 hdr = (struct linux_wlan_ng_prism_hdr *)
131                         skb_push(skb, phdrlen);
132                 memset(hdr, 0, phdrlen);
133                 hdr->msgcode = LWNG_CAP_DID_BASE;
134                 hdr->msglen = sizeof(*hdr);
135                 memcpy(hdr->devname, dev->name, sizeof(hdr->devname));
136 #define LWNG_SETVAL(f,i,s,l,d) \
137 hdr->f.did = LWNG_CAP_DID_BASE | (i << 12); \
138 hdr->f.status = s; hdr->f.len = l; hdr->f.data = d
139                 LWNG_SETVAL(hosttime, 1, 0, 4, jiffies);
140                 LWNG_SETVAL(mactime, 2, 0, 4, rx_stats->mac_time);
141                 LWNG_SETVAL(channel, 3, 1 /* no value */, 4, 0);
142                 LWNG_SETVAL(rssi, 4, 1 /* no value */, 4, 0);
143                 LWNG_SETVAL(sq, 5, 1 /* no value */, 4, 0);
144                 LWNG_SETVAL(signal, 6, 0, 4, rx_stats->signal);
145                 LWNG_SETVAL(noise, 7, 0, 4, rx_stats->noise);
146                 LWNG_SETVAL(rate, 8, 0, 4, rx_stats->rate / 5);
147                 LWNG_SETVAL(istx, 9, 0, 4, 0);
148                 LWNG_SETVAL(frmlen, 10, 0, 4, skb->len - phdrlen);
149 #undef LWNG_SETVAL
150         } else if (prism_header == 2) {
151                 struct linux_wlan_ng_cap_hdr *hdr;
152                 hdr = (struct linux_wlan_ng_cap_hdr *)
153                         skb_push(skb, phdrlen);
154                 memset(hdr, 0, phdrlen);
155                 hdr->version    = htonl(LWNG_CAPHDR_VERSION);
156                 hdr->length     = htonl(phdrlen);
157                 hdr->mactime    = __cpu_to_be64(rx_stats->mac_time);
158                 hdr->hosttime   = __cpu_to_be64(jiffies);
159                 hdr->phytype    = htonl(4); /* dss_dot11_b */
160                 hdr->channel    = htonl(local->channel);
161                 hdr->datarate   = htonl(rx_stats->rate);
162                 hdr->antenna    = htonl(0); /* unknown */
163                 hdr->priority   = htonl(0); /* unknown */
164                 hdr->ssi_type   = htonl(3); /* raw */
165                 hdr->ssi_signal = htonl(rx_stats->signal);
166                 hdr->ssi_noise  = htonl(rx_stats->noise);
167                 hdr->preamble   = htonl(0); /* unknown */
168                 hdr->encoding   = htonl(1); /* cck */
169         } else if (prism_header == 3) {
170                 struct hostap_radiotap_rx *hdr;
171                 hdr = (struct hostap_radiotap_rx *)skb_push(skb, phdrlen);
172                 memset(hdr, 0, phdrlen);
173                 hdr->hdr.it_len = cpu_to_le16(phdrlen);
174                 hdr->hdr.it_present =
175                         cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
176                                     (1 << IEEE80211_RADIOTAP_CHANNEL) |
177                                     (1 << IEEE80211_RADIOTAP_RATE) |
178                                     (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
179                                     (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE));
180                 hdr->tsft = cpu_to_le64(rx_stats->mac_time);
181                 hdr->chan_freq = cpu_to_le16(freq_list[local->channel - 1]);
182                 hdr->chan_flags = cpu_to_le16(IEEE80211_CHAN_CCK |
183                                                  IEEE80211_CHAN_2GHZ);
184                 hdr->rate = rx_stats->rate / 5;
185                 hdr->dbm_antsignal = rx_stats->signal;
186                 hdr->dbm_antnoise = rx_stats->noise;
187         }
188
189         ret = skb->len - phdrlen;
190         skb->dev = dev;
191         skb_reset_mac_header(skb);
192         skb_pull(skb, hdrlen);
193         if (prism_header)
194                 skb_pull(skb, phdrlen);
195         skb->pkt_type = PACKET_OTHERHOST;
196         skb->protocol = __constant_htons(ETH_P_802_2);
197         memset(skb->cb, 0, sizeof(skb->cb));
198         netif_rx(skb);
199
200         return ret;
201 }
202
203
204 /* Called only as a tasklet (software IRQ) */
205 static void monitor_rx(struct net_device *dev, struct sk_buff *skb,
206                        struct hostap_80211_rx_status *rx_stats)
207 {
208         struct net_device_stats *stats;
209         int len;
210
211         len = prism2_rx_80211(dev, skb, rx_stats, PRISM2_RX_MONITOR);
212         stats = hostap_get_stats(dev);
213         stats->rx_packets++;
214         stats->rx_bytes += len;
215 }
216
217
218 /* Called only as a tasklet (software IRQ) */
219 static struct prism2_frag_entry *
220 prism2_frag_cache_find(local_info_t *local, unsigned int seq,
221                        unsigned int frag, u8 *src, u8 *dst)
222 {
223         struct prism2_frag_entry *entry;
224         int i;
225
226         for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
227                 entry = &local->frag_cache[i];
228                 if (entry->skb != NULL &&
229                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
230                         printk(KERN_DEBUG "%s: expiring fragment cache entry "
231                                "seq=%u last_frag=%u\n",
232                                local->dev->name, entry->seq, entry->last_frag);
233                         dev_kfree_skb(entry->skb);
234                         entry->skb = NULL;
235                 }
236
237                 if (entry->skb != NULL && entry->seq == seq &&
238                     (entry->last_frag + 1 == frag || frag == -1) &&
239                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
240                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
241                         return entry;
242         }
243
244         return NULL;
245 }
246
247
248 /* Called only as a tasklet (software IRQ) */
249 static struct sk_buff *
250 prism2_frag_cache_get(local_info_t *local, struct ieee80211_hdr_4addr *hdr)
251 {
252         struct sk_buff *skb = NULL;
253         u16 sc;
254         unsigned int frag, seq;
255         struct prism2_frag_entry *entry;
256
257         sc = le16_to_cpu(hdr->seq_ctl);
258         frag = WLAN_GET_SEQ_FRAG(sc);
259         seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
260
261         if (frag == 0) {
262                 /* Reserve enough space to fit maximum frame length */
263                 skb = dev_alloc_skb(local->dev->mtu +
264                                     sizeof(struct ieee80211_hdr_4addr) +
265                                     8 /* LLC */ +
266                                     2 /* alignment */ +
267                                     8 /* WEP */ + ETH_ALEN /* WDS */);
268                 if (skb == NULL)
269                         return NULL;
270
271                 entry = &local->frag_cache[local->frag_next_idx];
272                 local->frag_next_idx++;
273                 if (local->frag_next_idx >= PRISM2_FRAG_CACHE_LEN)
274                         local->frag_next_idx = 0;
275
276                 if (entry->skb != NULL)
277                         dev_kfree_skb(entry->skb);
278
279                 entry->first_frag_time = jiffies;
280                 entry->seq = seq;
281                 entry->last_frag = frag;
282                 entry->skb = skb;
283                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
284                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
285         } else {
286                 /* received a fragment of a frame for which the head fragment
287                  * should have already been received */
288                 entry = prism2_frag_cache_find(local, seq, frag, hdr->addr2,
289                                                hdr->addr1);
290                 if (entry != NULL) {
291                         entry->last_frag = frag;
292                         skb = entry->skb;
293                 }
294         }
295
296         return skb;
297 }
298
299
300 /* Called only as a tasklet (software IRQ) */
301 static int prism2_frag_cache_invalidate(local_info_t *local,
302                                         struct ieee80211_hdr_4addr *hdr)
303 {
304         u16 sc;
305         unsigned int seq;
306         struct prism2_frag_entry *entry;
307
308         sc = le16_to_cpu(hdr->seq_ctl);
309         seq = WLAN_GET_SEQ_SEQ(sc) >> 4;
310
311         entry = prism2_frag_cache_find(local, seq, -1, hdr->addr2, hdr->addr1);
312
313         if (entry == NULL) {
314                 printk(KERN_DEBUG "%s: could not invalidate fragment cache "
315                        "entry (seq=%u)\n",
316                        local->dev->name, seq);
317                 return -1;
318         }
319
320         entry->skb = NULL;
321         return 0;
322 }
323
324
325 static struct hostap_bss_info *__hostap_get_bss(local_info_t *local, u8 *bssid,
326                                                 u8 *ssid, size_t ssid_len)
327 {
328         struct list_head *ptr;
329         struct hostap_bss_info *bss;
330
331         list_for_each(ptr, &local->bss_list) {
332                 bss = list_entry(ptr, struct hostap_bss_info, list);
333                 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
334                     (ssid == NULL ||
335                      (ssid_len == bss->ssid_len &&
336                       memcmp(ssid, bss->ssid, ssid_len) == 0))) {
337                         list_move(&bss->list, &local->bss_list);
338                         return bss;
339                 }
340         }
341
342         return NULL;
343 }
344
345
346 static struct hostap_bss_info *__hostap_add_bss(local_info_t *local, u8 *bssid,
347                                                 u8 *ssid, size_t ssid_len)
348 {
349         struct hostap_bss_info *bss;
350
351         if (local->num_bss_info >= HOSTAP_MAX_BSS_COUNT) {
352                 bss = list_entry(local->bss_list.prev,
353                                  struct hostap_bss_info, list);
354                 list_del(&bss->list);
355                 local->num_bss_info--;
356         } else {
357                 bss = (struct hostap_bss_info *)
358                         kmalloc(sizeof(*bss), GFP_ATOMIC);
359                 if (bss == NULL)
360                         return NULL;
361         }
362
363         memset(bss, 0, sizeof(*bss));
364         memcpy(bss->bssid, bssid, ETH_ALEN);
365         memcpy(bss->ssid, ssid, ssid_len);
366         bss->ssid_len = ssid_len;
367         local->num_bss_info++;
368         list_add(&bss->list, &local->bss_list);
369         return bss;
370 }
371
372
373 static void __hostap_expire_bss(local_info_t *local)
374 {
375         struct hostap_bss_info *bss;
376
377         while (local->num_bss_info > 0) {
378                 bss = list_entry(local->bss_list.prev,
379                                  struct hostap_bss_info, list);
380                 if (!time_after(jiffies, bss->last_update + 60 * HZ))
381                         break;
382
383                 list_del(&bss->list);
384                 local->num_bss_info--;
385                 kfree(bss);
386         }
387 }
388
389
390 /* Both IEEE 802.11 Beacon and Probe Response frames have similar structure, so
391  * the same routine can be used to parse both of them. */
392 static void hostap_rx_sta_beacon(local_info_t *local, struct sk_buff *skb,
393                                  int stype)
394 {
395         struct hostap_ieee80211_mgmt *mgmt;
396         int left, chan = 0;
397         u8 *pos;
398         u8 *ssid = NULL, *wpa = NULL, *rsn = NULL;
399         size_t ssid_len = 0, wpa_len = 0, rsn_len = 0;
400         struct hostap_bss_info *bss;
401
402         if (skb->len < IEEE80211_MGMT_HDR_LEN + sizeof(mgmt->u.beacon))
403                 return;
404
405         mgmt = (struct hostap_ieee80211_mgmt *) skb->data;
406         pos = mgmt->u.beacon.variable;
407         left = skb->len - (pos - skb->data);
408
409         while (left >= 2) {
410                 if (2 + pos[1] > left)
411                         return; /* parse failed */
412                 switch (*pos) {
413                 case WLAN_EID_SSID:
414                         ssid = pos + 2;
415                         ssid_len = pos[1];
416                         break;
417                 case WLAN_EID_GENERIC:
418                         if (pos[1] >= 4 &&
419                             pos[2] == 0x00 && pos[3] == 0x50 &&
420                             pos[4] == 0xf2 && pos[5] == 1) {
421                                 wpa = pos;
422                                 wpa_len = pos[1] + 2;
423                         }
424                         break;
425                 case WLAN_EID_RSN:
426                         rsn = pos;
427                         rsn_len = pos[1] + 2;
428                         break;
429                 case WLAN_EID_DS_PARAMS:
430                         if (pos[1] >= 1)
431                                 chan = pos[2];
432                         break;
433                 }
434                 left -= 2 + pos[1];
435                 pos += 2 + pos[1];
436         }
437
438         if (wpa_len > MAX_WPA_IE_LEN)
439                 wpa_len = MAX_WPA_IE_LEN;
440         if (rsn_len > MAX_WPA_IE_LEN)
441                 rsn_len = MAX_WPA_IE_LEN;
442         if (ssid_len > sizeof(bss->ssid))
443                 ssid_len = sizeof(bss->ssid);
444
445         spin_lock(&local->lock);
446         bss = __hostap_get_bss(local, mgmt->bssid, ssid, ssid_len);
447         if (bss == NULL)
448                 bss = __hostap_add_bss(local, mgmt->bssid, ssid, ssid_len);
449         if (bss) {
450                 bss->last_update = jiffies;
451                 bss->count++;
452                 bss->capab_info = le16_to_cpu(mgmt->u.beacon.capab_info);
453                 if (wpa) {
454                         memcpy(bss->wpa_ie, wpa, wpa_len);
455                         bss->wpa_ie_len = wpa_len;
456                 } else
457                         bss->wpa_ie_len = 0;
458                 if (rsn) {
459                         memcpy(bss->rsn_ie, rsn, rsn_len);
460                         bss->rsn_ie_len = rsn_len;
461                 } else
462                         bss->rsn_ie_len = 0;
463                 bss->chan = chan;
464         }
465         __hostap_expire_bss(local);
466         spin_unlock(&local->lock);
467 }
468
469
470 static int
471 hostap_rx_frame_mgmt(local_info_t *local, struct sk_buff *skb,
472                      struct hostap_80211_rx_status *rx_stats, u16 type,
473                      u16 stype)
474 {
475         if (local->iw_mode == IW_MODE_MASTER) {
476                 hostap_update_sta_ps(local, (struct ieee80211_hdr_4addr *)
477                                      skb->data);
478         }
479
480         if (local->hostapd && type == IEEE80211_FTYPE_MGMT) {
481                 if (stype == IEEE80211_STYPE_BEACON &&
482                     local->iw_mode == IW_MODE_MASTER) {
483                         struct sk_buff *skb2;
484                         /* Process beacon frames also in kernel driver to
485                          * update STA(AP) table statistics */
486                         skb2 = skb_clone(skb, GFP_ATOMIC);
487                         if (skb2)
488                                 hostap_rx(skb2->dev, skb2, rx_stats);
489                 }
490
491                 /* send management frames to the user space daemon for
492                  * processing */
493                 local->apdevstats.rx_packets++;
494                 local->apdevstats.rx_bytes += skb->len;
495                 if (local->apdev == NULL)
496                         return -1;
497                 prism2_rx_80211(local->apdev, skb, rx_stats, PRISM2_RX_MGMT);
498                 return 0;
499         }
500
501         if (local->iw_mode == IW_MODE_MASTER) {
502                 if (type != IEEE80211_FTYPE_MGMT &&
503                     type != IEEE80211_FTYPE_CTL) {
504                         printk(KERN_DEBUG "%s: unknown management frame "
505                                "(type=0x%02x, stype=0x%02x) dropped\n",
506                                skb->dev->name, type >> 2, stype >> 4);
507                         return -1;
508                 }
509
510                 hostap_rx(skb->dev, skb, rx_stats);
511                 return 0;
512         } else if (type == IEEE80211_FTYPE_MGMT &&
513                    (stype == IEEE80211_STYPE_BEACON ||
514                     stype == IEEE80211_STYPE_PROBE_RESP)) {
515                 hostap_rx_sta_beacon(local, skb, stype);
516                 return -1;
517         } else if (type == IEEE80211_FTYPE_MGMT &&
518                    (stype == IEEE80211_STYPE_ASSOC_RESP ||
519                     stype == IEEE80211_STYPE_REASSOC_RESP)) {
520                 /* Ignore (Re)AssocResp silently since these are not currently
521                  * needed but are still received when WPA/RSN mode is enabled.
522                  */
523                 return -1;
524         } else {
525                 printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: dropped unhandled"
526                        " management frame in non-Host AP mode (type=%d:%d)\n",
527                        skb->dev->name, type >> 2, stype >> 4);
528                 return -1;
529         }
530 }
531
532
533 /* Called only as a tasklet (software IRQ) */
534 static struct net_device *prism2_rx_get_wds(local_info_t *local,
535                                                    u8 *addr)
536 {
537         struct hostap_interface *iface = NULL;
538         struct list_head *ptr;
539
540         read_lock_bh(&local->iface_lock);
541         list_for_each(ptr, &local->hostap_interfaces) {
542                 iface = list_entry(ptr, struct hostap_interface, list);
543                 if (iface->type == HOSTAP_INTERFACE_WDS &&
544                     memcmp(iface->u.wds.remote_addr, addr, ETH_ALEN) == 0)
545                         break;
546                 iface = NULL;
547         }
548         read_unlock_bh(&local->iface_lock);
549
550         return iface ? iface->dev : NULL;
551 }
552
553
554 static int
555 hostap_rx_frame_wds(local_info_t *local, struct ieee80211_hdr_4addr *hdr,
556                     u16 fc, struct net_device **wds)
557 {
558         /* FIX: is this really supposed to accept WDS frames only in Master
559          * mode? What about Repeater or Managed with WDS frames? */
560         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) !=
561             (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS) &&
562             (local->iw_mode != IW_MODE_MASTER || !(fc & IEEE80211_FCTL_TODS)))
563                 return 0; /* not a WDS frame */
564
565         /* Possible WDS frame: either IEEE 802.11 compliant (if FromDS)
566          * or own non-standard frame with 4th address after payload */
567         if (memcmp(hdr->addr1, local->dev->dev_addr, ETH_ALEN) != 0 &&
568             (hdr->addr1[0] != 0xff || hdr->addr1[1] != 0xff ||
569              hdr->addr1[2] != 0xff || hdr->addr1[3] != 0xff ||
570              hdr->addr1[4] != 0xff || hdr->addr1[5] != 0xff)) {
571                 /* RA (or BSSID) is not ours - drop */
572                 PDEBUG(DEBUG_EXTRA2, "%s: received WDS frame with "
573                        "not own or broadcast %s=%pM\n",
574                        local->dev->name,
575                        fc & IEEE80211_FCTL_FROMDS ? "RA" : "BSSID",
576                        hdr->addr1);
577                 return -1;
578         }
579
580         /* check if the frame came from a registered WDS connection */
581         *wds = prism2_rx_get_wds(local, hdr->addr2);
582         if (*wds == NULL && fc & IEEE80211_FCTL_FROMDS &&
583             (local->iw_mode != IW_MODE_INFRA ||
584              !(local->wds_type & HOSTAP_WDS_AP_CLIENT) ||
585              memcmp(hdr->addr2, local->bssid, ETH_ALEN) != 0)) {
586                 /* require that WDS link has been registered with TA or the
587                  * frame is from current AP when using 'AP client mode' */
588                 PDEBUG(DEBUG_EXTRA, "%s: received WDS[4 addr] frame "
589                        "from unknown TA=%pM\n",
590                        local->dev->name, hdr->addr2);
591                 if (local->ap && local->ap->autom_ap_wds)
592                         hostap_wds_link_oper(local, hdr->addr2, WDS_ADD);
593                 return -1;
594         }
595
596         if (*wds && !(fc & IEEE80211_FCTL_FROMDS) && local->ap &&
597             hostap_is_sta_assoc(local->ap, hdr->addr2)) {
598                 /* STA is actually associated with us even though it has a
599                  * registered WDS link. Assume it is in 'AP client' mode.
600                  * Since this is a 3-addr frame, assume it is not (bogus) WDS
601                  * frame and process it like any normal ToDS frame from
602                  * associated STA. */
603                 *wds = NULL;
604         }
605
606         return 0;
607 }
608
609
610 static int hostap_is_eapol_frame(local_info_t *local, struct sk_buff *skb)
611 {
612         struct net_device *dev = local->dev;
613         u16 fc, ethertype;
614         struct ieee80211_hdr_4addr *hdr;
615         u8 *pos;
616
617         if (skb->len < 24)
618                 return 0;
619
620         hdr = (struct ieee80211_hdr_4addr *) skb->data;
621         fc = le16_to_cpu(hdr->frame_ctl);
622
623         /* check that the frame is unicast frame to us */
624         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
625             IEEE80211_FCTL_TODS &&
626             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
627             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
628                 /* ToDS frame with own addr BSSID and DA */
629         } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
630                    IEEE80211_FCTL_FROMDS &&
631                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
632                 /* FromDS frame with own addr as DA */
633         } else
634                 return 0;
635
636         if (skb->len < 24 + 8)
637                 return 0;
638
639         /* check for port access entity Ethernet type */
640         pos = skb->data + 24;
641         ethertype = (pos[6] << 8) | pos[7];
642         if (ethertype == ETH_P_PAE)
643                 return 1;
644
645         return 0;
646 }
647
648
649 /* Called only as a tasklet (software IRQ) */
650 static int
651 hostap_rx_frame_decrypt(local_info_t *local, struct sk_buff *skb,
652                         struct lib80211_crypt_data *crypt)
653 {
654         struct ieee80211_hdr_4addr *hdr;
655         int res, hdrlen;
656
657         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
658                 return 0;
659
660         hdr = (struct ieee80211_hdr_4addr *) skb->data;
661         hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
662
663         if (local->tkip_countermeasures &&
664             strcmp(crypt->ops->name, "TKIP") == 0) {
665                 if (net_ratelimit()) {
666                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
667                                "received packet from %pM\n",
668                                local->dev->name, hdr->addr2);
669                 }
670                 return -1;
671         }
672
673         atomic_inc(&crypt->refcnt);
674         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
675         atomic_dec(&crypt->refcnt);
676         if (res < 0) {
677                 printk(KERN_DEBUG "%s: decryption failed (SA=%pM) res=%d\n",
678                        local->dev->name, hdr->addr2, res);
679                 local->comm_tallies.rx_discards_wep_undecryptable++;
680                 return -1;
681         }
682
683         return res;
684 }
685
686
687 /* Called only as a tasklet (software IRQ) */
688 static int
689 hostap_rx_frame_decrypt_msdu(local_info_t *local, struct sk_buff *skb,
690                              int keyidx, struct lib80211_crypt_data *crypt)
691 {
692         struct ieee80211_hdr_4addr *hdr;
693         int res, hdrlen;
694
695         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
696                 return 0;
697
698         hdr = (struct ieee80211_hdr_4addr *) skb->data;
699         hdrlen = hostap_80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
700
701         atomic_inc(&crypt->refcnt);
702         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
703         atomic_dec(&crypt->refcnt);
704         if (res < 0) {
705                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
706                        " (SA=%pM keyidx=%d)\n",
707                        local->dev->name, hdr->addr2, keyidx);
708                 return -1;
709         }
710
711         return 0;
712 }
713
714
715 /* All received frames are sent to this function. @skb contains the frame in
716  * IEEE 802.11 format, i.e., in the format it was sent over air.
717  * This function is called only as a tasklet (software IRQ). */
718 void hostap_80211_rx(struct net_device *dev, struct sk_buff *skb,
719                      struct hostap_80211_rx_status *rx_stats)
720 {
721         struct hostap_interface *iface;
722         local_info_t *local;
723         struct ieee80211_hdr_4addr *hdr;
724         size_t hdrlen;
725         u16 fc, type, stype, sc;
726         struct net_device *wds = NULL;
727         struct net_device_stats *stats;
728         unsigned int frag;
729         u8 *payload;
730         struct sk_buff *skb2 = NULL;
731         u16 ethertype;
732         int frame_authorized = 0;
733         int from_assoc_ap = 0;
734         u8 dst[ETH_ALEN];
735         u8 src[ETH_ALEN];
736         struct lib80211_crypt_data *crypt = NULL;
737         void *sta = NULL;
738         int keyidx = 0;
739
740         iface = netdev_priv(dev);
741         local = iface->local;
742         iface->stats.rx_packets++;
743         iface->stats.rx_bytes += skb->len;
744
745         /* dev is the master radio device; change this to be the default
746          * virtual interface (this may be changed to WDS device below) */
747         dev = local->ddev;
748         iface = netdev_priv(dev);
749
750         hdr = (struct ieee80211_hdr_4addr *) skb->data;
751         stats = hostap_get_stats(dev);
752
753         if (skb->len < 10)
754                 goto rx_dropped;
755
756         fc = le16_to_cpu(hdr->frame_ctl);
757         type = WLAN_FC_GET_TYPE(fc);
758         stype = WLAN_FC_GET_STYPE(fc);
759         sc = le16_to_cpu(hdr->seq_ctl);
760         frag = WLAN_GET_SEQ_FRAG(sc);
761         hdrlen = hostap_80211_get_hdrlen(fc);
762
763         /* Put this code here so that we avoid duplicating it in all
764          * Rx paths. - Jean II */
765 #ifdef IW_WIRELESS_SPY          /* defined in iw_handler.h */
766         /* If spy monitoring on */
767         if (iface->spy_data.spy_number > 0) {
768                 struct iw_quality wstats;
769                 wstats.level = rx_stats->signal;
770                 wstats.noise = rx_stats->noise;
771                 wstats.updated = IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_UPDATED
772                         | IW_QUAL_QUAL_INVALID | IW_QUAL_DBM;
773                 /* Update spy records */
774                 wireless_spy_update(dev, hdr->addr2, &wstats);
775         }
776 #endif /* IW_WIRELESS_SPY */
777         hostap_update_rx_stats(local->ap, hdr, rx_stats);
778
779         if (local->iw_mode == IW_MODE_MONITOR) {
780                 monitor_rx(dev, skb, rx_stats);
781                 return;
782         }
783
784         if (local->host_decrypt) {
785                 int idx = 0;
786                 if (skb->len >= hdrlen + 3)
787                         idx = skb->data[hdrlen + 3] >> 6;
788                 crypt = local->crypt_info.crypt[idx];
789                 sta = NULL;
790
791                 /* Use station specific key to override default keys if the
792                  * receiver address is a unicast address ("individual RA"). If
793                  * bcrx_sta_key parameter is set, station specific key is used
794                  * even with broad/multicast targets (this is against IEEE
795                  * 802.11, but makes it easier to use different keys with
796                  * stations that do not support WEP key mapping). */
797
798                 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
799                         (void) hostap_handle_sta_crypto(local, hdr, &crypt,
800                                                         &sta);
801
802                 /* allow NULL decrypt to indicate an station specific override
803                  * for default encryption */
804                 if (crypt && (crypt->ops == NULL ||
805                               crypt->ops->decrypt_mpdu == NULL))
806                         crypt = NULL;
807
808                 if (!crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
809 #if 0
810                         /* This seems to be triggered by some (multicast?)
811                          * frames from other than current BSS, so just drop the
812                          * frames silently instead of filling system log with
813                          * these reports. */
814                         printk(KERN_DEBUG "%s: WEP decryption failed (not set)"
815                                " (SA=%pM)\n",
816                                local->dev->name, hdr->addr2);
817 #endif
818                         local->comm_tallies.rx_discards_wep_undecryptable++;
819                         goto rx_dropped;
820                 }
821         }
822
823         if (type != IEEE80211_FTYPE_DATA) {
824                 if (type == IEEE80211_FTYPE_MGMT &&
825                     stype == IEEE80211_STYPE_AUTH &&
826                     fc & IEEE80211_FCTL_PROTECTED && local->host_decrypt &&
827                     (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
828                 {
829                         printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
830                                "from %pM\n", dev->name, hdr->addr2);
831                         /* TODO: could inform hostapd about this so that it
832                          * could send auth failure report */
833                         goto rx_dropped;
834                 }
835
836                 if (hostap_rx_frame_mgmt(local, skb, rx_stats, type, stype))
837                         goto rx_dropped;
838                 else
839                         goto rx_exit;
840         }
841
842         /* Data frame - extract src/dst addresses */
843         if (skb->len < IEEE80211_DATA_HDR3_LEN)
844                 goto rx_dropped;
845
846         switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
847         case IEEE80211_FCTL_FROMDS:
848                 memcpy(dst, hdr->addr1, ETH_ALEN);
849                 memcpy(src, hdr->addr3, ETH_ALEN);
850                 break;
851         case IEEE80211_FCTL_TODS:
852                 memcpy(dst, hdr->addr3, ETH_ALEN);
853                 memcpy(src, hdr->addr2, ETH_ALEN);
854                 break;
855         case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
856                 if (skb->len < IEEE80211_DATA_HDR4_LEN)
857                         goto rx_dropped;
858                 memcpy(dst, hdr->addr3, ETH_ALEN);
859                 memcpy(src, hdr->addr4, ETH_ALEN);
860                 break;
861         case 0:
862                 memcpy(dst, hdr->addr1, ETH_ALEN);
863                 memcpy(src, hdr->addr2, ETH_ALEN);
864                 break;
865         }
866
867         if (hostap_rx_frame_wds(local, hdr, fc, &wds))
868                 goto rx_dropped;
869         if (wds) {
870                 skb->dev = dev = wds;
871                 stats = hostap_get_stats(dev);
872         }
873
874         if (local->iw_mode == IW_MODE_MASTER && !wds &&
875             (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
876             IEEE80211_FCTL_FROMDS &&
877             local->stadev &&
878             memcmp(hdr->addr2, local->assoc_ap_addr, ETH_ALEN) == 0) {
879                 /* Frame from BSSID of the AP for which we are a client */
880                 skb->dev = dev = local->stadev;
881                 stats = hostap_get_stats(dev);
882                 from_assoc_ap = 1;
883         }
884
885         if ((local->iw_mode == IW_MODE_MASTER ||
886              local->iw_mode == IW_MODE_REPEAT) &&
887             !from_assoc_ap) {
888                 switch (hostap_handle_sta_rx(local, dev, skb, rx_stats,
889                                              wds != NULL)) {
890                 case AP_RX_CONTINUE_NOT_AUTHORIZED:
891                         frame_authorized = 0;
892                         break;
893                 case AP_RX_CONTINUE:
894                         frame_authorized = 1;
895                         break;
896                 case AP_RX_DROP:
897                         goto rx_dropped;
898                 case AP_RX_EXIT:
899                         goto rx_exit;
900                 }
901         }
902
903         /* Nullfunc frames may have PS-bit set, so they must be passed to
904          * hostap_handle_sta_rx() before being dropped here. */
905         if (stype != IEEE80211_STYPE_DATA &&
906             stype != IEEE80211_STYPE_DATA_CFACK &&
907             stype != IEEE80211_STYPE_DATA_CFPOLL &&
908             stype != IEEE80211_STYPE_DATA_CFACKPOLL) {
909                 if (stype != IEEE80211_STYPE_NULLFUNC)
910                         printk(KERN_DEBUG "%s: RX: dropped data frame "
911                                "with no data (type=0x%02x, subtype=0x%02x)\n",
912                                dev->name, type >> 2, stype >> 4);
913                 goto rx_dropped;
914         }
915
916         /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
917
918         if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
919             (keyidx = hostap_rx_frame_decrypt(local, skb, crypt)) < 0)
920                 goto rx_dropped;
921         hdr = (struct ieee80211_hdr_4addr *) skb->data;
922
923         /* skb: hdr + (possibly fragmented) plaintext payload */
924
925         if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
926             (frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
927                 int flen;
928                 struct sk_buff *frag_skb =
929                         prism2_frag_cache_get(local, hdr);
930                 if (!frag_skb) {
931                         printk(KERN_DEBUG "%s: Rx cannot get skb from "
932                                "fragment cache (morefrag=%d seq=%u frag=%u)\n",
933                                dev->name, (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
934                                WLAN_GET_SEQ_SEQ(sc) >> 4, frag);
935                         goto rx_dropped;
936                 }
937
938                 flen = skb->len;
939                 if (frag != 0)
940                         flen -= hdrlen;
941
942                 if (frag_skb->tail + flen > frag_skb->end) {
943                         printk(KERN_WARNING "%s: host decrypted and "
944                                "reassembled frame did not fit skb\n",
945                                dev->name);
946                         prism2_frag_cache_invalidate(local, hdr);
947                         goto rx_dropped;
948                 }
949
950                 if (frag == 0) {
951                         /* copy first fragment (including full headers) into
952                          * beginning of the fragment cache skb */
953                         skb_copy_from_linear_data(skb, skb_put(frag_skb, flen),
954                                                   flen);
955                 } else {
956                         /* append frame payload to the end of the fragment
957                          * cache skb */
958                         skb_copy_from_linear_data_offset(skb, hdrlen,
959                                                          skb_put(frag_skb,
960                                                                  flen), flen);
961                 }
962                 dev_kfree_skb(skb);
963                 skb = NULL;
964
965                 if (fc & IEEE80211_FCTL_MOREFRAGS) {
966                         /* more fragments expected - leave the skb in fragment
967                          * cache for now; it will be delivered to upper layers
968                          * after all fragments have been received */
969                         goto rx_exit;
970                 }
971
972                 /* this was the last fragment and the frame will be
973                  * delivered, so remove skb from fragment cache */
974                 skb = frag_skb;
975                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
976                 prism2_frag_cache_invalidate(local, hdr);
977         }
978
979         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
980          * encrypted/authenticated */
981
982         if (local->host_decrypt && (fc & IEEE80211_FCTL_PROTECTED) &&
983             hostap_rx_frame_decrypt_msdu(local, skb, keyidx, crypt))
984                 goto rx_dropped;
985
986         hdr = (struct ieee80211_hdr_4addr *) skb->data;
987         if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !local->open_wep) {
988                 if (local->ieee_802_1x &&
989                     hostap_is_eapol_frame(local, skb)) {
990                         /* pass unencrypted EAPOL frames even if encryption is
991                          * configured */
992                         PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X - passing "
993                                "unencrypted EAPOL frame\n", local->dev->name);
994                 } else {
995                         printk(KERN_DEBUG "%s: encryption configured, but RX "
996                                "frame not encrypted (SA=%pM)\n",
997                                local->dev->name, hdr->addr2);
998                         goto rx_dropped;
999                 }
1000         }
1001
1002         if (local->drop_unencrypted && !(fc & IEEE80211_FCTL_PROTECTED) &&
1003             !hostap_is_eapol_frame(local, skb)) {
1004                 if (net_ratelimit()) {
1005                         printk(KERN_DEBUG "%s: dropped unencrypted RX data "
1006                                "frame from %pM (drop_unencrypted=1)\n",
1007                                dev->name, hdr->addr2);
1008                 }
1009                 goto rx_dropped;
1010         }
1011
1012         /* skb: hdr + (possible reassembled) full plaintext payload */
1013
1014         payload = skb->data + hdrlen;
1015         ethertype = (payload[6] << 8) | payload[7];
1016
1017         /* If IEEE 802.1X is used, check whether the port is authorized to send
1018          * the received frame. */
1019         if (local->ieee_802_1x && local->iw_mode == IW_MODE_MASTER) {
1020                 if (ethertype == ETH_P_PAE) {
1021                         PDEBUG(DEBUG_EXTRA2, "%s: RX: IEEE 802.1X frame\n",
1022                                dev->name);
1023                         if (local->hostapd && local->apdev) {
1024                                 /* Send IEEE 802.1X frames to the user
1025                                  * space daemon for processing */
1026                                 prism2_rx_80211(local->apdev, skb, rx_stats,
1027                                                 PRISM2_RX_MGMT);
1028                                 local->apdevstats.rx_packets++;
1029                                 local->apdevstats.rx_bytes += skb->len;
1030                                 goto rx_exit;
1031                         }
1032                 } else if (!frame_authorized) {
1033                         printk(KERN_DEBUG "%s: dropped frame from "
1034                                "unauthorized port (IEEE 802.1X): "
1035                                "ethertype=0x%04x\n",
1036                                dev->name, ethertype);
1037                         goto rx_dropped;
1038                 }
1039         }
1040
1041         /* convert hdr + possible LLC headers into Ethernet header */
1042         if (skb->len - hdrlen >= 8 &&
1043             ((memcmp(payload, rfc1042_header, 6) == 0 &&
1044               ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1045              memcmp(payload, bridge_tunnel_header, 6) == 0)) {
1046                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
1047                  * replace EtherType */
1048                 skb_pull(skb, hdrlen + 6);
1049                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1050                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1051         } else {
1052                 __be16 len;
1053                 /* Leave Ethernet header part of hdr and full payload */
1054                 skb_pull(skb, hdrlen);
1055                 len = htons(skb->len);
1056                 memcpy(skb_push(skb, 2), &len, 2);
1057                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
1058                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
1059         }
1060
1061         if (wds && ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
1062                     IEEE80211_FCTL_TODS) &&
1063             skb->len >= ETH_HLEN + ETH_ALEN) {
1064                 /* Non-standard frame: get addr4 from its bogus location after
1065                  * the payload */
1066                 skb_copy_from_linear_data_offset(skb, skb->len - ETH_ALEN,
1067                                                  skb->data + ETH_ALEN,
1068                                                  ETH_ALEN);
1069                 skb_trim(skb, skb->len - ETH_ALEN);
1070         }
1071
1072         stats->rx_packets++;
1073         stats->rx_bytes += skb->len;
1074
1075         if (local->iw_mode == IW_MODE_MASTER && !wds &&
1076             local->ap->bridge_packets) {
1077                 if (dst[0] & 0x01) {
1078                         /* copy multicast frame both to the higher layers and
1079                          * to the wireless media */
1080                         local->ap->bridged_multicast++;
1081                         skb2 = skb_clone(skb, GFP_ATOMIC);
1082                         if (skb2 == NULL)
1083                                 printk(KERN_DEBUG "%s: skb_clone failed for "
1084                                        "multicast frame\n", dev->name);
1085                 } else if (hostap_is_sta_authorized(local->ap, dst)) {
1086                         /* send frame directly to the associated STA using
1087                          * wireless media and not passing to higher layers */
1088                         local->ap->bridged_unicast++;
1089                         skb2 = skb;
1090                         skb = NULL;
1091                 }
1092         }
1093
1094         if (skb2 != NULL) {
1095                 /* send to wireless media */
1096                 skb2->dev = dev;
1097                 skb2->protocol = __constant_htons(ETH_P_802_3);
1098                 skb_reset_mac_header(skb2);
1099                 skb_reset_network_header(skb2);
1100                 /* skb2->network_header += ETH_HLEN; */
1101                 dev_queue_xmit(skb2);
1102         }
1103
1104         if (skb) {
1105                 skb->protocol = eth_type_trans(skb, dev);
1106                 memset(skb->cb, 0, sizeof(skb->cb));
1107                 netif_rx(skb);
1108         }
1109
1110  rx_exit:
1111         if (sta)
1112                 hostap_handle_sta_release(sta);
1113         return;
1114
1115  rx_dropped:
1116         dev_kfree_skb(skb);
1117
1118         stats->rx_dropped++;
1119         goto rx_exit;
1120 }
1121
1122
1123 EXPORT_SYMBOL(hostap_80211_rx);