1 #include "hostap_80211.h"
2 #include "hostap_common.h"
3 #include "hostap_wlan.h"
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) */
16 void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
18 struct ieee80211_hdr_4addr *hdr;
21 hdr = (struct ieee80211_hdr_4addr *) skb->data;
23 printk(KERN_DEBUG "%s: TX len=%d jiffies=%ld\n",
24 name, skb->len, jiffies);
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]" : "");
35 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
40 printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
41 le16_to_cpu(hdr->seq_ctl));
43 printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
44 MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
46 printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
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)
56 struct hostap_interface *iface;
58 int need_headroom, need_tailroom = 0;
59 struct ieee80211_hdr_4addr hdr;
60 u16 fc, ethertype = 0;
62 WDS_NO = 0, WDS_OWN_FRAME, WDS_COMPLIANT_FRAME
65 int hdr_len, encaps_len, skip_header_bytes;
67 struct hostap_skb_tx_data *meta;
69 iface = netdev_priv(dev);
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);
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) {
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);
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);
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,
102 /* AP client mode: send frames with foreign src addr
103 * using 4-addr WDS frames */
104 use_wds = WDS_COMPLIANT_FRAME;
108 /* Incoming skb->data: dst_addr[6], src_addr[6], proto[2], payload
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]} */
115 ethertype = (skb->data[12] << 8) | skb->data[13];
117 memset(&hdr, 0, sizeof(hdr));
119 /* Length of data after IEEE 802.11 header */
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;
133 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
134 hdr_len = IEEE80211_DATA_HDR3_LEN;
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
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,
149 skb_copy_from_linear_data_offset(skb, ETH_ALEN,
150 &hdr.addr4, ETH_ALEN);
153 /* bogus 4-addr format to workaround Prism2 station
155 fc |= IEEE80211_FCTL_TODS;
156 /* From DS: Addr1 = DA (used as RA),
157 * Addr2 = BSSID (used as TA), Addr3 = SA (used as DA),
160 /* SA from skb->data + ETH_ALEN will be added after
161 * frame payload; use hdr.addr4 as a temporary buffer
163 skb_copy_from_linear_data_offset(skb, ETH_ALEN,
164 &hdr.addr4, ETH_ALEN);
165 need_tailroom += ETH_ALEN;
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) &&
172 memset(&hdr.addr1, 0xff, ETH_ALEN);
173 else if (iface->type == HOSTAP_INTERFACE_WDS)
174 memcpy(&hdr.addr1, iface->u.wds.remote_addr,
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,
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,
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,
200 memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
203 hdr.frame_ctl = cpu_to_le16(fc);
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);
210 iface->stats.tx_dropped++;
213 if (pskb_expand_head(skb, need_headroom, need_tailroom,
216 iface->stats.tx_dropped++;
219 } else if (skb_headroom(skb) < need_headroom) {
220 struct sk_buff *tmp = skb;
221 skb = skb_realloc_headroom(skb, need_headroom);
224 iface->stats.tx_dropped++;
228 skb = skb_unshare(skb, GFP_ATOMIC);
230 iface->stats.tx_dropped++;
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);
242 iface->stats.tx_packets++;
243 iface->stats.tx_bytes += skb->len;
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;
250 meta->flags |= HOSTAP_TX_FLAGS_WDS;
251 meta->ethertype = ethertype;
254 /* Send IEEE 802.11 encapsulated frame using the master radio device */
255 skb->dev = local->dev;
261 /* hard_start_xmit function for hostapd wlan#ap interfaces */
262 int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
264 struct hostap_interface *iface;
266 struct hostap_skb_tx_data *meta;
267 struct ieee80211_hdr_4addr *hdr;
270 iface = netdev_priv(dev);
271 local = iface->local;
274 printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
275 "(len=%d)\n", dev->name, skb->len);
280 iface->stats.tx_packets++;
281 iface->stats.tx_bytes += skb->len;
283 meta = (struct hostap_skb_tx_data *) skb->cb;
284 memset(meta, 0, sizeof(*meta));
285 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
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];
299 /* Send IEEE 802.11 encapsulated frame using the master radio device */
300 skb->dev = local->dev;
306 /* Called only from software IRQ */
307 static struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
308 struct ieee80211_crypt_data *crypt)
310 struct hostap_interface *iface;
312 struct ieee80211_hdr_4addr *hdr;
314 int prefix_len, postfix_len, hdr_len, res;
316 iface = netdev_priv(skb->dev);
317 local = iface->local;
319 if (skb->len < IEEE80211_DATA_HDR3_LEN) {
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));
336 skb = skb_unshare(skb, GFP_ATOMIC);
340 prefix_len = crypt->ops->extra_mpdu_prefix_len +
341 crypt->ops->extra_msdu_prefix_len;
342 postfix_len = crypt->ops->extra_mpdu_postfix_len +
343 crypt->ops->extra_msdu_postfix_len;
344 if ((skb_headroom(skb) < prefix_len ||
345 skb_tailroom(skb) < postfix_len) &&
346 pskb_expand_head(skb, prefix_len, postfix_len, GFP_ATOMIC)) {
351 hdr = (struct ieee80211_hdr_4addr *) skb->data;
352 fc = le16_to_cpu(hdr->frame_ctl);
353 hdr_len = hostap_80211_get_hdrlen(fc);
355 /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
356 * call both MSDU and MPDU encryption functions from here. */
357 atomic_inc(&crypt->refcnt);
359 if (crypt->ops->encrypt_msdu)
360 res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
361 if (res == 0 && crypt->ops->encrypt_mpdu)
362 res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
363 atomic_dec(&crypt->refcnt);
373 /* hard_start_xmit function for master radio interface wifi#.
374 * AP processing (TX rate control, power save buffering, etc.).
375 * Use hardware TX function to send the frame. */
376 int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
378 struct hostap_interface *iface;
382 struct hostap_tx_data tx;
384 struct hostap_skb_tx_data *meta;
386 struct ieee80211_hdr_4addr *hdr;
388 iface = netdev_priv(dev);
389 local = iface->local;
394 meta = (struct hostap_skb_tx_data *) skb->cb;
395 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
396 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
397 "expected 0x%08x)\n",
398 dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
400 iface->stats.tx_dropped++;
404 if (local->host_encrypt) {
405 /* Set crypt to default algorithm and key; will be replaced in
406 * AP code if STA has own alg/key */
407 tx.crypt = local->crypt[local->tx_keyidx];
415 printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
416 "(len=%d)\n", dev->name, skb->len);
418 iface->stats.tx_dropped++;
423 * Wi-Fi 802.11b test plan suggests that AP should ignore power save
424 * bit in authentication and (re)association frames and assume tha
425 * STA remains awake for the response. */
426 tx_ret = hostap_handle_sta_tx(local, &tx);
428 meta = (struct hostap_skb_tx_data *) skb->cb;
429 hdr = (struct ieee80211_hdr_4addr *) skb->data;
430 fc = le16_to_cpu(hdr->frame_ctl);
434 case AP_TX_CONTINUE_NOT_AUTHORIZED:
435 if (local->ieee_802_1x &&
436 WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
437 meta->ethertype != ETH_P_PAE &&
438 !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
439 printk(KERN_DEBUG "%s: dropped frame to unauthorized "
440 "port (IEEE 802.1X): ethertype=0x%04x\n",
441 dev->name, meta->ethertype);
442 hostap_dump_tx_80211(dev->name, skb);
444 ret = 0; /* drop packet */
445 iface->stats.tx_dropped++;
450 ret = 0; /* drop packet */
451 iface->stats.tx_dropped++;
456 /* do not free skb here, it will be freed when the
457 * buffered frame is sent/timed out */
462 /* Request TX callback if protocol version is 2 in 802.11 header;
463 * this version 2 is a special case used between hostapd and kernel
465 if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
466 local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
467 meta->tx_cb_idx = local->ap->tx_callback_idx;
469 /* remove special version from the frame header */
470 fc &= ~IEEE80211_FCTL_VERS;
471 hdr->frame_ctl = cpu_to_le16(fc);
474 if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_DATA) {
479 if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
480 !(fc & IEEE80211_FCTL_PROTECTED)) {
482 PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
483 "unencrypted EAPOL frame\n", dev->name);
484 tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
487 if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
489 else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
490 /* Add ISWEP flag both for firmware and host based encryption
492 fc |= IEEE80211_FCTL_PROTECTED;
493 hdr->frame_ctl = cpu_to_le16(fc);
494 } else if (local->drop_unencrypted &&
495 WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
496 meta->ethertype != ETH_P_PAE) {
497 if (net_ratelimit()) {
498 printk(KERN_DEBUG "%s: dropped unencrypted TX data "
499 "frame (drop_unencrypted=1)\n", dev->name);
501 iface->stats.tx_dropped++;
507 skb = hostap_tx_encrypt(skb, tx.crypt);
509 printk(KERN_DEBUG "%s: TX - encryption failed\n",
514 meta = (struct hostap_skb_tx_data *) skb->cb;
515 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
516 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
517 "expected 0x%08x) after hostap_tx_encrypt\n",
518 dev->name, meta->magic,
519 HOSTAP_SKB_TX_DATA_MAGIC);
521 iface->stats.tx_dropped++;
526 if (local->func->tx == NULL || local->func->tx(skb, dev)) {
528 iface->stats.tx_dropped++;
531 iface->stats.tx_packets++;
532 iface->stats.tx_bytes += skb->len;
540 hostap_handle_sta_release(tx.sta_ptr);
545 EXPORT_SYMBOL(hostap_master_start_xmit);