0001
0002 #include <linux/slab.h>
0003 #include <linux/export.h>
0004 #include <linux/etherdevice.h>
0005
0006 #include "hostap_80211.h"
0007 #include "hostap_common.h"
0008 #include "hostap_wlan.h"
0009 #include "hostap.h"
0010 #include "hostap_ap.h"
0011
0012
0013
0014 static unsigned char rfc1042_header[] =
0015 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
0016
0017 static unsigned char bridge_tunnel_header[] =
0018 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
0019
0020
0021 void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
0022 {
0023 struct ieee80211_hdr *hdr;
0024 u16 fc;
0025
0026 hdr = (struct ieee80211_hdr *) skb->data;
0027
0028 printk(KERN_DEBUG "%s: TX len=%d jiffies=%ld\n",
0029 name, skb->len, jiffies);
0030
0031 if (skb->len < 2)
0032 return;
0033
0034 fc = le16_to_cpu(hdr->frame_control);
0035 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d)%s%s",
0036 fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
0037 (fc & IEEE80211_FCTL_STYPE) >> 4,
0038 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
0039 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
0040
0041 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
0042 printk("\n");
0043 return;
0044 }
0045
0046 printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
0047 le16_to_cpu(hdr->seq_ctrl));
0048
0049 printk(KERN_DEBUG " A1=%pM", hdr->addr1);
0050 printk(" A2=%pM", hdr->addr2);
0051 printk(" A3=%pM", hdr->addr3);
0052 if (skb->len >= 30)
0053 printk(" A4=%pM", hdr->addr4);
0054 printk("\n");
0055 }
0056
0057
0058
0059
0060
0061 netdev_tx_t hostap_data_start_xmit(struct sk_buff *skb,
0062 struct net_device *dev)
0063 {
0064 struct hostap_interface *iface;
0065 local_info_t *local;
0066 int need_headroom, need_tailroom = 0;
0067 struct ieee80211_hdr hdr;
0068 u16 fc, ethertype = 0;
0069 enum {
0070 WDS_NO = 0, WDS_OWN_FRAME, WDS_COMPLIANT_FRAME
0071 } use_wds = WDS_NO;
0072 u8 *encaps_data;
0073 int hdr_len, encaps_len, skip_header_bytes;
0074 int to_assoc_ap = 0;
0075 struct hostap_skb_tx_data *meta;
0076
0077 iface = netdev_priv(dev);
0078 local = iface->local;
0079
0080 if (skb->len < ETH_HLEN) {
0081 printk(KERN_DEBUG "%s: hostap_data_start_xmit: short skb "
0082 "(len=%d)\n", dev->name, skb->len);
0083 kfree_skb(skb);
0084 return NETDEV_TX_OK;
0085 }
0086
0087 if (local->ddev != dev) {
0088 use_wds = (local->iw_mode == IW_MODE_MASTER &&
0089 !(local->wds_type & HOSTAP_WDS_STANDARD_FRAME)) ?
0090 WDS_OWN_FRAME : WDS_COMPLIANT_FRAME;
0091 if (dev == local->stadev) {
0092 to_assoc_ap = 1;
0093 use_wds = WDS_NO;
0094 } else if (dev == local->apdev) {
0095 printk(KERN_DEBUG "%s: prism2_tx: trying to use "
0096 "AP device with Ethernet net dev\n", dev->name);
0097 kfree_skb(skb);
0098 return NETDEV_TX_OK;
0099 }
0100 } else {
0101 if (local->iw_mode == IW_MODE_REPEAT) {
0102 printk(KERN_DEBUG "%s: prism2_tx: trying to use "
0103 "non-WDS link in Repeater mode\n", dev->name);
0104 kfree_skb(skb);
0105 return NETDEV_TX_OK;
0106 } else if (local->iw_mode == IW_MODE_INFRA &&
0107 (local->wds_type & HOSTAP_WDS_AP_CLIENT) &&
0108 !ether_addr_equal(skb->data + ETH_ALEN, dev->dev_addr)) {
0109
0110
0111 use_wds = WDS_COMPLIANT_FRAME;
0112 }
0113 }
0114
0115
0116
0117
0118
0119
0120
0121
0122 ethertype = (skb->data[12] << 8) | skb->data[13];
0123
0124 memset(&hdr, 0, sizeof(hdr));
0125
0126
0127 encaps_data = NULL;
0128 encaps_len = 0;
0129 skip_header_bytes = ETH_HLEN;
0130 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
0131 encaps_data = bridge_tunnel_header;
0132 encaps_len = sizeof(bridge_tunnel_header);
0133 skip_header_bytes -= 2;
0134 } else if (ethertype >= 0x600) {
0135 encaps_data = rfc1042_header;
0136 encaps_len = sizeof(rfc1042_header);
0137 skip_header_bytes -= 2;
0138 }
0139
0140 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
0141 hdr_len = IEEE80211_DATA_HDR3_LEN;
0142
0143 if (use_wds != WDS_NO) {
0144
0145
0146
0147
0148
0149
0150
0151
0152 if (use_wds == WDS_COMPLIANT_FRAME) {
0153 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
0154
0155
0156 skb_copy_from_linear_data_offset(skb, ETH_ALEN,
0157 &hdr.addr4, ETH_ALEN);
0158 hdr_len += ETH_ALEN;
0159 } else {
0160
0161
0162 fc |= IEEE80211_FCTL_TODS;
0163
0164
0165
0166
0167
0168
0169
0170 skb_copy_from_linear_data_offset(skb, ETH_ALEN,
0171 &hdr.addr4, ETH_ALEN);
0172 need_tailroom += ETH_ALEN;
0173 }
0174
0175
0176
0177 if ((local->wds_type & HOSTAP_WDS_BROADCAST_RA) &&
0178 is_multicast_ether_addr(skb->data))
0179 eth_broadcast_addr(hdr.addr1);
0180 else if (iface->type == HOSTAP_INTERFACE_WDS)
0181 memcpy(&hdr.addr1, iface->u.wds.remote_addr,
0182 ETH_ALEN);
0183 else
0184 memcpy(&hdr.addr1, local->bssid, ETH_ALEN);
0185 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
0186 skb_copy_from_linear_data(skb, &hdr.addr3, ETH_ALEN);
0187 } else if (local->iw_mode == IW_MODE_MASTER && !to_assoc_ap) {
0188 fc |= IEEE80211_FCTL_FROMDS;
0189
0190 skb_copy_from_linear_data(skb, &hdr.addr1, ETH_ALEN);
0191 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
0192 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr3,
0193 ETH_ALEN);
0194 } else if (local->iw_mode == IW_MODE_INFRA || to_assoc_ap) {
0195 fc |= IEEE80211_FCTL_TODS;
0196
0197 memcpy(&hdr.addr1, to_assoc_ap ?
0198 local->assoc_ap_addr : local->bssid, ETH_ALEN);
0199 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr2,
0200 ETH_ALEN);
0201 skb_copy_from_linear_data(skb, &hdr.addr3, ETH_ALEN);
0202 } else if (local->iw_mode == IW_MODE_ADHOC) {
0203
0204 skb_copy_from_linear_data(skb, &hdr.addr1, ETH_ALEN);
0205 skb_copy_from_linear_data_offset(skb, ETH_ALEN, &hdr.addr2,
0206 ETH_ALEN);
0207 memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
0208 }
0209
0210 hdr.frame_control = cpu_to_le16(fc);
0211
0212 skb_pull(skb, skip_header_bytes);
0213 need_headroom = local->func->need_tx_headroom + hdr_len + encaps_len;
0214 if (skb_tailroom(skb) < need_tailroom) {
0215 skb = skb_unshare(skb, GFP_ATOMIC);
0216 if (skb == NULL) {
0217 iface->stats.tx_dropped++;
0218 return NETDEV_TX_OK;
0219 }
0220 if (pskb_expand_head(skb, need_headroom, need_tailroom,
0221 GFP_ATOMIC)) {
0222 kfree_skb(skb);
0223 iface->stats.tx_dropped++;
0224 return NETDEV_TX_OK;
0225 }
0226 } else if (skb_headroom(skb) < need_headroom) {
0227 struct sk_buff *tmp = skb;
0228 skb = skb_realloc_headroom(skb, need_headroom);
0229 kfree_skb(tmp);
0230 if (skb == NULL) {
0231 iface->stats.tx_dropped++;
0232 return NETDEV_TX_OK;
0233 }
0234 } else {
0235 skb = skb_unshare(skb, GFP_ATOMIC);
0236 if (skb == NULL) {
0237 iface->stats.tx_dropped++;
0238 return NETDEV_TX_OK;
0239 }
0240 }
0241
0242 if (encaps_data)
0243 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
0244 memcpy(skb_push(skb, hdr_len), &hdr, hdr_len);
0245 if (use_wds == WDS_OWN_FRAME) {
0246 skb_put_data(skb, &hdr.addr4, ETH_ALEN);
0247 }
0248
0249 iface->stats.tx_packets++;
0250 iface->stats.tx_bytes += skb->len;
0251
0252 skb_reset_mac_header(skb);
0253 meta = (struct hostap_skb_tx_data *) skb->cb;
0254 memset(meta, 0, sizeof(*meta));
0255 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
0256 if (use_wds)
0257 meta->flags |= HOSTAP_TX_FLAGS_WDS;
0258 meta->ethertype = ethertype;
0259 meta->iface = iface;
0260
0261
0262 skb->dev = local->dev;
0263 dev_queue_xmit(skb);
0264 return NETDEV_TX_OK;
0265 }
0266
0267
0268
0269 netdev_tx_t hostap_mgmt_start_xmit(struct sk_buff *skb,
0270 struct net_device *dev)
0271 {
0272 struct hostap_interface *iface;
0273 local_info_t *local;
0274 struct hostap_skb_tx_data *meta;
0275 struct ieee80211_hdr *hdr;
0276 u16 fc;
0277
0278 iface = netdev_priv(dev);
0279 local = iface->local;
0280
0281 if (skb->len < 10) {
0282 printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
0283 "(len=%d)\n", dev->name, skb->len);
0284 kfree_skb(skb);
0285 return NETDEV_TX_OK;
0286 }
0287
0288 iface->stats.tx_packets++;
0289 iface->stats.tx_bytes += skb->len;
0290
0291 meta = (struct hostap_skb_tx_data *) skb->cb;
0292 memset(meta, 0, sizeof(*meta));
0293 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
0294 meta->iface = iface;
0295
0296 if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
0297 hdr = (struct ieee80211_hdr *) skb->data;
0298 fc = le16_to_cpu(hdr->frame_control);
0299 if (ieee80211_is_data(hdr->frame_control) &&
0300 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DATA) {
0301 u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
0302 sizeof(rfc1042_header)];
0303 meta->ethertype = (pos[0] << 8) | pos[1];
0304 }
0305 }
0306
0307
0308 skb->dev = local->dev;
0309 dev_queue_xmit(skb);
0310 return NETDEV_TX_OK;
0311 }
0312
0313
0314
0315 static struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
0316 struct lib80211_crypt_data *crypt)
0317 {
0318 struct hostap_interface *iface;
0319 local_info_t *local;
0320 struct ieee80211_hdr *hdr;
0321 int prefix_len, postfix_len, hdr_len, res;
0322
0323 iface = netdev_priv(skb->dev);
0324 local = iface->local;
0325
0326 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
0327 kfree_skb(skb);
0328 return NULL;
0329 }
0330
0331 if (local->tkip_countermeasures &&
0332 strcmp(crypt->ops->name, "TKIP") == 0) {
0333 hdr = (struct ieee80211_hdr *) skb->data;
0334 if (net_ratelimit()) {
0335 printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
0336 "TX packet to %pM\n",
0337 local->dev->name, hdr->addr1);
0338 }
0339 kfree_skb(skb);
0340 return NULL;
0341 }
0342
0343 skb = skb_unshare(skb, GFP_ATOMIC);
0344 if (skb == NULL)
0345 return NULL;
0346
0347 prefix_len = crypt->ops->extra_mpdu_prefix_len +
0348 crypt->ops->extra_msdu_prefix_len;
0349 postfix_len = crypt->ops->extra_mpdu_postfix_len +
0350 crypt->ops->extra_msdu_postfix_len;
0351 if ((skb_headroom(skb) < prefix_len ||
0352 skb_tailroom(skb) < postfix_len) &&
0353 pskb_expand_head(skb, prefix_len, postfix_len, GFP_ATOMIC)) {
0354 kfree_skb(skb);
0355 return NULL;
0356 }
0357
0358 hdr = (struct ieee80211_hdr *) skb->data;
0359 hdr_len = hostap_80211_get_hdrlen(hdr->frame_control);
0360
0361
0362
0363 atomic_inc(&crypt->refcnt);
0364 res = 0;
0365 if (crypt->ops->encrypt_msdu)
0366 res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
0367 if (res == 0 && crypt->ops->encrypt_mpdu)
0368 res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
0369 atomic_dec(&crypt->refcnt);
0370 if (res < 0) {
0371 kfree_skb(skb);
0372 return NULL;
0373 }
0374
0375 return skb;
0376 }
0377
0378
0379
0380
0381
0382 netdev_tx_t hostap_master_start_xmit(struct sk_buff *skb,
0383 struct net_device *dev)
0384 {
0385 struct hostap_interface *iface;
0386 local_info_t *local;
0387 netdev_tx_t ret = NETDEV_TX_BUSY;
0388 u16 fc;
0389 struct hostap_tx_data tx;
0390 ap_tx_ret tx_ret;
0391 struct hostap_skb_tx_data *meta;
0392 int no_encrypt = 0;
0393 struct ieee80211_hdr *hdr;
0394
0395 iface = netdev_priv(dev);
0396 local = iface->local;
0397
0398 tx.skb = skb;
0399 tx.sta_ptr = NULL;
0400
0401 meta = (struct hostap_skb_tx_data *) skb->cb;
0402 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
0403 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
0404 "expected 0x%08x)\n",
0405 dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
0406 ret = NETDEV_TX_OK;
0407 iface->stats.tx_dropped++;
0408 goto fail;
0409 }
0410
0411 if (local->host_encrypt) {
0412
0413
0414 tx.crypt = local->crypt_info.crypt[local->crypt_info.tx_keyidx];
0415 tx.host_encrypt = 1;
0416 } else {
0417 tx.crypt = NULL;
0418 tx.host_encrypt = 0;
0419 }
0420
0421 if (skb->len < 24) {
0422 printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
0423 "(len=%d)\n", dev->name, skb->len);
0424 ret = NETDEV_TX_OK;
0425 iface->stats.tx_dropped++;
0426 goto fail;
0427 }
0428
0429
0430
0431
0432
0433 tx_ret = hostap_handle_sta_tx(local, &tx);
0434 skb = tx.skb;
0435 meta = (struct hostap_skb_tx_data *) skb->cb;
0436 hdr = (struct ieee80211_hdr *) skb->data;
0437 fc = le16_to_cpu(hdr->frame_control);
0438 switch (tx_ret) {
0439 case AP_TX_CONTINUE:
0440 break;
0441 case AP_TX_CONTINUE_NOT_AUTHORIZED:
0442 if (local->ieee_802_1x &&
0443 ieee80211_is_data(hdr->frame_control) &&
0444 meta->ethertype != ETH_P_PAE &&
0445 !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
0446 printk(KERN_DEBUG "%s: dropped frame to unauthorized "
0447 "port (IEEE 802.1X): ethertype=0x%04x\n",
0448 dev->name, meta->ethertype);
0449 hostap_dump_tx_80211(dev->name, skb);
0450
0451 ret = NETDEV_TX_OK;
0452 iface->stats.tx_dropped++;
0453 goto fail;
0454 }
0455 break;
0456 case AP_TX_DROP:
0457 ret = NETDEV_TX_OK;
0458 iface->stats.tx_dropped++;
0459 goto fail;
0460 case AP_TX_RETRY:
0461 goto fail;
0462 case AP_TX_BUFFERED:
0463
0464
0465 ret = NETDEV_TX_OK;
0466 goto tx_exit;
0467 }
0468
0469
0470
0471
0472 if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
0473 local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
0474 meta->tx_cb_idx = local->ap->tx_callback_idx;
0475
0476
0477 fc &= ~IEEE80211_FCTL_VERS;
0478 hdr->frame_control = cpu_to_le16(fc);
0479 }
0480
0481 if (!ieee80211_is_data(hdr->frame_control)) {
0482 no_encrypt = 1;
0483 tx.crypt = NULL;
0484 }
0485
0486 if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
0487 !(fc & IEEE80211_FCTL_PROTECTED)) {
0488 no_encrypt = 1;
0489 PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
0490 "unencrypted EAPOL frame\n", dev->name);
0491 tx.crypt = NULL;
0492 }
0493
0494 if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
0495 tx.crypt = NULL;
0496 else if ((tx.crypt ||
0497 local->crypt_info.crypt[local->crypt_info.tx_keyidx]) &&
0498 !no_encrypt) {
0499
0500
0501 fc |= IEEE80211_FCTL_PROTECTED;
0502 hdr->frame_control = cpu_to_le16(fc);
0503 } else if (local->drop_unencrypted &&
0504 ieee80211_is_data(hdr->frame_control) &&
0505 meta->ethertype != ETH_P_PAE) {
0506 if (net_ratelimit()) {
0507 printk(KERN_DEBUG "%s: dropped unencrypted TX data "
0508 "frame (drop_unencrypted=1)\n", dev->name);
0509 }
0510 iface->stats.tx_dropped++;
0511 ret = NETDEV_TX_OK;
0512 goto fail;
0513 }
0514
0515 if (tx.crypt) {
0516 skb = hostap_tx_encrypt(skb, tx.crypt);
0517 if (skb == NULL) {
0518 printk(KERN_DEBUG "%s: TX - encryption failed\n",
0519 dev->name);
0520 ret = NETDEV_TX_OK;
0521 goto fail;
0522 }
0523 meta = (struct hostap_skb_tx_data *) skb->cb;
0524 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
0525 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
0526 "expected 0x%08x) after hostap_tx_encrypt\n",
0527 dev->name, meta->magic,
0528 HOSTAP_SKB_TX_DATA_MAGIC);
0529 ret = NETDEV_TX_OK;
0530 iface->stats.tx_dropped++;
0531 goto fail;
0532 }
0533 }
0534
0535 if (local->func->tx == NULL || local->func->tx(skb, dev)) {
0536 ret = NETDEV_TX_OK;
0537 iface->stats.tx_dropped++;
0538 } else {
0539 ret = NETDEV_TX_OK;
0540 iface->stats.tx_packets++;
0541 iface->stats.tx_bytes += skb->len;
0542 }
0543
0544 fail:
0545 if (ret == NETDEV_TX_OK && skb)
0546 dev_kfree_skb(skb);
0547 tx_exit:
0548 if (tx.sta_ptr)
0549 hostap_handle_sta_release(tx.sta_ptr);
0550 return ret;
0551 }
0552
0553
0554 EXPORT_SYMBOL(hostap_master_start_xmit);