xref: /openbmc/linux/net/mac80211/wme.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2004, Instant802 Networks, Inc.
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5bfd8403aSJohannes Berg  * Copyright (C) 2022 Intel Corporation
6f0706e82SJiri Benc  */
7f0706e82SJiri Benc 
8f0706e82SJiri Benc #include <linux/netdevice.h>
9f0706e82SJiri Benc #include <linux/skbuff.h>
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/if_arp.h>
12f0706e82SJiri Benc #include <linux/types.h>
13f0706e82SJiri Benc #include <net/ip.h>
14f0706e82SJiri Benc #include <net/pkt_sched.h>
15f0706e82SJiri Benc 
16f0706e82SJiri Benc #include <net/mac80211.h>
17f0706e82SJiri Benc #include "ieee80211_i.h"
18f0706e82SJiri Benc #include "wme.h"
19f0706e82SJiri Benc 
2051cb6db0SDavid S. Miller /* Default mapping in classifier to work with default
21e100bb64SJohannes Berg  * queue setup.
22e100bb64SJohannes Berg  */
234bce22b9SJohannes Berg const int ieee802_1d_to_ac[8] = {
244bce22b9SJohannes Berg 	IEEE80211_AC_BE,
254bce22b9SJohannes Berg 	IEEE80211_AC_BK,
264bce22b9SJohannes Berg 	IEEE80211_AC_BK,
274bce22b9SJohannes Berg 	IEEE80211_AC_BE,
284bce22b9SJohannes Berg 	IEEE80211_AC_VI,
294bce22b9SJohannes Berg 	IEEE80211_AC_VI,
304bce22b9SJohannes Berg 	IEEE80211_AC_VO,
314bce22b9SJohannes Berg 	IEEE80211_AC_VO
324bce22b9SJohannes Berg };
33f0706e82SJiri Benc 
wme_downgrade_ac(struct sk_buff * skb)3451cb6db0SDavid S. Miller static int wme_downgrade_ac(struct sk_buff *skb)
35f0706e82SJiri Benc {
36f0706e82SJiri Benc 	switch (skb->priority) {
37f0706e82SJiri Benc 	case 6:
38f0706e82SJiri Benc 	case 7:
39f0706e82SJiri Benc 		skb->priority = 5; /* VO -> VI */
40f0706e82SJiri Benc 		return 0;
41f0706e82SJiri Benc 	case 4:
42f0706e82SJiri Benc 	case 5:
43f0706e82SJiri Benc 		skb->priority = 3; /* VI -> BE */
44f0706e82SJiri Benc 		return 0;
45f0706e82SJiri Benc 	case 0:
46f0706e82SJiri Benc 	case 3:
47f0706e82SJiri Benc 		skb->priority = 2; /* BE -> BK */
48f0706e82SJiri Benc 		return 0;
49f0706e82SJiri Benc 	default:
50f0706e82SJiri Benc 		return -1;
51f0706e82SJiri Benc 	}
52f0706e82SJiri Benc }
53f0706e82SJiri Benc 
54b6da911bSLiad Kaufman /**
55b6da911bSLiad Kaufman  * ieee80211_fix_reserved_tid - return the TID to use if this one is reserved
56b6da911bSLiad Kaufman  * @tid: the assumed-reserved TID
57b6da911bSLiad Kaufman  *
58b6da911bSLiad Kaufman  * Returns: the alternative TID to use, or 0 on error
59b6da911bSLiad Kaufman  */
ieee80211_fix_reserved_tid(u8 tid)60b6da911bSLiad Kaufman static inline u8 ieee80211_fix_reserved_tid(u8 tid)
61b6da911bSLiad Kaufman {
62b6da911bSLiad Kaufman 	switch (tid) {
63b6da911bSLiad Kaufman 	case 0:
64b6da911bSLiad Kaufman 		return 3;
65b6da911bSLiad Kaufman 	case 1:
66b6da911bSLiad Kaufman 		return 2;
67b6da911bSLiad Kaufman 	case 2:
68b6da911bSLiad Kaufman 		return 1;
69b6da911bSLiad Kaufman 	case 3:
70b6da911bSLiad Kaufman 		return 0;
71b6da911bSLiad Kaufman 	case 4:
72b6da911bSLiad Kaufman 		return 5;
73b6da911bSLiad Kaufman 	case 5:
74b6da911bSLiad Kaufman 		return 4;
75b6da911bSLiad Kaufman 	case 6:
76b6da911bSLiad Kaufman 		return 7;
77b6da911bSLiad Kaufman 	case 7:
78b6da911bSLiad Kaufman 		return 6;
79b6da911bSLiad Kaufman 	}
80b6da911bSLiad Kaufman 
81b6da911bSLiad Kaufman 	return 0;
82b6da911bSLiad Kaufman }
83b6da911bSLiad Kaufman 
ieee80211_downgrade_queue(struct ieee80211_sub_if_data * sdata,struct sta_info * sta,struct sk_buff * skb)8400e96decSYoni Divinsky static u16 ieee80211_downgrade_queue(struct ieee80211_sub_if_data *sdata,
8502219b3aSJohannes Berg 				     struct sta_info *sta, struct sk_buff *skb)
864670cf7aSJohannes Berg {
8702219b3aSJohannes Berg 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
8802219b3aSJohannes Berg 
894670cf7aSJohannes Berg 	/* in case we are a client verify acm is not set for this ac */
9002219b3aSJohannes Berg 	while (sdata->wmm_acm & BIT(skb->priority)) {
9102219b3aSJohannes Berg 		int ac = ieee802_1d_to_ac[skb->priority];
9202219b3aSJohannes Berg 
9302219b3aSJohannes Berg 		if (ifmgd->tx_tspec[ac].admitted_time &&
9402219b3aSJohannes Berg 		    skb->priority == ifmgd->tx_tspec[ac].up)
9502219b3aSJohannes Berg 			return ac;
9602219b3aSJohannes Berg 
974670cf7aSJohannes Berg 		if (wme_downgrade_ac(skb)) {
984670cf7aSJohannes Berg 			/*
994670cf7aSJohannes Berg 			 * This should not really happen. The AP has marked all
1004670cf7aSJohannes Berg 			 * lower ACs to require admission control which is not
1014670cf7aSJohannes Berg 			 * a reasonable configuration. Allow the frame to be
1024670cf7aSJohannes Berg 			 * transmitted using AC_BK as a workaround.
1034670cf7aSJohannes Berg 			 */
1044670cf7aSJohannes Berg 			break;
1054670cf7aSJohannes Berg 		}
1064670cf7aSJohannes Berg 	}
1074670cf7aSJohannes Berg 
108b6da911bSLiad Kaufman 	/* Check to see if this is a reserved TID */
109b6da911bSLiad Kaufman 	if (sta && sta->reserved_tid == skb->priority)
110b6da911bSLiad Kaufman 		skb->priority = ieee80211_fix_reserved_tid(skb->priority);
111b6da911bSLiad Kaufman 
1124670cf7aSJohannes Berg 	/* look up which queue to use for frames with this 1d tag */
1134670cf7aSJohannes Berg 	return ieee802_1d_to_ac[skb->priority];
1144670cf7aSJohannes Berg }
1154670cf7aSJohannes Berg 
116d3c1597bSThomas Pedersen /* Indicate which queue to use for this fully formed 802.11 frame */
ieee80211_select_queue_80211(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb,struct ieee80211_hdr * hdr)11700e96decSYoni Divinsky u16 ieee80211_select_queue_80211(struct ieee80211_sub_if_data *sdata,
118d3c1597bSThomas Pedersen 				 struct sk_buff *skb,
119d3c1597bSThomas Pedersen 				 struct ieee80211_hdr *hdr)
120d3c1597bSThomas Pedersen {
12100e96decSYoni Divinsky 	struct ieee80211_local *local = sdata->local;
12266d06c84SMathy Vanhoef 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
123d3c1597bSThomas Pedersen 	u8 *p;
124d3c1597bSThomas Pedersen 
125107395f9SAlexander Wetzel 	/* Ensure hash is set prior to potential SW encryption */
126107395f9SAlexander Wetzel 	skb_get_hash(skb);
127107395f9SAlexander Wetzel 
12866d06c84SMathy Vanhoef 	if ((info->control.flags & IEEE80211_TX_CTRL_DONT_REORDER) ||
12966d06c84SMathy Vanhoef 	    local->hw.queues < IEEE80211_NUM_ACS)
130d3c1597bSThomas Pedersen 		return 0;
131d3c1597bSThomas Pedersen 
132d3c1597bSThomas Pedersen 	if (!ieee80211_is_data(hdr->frame_control)) {
133d3c1597bSThomas Pedersen 		skb->priority = 7;
134d3c1597bSThomas Pedersen 		return ieee802_1d_to_ac[skb->priority];
135d3c1597bSThomas Pedersen 	}
136d3c1597bSThomas Pedersen 	if (!ieee80211_is_data_qos(hdr->frame_control)) {
137d3c1597bSThomas Pedersen 		skb->priority = 0;
138d3c1597bSThomas Pedersen 		return ieee802_1d_to_ac[skb->priority];
139d3c1597bSThomas Pedersen 	}
140d3c1597bSThomas Pedersen 
141d3c1597bSThomas Pedersen 	p = ieee80211_get_qos_ctl(hdr);
142d3c1597bSThomas Pedersen 	skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
143d3c1597bSThomas Pedersen 
14402219b3aSJohannes Berg 	return ieee80211_downgrade_queue(sdata, NULL, skb);
145d3c1597bSThomas Pedersen }
146f0706e82SJiri Benc 
ieee80211_select_queue(struct ieee80211_sub_if_data * sdata,struct sta_info * sta,struct sk_buff * skb)147107395f9SAlexander Wetzel u16 ieee80211_select_queue(struct ieee80211_sub_if_data *sdata,
1481974da8bSFelix Fietkau 			   struct sta_info *sta, struct sk_buff *skb)
149f0706e82SJiri Benc {
150*4e348c6cSFelix Fietkau 	const struct ethhdr *eth = (void *)skb->data;
15132db6b54SKyeyoon Park 	struct mac80211_qos_map *qos_map;
1521974da8bSFelix Fietkau 	bool qos;
153f0706e82SJiri Benc 
154107395f9SAlexander Wetzel 	/* Ensure hash is set prior to potential SW encryption */
155107395f9SAlexander Wetzel 	skb_get_hash(skb);
156107395f9SAlexander Wetzel 
1571974da8bSFelix Fietkau 	/* all mesh/ocb stations are required to support WME */
158*4e348c6cSFelix Fietkau 	if ((sdata->vif.type == NL80211_IFTYPE_MESH_POINT &&
159*4e348c6cSFelix Fietkau 	    !is_multicast_ether_addr(eth->h_dest)) ||
160*4e348c6cSFelix Fietkau 	    (sdata->vif.type == NL80211_IFTYPE_OCB && sta))
161d0ce1855SJavier Cardona 		qos = true;
1621974da8bSFelix Fietkau 	else if (sta)
163b6da911bSLiad Kaufman 		qos = sta->sta.wme;
1641974da8bSFelix Fietkau 	else
1651974da8bSFelix Fietkau 		qos = false;
166cf0277e7SJohannes Berg 
167f6ab25d4SFelix Fietkau 	if (!qos) {
168f0706e82SJiri Benc 		skb->priority = 0; /* required for correct WPA/11i MIC */
1691974da8bSFelix Fietkau 		return IEEE80211_AC_BE;
170f0706e82SJiri Benc 	}
171f0706e82SJiri Benc 
1721bf4bbb4SFelix Fietkau 	if (skb->protocol == sdata->control_port_protocol) {
1731bf4bbb4SFelix Fietkau 		skb->priority = 7;
17402219b3aSJohannes Berg 		goto downgrade;
1751bf4bbb4SFelix Fietkau 	}
1761bf4bbb4SFelix Fietkau 
177f0706e82SJiri Benc 	/* use the data classifier to determine what 802.1d tag the
178f0706e82SJiri Benc 	 * data frame has */
17932db6b54SKyeyoon Park 	qos_map = rcu_dereference(sdata->qos_map);
18032db6b54SKyeyoon Park 	skb->priority = cfg80211_classify8021d(skb, qos_map ?
18132db6b54SKyeyoon Park 					       &qos_map->qos_map : NULL);
182f0706e82SJiri Benc 
18302219b3aSJohannes Berg  downgrade:
1841974da8bSFelix Fietkau 	return ieee80211_downgrade_queue(sdata, sta, skb);
1851974da8bSFelix Fietkau }
1861974da8bSFelix Fietkau 
18740aefedcSMarco Porsch /**
18840aefedcSMarco Porsch  * ieee80211_set_qos_hdr - Fill in the QoS header if there is one.
18940aefedcSMarco Porsch  *
19040aefedcSMarco Porsch  * @sdata: local subif
19140aefedcSMarco Porsch  * @skb: packet to be updated
19240aefedcSMarco Porsch  */
ieee80211_set_qos_hdr(struct ieee80211_sub_if_data * sdata,struct sk_buff * skb)1932154c81cSJavier Cardona void ieee80211_set_qos_hdr(struct ieee80211_sub_if_data *sdata,
1942154c81cSJavier Cardona 			   struct sk_buff *skb)
195f0706e82SJiri Benc {
196cf0277e7SJohannes Berg 	struct ieee80211_hdr *hdr = (void *)skb->data;
197b53be792SSimon Wunderlich 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
19832910bb9SJohannes Berg 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
19932910bb9SJohannes Berg 	u8 flags;
20040aefedcSMarco Porsch 	u8 *p;
201cf0277e7SJohannes Berg 
20240aefedcSMarco Porsch 	if (!ieee80211_is_data_qos(hdr->frame_control))
20340aefedcSMarco Porsch 		return;
20440aefedcSMarco Porsch 
20540aefedcSMarco Porsch 	p = ieee80211_get_qos_ctl(hdr);
206cf0277e7SJohannes Berg 
207527d6759SMathy Vanhoef 	/* don't overwrite the QoS field of injected frames */
208527d6759SMathy Vanhoef 	if (info->flags & IEEE80211_TX_CTL_INJECTED) {
209527d6759SMathy Vanhoef 		/* do take into account Ack policy of injected frames */
210527d6759SMathy Vanhoef 		if (*p & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
211527d6759SMathy Vanhoef 			info->flags |= IEEE80211_TX_CTL_NO_ACK;
212527d6759SMathy Vanhoef 		return;
213527d6759SMathy Vanhoef 	}
214527d6759SMathy Vanhoef 
21532910bb9SJohannes Berg 	/* set up the first byte */
21632910bb9SJohannes Berg 
21732910bb9SJohannes Berg 	/*
21832910bb9SJohannes Berg 	 * preserve everything but the TID and ACK policy
21932910bb9SJohannes Berg 	 * (which we both write here)
22032910bb9SJohannes Berg 	 */
22132910bb9SJohannes Berg 	flags = *p & ~(IEEE80211_QOS_CTL_TID_MASK |
22232910bb9SJohannes Berg 		       IEEE80211_QOS_CTL_ACK_POLICY_MASK);
22368629c61SJohannes Berg 
224b53be792SSimon Wunderlich 	if (is_multicast_ether_addr(hdr->addr1) ||
225b53be792SSimon Wunderlich 	    sdata->noack_map & BIT(tid)) {
22632910bb9SJohannes Berg 		flags |= IEEE80211_QOS_CTL_ACK_POLICY_NOACK;
227b53be792SSimon Wunderlich 		info->flags |= IEEE80211_TX_CTL_NO_ACK;
228b53be792SSimon Wunderlich 	}
229b53be792SSimon Wunderlich 
23032910bb9SJohannes Berg 	*p = flags | tid;
23132910bb9SJohannes Berg 
23232910bb9SJohannes Berg 	/* set up the second byte */
23332910bb9SJohannes Berg 	p++;
23432910bb9SJohannes Berg 
2353f52b7e3SMarco Porsch 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2363f52b7e3SMarco Porsch 		/* preserve RSPI and Mesh PS Level bit */
2373f52b7e3SMarco Porsch 		*p &= ((IEEE80211_QOS_CTL_RSPI |
2383f52b7e3SMarco Porsch 			IEEE80211_QOS_CTL_MESH_PS_LEVEL) >> 8);
2393f52b7e3SMarco Porsch 
2403f52b7e3SMarco Porsch 		/* Nulls don't have a mesh header (frame body) */
2413f52b7e3SMarco Porsch 		if (!ieee80211_is_qos_nullfunc(hdr->frame_control))
2423f52b7e3SMarco Porsch 			*p |= (IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT >> 8);
2433f52b7e3SMarco Porsch 	} else {
2443f52b7e3SMarco Porsch 		*p = 0;
2453f52b7e3SMarco Porsch 	}
246f0706e82SJiri Benc }
247