bpf: Update h_proto of ethhdr when the outer protocol changed
Subsystems (5)
cover bpf: Update h_proto of ethhdr when the outer protocol changed
When use bpf_skb_adjust_room() to encapsulate or decapsulate packet, and outer protocol changed, we can update h_proto of ethhdr directly.
$./test_tc_tunnel.sh
ipip
encap 192.168.1.1 to 192.168.1.2, type ipip, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
ipip6
encap 192.168.1.1 to 192.168.1.2, type ipip6, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
ip6ip6
encap fd::1 to fd::2, type ip6tnl, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
sit
encap fd::1 to fd::2, type sit, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
...
OK. All tests passed
Ziyang Xuan (2):
bpf: Update h_proto of ethhdr when the outer protocol changed selftests/bpf: Remove unnecessary codes for updating h_proto of ethhdr
net/core/filter.c | 20 +++++++++++++------ .../selftests/bpf/progs/test_tc_tunnel.c | 18 ----------------- 2 files changed, 14 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 bpf: Update h_proto of ethhdr when the outer protocol changed
When use bpf_skb_adjust_room() to encapsulate or decapsulate packet, and outer protocol changed, we can update h_proto of ethhdr directly.
--- net/core/filter.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
net/core/filter.c +14 -6
@@ -3409,6 +3409,14 @@ static u32 bpf_skb_net_base_len(const struct sk_buff *skb) BPF_ADJ_ROOM_ENCAP_L2_MASK) | \ BPF_F_ADJ_ROOM_DECAP_L3_MASK) +static inline void skb_update_protocol (struct sk_buff *skb,__be16 protocol) +{ +struct ethhdr *hdr =eth_hdr (skb); + + hdr->h_proto = protocol; + skb->protocol = protocol; +} +static int bpf_skb_net_grow (struct sk_buff *skb,u32 off,u32 len_diff,u64 flags) {
@@ -3491,13 +3499,13 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,skb_set_transport_header (skb, mac_len + nh_len); } -/* Match skb->protocol to new outer l3 protocol */ +/* Match skb->protocol and ethhdr->h_proto to new outer l3 protocol */ if (skb->protocol ==htons (ETH_P_IP ) && flags &BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ) - skb->protocol =htons (ETH_P_IPV6 ); +skb_update_protocol (skb,htons (ETH_P_IPV6 ));else if (skb->protocol ==htons (ETH_P_IPV6 ) && flags &BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 ) - skb->protocol =htons (ETH_P_IP ); +skb_update_protocol (skb,htons (ETH_P_IP )); }if (skb_is_gso (skb)) {
@@ -3540,13 +3548,13 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,if (unlikely (ret <0 ))return ret; -/* Match skb->protocol to new outer l3 protocol */ +/* Match skb->protocol and ethhdr->h_proto to new outer l3 protocol */ if (skb->protocol ==htons (ETH_P_IP ) && flags &BPF_F_ADJ_ROOM_DECAP_L3_IPV6 ) - skb->protocol =htons (ETH_P_IPV6 ); +skb_update_protocol (skb,htons (ETH_P_IPV6 ));else if (skb->protocol ==htons (ETH_P_IPV6 ) && flags &BPF_F_ADJ_ROOM_DECAP_L3_IPV4 ) - skb->protocol =htons (ETH_P_IP ); +skb_update_protocol (skb,htons (ETH_P_IP ));if (skb_is_gso (skb)) {struct skb_shared_info *shinfo =skb_shinfo (skb);
Signature · 1 line
2.25.1
Martin KaFai Lau
reviewerQuoted 2 lines · Ziyang Xuan
When use
bpf_skb_adjust_room()to encapsulate or decapsulate packet, and outer protocol changed, we can updateh_protoof ethhdr directly.
This could break some existing bpf programs. e.g what if the existing prog is testing the h_proto after bpf_skb_adjust_room() and expect it hasn't changed yet?
Quoted 6 lines · Ziyang Xuan
Quoted 2 lines
When use
bpf_skb_adjust_room()to encapsulate or decapsulate packet, and outer protocol changed, we can updateh_protoof ethhdr directly.This could break some existing bpf programs. e.g what if the existing prog is testing the
h_protoafterbpf_skb_adjust_room()and expect it hasn't changed yet?
I think some new modifications break some existing things are not unacceptable. Maybe my modification is inappropriate because its benefits are small and some kind of principle is broken, such as Yonghong Song pointed:
"bpf_skb_adjust_room() only changes skb meta data and tries not to modify the packet." If it is, the modification should be rejected.
Thank you!
William Xuan
Quoted 2 lines
.
PATCH 2/2 selftests/bpf: Remove unnecessary codes for updating h_proto of ethhdr
In reply to Ziyang XuanSince updating h_proto of ethhdr in kernel, remove the codes in user bpf program.
--- .../selftests/bpf/progs/test_tc_tunnel.c | 18 ------------------ 1 file changed, 18 deletions(-)
tools/testing/selftests/bpf/progs/test_tc_tunnel.c +0 -18
@@ -236,17 +236,6 @@ static __always_inline int __encap_ipv4(struct __sk_buff *skb, __u8 encap_proto, BPF_F_INVALIDATE_HASH) < 0)return TC_ACT_SHOT ; -/* if changing outer proto type, update eth->h_proto */ -if (encap_proto ==IPPROTO_IPV6 ) { -struct ethhdr eth; - -if (bpf_skb_load_bytes (skb,0 , ð,sizeof (eth)) <0 ) -return TC_ACT_SHOT ; - eth.h_proto =bpf_htons (ETH_P_IP ); -if (bpf_skb_store_bytes (skb,0 , ð,sizeof (eth),0 ) <0 ) -return TC_ACT_SHOT ; - } -return TC_ACT_OK ; }
@@ -412,13 +401,6 @@ static int encap_ipv6_ipip6(struct __sk_buff *skb) BPF_F_INVALIDATE_HASH) < 0)return TC_ACT_SHOT ; -/* update eth->h_proto */ -if (bpf_skb_load_bytes (skb,0 , ð,sizeof (eth)) <0 ) -return TC_ACT_SHOT ; - eth.h_proto =bpf_htons (ETH_P_IPV6 ); -if (bpf_skb_store_bytes (skb,0 , ð,sizeof (eth),0 ) <0 ) -return TC_ACT_SHOT ; -return TC_ACT_OK ; }
Signature · 1 line
2.25.1
Yonghong Song
reviewerQuoted 2 lines · Ziyang Xuan
When use
bpf_skb_adjust_room()to encapsulate or decapsulate packet, and outer protocol changed, we can updateh_protoof ethhdr directly.
My mailbox somehow lost patch 1/2.
Looks like current bpf_skb_adjust_room() only changes skb meta data and tries not to modify the packet. Probably there is a reason for this.
Quoted 15 lines
$./test_tc_tunnel.sh
ipip
encap 192.168.1.1 to 192.168.1.2, type ipip, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
ipip6
[...]
Quoted 9 lines · Ziyang Xuan
Quoted 2 lines
When use
bpf_skb_adjust_room()to encapsulate or decapsulate packet, and outer protocol changed, we can updateh_protoof ethhdr directly.My mailbox somehow lost patch 1/2.
Looks like current
bpf_skb_adjust_room()only changes skb meta data and tries not to modify the packet. Probably there is a reason for this.
I found that this could reduce updating h_proto of ethhdr by bpf_skb_store_bytes() in bpf program when I used bpf_skb_adjust_room(). I thought it should be a small optimization. So I did it. No other problems.
Thank you!
William Xuan
Quoted 18 lines
Quoted 15 lines
$./test_tc_tunnel.sh
ipip
encap 192.168.1.1 to 192.168.1.2, type ipip, mac none len 100 test basic connectivity
0
test bpf encap without decap (expect failure)
Ncat: TIMEOUT.
1
test bpf encap with tunnel device decap
0
test bpf encap with bpf decap
0
OK
ipip6[...]
.