Skip to content

Commit 9eeb3aa

Browse files
chenhengqiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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 <[email protected]> Signed-off-by: Hengqi Chen <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Song Liu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 44ce0ac commit 9eeb3aa

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

Diff for: include/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
20932093
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
20942094
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
20952095
extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
2096+
extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
20962097
extern const struct bpf_func_proto bpf_copy_from_user_proto;
20972098
extern const struct bpf_func_proto bpf_snprintf_btf_proto;
20982099
extern const struct bpf_func_proto bpf_snprintf_proto;

Diff for: include/uapi/linux/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -4909,6 +4909,12 @@ union bpf_attr {
49094909
* Return
49104910
* The number of bytes written to the buffer, or a negative error
49114911
* in case of failure.
4912+
*
4913+
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
4914+
* Description
4915+
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
4916+
* Return
4917+
* *sk* if casting is valid, or **NULL** otherwise.
49124918
*/
49134919
#define __BPF_FUNC_MAPPER(FN) \
49144920
FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {
50895095
FN(task_pt_regs), \
50905096
FN(get_branch_snapshot), \
50915097
FN(trace_vprintk), \
5098+
FN(skc_to_unix_sock), \
50925099
/* */
50935100

50945101
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

Diff for: kernel/trace/bpf_trace.c

+2
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16081608
return &bpf_skc_to_tcp_request_sock_proto;
16091609
case BPF_FUNC_skc_to_udp6_sock:
16101610
return &bpf_skc_to_udp6_sock_proto;
1611+
case BPF_FUNC_skc_to_unix_sock:
1612+
return &bpf_skc_to_unix_sock_proto;
16111613
case BPF_FUNC_sk_storage_get:
16121614
return &bpf_sk_storage_get_tracing_proto;
16131615
case BPF_FUNC_sk_storage_delete:

Diff for: net/core/filter.c

+23
Original file line numberDiff line numberDiff line change
@@ -10723,6 +10723,26 @@ const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
1072310723
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
1072410724
};
1072510725

10726+
BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
10727+
{
10728+
/* unix_sock type is not generated in dwarf and hence btf,
10729+
* trigger an explicit type generation here.
10730+
*/
10731+
BTF_TYPE_EMIT(struct unix_sock);
10732+
if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
10733+
return (unsigned long)sk;
10734+
10735+
return (unsigned long)NULL;
10736+
}
10737+
10738+
const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
10739+
.func = bpf_skc_to_unix_sock,
10740+
.gpl_only = false,
10741+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10742+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10743+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
10744+
};
10745+
1072610746
BPF_CALL_1(bpf_sock_from_file, struct file *, file)
1072710747
{
1072810748
return (unsigned long)sock_from_file(file);
@@ -10762,6 +10782,9 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
1076210782
case BPF_FUNC_skc_to_udp6_sock:
1076310783
func = &bpf_skc_to_udp6_sock_proto;
1076410784
break;
10785+
case BPF_FUNC_skc_to_unix_sock:
10786+
func = &bpf_skc_to_unix_sock_proto;
10787+
break;
1076510788
default:
1076610789
return bpf_base_func_proto(func_id);
1076710790
}

Diff for: scripts/bpf_doc.py

+2
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ def __init__(self, parser):
537537
'struct tcp_timewait_sock',
538538
'struct tcp_request_sock',
539539
'struct udp6_sock',
540+
'struct unix_sock',
540541
'struct task_struct',
541542

542543
'struct __sk_buff',
@@ -589,6 +590,7 @@ def __init__(self, parser):
589590
'struct tcp_timewait_sock',
590591
'struct tcp_request_sock',
591592
'struct udp6_sock',
593+
'struct unix_sock',
592594
'struct task_struct',
593595
'struct path',
594596
'struct btf_ptr',

Diff for: tools/include/uapi/linux/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -4909,6 +4909,12 @@ union bpf_attr {
49094909
* Return
49104910
* The number of bytes written to the buffer, or a negative error
49114911
* in case of failure.
4912+
*
4913+
* struct unix_sock *bpf_skc_to_unix_sock(void *sk)
4914+
* Description
4915+
* Dynamically cast a *sk* pointer to a *unix_sock* pointer.
4916+
* Return
4917+
* *sk* if casting is valid, or **NULL** otherwise.
49124918
*/
49134919
#define __BPF_FUNC_MAPPER(FN) \
49144920
FN(unspec), \
@@ -5089,6 +5095,7 @@ union bpf_attr {
50895095
FN(task_pt_regs), \
50905096
FN(get_branch_snapshot), \
50915097
FN(trace_vprintk), \
5098+
FN(skc_to_unix_sock), \
50925099
/* */
50935100

50945101
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

0 commit comments

Comments
 (0)