net/sched: fix QinQ when actions read IPv4/IPv6 header
Subsystems (3)
PATCH v2 1/3 net/sched: act_csum: pull all VLAN headers before checksumming
In reply to Davide CarattiTC 'csum' action needs to read the packets' ethertype. In case the value is encoded in unstripped/nested VLAN headers, call pskb_may_pull() to be sure that all tags are in the linear part of skb. While at it, move this code in a helper function: other TC actions (like 'pedit' and 'skbedit') might need to read the innermost ethertype.
--- include/net/pkt_cls.h | 21 +++++++++++++++++++++ net/sched/act_csum.c | 14 ++------------ 2 files changed, 23 insertions(+), 12 deletions(-)
include/net/pkt_cls.h +21 -0
@@ -953,4 +953,25 @@ struct tc_root_qopt_offload { bool ingress; }; +static inline int tc_skb_pull_vlans (struct sk_buff *skb, +unsigned int *hdr_count, +__be16 *proto) +{ +if (skb_vlan_tag_present (skb)) + *proto = skb->protocol; + +while (eth_type_vlan (*proto)) { +struct vlan_hdr *vlan; + +if (unlikely (!pskb_may_pull (skb,VLAN_HLEN ))) +return -ENOMEM ; + + vlan = (struct vlan_hdr *)skb->data; + *proto = vlan->h_vlan_encapsulated_proto; +skb_pull (skb,VLAN_HLEN ); +skb_reset_network_header (skb); + (*hdr_count)++; + } +return 0 ; +}#endif
net/sched/act_csum.c +2 -12
@@ -574,7 +574,6 @@ static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) {struct tcf_csum *p =to_tcf_csum (a); -bool orig_vlan_tag_present = false;unsigned int vlan_hdr_count =0 ;struct tcf_csum_params *params;u32 update_flags;
@@ -604,17 +603,8 @@ static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a,break ;case cpu_to_be16 (ETH_P_8021AD ):/* fall through */ case cpu_to_be16 (ETH_P_8021Q ): -if (skb_vlan_tag_present (skb) && !orig_vlan_tag_present) { - protocol = skb->protocol; - orig_vlan_tag_present = true; - }else { -struct vlan_hdr *vlan = (struct vlan_hdr *)skb->data; - - protocol = vlan->h_vlan_encapsulated_proto; -skb_pull (skb,VLAN_HLEN ); -skb_reset_network_header (skb); - vlan_hdr_count++; - } +if (tc_skb_pull_vlans (skb, &vlan_hdr_count, &protocol)) + goto drop; goto again; }
Signature · 1 line
2.20.1
PATCH v2 2/3 net/sched: act_pedit: fix 'ex munge' on network header in case of QinQ packet
In reply to Davide Carattilike it has been done in commit 2ecba2d1e45b ("net: sched: act_csum: Fix csum calc for tagged packets"), also 'pedit' needs to adjust the network offset when multiple tags are present in the packets: otherwise wrong IP headers (but good checksums) can be observed with the following command:
# tc filter add dev test0 parent ffff: protocol 802.1Q flower \ vlan_ethtype ipv4 action \ pedit ex munge ip ttl set 10 pipe \ csum ip and icmp pipe \ mirred egress redirect dev test1
--- net/sched/act_pedit.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
net/sched/act_pedit.c +22 -4
@@ -277,9 +277,11 @@ static bool offset_valid(struct sk_buff *skb, int offset) }static int pedit_skb_hdr_offset (struct sk_buff *skb, -enum pedit_header_type htype,int *hoffset) +enum pedit_header_type htype,int *hoffset, +unsigned int *vlan_hdr_count) {int ret = -EINVAL ; +__be16 protocol;switch (htype) {case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH :
@@ -291,8 +293,18 @@ static int pedit_skb_hdr_offset(struct sk_buff *skb,case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK :case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 :case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6 : - *hoffset =skb_network_offset (skb); - ret =0 ; + protocol =tc_skb_protocol (skb); +again: +switch (protocol) { +case cpu_to_be16 (ETH_P_8021AD ):/* fall through */ +case cpu_to_be16 (ETH_P_8021Q ): +if (!tc_skb_pull_vlans (skb, vlan_hdr_count, &protocol)) + goto again; +return ret; +default : + *hoffset =skb_network_offset (skb); + ret =0 ; + }break ;case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP :case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP :
@@ -313,6 +325,7 @@ static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) {struct tcf_pedit *p =to_pedit (a); +unsigned int vlan_hdr_count =0 ;int i;if (skb_unclone (skb,GFP_ATOMIC ))
@@ -343,7 +356,8 @@ static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a, tkey_ex++; } - rc =pedit_skb_hdr_offset (skb, htype, &hoffset); + rc =pedit_skb_hdr_offset (skb, htype, &hoffset, + &vlan_hdr_count);if (rc) {pr_info ("tc action pedit bad header type specified (0x%x)\n" , htype);
@@ -407,6 +421,10 @@ static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a, bad: p->tcf_qstats.overlimits++; done: +while (vlan_hdr_count--) { +skb_push (skb,VLAN_HLEN ); +skb_reset_network_header (skb); + }bstats_update (&p->tcf_bstats, skb);spin_unlock (&p->tcf_lock);return p->tcf_action;
Signature · 1 line
2.20.1
v2 cover net/sched: fix QinQ when actions read IPv4/IPv6 header
'act_csum' was recently fixed to mangle the IPv4/IPv6 header if a packet having one or more VLAN headers was processed: patch #1 ensures that all VLAN headers are in the linear area of the skb.
Other actions might read or mangle the IPv4/IPv6 header: patch #2 and #3 fix 'act_pedit' and 'act_skbedit' respectively.
Changes since v1:
- add patch #1, thanks to Eric Dumazet
- add patch #3
Davide Caratti (3):
net/sched: act_csum: pull all VLAN headers before checksumming net/sched: act_pedit: fix 'ex munge' on network header in case of QinQ
packet
net/sched: act_skbedit: fix 'inheritdsfield' in case of QinQ packet
include/net/pkt_cls.h | 21 +++++++++++++++++++++ net/sched/act_csum.c | 14 ++------------ net/sched/act_pedit.c | 26 ++++++++++++++++++++++---- net/sched/act_skbedit.c | 26 +++++++++++++++++++++----- 4 files changed, 66 insertions(+), 21 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 3/3 net/sched: act_skbedit: fix 'inheritdsfield' in case of QinQ packet
like it was previously done on TC 'csum' (see commit 2ecba2d1e45b ("net:
sched: act_csum: Fix csum calc for tagged packets")), TC 'skbedit' might need to adjust the network offset, if the packet has unstripped/multiple
tags: otherwise 'inheritdsfield' will just be skipped for QinQ packets.
--- net/sched/act_skbedit.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
net/sched/act_skbedit.c +21 -5
@@ -39,6 +39,7 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, {struct tcf_skbedit *d =to_skbedit (a);struct tcf_skbedit_params *params; +unsigned int vlan_hdr_count =0 ;int action;tcf_lastuse_update (&d->tcf_tm);
@@ -50,9 +51,17 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,if (params->flags &SKBEDIT_F_PRIORITY ) skb->priority = params->priority;if (params->flags &SKBEDIT_F_INHERITDSFIELD ) { -int wlen =skb_network_offset (skb); - -switch (tc_skb_protocol (skb)) { +__be16 proto =tc_skb_protocol (skb); +int wlen; + +again: + wlen =skb_network_offset (skb); +switch (proto) { +case htons (ETH_P_8021AD ):/* Fall Through */ +case htons (ETH_P_8021Q ): +if (tc_skb_pull_vlans (skb, &vlan_hdr_count, &proto)) + goto err; + goto again;case htons (ETH_P_IP ): wlen +=sizeof (struct iphdr );if (!pskb_may_pull (skb, wlen))
@@ -61,7 +70,7 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a,break ;case htons (ETH_P_IPV6 ): - wlen +=sizeof (struct ipv6hdr ); + wlen +=sizeof (struct ipv6hdr );if (!pskb_may_pull (skb, wlen)) goto err; skb->priority =ipv6_get_dsfield (ipv6_hdr (skb)) >>2 ;
@@ -77,11 +86,18 @@ static int tcf_skbedit_act(struct sk_buff *skb, const struct tc_action *a, }if (params->flags &SKBEDIT_F_PTYPE ) skb->pkt_type = params->ptype; + +out: +while (vlan_hdr_count--) { +skb_push (skb,VLAN_HLEN ); +skb_reset_network_header (skb); + }return action; err:qstats_drop_inc (this_cpu_ptr (d->common.cpu_qstats)); -return TC_ACT_SHOT ; + action =TC_ACT_SHOT ; + goto out; }static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX +1 ] = {
Signature · 1 line
2.20.1
Re: [PATCH net v2 1/3] net/sched: act_csum: pull all VLAN headers before checksumming
In reply to Davide CarattiQuoted 12 lines · On Thu, 30 May 2019 20:03:41 +0200 Davide Caratti <[email protected]> wrote:
include/net/pkt_cls.h +11
-0+static inline int tc_skb_pull_vlans (struct sk_buff *skb, +unsigned int *hdr_count, +__be16 *proto) +{ +if (skb_vlan_tag_present (skb)) + *proto = skb->protocol; + +while (eth_type_vlan (*proto)) { +struct vlan_hdr *vlan; + +if (unlikely (!pskb_may_pull (skb,VLAN_HLEN )))
include/net/pkt_cls.h struct tc_root_qopt_offload { · line 976
Quoted 10 lines
include/net/pkt_cls.h +10
-0+return -ENOMEM ; + + vlan = (struct vlan_hdr *)skb->data; + *proto = vlan->h_vlan_encapsulated_proto; +skb_pull (skb,VLAN_HLEN ); +skb_reset_network_header (skb); + (*hdr_count)++; + } +return 0 ; +}
Does this really need to be an inline, or could it just be part of the sched_api?
Re: [PATCH net v2 1/3] net/sched: act_csum: pull all VLAN headers before checksumming
Quoted 20 lines · Stephen Hemminger
Quoted 17 lines · On Thu, 30 May 2019 20:03:41 +0200 Davide Caratti <[email protected]> wrote:
include/net/pkt_cls.h +16
-0+static inline int tc_skb_pull_vlans (struct sk_buff *skb, +unsigned int *hdr_count, +__be16 *proto) +{ +if (skb_vlan_tag_present (skb)) + *proto = skb->protocol; + +while (eth_type_vlan (*proto)) { +struct vlan_hdr *vlan; + +if (unlikely (!pskb_may_pull (skb,VLAN_HLEN ))) +return -ENOMEM ; + + vlan = (struct vlan_hdr *)skb->data; + *proto = vlan->h_vlan_encapsulated_proto; +skb_pull (skb,VLAN_HLEN );
Quoted 8 lines
include/net/pkt_cls.h struct tc_root_qopt_offload { · line 976
Quoted 5 lines
include/net/pkt_cls.h +5
-0+skb_reset_network_header (skb); + (*hdr_count)++; + } +return 0 ; +}Does this really need to be an inline, or could it just be part of the
sched_api?
yes, you are right: I will send a v3.
thanks,
Signature · 1 line
davide