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

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

[ Upstream commit d7bf2ebe ]

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 6d584c6e
...@@ -313,6 +313,34 @@ static inline bool eth_type_vlan(__be16 ethertype) ...@@ -313,6 +313,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, static inline bool vlan_hw_offload_capable(netdev_features_t features,
__be16 proto) __be16 proto)
{ {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <linux/ip.h> #include <linux/ip.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
#include <linux/if_vlan.h>
#include <net/inet_sock.h> #include <net/inet_sock.h>
#include <net/dsfield.h> #include <net/dsfield.h>
...@@ -142,7 +143,7 @@ static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner) ...@@ -142,7 +143,7 @@ static inline void ipv6_copy_dscp(unsigned int dscp, struct ipv6hdr *inner)
static inline int INET_ECN_set_ce(struct sk_buff *skb) 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): case cpu_to_be16(ETH_P_IP):
if (skb_network_header(skb) + sizeof(struct iphdr) <= if (skb_network_header(skb) + sizeof(struct iphdr) <=
skb_tail_pointer(skb)) skb_tail_pointer(skb))
...@@ -209,12 +210,16 @@ static inline int IP_ECN_decapsulate(const struct iphdr *oiph, ...@@ -209,12 +210,16 @@ static inline int IP_ECN_decapsulate(const struct iphdr *oiph,
{ {
__u8 inner; __u8 inner;
if (skb->protocol == htons(ETH_P_IP)) switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP):
inner = ip_hdr(skb)->tos; 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)); inner = ipv6_get_dsfield(ipv6_hdr(skb));
else break;
default:
return 0; return 0;
}
return INET_ECN_decapsulate(skb, oiph->tos, inner); return INET_ECN_decapsulate(skb, oiph->tos, inner);
} }
...@@ -224,12 +229,16 @@ static inline int IP6_ECN_decapsulate(const struct ipv6hdr *oipv6h, ...@@ -224,12 +229,16 @@ static inline int IP6_ECN_decapsulate(const struct ipv6hdr *oipv6h,
{ {
__u8 inner; __u8 inner;
if (skb->protocol == htons(ETH_P_IP)) switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP):
inner = ip_hdr(skb)->tos; 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)); inner = ipv6_get_dsfield(ipv6_hdr(skb));
else break;
default:
return 0; return 0;
}
return INET_ECN_decapsulate(skb, ipv6_get_dsfield(oipv6h), inner); return INET_ECN_decapsulate(skb, ipv6_get_dsfield(oipv6h), inner);
} }
......
...@@ -122,17 +122,6 @@ static inline void qdisc_run(struct Qdisc *q) ...@@ -122,17 +122,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 /* Calculate maximal size of packet seen by hard_start_xmit
routine of this device. routine of this device.
*/ */
......
...@@ -2695,7 +2695,7 @@ static int bpf_skb_proto_6_to_4(struct sk_buff *skb) ...@@ -2695,7 +2695,7 @@ static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
{ {
__be16 from_proto = skb->protocol; __be16 from_proto = skb_protocol(skb, true);
if (from_proto == htons(ETH_P_IP) && if (from_proto == htons(ETH_P_IP) &&
to_proto == htons(ETH_P_IPV6)) to_proto == htons(ETH_P_IPV6))
...@@ -2768,7 +2768,7 @@ static const struct bpf_func_proto bpf_skb_change_type_proto = { ...@@ -2768,7 +2768,7 @@ static const struct bpf_func_proto bpf_skb_change_type_proto = {
static u32 bpf_skb_net_base_len(const struct sk_buff *skb) static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
{ {
switch (skb->protocol) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
return sizeof(struct iphdr); return sizeof(struct iphdr);
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
...@@ -2848,7 +2848,7 @@ static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff) ...@@ -2848,7 +2848,7 @@ static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
u32 len_cur, len_diff_abs = abs(len_diff); u32 len_cur, len_diff_abs = abs(len_diff);
u32 len_min = bpf_skb_net_base_len(skb); u32 len_min = bpf_skb_net_base_len(skb);
u32 len_max = __bpf_skb_max_len(skb); u32 len_max = __bpf_skb_max_len(skb);
__be16 proto = skb->protocol; __be16 proto = skb_protocol(skb, true);
bool shrink = len_diff < 0; bool shrink = len_diff < 0;
int ret; int ret;
...@@ -4552,7 +4552,7 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len ...@@ -4552,7 +4552,7 @@ static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len
switch (type) { switch (type) {
case BPF_LWT_ENCAP_SEG6_INLINE: case BPF_LWT_ENCAP_SEG6_INLINE:
if (skb->protocol != htons(ETH_P_IPV6)) if (skb_protocol(skb, true) != htons(ETH_P_IPV6))
return -EBADMSG; return -EBADMSG;
err = seg6_do_srh_inline(skb, srh); err = seg6_do_srh_inline(skb, srh);
......
...@@ -46,17 +46,20 @@ static int tcf_connmark_act(struct sk_buff *skb, const struct tc_action *a, ...@@ -46,17 +46,20 @@ static int tcf_connmark_act(struct sk_buff *skb, const struct tc_action *a,
tcf_lastuse_update(&ca->tcf_tm); tcf_lastuse_update(&ca->tcf_tm);
bstats_update(&ca->tcf_bstats, skb); 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)) if (skb->len < sizeof(struct iphdr))
goto out; goto out;
proto = NFPROTO_IPV4; proto = NFPROTO_IPV4;
} else if (skb->protocol == htons(ETH_P_IPV6)) { break;
case htons(ETH_P_IPV6):
if (skb->len < sizeof(struct ipv6hdr)) if (skb->len < sizeof(struct ipv6hdr))
goto out; goto out;
proto = NFPROTO_IPV6; proto = NFPROTO_IPV6;
} else { break;
default:
goto out; goto out;
} }
......
...@@ -577,7 +577,7 @@ static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a, ...@@ -577,7 +577,7 @@ static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,
goto drop; goto drop;
update_flags = params->update_flags; update_flags = params->update_flags;
protocol = tc_skb_protocol(skb); protocol = skb_protocol(skb, false);
again: again:
switch (protocol) { switch (protocol) {
case cpu_to_be16(ETH_P_IP): case cpu_to_be16(ETH_P_IP):
......
...@@ -51,7 +51,7 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, ...@@ -51,7 +51,7 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,
if (params->flags & SKBEDIT_F_INHERITDSFIELD) { if (params->flags & SKBEDIT_F_INHERITDSFIELD) {
int wlen = skb_network_offset(skb); int wlen = skb_network_offset(skb);
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
wlen += sizeof(struct iphdr); wlen += sizeof(struct iphdr);
if (!pskb_may_pull(skb, wlen)) if (!pskb_may_pull(skb, wlen))
......
...@@ -969,7 +969,7 @@ int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, ...@@ -969,7 +969,7 @@ int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
reclassify: reclassify:
#endif #endif
for (; tp; tp = rcu_dereference_bh(tp->next)) { for (; tp; tp = rcu_dereference_bh(tp->next)) {
__be16 protocol = tc_skb_protocol(skb); __be16 protocol = skb_protocol(skb, false);
int err; int err;
if (tp->protocol != protocol && if (tp->protocol != protocol &&
......
...@@ -84,7 +84,7 @@ static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow) ...@@ -84,7 +84,7 @@ static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow)
if (dst) if (dst)
return ntohl(dst); return ntohl(dst);
return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb); return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
} }
static u32 flow_get_proto(const struct sk_buff *skb, static u32 flow_get_proto(const struct sk_buff *skb,
...@@ -108,7 +108,7 @@ static u32 flow_get_proto_dst(const struct sk_buff *skb, ...@@ -108,7 +108,7 @@ static u32 flow_get_proto_dst(const struct sk_buff *skb,
if (flow->ports.ports) if (flow->ports.ports)
return ntohs(flow->ports.dst); return ntohs(flow->ports.dst);
return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb); return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
} }
static u32 flow_get_iif(const struct sk_buff *skb) static u32 flow_get_iif(const struct sk_buff *skb)
...@@ -155,7 +155,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb) ...@@ -155,7 +155,7 @@ static u32 flow_get_nfct(const struct sk_buff *skb)
static u32 flow_get_nfct_src(const struct sk_buff *skb, static u32 flow_get_nfct_src(const struct sk_buff *skb,
const struct flow_keys *flow) const struct flow_keys *flow)
{ {
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
return ntohl(CTTUPLE(skb, src.u3.ip)); return ntohl(CTTUPLE(skb, src.u3.ip));
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
...@@ -168,7 +168,7 @@ static u32 flow_get_nfct_src(const struct sk_buff *skb, ...@@ -168,7 +168,7 @@ static u32 flow_get_nfct_src(const struct sk_buff *skb,
static u32 flow_get_nfct_dst(const struct sk_buff *skb, static u32 flow_get_nfct_dst(const struct sk_buff *skb,
const struct flow_keys *flow) const struct flow_keys *flow)
{ {
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
return ntohl(CTTUPLE(skb, dst.u3.ip)); return ntohl(CTTUPLE(skb, dst.u3.ip));
case htons(ETH_P_IPV6): case htons(ETH_P_IPV6):
......
...@@ -203,7 +203,7 @@ static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, ...@@ -203,7 +203,7 @@ static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
/* skb_flow_dissect() does not set n_proto in case an unknown /* skb_flow_dissect() does not set n_proto in case an unknown
* protocol, so do it rather here. * protocol, so do it rather here.
*/ */
skb_key.basic.n_proto = skb->protocol; skb_key.basic.n_proto = skb_protocol(skb, false);
skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
......
...@@ -62,7 +62,7 @@ static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em, ...@@ -62,7 +62,7 @@ static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em,
}; };
int ret, network_offset; int ret, network_offset;
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
state.pf = NFPROTO_IPV4; state.pf = NFPROTO_IPV4;
if (!pskb_network_may_pull(skb, sizeof(struct iphdr))) if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
......
...@@ -199,7 +199,7 @@ META_COLLECTOR(int_priority) ...@@ -199,7 +199,7 @@ META_COLLECTOR(int_priority)
META_COLLECTOR(int_protocol) META_COLLECTOR(int_protocol)
{ {
/* Let userspace take care of the byte ordering */ /* Let userspace take care of the byte ordering */
dst->value = tc_skb_protocol(skb); dst->value = skb_protocol(skb, false);
} }
META_COLLECTOR(int_pkttype) META_COLLECTOR(int_pkttype)
......
...@@ -589,7 +589,7 @@ static void cake_update_flowkeys(struct flow_keys *keys, ...@@ -589,7 +589,7 @@ static void cake_update_flowkeys(struct flow_keys *keys,
struct nf_conntrack_tuple tuple = {}; struct nf_conntrack_tuple tuple = {};
bool rev = !skb->_nfct; bool rev = !skb->_nfct;
if (tc_skb_protocol(skb) != htons(ETH_P_IP)) if (skb_protocol(skb, true) != htons(ETH_P_IP))
return; return;
if (!nf_ct_get_tuple_skb(&tuple, skb)) if (!nf_ct_get_tuple_skb(&tuple, skb))
...@@ -1514,7 +1514,7 @@ static u8 cake_handle_diffserv(struct sk_buff *skb, bool wash) ...@@ -1514,7 +1514,7 @@ static u8 cake_handle_diffserv(struct sk_buff *skb, bool wash)
u16 *buf, buf_; u16 *buf, buf_;
u8 dscp; u8 dscp;
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
buf = skb_header_pointer(skb, offset, sizeof(buf_), &buf_); buf = skb_header_pointer(skb, offset, sizeof(buf_), &buf_);
if (unlikely(!buf)) if (unlikely(!buf))
......
...@@ -207,7 +207,7 @@ static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch, ...@@ -207,7 +207,7 @@ static int dsmark_enqueue(struct sk_buff *skb, struct Qdisc *sch,
if (p->set_tc_index) { if (p->set_tc_index) {
int wlen = skb_network_offset(skb); int wlen = skb_network_offset(skb);
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
wlen += sizeof(struct iphdr); wlen += sizeof(struct iphdr);
if (!pskb_may_pull(skb, wlen) || if (!pskb_may_pull(skb, wlen) ||
...@@ -300,7 +300,7 @@ static struct sk_buff *dsmark_dequeue(struct Qdisc *sch) ...@@ -300,7 +300,7 @@ static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
index = skb->tc_index & (p->indices - 1); index = skb->tc_index & (p->indices - 1);
pr_debug("index %d->%d\n", skb->tc_index, index); pr_debug("index %d->%d\n", skb->tc_index, index);
switch (tc_skb_protocol(skb)) { switch (skb_protocol(skb, true)) {
case htons(ETH_P_IP): case htons(ETH_P_IP):
ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask, ipv4_change_dsfield(ip_hdr(skb), p->mv[index].mask,
p->mv[index].value); p->mv[index].value);
...@@ -317,7 +317,7 @@ static struct sk_buff *dsmark_dequeue(struct Qdisc *sch) ...@@ -317,7 +317,7 @@ static struct sk_buff *dsmark_dequeue(struct Qdisc *sch)
*/ */
if (p->mv[index].mask != 0xff || p->mv[index].value) if (p->mv[index].mask != 0xff || p->mv[index].value)
pr_warn("%s: unsupported protocol %d\n", pr_warn("%s: unsupported protocol %d\n",
__func__, ntohs(tc_skb_protocol(skb))); __func__, ntohs(skb_protocol(skb, true)));
break; break;
} }
......
...@@ -243,7 +243,7 @@ __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, ...@@ -243,7 +243,7 @@ __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res,
char haddr[MAX_ADDR_LEN]; char haddr[MAX_ADDR_LEN];
neigh_ha_snapshot(haddr, n, dev); neigh_ha_snapshot(haddr, n, dev);
err = dev_hard_header(skb, dev, ntohs(tc_skb_protocol(skb)), err = dev_hard_header(skb, dev, ntohs(skb_protocol(skb, false)),
haddr, NULL, skb->len); haddr, NULL, skb->len);
if (err < 0) if (err < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment