Part of the effort to make ksock have all basic socket operations. This is for example useful when replying to a peer which expects a message from a particular source port. Signed-off-by: Mahe Tardy --- net/core/bpf_ksock.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/net/core/bpf_ksock.c b/net/core/bpf_ksock.c index 37168f43c16c..c7b487d90cd0 100644 --- a/net/core/bpf_ksock.c +++ b/net/core/bpf_ksock.c @@ -178,6 +178,33 @@ bpf_ksock_create(const struct bpf_ksock_create_opts *opts, u32 opts__sz, return NULL; } +/** + * bpf_ksock_bind() - Bind a BPF kernel socket to a local address. + * @ks: The BPF kernel socket context. + * @addr: Pointer to an IPv4 or IPv6 socket address. + * @addr__sz: Size of the address union. + * + * Binds the socket to the specified local address and port. A port of zero + * selects an ephemeral port. + * + * This function may sleep, so it can only be used in sleepable BPF programs + * (SYSCALL). + * + * Return: 0 on success, negative errno on error. + */ +__bpf_kfunc int bpf_ksock_bind(struct bpf_ksock *ks, + const union bpf_ksock_addr *addr, u32 addr__sz) +{ + struct sockaddr_storage sa; + int addrlen; + + addrlen = bpf_ksock_addr_to_sockaddr(addr, addr__sz, &sa); + if (addrlen < 0) + return addrlen; + + return __sys_bind_socket(ks->sock, &sa, addrlen); +} + /** * bpf_ksock_connect() - Connect a BPF kernel socket to a remote address. * @ks: The BPF kernel socket context. @@ -285,6 +312,7 @@ __bpf_kfunc_end_defs(); BTF_KFUNCS_START(ksock_init_kfunc_btf_ids) BTF_ID_FLAGS(func, bpf_ksock_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_ksock_connect, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_ksock_bind, KF_SLEEPABLE) BTF_KFUNCS_END(ksock_init_kfunc_btf_ids) static const struct btf_kfunc_id_set ksock_init_kfunc_set = { -- 2.43.0