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