Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[powerpc.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                         skb_copy_from_linear_data_offset(skb, ETH_ALEN,
150                                                          &hdr.addr4, ETH_ALEN);
151                         hdr_len += ETH_ALEN;
152                 } else {
153                         /* bogus 4-addr format to workaround Prism2 station
154                          * f/w bug */
155                         fc |= IEEE80211_FCTL_TODS;
156                         /* From DS: Addr1 = DA (used as RA),
157                          * Addr2 = BSSID (used as TA), Addr3 = SA (used as DA),
158                          */
159
160                         /* SA from skb->data + ETH_ALEN will be added after
161                          * frame payload; use hdr.addr4 as a temporary buffer
162                          */
163                         skb_copy_from_linear_data_offset(skb, ETH_ALEN,
164                                                          &hdr.addr4, ETH_ALEN);
165                         need_tailroom += ETH_ALEN;
166                 }
167
168                 /* send broadcast and multicast frames to broadcast RA, if
169                  * configured; otherwise, use unicast RA of the WDS link */
170                 if ((local->wds_type & HOSTAP_WDS_BROADCAST_RA) &&
171                     skb->data[0] & 0x01)
172                         memset(&hdr.addr1, 0xff, ETH_ALEN);
173                 else if (iface->type == HOSTAP_INTERFACE_WDS)
174                         memcpy(&hdr.addr1, iface->u.wds.remote_addr,
175                                ETH_ALEN);
176                 else
177                         memcpy(&hdr.addr1, local->bssid, ETH_ALEN);
178                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
179                 skb_copy_from_linear_data(skb, &hdr.addr3, ETH_ALEN);
180         } else if (local->iw_mode == IW_MODE_MASTER && !to_assoc_ap) {
181                 fc |= IEEE80211_FCTL_FROMDS;
182                 /* From DS: Addr1 = DA, Addr2 = BSSID, Addr3 = SA */
183                 skb_copy_from_linear_data(skb, &hdr.addr1, ETH_ALEN);
184                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
185                 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr3,
186                                                  ETH_ALEN);
187         } else if (local->iw_mode == IW_MODE_INFRA || to_assoc_ap) {
188                 fc |= IEEE80211_FCTL_TODS;
189                 /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
190                 memcpy(&hdr.addr1, to_assoc_ap ?
191                        local->assoc_ap_addr : local->bssid, ETH_ALEN);
192                 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr2,
193                                                  ETH_ALEN);
194                 skb_copy_from_linear_data(skb, &hdr.addr3, ETH_ALEN);
195         } else if (local->iw_mode == IW_MODE_ADHOC) {
196                 /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
197                 skb_copy_from_linear_data(skb, &hdr.addr1, ETH_ALEN);
198                 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr2,
199                                                  ETH_ALEN);
200                 memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
201         }
202
203         hdr.frame_ctl = cpu_to_le16(fc);
204
205         skb_pull(skb, skip_header_bytes);
206         need_headroom = local->func->need_tx_headroom + hdr_len + encaps_len;
207         if (skb_tailroom(skb) < need_tailroom) {
208                 skb = skb_unshare(skb, GFP_ATOMIC);
209                 if (skb == NULL) {
210                         iface->stats.tx_dropped++;
211                         return 0;
212                 }
213                 if (pskb_expand_head(skb, need_headroom, need_tailroom,
214                                      GFP_ATOMIC)) {
215                         kfree_skb(skb);
216                         iface->stats.tx_dropped++;
217                         return 0;
218                 }
219         } else if (skb_headroom(skb) < need_headroom) {
220                 struct sk_buff *tmp = skb;
221                 skb = skb_realloc_headroom(skb, need_headroom);
222                 kfree_skb(tmp);
223                 if (skb == NULL) {
224                         iface->stats.tx_dropped++;
225                         return 0;
226                 }
227         } else {
228                 skb = skb_unshare(skb, GFP_ATOMIC);
229                 if (skb == NULL) {
230                         iface->stats.tx_dropped++;
231                         return 0;
232                 }
233         }
234
235         if (encaps_data)
236                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
237         memcpy(skb_push(skb, hdr_len), &hdr, hdr_len);
238         if (use_wds == WDS_OWN_FRAME) {
239                 memcpy(skb_put(skb, ETH_ALEN), &hdr.addr4, ETH_ALEN);
240         }
241
242         iface->stats.tx_packets++;
243         iface->stats.tx_bytes += skb->len;
244
245         skb_reset_mac_header(skb);
246         meta = (struct hostap_skb_tx_data *) skb->cb;
247         memset(meta, 0, sizeof(*meta));
248         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
249         if (use_wds)
250                 meta->flags |= HOSTAP_TX_FLAGS_WDS;
251         meta->ethertype = ethertype;
252         meta->iface = iface;
253
254         /* Send IEEE 802.11 encapsulated frame using the master radio device */
255         skb->dev = local->dev;
256         dev_queue_xmit(skb);
257         return 0;
258 }
259
260
261 /* hard_start_xmit function for hostapd wlan#ap interfaces */
262 int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
263 {
264         struct hostap_interface *iface;
265         local_info_t *local;
266         struct hostap_skb_tx_data *meta;
267         struct ieee80211_hdr_4addr *hdr;
268         u16 fc;
269
270         iface = netdev_priv(dev);
271         local = iface->local;
272
273         if (skb->len < 10) {
274                 printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
275                        "(len=%d)\n", dev->name, skb->len);
276                 kfree_skb(skb);
277                 return 0;
278         }
279
280         iface->stats.tx_packets++;
281         iface->stats.tx_bytes += skb->len;
282
283         meta = (struct hostap_skb_tx_data *) skb->cb;
284         memset(meta, 0, sizeof(*meta));
285         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
286         meta->iface = iface;
287
288         if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
289                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
290                 fc = le16_to_cpu(hdr->frame_ctl);
291                 if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
292                     WLAN_FC_GET_STYPE(fc) == IEEE80211_STYPE_DATA) {
293                         u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
294                                              sizeof(rfc1042_header)];
295                         meta->ethertype = (pos[0] << 8) | pos[1];
296                 }
297         }
298
299         /* Send IEEE 802.11 encapsulated frame using the master radio device */
300         skb->dev = local->dev;
301         dev_queue_xmit(skb);
302         return 0;
303 }
304
305
306 /* Called only from software IRQ */
307 static struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
308                                           struct ieee80211_crypt_data *crypt)
309 {
310         struct hostap_interface *iface;
311         local_info_t *local;
312         struct ieee80211_hdr_4addr *hdr;
313         u16 fc;
314         int hdr_len, res;
315
316         iface = netdev_priv(skb->dev);
317         local = iface->local;
318
319         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
320                 kfree_skb(skb);
321                 return NULL;
322         }
323
324         if (local->tkip_countermeasures &&
325             strcmp(crypt->ops->name, "TKIP") == 0) {
326                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
327                 if (net_ratelimit()) {
328                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
329                                "TX packet to " MACSTR "\n",
330                                local->dev->name, MAC2STR(hdr->addr1));
331                 }
332                 kfree_skb(skb);
333                 return NULL;
334         }
335
336         skb = skb_unshare(skb, GFP_ATOMIC);
337         if (skb == NULL)
338                 return NULL;
339
340         if ((skb_headroom(skb) < crypt->ops->extra_mpdu_prefix_len ||
341              skb_tailroom(skb) < crypt->ops->extra_mpdu_postfix_len) &&
342             pskb_expand_head(skb, crypt->ops->extra_mpdu_prefix_len,
343                              crypt->ops->extra_mpdu_postfix_len, GFP_ATOMIC)) {
344                 kfree_skb(skb);
345                 return NULL;
346         }
347
348         hdr = (struct ieee80211_hdr_4addr *) skb->data;
349         fc = le16_to_cpu(hdr->frame_ctl);
350         hdr_len = hostap_80211_get_hdrlen(fc);
351
352         /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
353          * call both MSDU and MPDU encryption functions from here. */
354         atomic_inc(&crypt->refcnt);
355         res = 0;
356         if (crypt->ops->encrypt_msdu)
357                 res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
358         if (res == 0 && crypt->ops->encrypt_mpdu)
359                 res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
360         atomic_dec(&crypt->refcnt);
361         if (res < 0) {
362                 kfree_skb(skb);
363                 return NULL;
364         }
365
366         return skb;
367 }
368
369
370 /* hard_start_xmit function for master radio interface wifi#.
371  * AP processing (TX rate control, power save buffering, etc.).
372  * Use hardware TX function to send the frame. */
373 int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
374 {
375         struct hostap_interface *iface;
376         local_info_t *local;
377         int ret = 1;
378         u16 fc;
379         struct hostap_tx_data tx;
380         ap_tx_ret tx_ret;
381         struct hostap_skb_tx_data *meta;
382         int no_encrypt = 0;
383         struct ieee80211_hdr_4addr *hdr;
384
385         iface = netdev_priv(dev);
386         local = iface->local;
387
388         tx.skb = skb;
389         tx.sta_ptr = NULL;
390
391         meta = (struct hostap_skb_tx_data *) skb->cb;
392         if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
393                 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
394                        "expected 0x%08x)\n",
395                        dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
396                 ret = 0;
397                 iface->stats.tx_dropped++;
398                 goto fail;
399         }
400
401         if (local->host_encrypt) {
402                 /* Set crypt to default algorithm and key; will be replaced in
403                  * AP code if STA has own alg/key */
404                 tx.crypt = local->crypt[local->tx_keyidx];
405                 tx.host_encrypt = 1;
406         } else {
407                 tx.crypt = NULL;
408                 tx.host_encrypt = 0;
409         }
410
411         if (skb->len < 24) {
412                 printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
413                        "(len=%d)\n", dev->name, skb->len);
414                 ret = 0;
415                 iface->stats.tx_dropped++;
416                 goto fail;
417         }
418
419         /* FIX (?):
420          * Wi-Fi 802.11b test plan suggests that AP should ignore power save
421          * bit in authentication and (re)association frames and assume tha
422          * STA remains awake for the response. */
423         tx_ret = hostap_handle_sta_tx(local, &tx);
424         skb = tx.skb;
425         meta = (struct hostap_skb_tx_data *) skb->cb;
426         hdr = (struct ieee80211_hdr_4addr *) skb->data;
427         fc = le16_to_cpu(hdr->frame_ctl);
428         switch (tx_ret) {
429         case AP_TX_CONTINUE:
430                 break;
431         case AP_TX_CONTINUE_NOT_AUTHORIZED:
432                 if (local->ieee_802_1x &&
433                     WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
434                     meta->ethertype != ETH_P_PAE &&
435                     !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
436                         printk(KERN_DEBUG "%s: dropped frame to unauthorized "
437                                "port (IEEE 802.1X): ethertype=0x%04x\n",
438                                dev->name, meta->ethertype);
439                         hostap_dump_tx_80211(dev->name, skb);
440
441                         ret = 0; /* drop packet */
442                         iface->stats.tx_dropped++;
443                         goto fail;
444                 }
445                 break;
446         case AP_TX_DROP:
447                 ret = 0; /* drop packet */
448                 iface->stats.tx_dropped++;
449                 goto fail;
450         case AP_TX_RETRY:
451                 goto fail;
452         case AP_TX_BUFFERED:
453                 /* do not free skb here, it will be freed when the
454                  * buffered frame is sent/timed out */
455                 ret = 0;
456                 goto tx_exit;
457         }
458
459         /* Request TX callback if protocol version is 2 in 802.11 header;
460          * this version 2 is a special case used between hostapd and kernel
461          * driver */
462         if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
463             local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
464                 meta->tx_cb_idx = local->ap->tx_callback_idx;
465
466                 /* remove special version from the frame header */
467                 fc &= ~IEEE80211_FCTL_VERS;
468                 hdr->frame_ctl = cpu_to_le16(fc);
469         }
470
471         if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_DATA) {
472                 no_encrypt = 1;
473                 tx.crypt = NULL;
474         }
475
476         if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
477             !(fc & IEEE80211_FCTL_PROTECTED)) {
478                 no_encrypt = 1;
479                 PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
480                        "unencrypted EAPOL frame\n", dev->name);
481                 tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
482         }
483
484         if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
485                 tx.crypt = NULL;
486         else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
487                 /* Add ISWEP flag both for firmware and host based encryption
488                  */
489                 fc |= IEEE80211_FCTL_PROTECTED;
490                 hdr->frame_ctl = cpu_to_le16(fc);
491         } else if (local->drop_unencrypted &&
492                    WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
493                    meta->ethertype != ETH_P_PAE) {
494                 if (net_ratelimit()) {
495                         printk(KERN_DEBUG "%s: dropped unencrypted TX data "
496                                "frame (drop_unencrypted=1)\n", dev->name);
497                 }
498                 iface->stats.tx_dropped++;
499                 ret = 0;
500                 goto fail;
501         }
502
503         if (tx.crypt) {
504                 skb = hostap_tx_encrypt(skb, tx.crypt);
505                 if (skb == NULL) {
506                         printk(KERN_DEBUG "%s: TX - encryption failed\n",
507                                dev->name);
508                         ret = 0;
509                         goto fail;
510                 }
511                 meta = (struct hostap_skb_tx_data *) skb->cb;
512                 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
513                         printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
514                                "expected 0x%08x) after hostap_tx_encrypt\n",
515                                dev->name, meta->magic,
516                                HOSTAP_SKB_TX_DATA_MAGIC);
517                         ret = 0;
518                         iface->stats.tx_dropped++;
519                         goto fail;
520                 }
521         }
522
523         if (local->func->tx == NULL || local->func->tx(skb, dev)) {
524                 ret = 0;
525                 iface->stats.tx_dropped++;
526         } else {
527                 ret = 0;
528                 iface->stats.tx_packets++;
529                 iface->stats.tx_bytes += skb->len;
530         }
531
532  fail:
533         if (!ret && skb)
534                 dev_kfree_skb(skb);
535  tx_exit:
536         if (tx.sta_ptr)
537                 hostap_handle_sta_release(tx.sta_ptr);
538         return ret;
539 }
540
541
542 EXPORT_SYMBOL(hostap_master_start_xmit);