Esempio n. 1
0
def _print_sk(sk):
    inet = inet_sk(sk)

    tcp_state = TcpState(sk_tcpstate(sk))

    if sk.__sk_common.skc_family == socket.AF_INET:
        src_ip = _ipv4(sk.__sk_common.skc_rcv_saddr)
        dst_ip = _ipv4(sk.__sk_common.skc_daddr)
    elif sk.__sk_common.skc_family == socket.AF_INET6:
        src_ip = _ipv6(sk.__sk_common.skc_v6_rcv_saddr)
        dst_ip = _ipv6(sk.__sk_common.skc_v6_daddr)
    else:
        return

    src_port = socket.ntohs(inet.inet_sport)
    dst_port = socket.ntohs(sk.__sk_common.skc_dport)

    cgrp_path = ""
    if sk_fullsock(sk):
        cgrp = sock_cgroup_ptr(sk.sk_cgrp_data)
        cgrp_path = cgroup_path(cgrp).decode()

    print(
        "{:<12} {} {} {}".format(
            tcp_state.name,
            _ip_port(src_ip, src_port),
            _ip_port(dst_ip, dst_port),
            cgrp_path,
        )
    )
Esempio n. 2
0
def print_cgroup_bpf_progs(cgrp):
    cgroup_printed = False
    for attach_type in BpfAttachType:
        attach_flags = cgrp.bpf.flags[attach_type.value].value_()
        for prog in cgroup_bpf_prog_for_each(cgrp, attach_type.value):
            prog_id = prog.aux.id.value_()
            prog_name = prog.aux.name.string_().decode()
            if not cgroup_printed:
                print(cgroup_path(cgrp).decode())
                cgroup_printed = True
            print("    {:<8} {:<30} {:<15} {:<15}".format(
                prog_id, attach_type.name, attach_flags, prog_name))
Esempio n. 3
0
        return prog["cgrp_dfl_root"].cgrp
    task = find_task(prog, os.getpid())
    with open_dir(sys.argv[1], os.O_RDONLY) as fd:
        file_ = fget(task, fd)
        kn = cast("struct kernfs_node *",
                  file_.f_path.dentry.d_inode.i_private)
        return cast("struct cgroup *", kn.priv)


def print_cgroup_bpf_progs(cgrp):
    cgroup_printed = False
    for attach_type in BpfAttachType:
        attach_flags = cgrp.bpf.flags[attach_type.value].value_()
        for prog in cgroup_bpf_prog_for_each(cgrp, attach_type.value):
            prog_id = prog.aux.id.value_()
            prog_name = prog.aux.name.string_().decode()
            if not cgroup_printed:
                print(cgroup_path(cgrp).decode())
                cgroup_printed = True
            print("    {:<8} {:<30} {:<15} {:<15}".format(
                prog_id, attach_type.name, attach_flags, prog_name))


css = get_cgroup().self.address_of_()

for pos in css_for_each_descendant_pre(css):
    if sys.argv[-1] == "bpf":
        print_cgroup_bpf_progs(pos.cgroup)
    else:
        print(cgroup_path(pos.cgroup).decode())