Commit 26101f5a authored by Kaixi Fan's avatar Kaixi Fan Committed by Alexei Starovoitov

bpf: Add source ip in "struct bpf_tunnel_key"

Add tunnel source ip field in "struct bpf_tunnel_key". Add related code
to set and get tunnel source field.
Signed-off-by: default avatarKaixi Fan <fankaixi.li@bytedance.com>
Link: https://lore.kernel.org/r/20220430074844.69214-2-fankaixi.li@bytedance.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent bd2331b3
...@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key { ...@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
__u8 tunnel_ttl; __u8 tunnel_ttl;
__u16 tunnel_ext; /* Padding, future use. */ __u16 tunnel_ext; /* Padding, future use. */
__u32 tunnel_label; __u32 tunnel_label;
union {
__u32 local_ipv4;
__u32 local_ipv6[4];
};
}; };
/* user accessible mirror of in-kernel xfrm_state. /* user accessible mirror of in-kernel xfrm_state.
......
...@@ -4498,6 +4498,7 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key ...@@ -4498,6 +4498,7 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
if (unlikely(size != sizeof(struct bpf_tunnel_key))) { if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
err = -EINVAL; err = -EINVAL;
switch (size) { switch (size) {
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
case offsetof(struct bpf_tunnel_key, tunnel_label): case offsetof(struct bpf_tunnel_key, tunnel_label):
case offsetof(struct bpf_tunnel_key, tunnel_ext): case offsetof(struct bpf_tunnel_key, tunnel_ext):
goto set_compat; goto set_compat;
...@@ -4523,10 +4524,14 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key ...@@ -4523,10 +4524,14 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
if (flags & BPF_F_TUNINFO_IPV6) { if (flags & BPF_F_TUNINFO_IPV6) {
memcpy(to->remote_ipv6, &info->key.u.ipv6.src, memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
sizeof(to->remote_ipv6)); sizeof(to->remote_ipv6));
memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
sizeof(to->local_ipv6));
to->tunnel_label = be32_to_cpu(info->key.label); to->tunnel_label = be32_to_cpu(info->key.label);
} else { } else {
to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3); memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
to->tunnel_label = 0; to->tunnel_label = 0;
} }
...@@ -4597,6 +4602,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, ...@@ -4597,6 +4602,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
return -EINVAL; return -EINVAL;
if (unlikely(size != sizeof(struct bpf_tunnel_key))) { if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
switch (size) { switch (size) {
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
case offsetof(struct bpf_tunnel_key, tunnel_label): case offsetof(struct bpf_tunnel_key, tunnel_label):
case offsetof(struct bpf_tunnel_key, tunnel_ext): case offsetof(struct bpf_tunnel_key, tunnel_ext):
case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
...@@ -4639,10 +4645,13 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb, ...@@ -4639,10 +4645,13 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
info->mode |= IP_TUNNEL_INFO_IPV6; info->mode |= IP_TUNNEL_INFO_IPV6;
memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
sizeof(from->remote_ipv6)); sizeof(from->remote_ipv6));
memcpy(&info->key.u.ipv6.src, from->local_ipv6,
sizeof(from->local_ipv6));
info->key.label = cpu_to_be32(from->tunnel_label) & info->key.label = cpu_to_be32(from->tunnel_label) &
IPV6_FLOWLABEL_MASK; IPV6_FLOWLABEL_MASK;
} else { } else {
info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
} }
return 0; return 0;
......
...@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key { ...@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
__u8 tunnel_ttl; __u8 tunnel_ttl;
__u16 tunnel_ext; /* Padding, future use. */ __u16 tunnel_ext; /* Padding, future use. */
__u32 tunnel_label; __u32 tunnel_label;
union {
__u32 local_ipv4;
__u32 local_ipv6[4];
};
}; };
/* user accessible mirror of in-kernel xfrm_state. /* user accessible mirror of in-kernel xfrm_state.
......
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