162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include "vmlinux.h"
362306a36Sopenharmony_ci#include "net_shared.h"
462306a36Sopenharmony_ci#include <bpf/bpf_helpers.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciSEC("cgroup/sock")
762306a36Sopenharmony_ciint bpf_prog1(struct bpf_sock *sk)
862306a36Sopenharmony_ci{
962306a36Sopenharmony_ci	char fmt[] = "socket: family %d type %d protocol %d\n";
1062306a36Sopenharmony_ci	char fmt2[] = "socket: uid %u gid %u\n";
1162306a36Sopenharmony_ci	__u64 gid_uid = bpf_get_current_uid_gid();
1262306a36Sopenharmony_ci	__u32 uid = gid_uid & 0xffffffff;
1362306a36Sopenharmony_ci	__u32 gid = gid_uid >> 32;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci	bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
1662306a36Sopenharmony_ci	bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci	/* block AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6 sockets
1962306a36Sopenharmony_ci	 * ie., make ping6 fail
2062306a36Sopenharmony_ci	 */
2162306a36Sopenharmony_ci	if (sk->family == AF_INET6 &&
2262306a36Sopenharmony_ci	    sk->type == SOCK_DGRAM   &&
2362306a36Sopenharmony_ci	    sk->protocol == IPPROTO_ICMPV6)
2462306a36Sopenharmony_ci		return 0;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	return 1;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciSEC("cgroup/sock")
3062306a36Sopenharmony_ciint bpf_prog2(struct bpf_sock *sk)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	char fmt[] = "socket: family %d type %d protocol %d\n";
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	/* block AF_INET, SOCK_DGRAM, IPPROTO_ICMP sockets
3762306a36Sopenharmony_ci	 * ie., make ping fail
3862306a36Sopenharmony_ci	 */
3962306a36Sopenharmony_ci	if (sk->family == AF_INET &&
4062306a36Sopenharmony_ci	    sk->type == SOCK_DGRAM  &&
4162306a36Sopenharmony_ci	    sk->protocol == IPPROTO_ICMP)
4262306a36Sopenharmony_ci		return 0;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	return 1;
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cichar _license[] SEC("license") = "GPL";
48