Commit 549beec0 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'Add bpf_skc_to_unix_sock() helper'

Hengqi Chen says:

====================

This patch set adds a new BPF helper bpf_skc_to_unix_sock().
The helper is used in tracing programs to cast a socket
pointer to a unix_sock pointer.

v2->v3:
 - Use abstract socket in selftest (Alexei)
 - Run checkpatch script over patches (Andrii)

v1->v2:
 - Update selftest, remove trailing spaces changes (Song)
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 44ce0ac1 b6c4e715
...@@ -2093,6 +2093,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto; ...@@ -2093,6 +2093,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto; extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto; extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto; extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
extern const struct bpf_func_proto bpf_copy_from_user_proto; extern const struct bpf_func_proto bpf_copy_from_user_proto;
extern const struct bpf_func_proto bpf_snprintf_btf_proto; extern const struct bpf_func_proto bpf_snprintf_btf_proto;
extern const struct bpf_func_proto bpf_snprintf_proto; extern const struct bpf_func_proto bpf_snprintf_proto;
......
...@@ -4909,6 +4909,12 @@ union bpf_attr { ...@@ -4909,6 +4909,12 @@ union bpf_attr {
* Return * Return
* The number of bytes written to the buffer, or a negative error * The number of bytes written to the buffer, or a negative error
* in case of failure. * in case of failure.
*
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
* Return
* *sk* if casting is valid, or **NULL** otherwise.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -5089,6 +5095,7 @@ union bpf_attr { ...@@ -5089,6 +5095,7 @@ union bpf_attr {
FN(task_pt_regs), \ FN(task_pt_regs), \
FN(get_branch_snapshot), \ FN(get_branch_snapshot), \
FN(trace_vprintk), \ FN(trace_vprintk), \
FN(skc_to_unix_sock), \
/* */ /* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper /* integer value in 'imm' field of BPF_CALL instruction selects which helper
......
...@@ -1608,6 +1608,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) ...@@ -1608,6 +1608,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
return &bpf_skc_to_tcp_request_sock_proto; return &bpf_skc_to_tcp_request_sock_proto;
case BPF_FUNC_skc_to_udp6_sock: case BPF_FUNC_skc_to_udp6_sock:
return &bpf_skc_to_udp6_sock_proto; return &bpf_skc_to_udp6_sock_proto;
case BPF_FUNC_skc_to_unix_sock:
return &bpf_skc_to_unix_sock_proto;
case BPF_FUNC_sk_storage_get: case BPF_FUNC_sk_storage_get:
return &bpf_sk_storage_get_tracing_proto; return &bpf_sk_storage_get_tracing_proto;
case BPF_FUNC_sk_storage_delete: case BPF_FUNC_sk_storage_delete:
......
...@@ -10723,6 +10723,26 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = { ...@@ -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], .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) BPF_CALL_1(bpf_sock_from_file, struct file *, file)
{ {
return (unsigned long)sock_from_file(file); return (unsigned long)sock_from_file(file);
...@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id) ...@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
case BPF_FUNC_skc_to_udp6_sock: case BPF_FUNC_skc_to_udp6_sock:
func = &bpf_skc_to_udp6_sock_proto; func = &bpf_skc_to_udp6_sock_proto;
break; break;
case BPF_FUNC_skc_to_unix_sock:
func = &bpf_skc_to_unix_sock_proto;
break;
default: default:
return bpf_base_func_proto(func_id); return bpf_base_func_proto(func_id);
} }
......
...@@ -537,6 +537,7 @@ class PrinterHelpers(Printer): ...@@ -537,6 +537,7 @@ class PrinterHelpers(Printer):
'struct tcp_timewait_sock', 'struct tcp_timewait_sock',
'struct tcp_request_sock', 'struct tcp_request_sock',
'struct udp6_sock', 'struct udp6_sock',
'struct unix_sock',
'struct task_struct', 'struct task_struct',
'struct __sk_buff', 'struct __sk_buff',
...@@ -589,6 +590,7 @@ class PrinterHelpers(Printer): ...@@ -589,6 +590,7 @@ class PrinterHelpers(Printer):
'struct tcp_timewait_sock', 'struct tcp_timewait_sock',
'struct tcp_request_sock', 'struct tcp_request_sock',
'struct udp6_sock', 'struct udp6_sock',
'struct unix_sock',
'struct task_struct', 'struct task_struct',
'struct path', 'struct path',
'struct btf_ptr', 'struct btf_ptr',
......
...@@ -4909,6 +4909,12 @@ union bpf_attr { ...@@ -4909,6 +4909,12 @@ union bpf_attr {
* Return * Return
* The number of bytes written to the buffer, or a negative error * The number of bytes written to the buffer, or a negative error
* in case of failure. * in case of failure.
*
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
* Description
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
* Return
* *sk* if casting is valid, or **NULL** otherwise.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
...@@ -5089,6 +5095,7 @@ union bpf_attr { ...@@ -5089,6 +5095,7 @@ union bpf_attr {
FN(task_pt_regs), \ FN(task_pt_regs), \
FN(get_branch_snapshot), \ FN(get_branch_snapshot), \
FN(trace_vprintk), \ FN(trace_vprintk), \
FN(skc_to_unix_sock), \
/* */ /* */
/* integer value in 'imm' field of BPF_CALL instruction selects which helper /* integer value in 'imm' field of BPF_CALL instruction selects which helper
......
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2021 Hengqi Chen */
#include <test_progs.h>
#include <sys/un.h>
#include "test_skc_to_unix_sock.skel.h"
static const char *sock_path = "@skc_to_unix_sock";
void test_skc_to_unix_sock(void)
{
struct test_skc_to_unix_sock *skel;
struct sockaddr_un sockaddr;
int err, sockfd = 0;
skel = test_skc_to_unix_sock__open();
if (!ASSERT_OK_PTR(skel, "could not open BPF object"))
return;
skel->rodata->my_pid = getpid();
err = test_skc_to_unix_sock__load(skel);
if (!ASSERT_OK(err, "could not load BPF object"))
goto cleanup;
err = test_skc_to_unix_sock__attach(skel);
if (!ASSERT_OK(err, "could not attach BPF object"))
goto cleanup;
/* trigger unix_listen */
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (!ASSERT_GT(sockfd, 0, "socket failed"))
goto cleanup;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sun_family = AF_UNIX;
strncpy(sockaddr.sun_path, sock_path, strlen(sock_path));
sockaddr.sun_path[0] = '\0';
err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (!ASSERT_OK(err, "bind failed"))
goto cleanup;
err = listen(sockfd, 1);
if (!ASSERT_OK(err, "listen failed"))
goto cleanup;
ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed");
cleanup:
if (sockfd)
close(sockfd);
test_skc_to_unix_sock__destroy(skel);
}
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2021 Hengqi Chen */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_tracing_net.h"
const volatile pid_t my_pid = 0;
char path[256] = {};
SEC("fentry/unix_listen")
int BPF_PROG(unix_listen, struct socket *sock, int backlog)
{
pid_t pid = bpf_get_current_pid_tgid() >> 32;
struct unix_sock *unix_sk;
int i, len;
if (pid != my_pid)
return 0;
unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
if (!unix_sk)
return 0;
if (!UNIX_ABSTRACT(unix_sk))
return 0;
len = unix_sk->addr->len - sizeof(short);
path[0] = '@';
for (i = 1; i < len; i++) {
if (i >= sizeof(struct sockaddr_un))
break;
path[i] = unix_sk->addr->name->sun_path[i];
}
return 0;
}
char _license[] SEC("license") = "GPL";
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