Merge commit '4e6ce4dc7ce71d0886908d55129d5d6482a27ff9' of git://git.kernel.org/pub...
[cascardo/linux.git] / net / mac80211 / wme.c
1 /*
2  * Copyright 2004, Instant802 Networks, Inc.
3  * Copyright 2013-2014  Intel Mobile Communications GmbH
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/module.h>
13 #include <linux/if_arp.h>
14 #include <linux/types.h>
15 #include <net/ip.h>
16 #include <net/pkt_sched.h>
17
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "wme.h"
21
22 /* Default mapping in classifier to work with default
23  * queue setup.
24  */
25 const int ieee802_1d_to_ac[8] = {
26         IEEE80211_AC_BE,
27         IEEE80211_AC_BK,
28         IEEE80211_AC_BK,
29         IEEE80211_AC_BE,
30         IEEE80211_AC_VI,
31         IEEE80211_AC_VI,
32         IEEE80211_AC_VO,
33         IEEE80211_AC_VO
34 };
35
36 static int wme_downgrade_ac(struct sk_buff *skb)
37 {
38         switch (skb->priority) {
39         case 6:
40         case 7:
41                 skb->priority = 5; /* VO -> VI */
42                 return 0;
43         case 4:
44         case 5:
45                 skb->priority = 3; /* VI -> BE */
46                 return 0;
47         case 0:
48         case 3:
49                 skb->priority = 2; /* BE -> BK */
50                 return 0;
51         default:
52                 return -1;
53         }
54 }
55
56 static u16 ieee80211_downgrade_queue(struct ieee80211_sub_if_data *sdata,
57                                      struct sta_info *sta, struct sk_buff *skb)
58 {
59         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
60
61         /* in case we are a client verify acm is not set for this ac */
62         while (sdata->wmm_acm & BIT(skb->priority)) {
63                 int ac = ieee802_1d_to_ac[skb->priority];
64
65                 if (ifmgd->tx_tspec[ac].admitted_time &&
66                     skb->priority == ifmgd->tx_tspec[ac].up)
67                         return ac;
68
69                 if (wme_downgrade_ac(skb)) {
70                         /*
71                          * This should not really happen. The AP has marked all
72                          * lower ACs to require admission control which is not
73                          * a reasonable configuration. Allow the frame to be
74                          * transmitted using AC_BK as a workaround.
75                          */
76                         break;
77                 }
78         }
79
80         /* look up which queue to use for frames with this 1d tag */
81         return ieee802_1d_to_ac[skb->priority];
82 }
83
84 /* Indicate which queue to use for this fully formed 802.11 frame */
85 u16 ieee80211_select_queue_80211(struct ieee80211_sub_if_data *sdata,
86                                  struct sk_buff *skb,
87                                  struct ieee80211_hdr *hdr)
88 {
89         struct ieee80211_local *local = sdata->local;
90         u8 *p;
91
92         if (local->hw.queues < IEEE80211_NUM_ACS)
93                 return 0;
94
95         if (!ieee80211_is_data(hdr->frame_control)) {
96                 skb->priority = 7;
97                 return ieee802_1d_to_ac[skb->priority];
98         }
99         if (!ieee80211_is_data_qos(hdr->frame_control)) {
100                 skb->priority = 0;
101                 return ieee802_1d_to_ac[skb->priority];
102         }
103
104         p = ieee80211_get_qos_ctl(hdr);
105         skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
106
107         return ieee80211_downgrade_queue(sdata, NULL, skb);
108 }
109
110 /* Indicate which queue to use. */
111 u16 ieee80211_select_queue(struct ieee80211_sub_if_data *sdata,
112                            struct sk_buff *skb)
113 {
114         struct ieee80211_local *local = sdata->local;
115         struct sta_info *sta = NULL;
116         const u8 *ra = NULL;
117         bool qos = false;
118         struct mac80211_qos_map *qos_map;
119         u16 ret;
120
121         if (local->hw.queues < IEEE80211_NUM_ACS || skb->len < 6) {
122                 skb->priority = 0; /* required for correct WPA/11i MIC */
123                 return 0;
124         }
125
126         rcu_read_lock();
127         switch (sdata->vif.type) {
128         case NL80211_IFTYPE_AP_VLAN:
129                 sta = rcu_dereference(sdata->u.vlan.sta);
130                 if (sta) {
131                         qos = sta->sta.wme;
132                         break;
133                 }
134         case NL80211_IFTYPE_AP:
135                 ra = skb->data;
136                 break;
137         case NL80211_IFTYPE_WDS:
138                 ra = sdata->u.wds.remote_addr;
139                 break;
140 #ifdef CONFIG_MAC80211_MESH
141         case NL80211_IFTYPE_MESH_POINT:
142                 qos = true;
143                 break;
144 #endif
145         case NL80211_IFTYPE_STATION:
146                 ra = sdata->u.mgd.bssid;
147                 break;
148         case NL80211_IFTYPE_ADHOC:
149                 ra = skb->data;
150                 break;
151         case NL80211_IFTYPE_OCB:
152                 /* all stations are required to support WME */
153                 qos = true;
154                 break;
155         default:
156                 break;
157         }
158
159         if (!sta && ra && !is_multicast_ether_addr(ra)) {
160                 sta = sta_info_get(sdata, ra);
161                 if (sta)
162                         qos = sta->sta.wme;
163         }
164
165         if (!qos) {
166                 skb->priority = 0; /* required for correct WPA/11i MIC */
167                 ret = IEEE80211_AC_BE;
168                 goto out;
169         }
170
171         if (skb->protocol == sdata->control_port_protocol) {
172                 skb->priority = 7;
173                 goto downgrade;
174         }
175
176         /* use the data classifier to determine what 802.1d tag the
177          * data frame has */
178         qos_map = rcu_dereference(sdata->qos_map);
179         skb->priority = cfg80211_classify8021d(skb, qos_map ?
180                                                &qos_map->qos_map : NULL);
181
182  downgrade:
183         ret = ieee80211_downgrade_queue(sdata, sta, skb);
184  out:
185         rcu_read_unlock();
186         return ret;
187 }
188
189 /**
190  * ieee80211_set_qos_hdr - Fill in the QoS header if there is one.
191  *
192  * @sdata: local subif
193  * @skb: packet to be updated
194  */
195 void ieee80211_set_qos_hdr(struct ieee80211_sub_if_data *sdata,
196                            struct sk_buff *skb)
197 {
198         struct ieee80211_hdr *hdr = (void *)skb->data;
199         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
200         u8 *p;
201         u8 ack_policy, tid;
202
203         if (!ieee80211_is_data_qos(hdr->frame_control))
204                 return;
205
206         p = ieee80211_get_qos_ctl(hdr);
207         tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
208
209         /* preserve EOSP bit */
210         ack_policy = *p & IEEE80211_QOS_CTL_EOSP;
211
212         if (is_multicast_ether_addr(hdr->addr1) ||
213             sdata->noack_map & BIT(tid)) {
214                 ack_policy |= IEEE80211_QOS_CTL_ACK_POLICY_NOACK;
215                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
216         }
217
218         /* qos header is 2 bytes */
219         *p++ = ack_policy | tid;
220         if (ieee80211_vif_is_mesh(&sdata->vif)) {
221                 /* preserve RSPI and Mesh PS Level bit */
222                 *p &= ((IEEE80211_QOS_CTL_RSPI |
223                         IEEE80211_QOS_CTL_MESH_PS_LEVEL) >> 8);
224
225                 /* Nulls don't have a mesh header (frame body) */
226                 if (!ieee80211_is_qos_nullfunc(hdr->frame_control))
227                         *p |= (IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8);
228         } else {
229                 *p = 0;
230         }
231 }