Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b7fd81c authored by Toke Høiland-Jørgensen's avatar Toke Høiland-Jørgensen Committed by Greg Kroah-Hartman
Browse files

sched: consistently handle layer3 header accesses in the presence of VLANs



[ Upstream commit d7bf2ebebc2bd61ab95e2a8e33541ef282f303d4 ]

There are a couple of places in net/sched/ that check skb->protocol and act
on the value there. However, in the presence of VLAN tags, the value stored
in skb->protocol can be inconsistent based on whether VLAN acceleration is
enabled. The commit quoted in the Fixes tag below fixed the users of
skb->protocol to use a helper that will always see the VLAN ethertype.

However, most of the callers don't actually handle the VLAN ethertype, but
expect to find the IP header type in the protocol field. This means that
things like changing the ECN field, or parsing diffserv values, stops
working if there's a VLAN tag, or if there are multiple nested VLAN
tags (QinQ).

To fix this, change the helper to take an argument that indicates whether
the caller wants to skip the VLAN tags or not. When skipping VLAN tags, we
make sure to skip all of them, so behaviour is consistent even in QinQ
mode.

To make the helper usable from the ECN code, move it to if_vlan.h instead
of pkt_sched.h.

v3:
- Remove empty lines
- Move vlan variable definitions inside loop in skb_protocol()
- Also use skb_protocol() helper in IP{,6}_ECN_decapsulate() and
  bpf_skb_ecn_set_ce()

v2:
- Use eth_type_vlan() helper in skb_protocol()
- Also fix code that reads skb->protocol directly
- Change a couple of 'if/else if' statements to switch constructs to avoid
  calling the helper twice

Reported-by: default avatarIlya Ponetayev <i.ponetaev@ndmsystems.com>
Fixes: d8b9605d ("net: sched: fix skb->protocol use in case of accelerated vlan path")
Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent aafe9dd1
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -308,6 +308,34 @@ static inline bool eth_type_vlan(__be16 ethertype)
	}
}

/* A getter for the SKB protocol field which will handle VLAN tags consistently
 * whether VLAN acceleration is enabled or not.
 */
static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan)
{
	unsigned int offset = skb_mac_offset(skb) + sizeof(struct ethhdr);
	__be16 proto = skb->protocol;

	if (!skip_vlan)
		/* VLAN acceleration strips the VLAN header from the skb and
		 * moves it to skb->vlan_proto
		 */
		return skb_vlan_tag_present(skb) ? skb->vlan_proto : proto;

	while (eth_type_vlan(proto)) {
		struct vlan_hdr vhdr, *vh;

		vh = skb_header_pointer(skb, offset, sizeof(vhdr), &vhdr);
		if (!vh)
			break;

		proto = vh->h_vlan_encapsulated_proto;
		offset += sizeof(vhdr);
	}

	return proto;
}

static inline bool vlan_hw_offload_capable(netdev_features_t features,
					   __be16 proto)
{
+17 −8
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

#include <linux/ip.h>
#include <linux/skbuff.h>
#include <linux/if_vlan.h>

#include <net/inet_sock.h>
#include <net/dsfield.h>
@@ -172,7 +173,7 @@ static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)

static inline int INET_ECN_set_ce(struct sk_buff *skb)
{
	switch (skb->protocol) {
	switch (skb_protocol(skb, true)) {
	case cpu_to_be16(ETH_P_IP):
		if (skb_network_header(skb) + sizeof(struct iphdr) <=
		    skb_tail_pointer(skb))
@@ -191,7 +192,7 @@ static inline int INET_ECN_set_ce(struct sk_buff *skb)

static inline int INET_ECN_set_ect1(struct sk_buff *skb)
{
	switch (skb->protocol) {
	switch (skb_protocol(skb, true)) {
	case cpu_to_be16(ETH_P_IP):
		if (skb_network_header(skb) + sizeof(struct iphdr) <=
		    skb_tail_pointer(skb))
@@ -272,12 +273,16 @@ static inline int IP_ECN_decapsulate(const struct iphdr *oiph,
{
	__u8 inner;

	if (skb->protocol == htons(ETH_P_IP))
	switch (skb_protocol(skb, true)) {
	case htons(ETH_P_IP):
		inner = ip_hdr(skb)->tos;
	else if (skb->protocol == htons(ETH_P_IPV6))
		break;
	case htons(ETH_P_IPV6):
		inner = ipv6_get_dsfield(ipv6_hdr(skb));
	else
		break;
	default:
		return 0;
	}

	return INET_ECN_decapsulate(skb, oiph->tos, inner);
}
@@ -287,12 +292,16 @@ static inline int IP6_ECN_decapsulate(const struct ipv6hdr *oipv6h,
{
	__u8 inner;

	if (skb->protocol == htons(ETH_P_IP))
	switch (skb_protocol(skb, true)) {
	case htons(ETH_P_IP):
		inner = ip_hdr(skb)->tos;
	else if (skb->protocol == htons(ETH_P_IPV6))
		break;
	case htons(ETH_P_IPV6):
		inner = ipv6_get_dsfield(ipv6_hdr(skb));
	else
		break;
	default:
		return 0;
	}

	return INET_ECN_decapsulate(skb, ipv6_get_dsfield(oipv6h), inner);
}
+0 −11
Original line number Diff line number Diff line
@@ -128,17 +128,6 @@ static inline void qdisc_run(struct Qdisc *q)
	}
}

static inline __be16 tc_skb_protocol(const struct sk_buff *skb)
{
	/* We need to take extra care in case the skb came via
	 * vlan accelerated path. In that case, use skb->vlan_proto
	 * as the original vlan header was already stripped.
	 */
	if (skb_vlan_tag_present(skb))
		return skb->vlan_proto;
	return skb->protocol;
}

/* Calculate maximal size of packet seen by hard_start_xmit
   routine of this device.
 */
+7 −3
Original line number Diff line number Diff line
@@ -5730,12 +5730,16 @@ BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
{
	unsigned int iphdr_len;

	if (skb->protocol == cpu_to_be16(ETH_P_IP))
	switch (skb_protocol(skb, true)) {
	case cpu_to_be16(ETH_P_IP):
		iphdr_len = sizeof(struct iphdr);
	else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
		break;
	case cpu_to_be16(ETH_P_IPV6):
		iphdr_len = sizeof(struct ipv6hdr);
	else
		break;
	default:
		return 0;
	}

	if (skb_headlen(skb) < iphdr_len)
		return 0;
+6 −3
Original line number Diff line number Diff line
@@ -43,17 +43,20 @@ static int tcf_connmark_act(struct sk_buff *skb, const struct tc_action *a,
	tcf_lastuse_update(&ca->tcf_tm);
	bstats_update(&ca->tcf_bstats, skb);

	if (skb->protocol == htons(ETH_P_IP)) {
	switch (skb_protocol(skb, true)) {
	case htons(ETH_P_IP):
		if (skb->len < sizeof(struct iphdr))
			goto out;

		proto = NFPROTO_IPV4;
	} else if (skb->protocol == htons(ETH_P_IPV6)) {
		break;
	case htons(ETH_P_IPV6):
		if (skb->len < sizeof(struct ipv6hdr))
			goto out;

		proto = NFPROTO_IPV6;
	} else {
		break;
	default:
		goto out;
	}

Loading