Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / net / wireless / hostap / hostap_80211_tx.c
1 #include "hostap_80211.h"
2 #include "hostap_common.h"
3 #include "hostap_wlan.h"
4 #include "hostap.h"
5 #include "hostap_ap.h"
6
7 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
8 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
9 static unsigned char rfc1042_header[] =
10 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
11 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
12 static unsigned char bridge_tunnel_header[] =
13 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
14 /* No encapsulation header if EtherType < 0x600 (=length) */
15
16 void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
17 {
18         struct ieee80211_hdr_4addr *hdr;
19         u16 fc;
20
21         hdr = (struct ieee80211_hdr_4addr *) skb->data;
22
23         printk(KERN_DEBUG "%s: TX len=%d jiffies=%ld\n",
24                name, skb->len, jiffies);
25
26         if (skb->len < 2)
27                 return;
28
29         fc = le16_to_cpu(hdr->frame_ctl);
30         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d)%s%s",
31                fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
32                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
33                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
34
35         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
36                 printk("\n");
37                 return;
38         }
39
40         printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
41                le16_to_cpu(hdr->seq_ctl));
42
43         printk(KERN_DEBUG "   A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
44                MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
45         if (skb->len >= 30)
46                 printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
47         printk("\n");
48 }
49
50
51 /* hard_start_xmit function for data interfaces (wlan#, wlan#wds#, wlan#sta)
52  * Convert Ethernet header into a suitable IEEE 802.11 header depending on
53  * device configuration. */
54 int hostap_data_start_xmit(struct sk_buff *skb, struct net_device *dev)
55 {
56         struct hostap_interface *iface;
57         local_info_t *local;
58         int need_headroom, need_tailroom = 0;
59         struct ieee80211_hdr_4addr hdr;
60         u16 fc, ethertype = 0;
61         enum {
62                 WDS_NO = 0, WDS_OWN_FRAME, WDS_COMPLIANT_FRAME
63         } use_wds = WDS_NO;
64         u8 *encaps_data;
65         int hdr_len, encaps_len, skip_header_bytes;
66         int to_assoc_ap = 0;
67         struct hostap_skb_tx_data *meta;
68
69         iface = netdev_priv(dev);
70         local = iface->local;
71
72         if (skb->len < ETH_HLEN) {
73                 printk(KERN_DEBUG "%s: hostap_data_start_xmit: short skb "
74                        "(len=%d)\n", dev->name, skb->len);
75                 kfree_skb(skb);
76                 return 0;
77         }
78
79         if (local->ddev != dev) {
80                 use_wds = (local->iw_mode == IW_MODE_MASTER &&
81                            !(local->wds_type & HOSTAP_WDS_STANDARD_FRAME)) ?
82                         WDS_OWN_FRAME : WDS_COMPLIANT_FRAME;
83                 if (dev == local->stadev) {
84                         to_assoc_ap = 1;
85                         use_wds = WDS_NO;
86                 } else if (dev == local->apdev) {
87                         printk(KERN_DEBUG "%s: prism2_tx: trying to use "
88                                "AP device with Ethernet net dev\n", dev->name);
89                         kfree_skb(skb);
90                         return 0;
91                 }
92         } else {
93                 if (local->iw_mode == IW_MODE_REPEAT) {
94                         printk(KERN_DEBUG "%s: prism2_tx: trying to use "
95                                "non-WDS link in Repeater mode\n", dev->name);
96                         kfree_skb(skb);
97                         return 0;
98                 } else if (local->iw_mode == IW_MODE_INFRA &&
99                            (local->wds_type & HOSTAP_WDS_AP_CLIENT) &&
100                            memcmp(skb->data + ETH_ALEN, dev->dev_addr,
101                                   ETH_ALEN) != 0) {
102                         /* AP client mode: send frames with foreign src addr
103                          * using 4-addr WDS frames */
104                         use_wds = WDS_COMPLIANT_FRAME;
105                 }
106         }
107
108         /* Incoming skb->data: dst_addr[6], src_addr[6], proto[2], payload
109          * ==>
110          * Prism2 TX frame with 802.11 header:
111          * txdesc (address order depending on used mode; includes dst_addr and
112          * src_addr), possible encapsulation (RFC1042/Bridge-Tunnel;
113          * proto[2], payload {, possible addr4[6]} */
114
115         ethertype = (skb->data[12] << 8) | skb->data[13];
116
117         memset(&hdr, 0, sizeof(hdr));
118
119         /* Length of data after IEEE 802.11 header */
120         encaps_data = NULL;
121         encaps_len = 0;
122         skip_header_bytes = ETH_HLEN;
123         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
124                 encaps_data = bridge_tunnel_header;
125                 encaps_len = sizeof(bridge_tunnel_header);
126                 skip_header_bytes -= 2;
127         } else if (ethertype >= 0x600) {
128                 encaps_data = rfc1042_header;
129                 encaps_len = sizeof(rfc1042_header);
130                 skip_header_bytes -= 2;
131         }
132
133         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
134         hdr_len = IEEE80211_DATA_HDR3_LEN;
135
136         if (use_wds != WDS_NO) {
137                 /* Note! Prism2 station firmware has problems with sending real
138                  * 802.11 frames with four addresses; until these problems can
139                  * be fixed or worked around, 4-addr frames needed for WDS are
140                  * using incompatible format: FromDS flag is not set and the
141                  * fourth address is added after the frame payload; it is
142                  * assumed, that the receiving station knows how to handle this
143                  * frame format */
144
145                 if (use_wds == WDS_COMPLIANT_FRAME) {
146                         fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
147                         /* From&To DS: Addr1 = RA, Addr2 = TA, Addr3 = DA,
148                          * Addr4 = SA */
149                         memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
150                         hdr_len += ETH_ALEN;
151                 } else {
152                         /* bogus 4-addr format to workaround Prism2 station
153                          * f/w bug */
154                         fc |= IEEE80211_FCTL_TODS;
155                         /* From DS: Addr1 = DA (used as RA),
156                          * Addr2 = BSSID (used as TA), Addr3 = SA (used as DA),
157                          */
158
159                         /* SA from skb->data + ETH_ALEN will be added after
160                          * frame payload; use hdr.addr4 as a temporary buffer
161                          */
162                         memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
163                         need_tailroom += ETH_ALEN;
164                 }
165
166                 /* send broadcast and multicast frames to broadcast RA, if
167                  * configured; otherwise, use unicast RA of the WDS link */
168                 if ((local->wds_type & HOSTAP_WDS_BROADCAST_RA) &&
169                     skb->data[0] & 0x01)
170                         memset(&hdr.addr1, 0xff, ETH_ALEN);
171                 else if (iface->type == HOSTAP_INTERFACE_WDS)
172                         memcpy(&hdr.addr1, iface->u.wds.remote_addr,
173                                ETH_ALEN);
174                 else
175                         memcpy(&hdr.addr1, local->bssid, ETH_ALEN);
176                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
177                 memcpy(&hdr.addr3, skb->data, ETH_ALEN);
178         } else if (local->iw_mode == IW_MODE_MASTER && !to_assoc_ap) {
179                 fc |= IEEE80211_FCTL_FROMDS;
180                 /* From DS: Addr1 = DA, Addr2 = BSSID, Addr3 = SA */
181                 memcpy(&hdr.addr1, skb->data, ETH_ALEN);
182                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
183                 memcpy(&hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
184         } else if (local->iw_mode == IW_MODE_INFRA || to_assoc_ap) {
185                 fc |= IEEE80211_FCTL_TODS;
186                 /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
187                 memcpy(&hdr.addr1, to_assoc_ap ?
188                        local->assoc_ap_addr : local->bssid, ETH_ALEN);
189                 memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
190                 memcpy(&hdr.addr3, skb->data, ETH_ALEN);
191         } else if (local->iw_mode == IW_MODE_ADHOC) {
192                 /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
193                 memcpy(&hdr.addr1, skb->data, ETH_ALEN);
194                 memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
195                 memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
196         }
197
198         hdr.frame_ctl = cpu_to_le16(fc);
199
200         skb_pull(skb, skip_header_bytes);
201         need_headroom = local->func->need_tx_headroom + hdr_len + encaps_len;
202         if (skb_tailroom(skb) < need_tailroom) {
203                 skb = skb_unshare(skb, GFP_ATOMIC);
204                 if (skb == NULL) {
205                         iface->stats.tx_dropped++;
206                         return 0;
207                 }
208                 if (pskb_expand_head(skb, need_headroom, need_tailroom,
209                                      GFP_ATOMIC)) {
210                         kfree_skb(skb);
211                         iface->stats.tx_dropped++;
212                         return 0;
213                 }
214         } else if (skb_headroom(skb) < need_headroom) {
215                 struct sk_buff *tmp = skb;
216                 skb = skb_realloc_headroom(skb, need_headroom);
217                 kfree_skb(tmp);
218                 if (skb == NULL) {
219                         iface->stats.tx_dropped++;
220                         return 0;
221                 }
222         } else {
223                 skb = skb_unshare(skb, GFP_ATOMIC);
224                 if (skb == NULL) {
225                         iface->stats.tx_dropped++;
226                         return 0;
227                 }
228         }
229
230         if (encaps_data)
231                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
232         memcpy(skb_push(skb, hdr_len), &hdr, hdr_len);
233         if (use_wds == WDS_OWN_FRAME) {
234                 memcpy(skb_put(skb, ETH_ALEN), &hdr.addr4, ETH_ALEN);
235         }
236
237         iface->stats.tx_packets++;
238         iface->stats.tx_bytes += skb->len;
239
240         skb->mac.raw = skb->data;
241         meta = (struct hostap_skb_tx_data *) skb->cb;
242         memset(meta, 0, sizeof(*meta));
243         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
244         if (use_wds)
245                 meta->flags |= HOSTAP_TX_FLAGS_WDS;
246         meta->ethertype = ethertype;
247         meta->iface = iface;
248
249         /* Send IEEE 802.11 encapsulated frame using the master radio device */
250         skb->dev = local->dev;
251         dev_queue_xmit(skb);
252         return 0;
253 }
254
255
256 /* hard_start_xmit function for hostapd wlan#ap interfaces */
257 int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
258 {
259         struct hostap_interface *iface;
260         local_info_t *local;
261         struct hostap_skb_tx_data *meta;
262         struct ieee80211_hdr_4addr *hdr;
263         u16 fc;
264
265         iface = netdev_priv(dev);
266         local = iface->local;
267
268         if (skb->len < 10) {
269                 printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
270                        "(len=%d)\n", dev->name, skb->len);
271                 kfree_skb(skb);
272                 return 0;
273         }
274
275         iface->stats.tx_packets++;
276         iface->stats.tx_bytes += skb->len;
277
278         meta = (struct hostap_skb_tx_data *) skb->cb;
279         memset(meta, 0, sizeof(*meta));
280         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
281         meta->iface = iface;
282
283         if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
284                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
285                 fc = le16_to_cpu(hdr->frame_ctl);
286                 if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
287                     WLAN_FC_GET_STYPE(fc) == IEEE80211_STYPE_DATA) {
288                         u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
289                                              sizeof(rfc1042_header)];
290                         meta->ethertype = (pos[0] << 8) | pos[1];
291                 }
292         }
293
294         /* Send IEEE 802.11 encapsulated frame using the master radio device */
295         skb->dev = local->dev;
296         dev_queue_xmit(skb);
297         return 0;
298 }
299
300
301 /* Called only from software IRQ */
302 static struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
303                                           struct ieee80211_crypt_data *crypt)
304 {
305         struct hostap_interface *iface;
306         local_info_t *local;
307         struct ieee80211_hdr_4addr *hdr;
308         u16 fc;
309         int hdr_len, res;
310
311         iface = netdev_priv(skb->dev);
312         local = iface->local;
313
314         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
315                 kfree_skb(skb);
316                 return NULL;
317         }
318
319         if (local->tkip_countermeasures &&
320             strcmp(crypt->ops->name, "TKIP") == 0) {
321                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
322                 if (net_ratelimit()) {
323                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
324                                "TX packet to " MACSTR "\n",
325                                local->dev->name, MAC2STR(hdr->addr1));
326                 }
327                 kfree_skb(skb);
328                 return NULL;
329         }
330
331         skb = skb_unshare(skb, GFP_ATOMIC);
332         if (skb == NULL)
333                 return NULL;
334
335         if ((skb_headroom(skb) < crypt->ops->extra_mpdu_prefix_len ||
336              skb_tailroom(skb) < crypt->ops->extra_mpdu_postfix_len) &&
337             pskb_expand_head(skb, crypt->ops->extra_mpdu_prefix_len,
338                              crypt->ops->extra_mpdu_postfix_len, GFP_ATOMIC)) {
339                 kfree_skb(skb);
340                 return NULL;
341         }
342
343         hdr = (struct ieee80211_hdr_4addr *) skb->data;
344         fc = le16_to_cpu(hdr->frame_ctl);
345         hdr_len = hostap_80211_get_hdrlen(fc);
346
347         /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
348          * call both MSDU and MPDU encryption functions from here. */
349         atomic_inc(&crypt->refcnt);
350         res = 0;
351         if (crypt->ops->encrypt_msdu)
352                 res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
353         if (res == 0 && crypt->ops->encrypt_mpdu)
354                 res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
355         atomic_dec(&crypt->refcnt);
356         if (res < 0) {
357                 kfree_skb(skb);
358                 return NULL;
359         }
360
361         return skb;
362 }
363
364
365 /* hard_start_xmit function for master radio interface wifi#.
366  * AP processing (TX rate control, power save buffering, etc.).
367  * Use hardware TX function to send the frame. */
368 int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
369 {
370         struct hostap_interface *iface;
371         local_info_t *local;
372         int ret = 1;
373         u16 fc;
374         struct hostap_tx_data tx;
375         ap_tx_ret tx_ret;
376         struct hostap_skb_tx_data *meta;
377         int no_encrypt = 0;
378         struct ieee80211_hdr_4addr *hdr;
379
380         iface = netdev_priv(dev);
381         local = iface->local;
382
383         tx.skb = skb;
384         tx.sta_ptr = NULL;
385
386         meta = (struct hostap_skb_tx_data *) skb->cb;
387         if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
388                 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
389                        "expected 0x%08x)\n",
390                        dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
391                 ret = 0;
392                 iface->stats.tx_dropped++;
393                 goto fail;
394         }
395
396         if (local->host_encrypt) {
397                 /* Set crypt to default algorithm and key; will be replaced in
398                  * AP code if STA has own alg/key */
399                 tx.crypt = local->crypt[local->tx_keyidx];
400                 tx.host_encrypt = 1;
401         } else {
402                 tx.crypt = NULL;
403                 tx.host_encrypt = 0;
404         }
405
406         if (skb->len < 24) {
407                 printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
408                        "(len=%d)\n", dev->name, skb->len);
409                 ret = 0;
410                 iface->stats.tx_dropped++;
411                 goto fail;
412         }
413
414         /* FIX (?):
415          * Wi-Fi 802.11b test plan suggests that AP should ignore power save
416          * bit in authentication and (re)association frames and assume tha
417          * STA remains awake for the response. */
418         tx_ret = hostap_handle_sta_tx(local, &tx);
419         skb = tx.skb;
420         meta = (struct hostap_skb_tx_data *) skb->cb;
421         hdr = (struct ieee80211_hdr_4addr *) skb->data;
422         fc = le16_to_cpu(hdr->frame_ctl);
423         switch (tx_ret) {
424         case AP_TX_CONTINUE:
425                 break;
426         case AP_TX_CONTINUE_NOT_AUTHORIZED:
427                 if (local->ieee_802_1x &&
428                     WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
429                     meta->ethertype != ETH_P_PAE &&
430                     !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
431                         printk(KERN_DEBUG "%s: dropped frame to unauthorized "
432                                "port (IEEE 802.1X): ethertype=0x%04x\n",
433                                dev->name, meta->ethertype);
434                         hostap_dump_tx_80211(dev->name, skb);
435
436                         ret = 0; /* drop packet */
437                         iface->stats.tx_dropped++;
438                         goto fail;
439                 }
440                 break;
441         case AP_TX_DROP:
442                 ret = 0; /* drop packet */
443                 iface->stats.tx_dropped++;
444                 goto fail;
445         case AP_TX_RETRY:
446                 goto fail;
447         case AP_TX_BUFFERED:
448                 /* do not free skb here, it will be freed when the
449                  * buffered frame is sent/timed out */
450                 ret = 0;
451                 goto tx_exit;
452         }
453
454         /* Request TX callback if protocol version is 2 in 802.11 header;
455          * this version 2 is a special case used between hostapd and kernel
456          * driver */
457         if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
458             local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
459                 meta->tx_cb_idx = local->ap->tx_callback_idx;
460
461                 /* remove special version from the frame header */
462                 fc &= ~IEEE80211_FCTL_VERS;
463                 hdr->frame_ctl = cpu_to_le16(fc);
464         }
465
466         if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_DATA) {
467                 no_encrypt = 1;
468                 tx.crypt = NULL;
469         }
470
471         if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
472             !(fc & IEEE80211_FCTL_PROTECTED)) {
473                 no_encrypt = 1;
474                 PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
475                        "unencrypted EAPOL frame\n", dev->name);
476                 tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
477         }
478
479         if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
480                 tx.crypt = NULL;
481         else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
482                 /* Add ISWEP flag both for firmware and host based encryption
483                  */
484                 fc |= IEEE80211_FCTL_PROTECTED;
485                 hdr->frame_ctl = cpu_to_le16(fc);
486         } else if (local->drop_unencrypted &&
487                    WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
488                    meta->ethertype != ETH_P_PAE) {
489                 if (net_ratelimit()) {
490                         printk(KERN_DEBUG "%s: dropped unencrypted TX data "
491                                "frame (drop_unencrypted=1)\n", dev->name);
492                 }
493                 iface->stats.tx_dropped++;
494                 ret = 0;
495                 goto fail;
496         }
497
498         if (tx.crypt) {
499                 skb = hostap_tx_encrypt(skb, tx.crypt);
500                 if (skb == NULL) {
501                         printk(KERN_DEBUG "%s: TX - encryption failed\n",
502                                dev->name);
503                         ret = 0;
504                         goto fail;
505                 }
506                 meta = (struct hostap_skb_tx_data *) skb->cb;
507                 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
508                         printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
509                                "expected 0x%08x) after hostap_tx_encrypt\n",
510                                dev->name, meta->magic,
511                                HOSTAP_SKB_TX_DATA_MAGIC);
512                         ret = 0;
513                         iface->stats.tx_dropped++;
514                         goto fail;
515                 }
516         }
517
518         if (local->func->tx == NULL || local->func->tx(skb, dev)) {
519                 ret = 0;
520                 iface->stats.tx_dropped++;
521         } else {
522                 ret = 0;
523                 iface->stats.tx_packets++;
524                 iface->stats.tx_bytes += skb->len;
525         }
526
527  fail:
528         if (!ret && skb)
529                 dev_kfree_skb(skb);
530  tx_exit:
531         if (tx.sta_ptr)
532                 hostap_handle_sta_release(tx.sta_ptr);
533         return ret;
534 }
535
536
537 EXPORT_SYMBOL(hostap_master_start_xmit);