Skip to content

Commit 26101f5

Browse files
Kaixi FanAlexei Starovoitov
Kaixi Fan
authored and
Alexei Starovoitov
committed
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: Kaixi Fan <fankaixi.li@bytedance.com> Link: https://lore.kernel.org/r/20220430074844.69214-2-fankaixi.li@bytedance.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent bd2331b commit 26101f5

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/uapi/linux/bpf.h

+4
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
56045604
__u8 tunnel_ttl;
56055605
__u16 tunnel_ext; /* Padding, future use. */
56065606
__u32 tunnel_label;
5607+
union {
5608+
__u32 local_ipv4;
5609+
__u32 local_ipv6[4];
5610+
};
56075611
};
56085612

56095613
/* user accessible mirror of in-kernel xfrm_state.

net/core/filter.c

+9
Original file line numberDiff line numberDiff line change
@@ -4498,6 +4498,7 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
44984498
if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
44994499
err = -EINVAL;
45004500
switch (size) {
4501+
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
45014502
case offsetof(struct bpf_tunnel_key, tunnel_label):
45024503
case offsetof(struct bpf_tunnel_key, tunnel_ext):
45034504
goto set_compat;
@@ -4523,10 +4524,14 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key
45234524
if (flags & BPF_F_TUNINFO_IPV6) {
45244525
memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
45254526
sizeof(to->remote_ipv6));
4527+
memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4528+
sizeof(to->local_ipv6));
45264529
to->tunnel_label = be32_to_cpu(info->key.label);
45274530
} else {
45284531
to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
45294532
memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4533+
to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4534+
memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
45304535
to->tunnel_label = 0;
45314536
}
45324537

@@ -4597,6 +4602,7 @@ BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
45974602
return -EINVAL;
45984603
if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
45994604
switch (size) {
4605+
case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
46004606
case offsetof(struct bpf_tunnel_key, tunnel_label):
46014607
case offsetof(struct bpf_tunnel_key, tunnel_ext):
46024608
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,
46394645
info->mode |= IP_TUNNEL_INFO_IPV6;
46404646
memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
46414647
sizeof(from->remote_ipv6));
4648+
memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4649+
sizeof(from->local_ipv6));
46424650
info->key.label = cpu_to_be32(from->tunnel_label) &
46434651
IPV6_FLOWLABEL_MASK;
46444652
} else {
46454653
info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4654+
info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
46464655
}
46474656

46484657
return 0;

tools/include/uapi/linux/bpf.h

+4
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,10 @@ struct bpf_tunnel_key {
56045604
__u8 tunnel_ttl;
56055605
__u16 tunnel_ext; /* Padding, future use. */
56065606
__u32 tunnel_label;
5607+
union {
5608+
__u32 local_ipv4;
5609+
__u32 local_ipv6[4];
5610+
};
56075611
};
56085612

56095613
/* user accessible mirror of in-kernel xfrm_state.

0 commit comments

Comments
 (0)