Back to home page

OSCL-LXR

 
 

    


0001 #include <uapi/linux/bpf.h>
0002 #include <linux/socket.h>
0003 #include <linux/net.h>
0004 #include <uapi/linux/in.h>
0005 #include <uapi/linux/in6.h>
0006 #include <bpf/bpf_helpers.h>
0007 
0008 SEC("cgroup/sock1")
0009 int bpf_prog1(struct bpf_sock *sk)
0010 {
0011     char fmt[] = "socket: family %d type %d protocol %d\n";
0012     char fmt2[] = "socket: uid %u gid %u\n";
0013     __u64 gid_uid = bpf_get_current_uid_gid();
0014     __u32 uid = gid_uid & 0xffffffff;
0015     __u32 gid = gid_uid >> 32;
0016 
0017     bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
0018     bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
0019 
0020     /* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
0021      * ie., make ping6 fail
0022      */
0023     if (sk->family == PF_INET6 &&
0024         sk->type == SOCK_RAW   &&
0025         sk->protocol == IPPROTO_ICMPV6)
0026         return 0;
0027 
0028     return 1;
0029 }
0030 
0031 SEC("cgroup/sock2")
0032 int bpf_prog2(struct bpf_sock *sk)
0033 {
0034     char fmt[] = "socket: family %d type %d protocol %d\n";
0035 
0036     bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
0037 
0038     /* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
0039      * ie., make ping fail
0040      */
0041     if (sk->family == PF_INET &&
0042         sk->type == SOCK_RAW  &&
0043         sk->protocol == IPPROTO_ICMP)
0044         return 0;
0045 
0046     return 1;
0047 }
0048 
0049 char _license[] SEC("license") = "GPL";