On 8/29/23 3:18 AM, Daan De Meyer wrote:
@@ -1482,11 +1485,22 @@ int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
if (!ctx.uaddr) {
memset(&unspec, 0, sizeof(unspec));
ctx.uaddr = (struct sockaddr *)&unspec;
- }
+ ctx.uaddrlen = sizeof(unspec);
ctx.uaddr could be NULL during BPF_CGROUP_RUN_PROG_UDP[46]_SENDMSG_LOCK(). There
is nothing from the sa_kern->uaddr that is useful to read, so ctx.uaddrlen
should be 0. The new kfunc bpf_sock_addr_set_addr() can do better than the
current sa->user_ip[46] uapi and should return error in this case because the
kernel will eventually ignore it.
+ } else if (uaddrlen)
+ ctx.uaddrlen = *uaddrlen;
+ else if (sk->sk_family == AF_INET)
+ ctx.uaddrlen = sizeof(struct sockaddr_in);
+ else if (sk->sk_family == AF_INET6)
+ ctx.uaddrlen = sizeof(struct sockaddr_in6);
cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
- return bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run,
- 0, flags);
+ ret = bpf_prog_run_array_cg(&cgrp->bpf, atype, &ctx, bpf_prog_run,
+ 0, flags);
+
+ if (!ret && uaddrlen)
+ *uaddrlen = ctx.uaddrlen;
+
+ return ret;
}
EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
[ ... ]
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 3a88545a265d..255b02d98404 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -135,7 +135,7 @@ static int tcp_v6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
sock_owned_by_me(sk);
- return BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr);
+ return BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr, NULL);
For IPv6, there is a minimum SIN6_LEN_RFC2133 which excludes the last '__u32
sin6_scope_id'. Meaning the last 4 bytes in 'struct sockaddr_in6' is optional.
It has been a few months since v2, so my memory faded a bit. I recalled one of
the concerns on passing addrlen to BPF_CGROUP_RUN_PROG is the bpf prog can
change it for AF_INET[6] and the addrlen change could be ignored by the kernel.
The bpf_sock_addr_set_addr() in v3 does not allow addrlen changes in AF_INET[6].
I think it now makes sense to pass addrlen (of AF_INET[6]) to BPF_CGROUP_RUN_*
whenever it is available such that the newly added sa_kern->uaddrlen can better
reflect what is in the sa_kern->uaddr.
I think the only addrlen missing case is in the inet[6]_getname(). Either NULL
can be passed or create a local addrlen var. afaict, the addrlen must be 'struct
sockaddr_in[6]' in those cases.