aboutsummaryrefslogtreecommitdiffstats
path: root/net/mac80211
diff options
context:
space:
mode:
authorJohannes Berg <johannes@sipsolutions.net>2007-07-27 09:43:23 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2007-10-10 19:47:36 -0400
commitc2d1560ad8c2f6e0dd0d34102d022f3709325c26 (patch)
tree40a64238627c8d6f9e20fa8ac33a1f35ff7315de /net/mac80211
parentb2c258fb11b3fc77a73f8b0453ff1256de812bc6 (diff)
[MAC80211]: introduce util.c
Introduce a new file util.c and move a whole bunch of functions into it. Signed-off-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: Jiri Benc <jbenc@suse.cz> Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'net/mac80211')
-rw-r--r--net/mac80211/Makefile1
-rw-r--r--net/mac80211/ieee80211.c437
-rw-r--r--net/mac80211/ieee80211_i.h18
-rw-r--r--net/mac80211/util.c463
4 files changed, 472 insertions, 447 deletions
diff --git a/net/mac80211/Makefile b/net/mac80211/Makefile
index d95de3f8a4b1..122b23eb15d1 100644
--- a/net/mac80211/Makefile
+++ b/net/mac80211/Makefile
@@ -21,4 +21,5 @@ mac80211-objs := \
21 rx.o \ 21 rx.o \
22 tx.o \ 22 tx.o \
23 key.o \ 23 key.o \
24 util.o \
24 $(mac80211-objs-y) 25 $(mac80211-objs-y)
diff --git a/net/mac80211/ieee80211.c b/net/mac80211/ieee80211.c
index f240f9706081..1981058907b7 100644
--- a/net/mac80211/ieee80211.c
+++ b/net/mac80211/ieee80211.c
@@ -34,23 +34,6 @@
34#include "debugfs.h" 34#include "debugfs.h"
35#include "debugfs_netdev.h" 35#include "debugfs_netdev.h"
36 36
37/* privid for wiphys to determine whether they belong to us or not */
38void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
39
40/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
41/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
42const unsigned char rfc1042_header[] =
43 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
44
45/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
46const unsigned char bridge_tunnel_header[] =
47 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
48
49/* No encapsulation header if EtherType < 0x600 (=length) */
50static const unsigned char eapol_header[] =
51 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
52
53
54/* 37/*
55 * For seeing transmitted packets on monitor interfaces 38 * For seeing transmitted packets on monitor interfaces
56 * we have a radiotap header too. 39 * we have a radiotap header too.
@@ -661,346 +644,6 @@ int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
661 644
662/* everything else */ 645/* everything else */
663 646
664static int rate_list_match(const int *rate_list, int rate)
665{
666 int i;
667
668 if (!rate_list)
669 return 0;
670
671 for (i = 0; rate_list[i] >= 0; i++)
672 if (rate_list[i] == rate)
673 return 1;
674
675 return 0;
676}
677
678void ieee80211_prepare_rates(struct ieee80211_local *local,
679 struct ieee80211_hw_mode *mode)
680{
681 int i;
682
683 for (i = 0; i < mode->num_rates; i++) {
684 struct ieee80211_rate *rate = &mode->rates[i];
685
686 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
687 IEEE80211_RATE_BASIC);
688
689 if (local->supp_rates[mode->mode]) {
690 if (!rate_list_match(local->supp_rates[mode->mode],
691 rate->rate))
692 continue;
693 }
694
695 rate->flags |= IEEE80211_RATE_SUPPORTED;
696
697 /* Use configured basic rate set if it is available. If not,
698 * use defaults that are sane for most cases. */
699 if (local->basic_rates[mode->mode]) {
700 if (rate_list_match(local->basic_rates[mode->mode],
701 rate->rate))
702 rate->flags |= IEEE80211_RATE_BASIC;
703 } else switch (mode->mode) {
704 case MODE_IEEE80211A:
705 if (rate->rate == 60 || rate->rate == 120 ||
706 rate->rate == 240)
707 rate->flags |= IEEE80211_RATE_BASIC;
708 break;
709 case MODE_IEEE80211B:
710 if (rate->rate == 10 || rate->rate == 20)
711 rate->flags |= IEEE80211_RATE_BASIC;
712 break;
713 case MODE_ATHEROS_TURBO:
714 if (rate->rate == 120 || rate->rate == 240 ||
715 rate->rate == 480)
716 rate->flags |= IEEE80211_RATE_BASIC;
717 break;
718 case MODE_IEEE80211G:
719 if (rate->rate == 10 || rate->rate == 20 ||
720 rate->rate == 55 || rate->rate == 110)
721 rate->flags |= IEEE80211_RATE_BASIC;
722 break;
723 }
724
725 /* Set ERP and MANDATORY flags based on phymode */
726 switch (mode->mode) {
727 case MODE_IEEE80211A:
728 if (rate->rate == 60 || rate->rate == 120 ||
729 rate->rate == 240)
730 rate->flags |= IEEE80211_RATE_MANDATORY;
731 break;
732 case MODE_IEEE80211B:
733 if (rate->rate == 10)
734 rate->flags |= IEEE80211_RATE_MANDATORY;
735 break;
736 case MODE_ATHEROS_TURBO:
737 break;
738 case MODE_IEEE80211G:
739 if (rate->rate == 10 || rate->rate == 20 ||
740 rate->rate == 55 || rate->rate == 110 ||
741 rate->rate == 60 || rate->rate == 120 ||
742 rate->rate == 240)
743 rate->flags |= IEEE80211_RATE_MANDATORY;
744 break;
745 }
746 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
747 rate->flags |= IEEE80211_RATE_ERP;
748 }
749}
750
751u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
752{
753 u16 fc;
754
755 if (len < 24)
756 return NULL;
757
758 fc = le16_to_cpu(hdr->frame_control);
759
760 switch (fc & IEEE80211_FCTL_FTYPE) {
761 case IEEE80211_FTYPE_DATA:
762 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
763 case IEEE80211_FCTL_TODS:
764 return hdr->addr1;
765 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
766 return NULL;
767 case IEEE80211_FCTL_FROMDS:
768 return hdr->addr2;
769 case 0:
770 return hdr->addr3;
771 }
772 break;
773 case IEEE80211_FTYPE_MGMT:
774 return hdr->addr3;
775 case IEEE80211_FTYPE_CTL:
776 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
777 return hdr->addr1;
778 else
779 return NULL;
780 }
781
782 return NULL;
783}
784
785int ieee80211_get_hdrlen(u16 fc)
786{
787 int hdrlen = 24;
788
789 switch (fc & IEEE80211_FCTL_FTYPE) {
790 case IEEE80211_FTYPE_DATA:
791 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
792 hdrlen = 30; /* Addr4 */
793 /*
794 * The QoS Control field is two bytes and its presence is
795 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
796 * hdrlen if that bit is set.
797 * This works by masking out the bit and shifting it to
798 * bit position 1 so the result has the value 0 or 2.
799 */
800 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
801 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
802 break;
803 case IEEE80211_FTYPE_CTL:
804 /*
805 * ACK and CTS are 10 bytes, all others 16. To see how
806 * to get this condition consider
807 * subtype mask: 0b0000000011110000 (0x00F0)
808 * ACK subtype: 0b0000000011010000 (0x00D0)
809 * CTS subtype: 0b0000000011000000 (0x00C0)
810 * bits that matter: ^^^ (0x00E0)
811 * value of those: 0b0000000011000000 (0x00C0)
812 */
813 if ((fc & 0xE0) == 0xC0)
814 hdrlen = 10;
815 else
816 hdrlen = 16;
817 break;
818 }
819
820 return hdrlen;
821}
822EXPORT_SYMBOL(ieee80211_get_hdrlen);
823
824int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
825{
826 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
827 int hdrlen;
828
829 if (unlikely(skb->len < 10))
830 return 0;
831 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
832 if (unlikely(hdrlen > skb->len))
833 return 0;
834 return hdrlen;
835}
836EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
837
838
839int ieee80211_is_eapol(const struct sk_buff *skb)
840{
841 const struct ieee80211_hdr *hdr;
842 u16 fc;
843 int hdrlen;
844
845 if (unlikely(skb->len < 10))
846 return 0;
847
848 hdr = (const struct ieee80211_hdr *) skb->data;
849 fc = le16_to_cpu(hdr->frame_control);
850
851 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
852 return 0;
853
854 hdrlen = ieee80211_get_hdrlen(fc);
855
856 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
857 memcmp(skb->data + hdrlen, eapol_header,
858 sizeof(eapol_header)) == 0))
859 return 1;
860
861 return 0;
862}
863
864void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
865{
866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
867
868 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
869 if (tx->u.tx.extra_frag) {
870 struct ieee80211_hdr *fhdr;
871 int i;
872 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
873 fhdr = (struct ieee80211_hdr *)
874 tx->u.tx.extra_frag[i]->data;
875 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
876 }
877 }
878}
879
880static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
881 int rate, int erp, int short_preamble)
882{
883 int dur;
884
885 /* calculate duration (in microseconds, rounded up to next higher
886 * integer if it includes a fractional microsecond) to send frame of
887 * len bytes (does not include FCS) at the given rate. Duration will
888 * also include SIFS.
889 *
890 * rate is in 100 kbps, so divident is multiplied by 10 in the
891 * DIV_ROUND_UP() operations.
892 */
893
894 if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
895 local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
896 /*
897 * OFDM:
898 *
899 * N_DBPS = DATARATE x 4
900 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
901 * (16 = SIGNAL time, 6 = tail bits)
902 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
903 *
904 * T_SYM = 4 usec
905 * 802.11a - 17.5.2: aSIFSTime = 16 usec
906 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
907 * signal ext = 6 usec
908 */
909 /* FIX: Atheros Turbo may have different (shorter) duration? */
910 dur = 16; /* SIFS + signal ext */
911 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
912 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
913 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
914 4 * rate); /* T_SYM x N_SYM */
915 } else {
916 /*
917 * 802.11b or 802.11g with 802.11b compatibility:
918 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
919 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
920 *
921 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
922 * aSIFSTime = 10 usec
923 * aPreambleLength = 144 usec or 72 usec with short preamble
924 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
925 */
926 dur = 10; /* aSIFSTime = 10 usec */
927 dur += short_preamble ? (72 + 24) : (144 + 48);
928
929 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
930 }
931
932 return dur;
933}
934
935/* Exported duration function for driver use */
936__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
937 size_t frame_len, int rate)
938{
939 struct ieee80211_local *local = hw_to_local(hw);
940 u16 dur;
941 int erp;
942
943 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
944 dur = ieee80211_frame_duration(local, frame_len, rate,
945 erp, local->short_preamble);
946
947 return cpu_to_le16(dur);
948}
949EXPORT_SYMBOL(ieee80211_generic_frame_duration);
950
951__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
952 size_t frame_len,
953 const struct ieee80211_tx_control *frame_txctl)
954{
955 struct ieee80211_local *local = hw_to_local(hw);
956 struct ieee80211_rate *rate;
957 int short_preamble = local->short_preamble;
958 int erp;
959 u16 dur;
960
961 rate = frame_txctl->rts_rate;
962 erp = !!(rate->flags & IEEE80211_RATE_ERP);
963
964 /* CTS duration */
965 dur = ieee80211_frame_duration(local, 10, rate->rate,
966 erp, short_preamble);
967 /* Data frame duration */
968 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
969 erp, short_preamble);
970 /* ACK duration */
971 dur += ieee80211_frame_duration(local, 10, rate->rate,
972 erp, short_preamble);
973
974 return cpu_to_le16(dur);
975}
976EXPORT_SYMBOL(ieee80211_rts_duration);
977
978__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
979 size_t frame_len,
980 const struct ieee80211_tx_control *frame_txctl)
981{
982 struct ieee80211_local *local = hw_to_local(hw);
983 struct ieee80211_rate *rate;
984 int short_preamble = local->short_preamble;
985 int erp;
986 u16 dur;
987
988 rate = frame_txctl->rts_rate;
989 erp = !!(rate->flags & IEEE80211_RATE_ERP);
990
991 /* Data frame duration */
992 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
993 erp, short_preamble);
994 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
995 /* ACK duration */
996 dur += ieee80211_frame_duration(local, 10, rate->rate,
997 erp, short_preamble);
998 }
999
1000 return cpu_to_le16(dur);
1001}
1002EXPORT_SYMBOL(ieee80211_ctstoself_duration);
1003
1004static int __ieee80211_if_config(struct net_device *dev, 647static int __ieee80211_if_config(struct net_device *dev,
1005 struct sk_buff *beacon, 648 struct sk_buff *beacon,
1006 struct ieee80211_tx_control *control) 649 struct ieee80211_tx_control *control)
@@ -1123,27 +766,6 @@ struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
1123} 766}
1124EXPORT_SYMBOL(ieee80211_get_mc_list_item); 767EXPORT_SYMBOL(ieee80211_get_mc_list_item);
1125 768
1126struct ieee80211_rate *
1127ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
1128{
1129 struct ieee80211_hw_mode *mode;
1130 int r;
1131
1132 list_for_each_entry(mode, &local->modes_list, list) {
1133 if (mode->mode != phymode)
1134 continue;
1135 for (r = 0; r < mode->num_rates; r++) {
1136 struct ieee80211_rate *rate = &mode->rates[r];
1137 if (rate->val == hw_rate ||
1138 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
1139 rate->val2 == hw_rate))
1140 return rate;
1141 }
1142 }
1143
1144 return NULL;
1145}
1146
1147static void ieee80211_stat_refresh(unsigned long data) 769static void ieee80211_stat_refresh(unsigned long data)
1148{ 770{
1149 struct ieee80211_local *local = (struct ieee80211_local *) data; 771 struct ieee80211_local *local = (struct ieee80211_local *) data;
@@ -1856,65 +1478,6 @@ void ieee80211_free_hw(struct ieee80211_hw *hw)
1856} 1478}
1857EXPORT_SYMBOL(ieee80211_free_hw); 1479EXPORT_SYMBOL(ieee80211_free_hw);
1858 1480
1859void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
1860{
1861 struct ieee80211_local *local = hw_to_local(hw);
1862
1863 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
1864 &local->state[queue])) {
1865 if (test_bit(IEEE80211_LINK_STATE_PENDING,
1866 &local->state[queue]))
1867 tasklet_schedule(&local->tx_pending_tasklet);
1868 else
1869 if (!ieee80211_qdisc_installed(local->mdev)) {
1870 if (queue == 0)
1871 netif_wake_queue(local->mdev);
1872 } else
1873 __netif_schedule(local->mdev);
1874 }
1875}
1876EXPORT_SYMBOL(ieee80211_wake_queue);
1877
1878void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
1879{
1880 struct ieee80211_local *local = hw_to_local(hw);
1881
1882 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
1883 netif_stop_queue(local->mdev);
1884 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
1885}
1886EXPORT_SYMBOL(ieee80211_stop_queue);
1887
1888void ieee80211_start_queues(struct ieee80211_hw *hw)
1889{
1890 struct ieee80211_local *local = hw_to_local(hw);
1891 int i;
1892
1893 for (i = 0; i < local->hw.queues; i++)
1894 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
1895 if (!ieee80211_qdisc_installed(local->mdev))
1896 netif_start_queue(local->mdev);
1897}
1898EXPORT_SYMBOL(ieee80211_start_queues);
1899
1900void ieee80211_stop_queues(struct ieee80211_hw *hw)
1901{
1902 int i;
1903
1904 for (i = 0; i < hw->queues; i++)
1905 ieee80211_stop_queue(hw, i);
1906}
1907EXPORT_SYMBOL(ieee80211_stop_queues);
1908
1909void ieee80211_wake_queues(struct ieee80211_hw *hw)
1910{
1911 int i;
1912
1913 for (i = 0; i < hw->queues; i++)
1914 ieee80211_wake_queue(hw, i);
1915}
1916EXPORT_SYMBOL(ieee80211_wake_queues);
1917
1918struct net_device_stats *ieee80211_dev_stats(struct net_device *dev) 1481struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
1919{ 1482{
1920 struct ieee80211_sub_if_data *sdata; 1483 struct ieee80211_sub_if_data *sdata;
diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h
index 28414b3e0c34..ed00552b3418 100644
--- a/net/mac80211/ieee80211_i.h
+++ b/net/mac80211/ieee80211_i.h
@@ -733,14 +733,6 @@ struct ieee80211_rate *ieee80211_get_rate(struct ieee80211_local *local,
733void ieee80211_key_threshold_notify(struct net_device *dev, 733void ieee80211_key_threshold_notify(struct net_device *dev,
734 struct ieee80211_key *key, 734 struct ieee80211_key *key,
735 struct sta_info *sta); 735 struct sta_info *sta);
736u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len);
737int ieee80211_is_eapol(const struct sk_buff *skb);
738
739extern const unsigned char rfc1042_header[6];
740extern const unsigned char bridge_tunnel_header[6];
741
742int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
743 int rate, int erp, int short_preamble);
744 736
745/* ieee80211_ioctl.c */ 737/* ieee80211_ioctl.c */
746extern const struct iw_handler_def ieee80211_iw_handler_def; 738extern const struct iw_handler_def ieee80211_iw_handler_def;
@@ -833,7 +825,13 @@ struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
833 int idx, size_t key_len, gfp_t flags); 825 int idx, size_t key_len, gfp_t flags);
834void ieee80211_key_free(struct ieee80211_key *key); 826void ieee80211_key_free(struct ieee80211_key *key);
835 827
836/* for wiphy privid */ 828/* utility functions/constants */
837extern void *mac80211_wiphy_privid; 829extern void *mac80211_wiphy_privid; /* for wiphy privid */
830extern const unsigned char rfc1042_header[6];
831extern const unsigned char bridge_tunnel_header[6];
832u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len);
833int ieee80211_is_eapol(const struct sk_buff *skb);
834int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
835 int rate, int erp, int short_preamble);
838 836
839#endif /* IEEE80211_I_H */ 837#endif /* IEEE80211_I_H */
diff --git a/net/mac80211/util.c b/net/mac80211/util.c
new file mode 100644
index 000000000000..c45658309472
--- /dev/null
+++ b/net/mac80211/util.c
@@ -0,0 +1,463 @@
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * utilities for mac80211
12 */
13
14#include <net/mac80211.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
21#include <linux/wireless.h>
22#include <linux/bitmap.h>
23#include <net/cfg80211.h>
24
25#include "ieee80211_i.h"
26#include "ieee80211_rate.h"
27#include "wme.h"
28
29/* privid for wiphys to determine whether they belong to us or not */
30void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
31
32/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
33/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
34const unsigned char rfc1042_header[] =
35 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
36
37/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
38const unsigned char bridge_tunnel_header[] =
39 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
40
41/* No encapsulation header if EtherType < 0x600 (=length) */
42static const unsigned char eapol_header[] =
43 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
44
45
46static int rate_list_match(const int *rate_list, int rate)
47{
48 int i;
49
50 if (!rate_list)
51 return 0;
52
53 for (i = 0; rate_list[i] >= 0; i++)
54 if (rate_list[i] == rate)
55 return 1;
56
57 return 0;
58}
59
60void ieee80211_prepare_rates(struct ieee80211_local *local,
61 struct ieee80211_hw_mode *mode)
62{
63 int i;
64
65 for (i = 0; i < mode->num_rates; i++) {
66 struct ieee80211_rate *rate = &mode->rates[i];
67
68 rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
69 IEEE80211_RATE_BASIC);
70
71 if (local->supp_rates[mode->mode]) {
72 if (!rate_list_match(local->supp_rates[mode->mode],
73 rate->rate))
74 continue;
75 }
76
77 rate->flags |= IEEE80211_RATE_SUPPORTED;
78
79 /* Use configured basic rate set if it is available. If not,
80 * use defaults that are sane for most cases. */
81 if (local->basic_rates[mode->mode]) {
82 if (rate_list_match(local->basic_rates[mode->mode],
83 rate->rate))
84 rate->flags |= IEEE80211_RATE_BASIC;
85 } else switch (mode->mode) {
86 case MODE_IEEE80211A:
87 if (rate->rate == 60 || rate->rate == 120 ||
88 rate->rate == 240)
89 rate->flags |= IEEE80211_RATE_BASIC;
90 break;
91 case MODE_IEEE80211B:
92 if (rate->rate == 10 || rate->rate == 20)
93 rate->flags |= IEEE80211_RATE_BASIC;
94 break;
95 case MODE_ATHEROS_TURBO:
96 if (rate->rate == 120 || rate->rate == 240 ||
97 rate->rate == 480)
98 rate->flags |= IEEE80211_RATE_BASIC;
99 break;
100 case MODE_IEEE80211G:
101 if (rate->rate == 10 || rate->rate == 20 ||
102 rate->rate == 55 || rate->rate == 110)
103 rate->flags |= IEEE80211_RATE_BASIC;
104 break;
105 }
106
107 /* Set ERP and MANDATORY flags based on phymode */
108 switch (mode->mode) {
109 case MODE_IEEE80211A:
110 if (rate->rate == 60 || rate->rate == 120 ||
111 rate->rate == 240)
112 rate->flags |= IEEE80211_RATE_MANDATORY;
113 break;
114 case MODE_IEEE80211B:
115 if (rate->rate == 10)
116 rate->flags |= IEEE80211_RATE_MANDATORY;
117 break;
118 case MODE_ATHEROS_TURBO:
119 break;
120 case MODE_IEEE80211G:
121 if (rate->rate == 10 || rate->rate == 20 ||
122 rate->rate == 55 || rate->rate == 110 ||
123 rate->rate == 60 || rate->rate == 120 ||
124 rate->rate == 240)
125 rate->flags |= IEEE80211_RATE_MANDATORY;
126 break;
127 }
128 if (ieee80211_is_erp_rate(mode->mode, rate->rate))
129 rate->flags |= IEEE80211_RATE_ERP;
130 }
131}
132
133u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
134{
135 u16 fc;
136
137 if (len < 24)
138 return NULL;
139
140 fc = le16_to_cpu(hdr->frame_control);
141
142 switch (fc & IEEE80211_FCTL_FTYPE) {
143 case IEEE80211_FTYPE_DATA:
144 switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
145 case IEEE80211_FCTL_TODS:
146 return hdr->addr1;
147 case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
148 return NULL;
149 case IEEE80211_FCTL_FROMDS:
150 return hdr->addr2;
151 case 0:
152 return hdr->addr3;
153 }
154 break;
155 case IEEE80211_FTYPE_MGMT:
156 return hdr->addr3;
157 case IEEE80211_FTYPE_CTL:
158 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
159 return hdr->addr1;
160 else
161 return NULL;
162 }
163
164 return NULL;
165}
166
167int ieee80211_get_hdrlen(u16 fc)
168{
169 int hdrlen = 24;
170
171 switch (fc & IEEE80211_FCTL_FTYPE) {
172 case IEEE80211_FTYPE_DATA:
173 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
174 hdrlen = 30; /* Addr4 */
175 /*
176 * The QoS Control field is two bytes and its presence is
177 * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
178 * hdrlen if that bit is set.
179 * This works by masking out the bit and shifting it to
180 * bit position 1 so the result has the value 0 or 2.
181 */
182 hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
183 >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
184 break;
185 case IEEE80211_FTYPE_CTL:
186 /*
187 * ACK and CTS are 10 bytes, all others 16. To see how
188 * to get this condition consider
189 * subtype mask: 0b0000000011110000 (0x00F0)
190 * ACK subtype: 0b0000000011010000 (0x00D0)
191 * CTS subtype: 0b0000000011000000 (0x00C0)
192 * bits that matter: ^^^ (0x00E0)
193 * value of those: 0b0000000011000000 (0x00C0)
194 */
195 if ((fc & 0xE0) == 0xC0)
196 hdrlen = 10;
197 else
198 hdrlen = 16;
199 break;
200 }
201
202 return hdrlen;
203}
204EXPORT_SYMBOL(ieee80211_get_hdrlen);
205
206int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
207{
208 const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
209 int hdrlen;
210
211 if (unlikely(skb->len < 10))
212 return 0;
213 hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
214 if (unlikely(hdrlen > skb->len))
215 return 0;
216 return hdrlen;
217}
218EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
219
220int ieee80211_is_eapol(const struct sk_buff *skb)
221{
222 const struct ieee80211_hdr *hdr;
223 u16 fc;
224 int hdrlen;
225
226 if (unlikely(skb->len < 10))
227 return 0;
228
229 hdr = (const struct ieee80211_hdr *) skb->data;
230 fc = le16_to_cpu(hdr->frame_control);
231
232 if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
233 return 0;
234
235 hdrlen = ieee80211_get_hdrlen(fc);
236
237 if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
238 memcmp(skb->data + hdrlen, eapol_header,
239 sizeof(eapol_header)) == 0))
240 return 1;
241
242 return 0;
243}
244
245void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
246{
247 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
248
249 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
250 if (tx->u.tx.extra_frag) {
251 struct ieee80211_hdr *fhdr;
252 int i;
253 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
254 fhdr = (struct ieee80211_hdr *)
255 tx->u.tx.extra_frag[i]->data;
256 fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
257 }
258 }
259}
260
261int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
262 int rate, int erp, int short_preamble)
263{
264 int dur;
265
266 /* calculate duration (in microseconds, rounded up to next higher
267 * integer if it includes a fractional microsecond) to send frame of
268 * len bytes (does not include FCS) at the given rate. Duration will
269 * also include SIFS.
270 *
271 * rate is in 100 kbps, so divident is multiplied by 10 in the
272 * DIV_ROUND_UP() operations.
273 */
274
275 if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
276 local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
277 /*
278 * OFDM:
279 *
280 * N_DBPS = DATARATE x 4
281 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
282 * (16 = SIGNAL time, 6 = tail bits)
283 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
284 *
285 * T_SYM = 4 usec
286 * 802.11a - 17.5.2: aSIFSTime = 16 usec
287 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
288 * signal ext = 6 usec
289 */
290 /* FIX: Atheros Turbo may have different (shorter) duration? */
291 dur = 16; /* SIFS + signal ext */
292 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
293 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
294 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
295 4 * rate); /* T_SYM x N_SYM */
296 } else {
297 /*
298 * 802.11b or 802.11g with 802.11b compatibility:
299 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
300 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
301 *
302 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
303 * aSIFSTime = 10 usec
304 * aPreambleLength = 144 usec or 72 usec with short preamble
305 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
306 */
307 dur = 10; /* aSIFSTime = 10 usec */
308 dur += short_preamble ? (72 + 24) : (144 + 48);
309
310 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
311 }
312
313 return dur;
314}
315
316/* Exported duration function for driver use */
317__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
318 size_t frame_len, int rate)
319{
320 struct ieee80211_local *local = hw_to_local(hw);
321 u16 dur;
322 int erp;
323
324 erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
325 dur = ieee80211_frame_duration(local, frame_len, rate,
326 erp, local->short_preamble);
327
328 return cpu_to_le16(dur);
329}
330EXPORT_SYMBOL(ieee80211_generic_frame_duration);
331
332__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
333 size_t frame_len,
334 const struct ieee80211_tx_control *frame_txctl)
335{
336 struct ieee80211_local *local = hw_to_local(hw);
337 struct ieee80211_rate *rate;
338 int short_preamble = local->short_preamble;
339 int erp;
340 u16 dur;
341
342 rate = frame_txctl->rts_rate;
343 erp = !!(rate->flags & IEEE80211_RATE_ERP);
344
345 /* CTS duration */
346 dur = ieee80211_frame_duration(local, 10, rate->rate,
347 erp, short_preamble);
348 /* Data frame duration */
349 dur += ieee80211_frame_duration(local, frame_len, rate->rate,
350 erp, short_preamble);
351 /* ACK duration */
352 dur += ieee80211_frame_duration(local, 10, rate->rate,
353 erp, short_preamble);
354
355 return cpu_to_le16(dur);
356}
357EXPORT_SYMBOL(ieee80211_rts_duration);
358
359__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
360 size_t frame_len,
361 const struct ieee80211_tx_control *frame_txctl)
362{
363 struct ieee80211_local *local = hw_to_local(hw);
364 struct ieee80211_rate *rate;
365 int short_preamble = local->short_preamble;
366 int erp;
367 u16 dur;
368
369 rate = frame_txctl->rts_rate;
370 erp = !!(rate->flags & IEEE80211_RATE_ERP);
371
372 /* Data frame duration */
373 dur = ieee80211_frame_duration(local, frame_len, rate->rate,
374 erp, short_preamble);
375 if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
376 /* ACK duration */
377 dur += ieee80211_frame_duration(local, 10, rate->rate,
378 erp, short_preamble);
379 }
380
381 return cpu_to_le16(dur);
382}
383EXPORT_SYMBOL(ieee80211_ctstoself_duration);
384
385struct ieee80211_rate *
386ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
387{
388 struct ieee80211_hw_mode *mode;
389 int r;
390
391 list_for_each_entry(mode, &local->modes_list, list) {
392 if (mode->mode != phymode)
393 continue;
394 for (r = 0; r < mode->num_rates; r++) {
395 struct ieee80211_rate *rate = &mode->rates[r];
396 if (rate->val == hw_rate ||
397 (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
398 rate->val2 == hw_rate))
399 return rate;
400 }
401 }
402
403 return NULL;
404}
405
406void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
407{
408 struct ieee80211_local *local = hw_to_local(hw);
409
410 if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
411 &local->state[queue])) {
412 if (test_bit(IEEE80211_LINK_STATE_PENDING,
413 &local->state[queue]))
414 tasklet_schedule(&local->tx_pending_tasklet);
415 else
416 if (!ieee80211_qdisc_installed(local->mdev)) {
417 if (queue == 0)
418 netif_wake_queue(local->mdev);
419 } else
420 __netif_schedule(local->mdev);
421 }
422}
423EXPORT_SYMBOL(ieee80211_wake_queue);
424
425void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
426{
427 struct ieee80211_local *local = hw_to_local(hw);
428
429 if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
430 netif_stop_queue(local->mdev);
431 set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
432}
433EXPORT_SYMBOL(ieee80211_stop_queue);
434
435void ieee80211_start_queues(struct ieee80211_hw *hw)
436{
437 struct ieee80211_local *local = hw_to_local(hw);
438 int i;
439
440 for (i = 0; i < local->hw.queues; i++)
441 clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
442 if (!ieee80211_qdisc_installed(local->mdev))
443 netif_start_queue(local->mdev);
444}
445EXPORT_SYMBOL(ieee80211_start_queues);
446
447void ieee80211_stop_queues(struct ieee80211_hw *hw)
448{
449 int i;
450
451 for (i = 0; i < hw->queues; i++)
452 ieee80211_stop_queue(hw, i);
453}
454EXPORT_SYMBOL(ieee80211_stop_queues);
455
456void ieee80211_wake_queues(struct ieee80211_hw *hw)
457{
458 int i;
459
460 for (i = 0; i < hw->queues; i++)
461 ieee80211_wake_queue(hw, i);
462}
463EXPORT_SYMBOL(ieee80211_wake_queues);