2016-12-02 00:48:08 +08:00
|
|
|
#include <uapi/linux/bpf.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <uapi/linux/in.h>
|
|
|
|
#include <uapi/linux/in6.h>
|
2020-01-20 21:06:49 +08:00
|
|
|
#include <bpf/bpf_helpers.h>
|
2016-12-02 00:48:08 +08:00
|
|
|
|
|
|
|
SEC("cgroup/sock1")
|
|
|
|
int bpf_prog1(struct bpf_sock *sk)
|
|
|
|
{
|
|
|
|
char fmt[] = "socket: family %d type %d protocol %d\n";
|
2017-09-01 06:05:50 +08:00
|
|
|
char fmt2[] = "socket: uid %u gid %u\n";
|
|
|
|
__u64 gid_uid = bpf_get_current_uid_gid();
|
|
|
|
__u32 uid = gid_uid & 0xffffffff;
|
|
|
|
__u32 gid = gid_uid >> 32;
|
2016-12-02 00:48:08 +08:00
|
|
|
|
|
|
|
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
|
2017-09-01 06:05:50 +08:00
|
|
|
bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
|
2016-12-02 00:48:08 +08:00
|
|
|
|
|
|
|
/* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
|
|
|
|
* ie., make ping6 fail
|
|
|
|
*/
|
|
|
|
if (sk->family == PF_INET6 &&
|
|
|
|
sk->type == SOCK_RAW &&
|
|
|
|
sk->protocol == IPPROTO_ICMPV6)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEC("cgroup/sock2")
|
|
|
|
int bpf_prog2(struct bpf_sock *sk)
|
|
|
|
{
|
|
|
|
char fmt[] = "socket: family %d type %d protocol %d\n";
|
|
|
|
|
|
|
|
bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
|
|
|
|
|
|
|
|
/* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
|
|
|
|
* ie., make ping fail
|
|
|
|
*/
|
|
|
|
if (sk->family == PF_INET &&
|
|
|
|
sk->type == SOCK_RAW &&
|
|
|
|
sk->protocol == IPPROTO_ICMP)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char _license[] SEC("license") = "GPL";
|