xref: /linux/tools/testing/selftests/bpf/progs/recvmsg6_prog.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
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 
24 	sk = ctx->sk;
25 	if (!sk)
26 		return 1;
27 
28 	if (sk->family != AF_INET6)
29 		return 1;
30 
31 	if (ctx->type != SOCK_STREAM && ctx->type != SOCK_DGRAM)
32 		return 1;
33 
34 	if (!get_set_sk_priority(ctx))
35 		return 1;
36 
37 	ctx->user_ip6[0] = bpf_htonl(SERV6_IP_0);
38 	ctx->user_ip6[1] = bpf_htonl(SERV6_IP_1);
39 	ctx->user_ip6[2] = bpf_htonl(SERV6_IP_2);
40 	ctx->user_ip6[3] = bpf_htonl(SERV6_IP_3);
41 	ctx->user_port = bpf_htons(SERV6_PORT);
42 
43 	return 1;
44 }
45 
46 char _license[] SEC("license") = "GPL";
47