summaryrefslogtreecommitdiffstats
path: root/net/core
diff options
context:
space:
mode:
authorHengqi Chen <hengqi.chen@gmail.com>2021-10-21 21:47:51 +0800
committerAlexei Starovoitov <ast@kernel.org>2021-10-21 15:11:06 -0700
commit9eeb3aa33ae005526f672b394c1791578463513f (patch)
tree0c70fe4027644192a334d3bd60bbbcb8e4f7758d /net/core
parent44ce0ac11e4e6dd1aee2e41e7d74f95f3b961cfb (diff)
downloadlinux-stable-9eeb3aa33ae005526f672b394c1791578463513f.tar.gz
linux-stable-9eeb3aa33ae005526f672b394c1791578463513f.tar.bz2
linux-stable-9eeb3aa33ae005526f672b394c1791578463513f.zip
bpf: Add bpf_skc_to_unix_sock() helper
The helper is used in tracing programs to cast a socket pointer to a unix_sock pointer. The return value could be NULL if the casting is illegal. Suggested-by: Yonghong Song <yhs@fb.com> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20211021134752.1223426-2-hengqi.chen@gmail.com
Diffstat (limited to 'net/core')
-rw-r--r--net/core/filter.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/net/core/filter.c b/net/core/filter.c
index 4bace37a6a44..8e8d3b49c297 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -10723,6 +10723,26 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
};
+BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
+{
+ /* unix_sock type is not generated in dwarf and hence btf,
+ * trigger an explicit type generation here.
+ */
+ BTF_TYPE_EMIT(struct unix_sock);
+ if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
+ return (unsigned long)sk;
+
+ return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
+ .func = bpf_skc_to_unix_sock,
+ .gpl_only = false,
+ .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
+ .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
+ .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
+};
+
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
{
return (unsigned long)sock_from_file(file);
@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
case BPF_FUNC_skc_to_udp6_sock:
func = &bpf_skc_to_udp6_sock_proto;
break;
+ case BPF_FUNC_skc_to_unix_sock:
+ func = &bpf_skc_to_unix_sock_proto;
+ break;
default:
return bpf_base_func_proto(func_id);
}