104e92818SKuniyuki Iwashima // SPDX-License-Identifier: GPL-2.0
204e92818SKuniyuki Iwashima /* Copyright Amazon.com Inc. or its affiliates. */
304e92818SKuniyuki Iwashima #include "bpf_iter.h"
404e92818SKuniyuki Iwashima #include "bpf_tracing_net.h"
504e92818SKuniyuki Iwashima #include <bpf/bpf_helpers.h>
604e92818SKuniyuki Iwashima #include <bpf/bpf_endian.h>
704e92818SKuniyuki Iwashima
804e92818SKuniyuki Iwashima char _license[] SEC("license") = "GPL";
904e92818SKuniyuki Iwashima
sock_i_ino(const struct sock * sk)1004e92818SKuniyuki Iwashima static long sock_i_ino(const struct sock *sk)
1104e92818SKuniyuki Iwashima {
1204e92818SKuniyuki Iwashima const struct socket *sk_socket = sk->sk_socket;
1304e92818SKuniyuki Iwashima const struct inode *inode;
1404e92818SKuniyuki Iwashima unsigned long ino;
1504e92818SKuniyuki Iwashima
1604e92818SKuniyuki Iwashima if (!sk_socket)
1704e92818SKuniyuki Iwashima return 0;
1804e92818SKuniyuki Iwashima
1904e92818SKuniyuki Iwashima inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
2004e92818SKuniyuki Iwashima bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
2104e92818SKuniyuki Iwashima return ino;
2204e92818SKuniyuki Iwashima }
2304e92818SKuniyuki Iwashima
2404e92818SKuniyuki Iwashima SEC("iter/unix")
dump_unix(struct bpf_iter__unix * ctx)2504e92818SKuniyuki Iwashima int dump_unix(struct bpf_iter__unix *ctx)
2604e92818SKuniyuki Iwashima {
2704e92818SKuniyuki Iwashima struct unix_sock *unix_sk = ctx->unix_sk;
2804e92818SKuniyuki Iwashima struct sock *sk = (struct sock *)unix_sk;
2904e92818SKuniyuki Iwashima struct seq_file *seq;
3004e92818SKuniyuki Iwashima __u32 seq_num;
3104e92818SKuniyuki Iwashima
3204e92818SKuniyuki Iwashima if (!unix_sk)
3304e92818SKuniyuki Iwashima return 0;
3404e92818SKuniyuki Iwashima
3504e92818SKuniyuki Iwashima seq = ctx->meta->seq;
3604e92818SKuniyuki Iwashima seq_num = ctx->meta->seq_num;
3704e92818SKuniyuki Iwashima if (seq_num == 0)
3804e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, "Num RefCount Protocol Flags Type St Inode Path\n");
3904e92818SKuniyuki Iwashima
4004e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, "%pK: %08X %08X %08X %04X %02X %8lu",
4104e92818SKuniyuki Iwashima unix_sk,
4204e92818SKuniyuki Iwashima sk->sk_refcnt.refs.counter,
4304e92818SKuniyuki Iwashima 0,
4404e92818SKuniyuki Iwashima sk->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
4504e92818SKuniyuki Iwashima sk->sk_type,
4604e92818SKuniyuki Iwashima sk->sk_socket ?
4704e92818SKuniyuki Iwashima (sk->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
4804e92818SKuniyuki Iwashima (sk->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
4904e92818SKuniyuki Iwashima sock_i_ino(sk));
5004e92818SKuniyuki Iwashima
5104e92818SKuniyuki Iwashima if (unix_sk->addr) {
525ce7ab49SKuniyuki Iwashima if (unix_sk->addr->name->sun_path[0]) {
5304e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, " %s", unix_sk->addr->name->sun_path);
5404e92818SKuniyuki Iwashima } else {
5504e92818SKuniyuki Iwashima /* The name of the abstract UNIX domain socket starts
5604e92818SKuniyuki Iwashima * with '\0' and can contain '\0'. The null bytes
5704e92818SKuniyuki Iwashima * should be escaped as done in unix_seq_show().
5804e92818SKuniyuki Iwashima */
5904e92818SKuniyuki Iwashima __u64 i, len;
6004e92818SKuniyuki Iwashima
6104e92818SKuniyuki Iwashima len = unix_sk->addr->len - sizeof(short);
6204e92818SKuniyuki Iwashima
6304e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, " @");
6404e92818SKuniyuki Iwashima
6504e92818SKuniyuki Iwashima for (i = 1; i < len; i++) {
66*a796966bSKuniyuki Iwashima /* unix_validate_addr() tests this upper bound. */
6704e92818SKuniyuki Iwashima if (i >= sizeof(struct sockaddr_un))
6804e92818SKuniyuki Iwashima break;
6904e92818SKuniyuki Iwashima
7004e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, "%c",
7104e92818SKuniyuki Iwashima unix_sk->addr->name->sun_path[i] ?:
7204e92818SKuniyuki Iwashima '@');
7304e92818SKuniyuki Iwashima }
7404e92818SKuniyuki Iwashima }
7504e92818SKuniyuki Iwashima }
7604e92818SKuniyuki Iwashima
7704e92818SKuniyuki Iwashima BPF_SEQ_PRINTF(seq, "\n");
7804e92818SKuniyuki Iwashima
7904e92818SKuniyuki Iwashima return 0;
8004e92818SKuniyuki Iwashima }
81