xref: /linux/tools/testing/selftests/bpf/progs/recvmsg6_prog.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/stddef.h>
4 #include <linux/bpf.h>
5 #include <linux/in6.h>
6 #include <sys/socket.h>
7 
8 #include <bpf/bpf_helpers.h>
9 #include <bpf/bpf_endian.h>
10 
11 #include <bpf_sockopt_helpers.h>
12 
13 #define SERV6_IP_0		0xfaceb00c /* face:b00c:1234:5678::abcd */
14 #define SERV6_IP_1		0x12345678
15 #define SERV6_IP_2		0x00000000
16 #define SERV6_IP_3		0x0000abcd
17 #define SERV6_PORT		6060
18 
19 SEC("cgroup/recvmsg6")
20 int recvmsg6_prog(struct bpf_sock_addr *ctx)
21 {
22 	struct bpf_sock *sk;
23 	__u32 user_ip4;
24 	__u16 user_port;
25 
26 	sk = ctx->sk;
27 	if (!sk)
28 		return 1;
29 
30 	if (sk->family != AF_INET6)
31 		return 1;
32 
33 	if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
34 		return 1;
35 
36 	if (!get_set_sk_priority(ctx))
37 		return 1;
38 
39 	ctx->user_ip6[0] = bpf_htonl(SERV6_IP_0);
40 	ctx->user_ip6[1] = bpf_htonl(SERV6_IP_1);
41 	ctx->user_ip6[2] = bpf_htonl(SERV6_IP_2);
42 	ctx->user_ip6[3] = bpf_htonl(SERV6_IP_3);
43 	ctx->user_port = bpf_htons(SERV6_PORT);
44 
45 	return 1;
46 }
47 
48 char _license[] SEC("license") = "GPL";
49