xref: /openbmc/linux/tools/testing/selftests/bpf/progs/sockopt_sk.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
18a027dc0SStanislav Fomichev // SPDX-License-Identifier: GPL-2.0
2fd5ef31fSStanislav Fomichev #include <string.h>
39cacf81fSStanislav Fomichev #include <linux/tcp.h>
48a027dc0SStanislav Fomichev #include <linux/bpf.h>
59cacf81fSStanislav Fomichev #include <netinet/in.h>
63e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
78a027dc0SStanislav Fomichev 
88a027dc0SStanislav Fomichev char _license[] SEC("license") = "GPL";
98a027dc0SStanislav Fomichev 
10361d3202SYauheni Kaliuta int page_size = 0; /* userspace should set it */
11a0cb12b0SStanislav Fomichev 
129cacf81fSStanislav Fomichev #ifndef SOL_TCP
139cacf81fSStanislav Fomichev #define SOL_TCP IPPROTO_TCP
149cacf81fSStanislav Fomichev #endif
159cacf81fSStanislav Fomichev 
168a027dc0SStanislav Fomichev #define SOL_CUSTOM			0xdeadbeef
178a027dc0SStanislav Fomichev 
188a027dc0SStanislav Fomichev struct sockopt_sk {
198a027dc0SStanislav Fomichev 	__u8 val;
208a027dc0SStanislav Fomichev };
218a027dc0SStanislav Fomichev 
2236b5d471SAndrii Nakryiko struct {
2336b5d471SAndrii Nakryiko 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
2436b5d471SAndrii Nakryiko 	__uint(map_flags, BPF_F_NO_PREALLOC);
2536b5d471SAndrii Nakryiko 	__type(key, int);
2636b5d471SAndrii Nakryiko 	__type(value, struct sockopt_sk);
2736b5d471SAndrii Nakryiko } socket_storage_map SEC(".maps");
288a027dc0SStanislav Fomichev 
298a027dc0SStanislav Fomichev SEC("cgroup/getsockopt")
_getsockopt(struct bpf_sockopt * ctx)308a027dc0SStanislav Fomichev int _getsockopt(struct bpf_sockopt *ctx)
318a027dc0SStanislav Fomichev {
328a027dc0SStanislav Fomichev 	__u8 *optval_end = ctx->optval_end;
338a027dc0SStanislav Fomichev 	__u8 *optval = ctx->optval;
348a027dc0SStanislav Fomichev 	struct sockopt_sk *storage;
35*833d67ecSStanislav Fomichev 	struct bpf_sock *sk;
36*833d67ecSStanislav Fomichev 
37*833d67ecSStanislav Fomichev 	/* Bypass AF_NETLINK. */
38*833d67ecSStanislav Fomichev 	sk = ctx->sk;
39*833d67ecSStanislav Fomichev 	if (sk && sk->family == AF_NETLINK)
40*833d67ecSStanislav Fomichev 		goto out;
418a027dc0SStanislav Fomichev 
426a3a3dccSStanislav Fomichev 	/* Make sure bpf_get_netns_cookie is callable.
436a3a3dccSStanislav Fomichev 	 */
446a3a3dccSStanislav Fomichev 	if (bpf_get_netns_cookie(NULL) == 0)
456a3a3dccSStanislav Fomichev 		return 0;
466a3a3dccSStanislav Fomichev 
476a3a3dccSStanislav Fomichev 	if (bpf_get_netns_cookie(ctx) == 0)
486a3a3dccSStanislav Fomichev 		return 0;
496a3a3dccSStanislav Fomichev 
50a0cb12b0SStanislav Fomichev 	if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
518a027dc0SStanislav Fomichev 		/* Not interested in SOL_IP:IP_TOS;
528a027dc0SStanislav Fomichev 		 * let next BPF program in the cgroup chain or kernel
538a027dc0SStanislav Fomichev 		 * handle it.
548a027dc0SStanislav Fomichev 		 */
55a0cb12b0SStanislav Fomichev 		goto out;
568a027dc0SStanislav Fomichev 	}
57a0cb12b0SStanislav Fomichev 
588a027dc0SStanislav Fomichev 	if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
598a027dc0SStanislav Fomichev 		/* Not interested in SOL_SOCKET:SO_SNDBUF;
608a027dc0SStanislav Fomichev 		 * let next BPF program in the cgroup chain or kernel
618a027dc0SStanislav Fomichev 		 * handle it.
628a027dc0SStanislav Fomichev 		 */
638a027dc0SStanislav Fomichev 		goto out;
648a027dc0SStanislav Fomichev 	}
658a027dc0SStanislav Fomichev 
668a027dc0SStanislav Fomichev 	if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
67fd5ef31fSStanislav Fomichev 		/* Not interested in SOL_TCP:TCP_CONGESTION;
68fd5ef31fSStanislav Fomichev 		 * let next BPF program in the cgroup chain or kernel
69fd5ef31fSStanislav Fomichev 		 * handle it.
70fd5ef31fSStanislav Fomichev 		 */
71fd5ef31fSStanislav Fomichev 		goto out;
72fd5ef31fSStanislav Fomichev 	}
73fd5ef31fSStanislav Fomichev 
74fd5ef31fSStanislav Fomichev 	if (ctx->level == SOL_TCP && ctx->optname == TCP_ZEROCOPY_RECEIVE) {
759cacf81fSStanislav Fomichev 		/* Verify that TCP_ZEROCOPY_RECEIVE triggers.
769cacf81fSStanislav Fomichev 		 * It has a custom implementation for performance
779cacf81fSStanislav Fomichev 		 * reasons.
789cacf81fSStanislav Fomichev 		 */
799cacf81fSStanislav Fomichev 
809cacf81fSStanislav Fomichev 		/* Check that optval contains address (__u64) */
81fc1ca955SFelix Maurer 		if (optval + sizeof(__u64) > optval_end)
82fc1ca955SFelix Maurer 			return 0; /* bounds check */
831080ef5cSYiFei Zhu 
849cacf81fSStanislav Fomichev 		if (((struct tcp_zerocopy_receive *)optval)->address != 0)
859cacf81fSStanislav Fomichev 			return 0; /* unexpected data */
861080ef5cSYiFei Zhu 
879cacf81fSStanislav Fomichev 		goto out;
889cacf81fSStanislav Fomichev 	}
899cacf81fSStanislav Fomichev 
909cacf81fSStanislav Fomichev 	if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
91a0cb12b0SStanislav Fomichev 		if (optval + 1 > optval_end)
92a0cb12b0SStanislav Fomichev 			return 0; /* bounds check */
931080ef5cSYiFei Zhu 
94a0cb12b0SStanislav Fomichev 		ctx->retval = 0; /* Reset system call return value to zero */
95a0cb12b0SStanislav Fomichev 
96a0cb12b0SStanislav Fomichev 		/* Always export 0x55 */
97a0cb12b0SStanislav Fomichev 		optval[0] = 0x55;
98a0cb12b0SStanislav Fomichev 		ctx->optlen = 1;
99a0cb12b0SStanislav Fomichev 
100a0cb12b0SStanislav Fomichev 		/* Userspace buffer is PAGE_SIZE * 2, but BPF
101a0cb12b0SStanislav Fomichev 		 * program can only see the first PAGE_SIZE
102a0cb12b0SStanislav Fomichev 		 * bytes of data.
103a0cb12b0SStanislav Fomichev 		 */
104a0cb12b0SStanislav Fomichev 		if (optval_end - optval != page_size)
105361d3202SYauheni Kaliuta 			return 0; /* unexpected data size */
1061080ef5cSYiFei Zhu 
107a0cb12b0SStanislav Fomichev 		return 1;
108a0cb12b0SStanislav Fomichev 	}
109a0cb12b0SStanislav Fomichev 
110a0cb12b0SStanislav Fomichev 	if (ctx->level != SOL_CUSTOM)
1118a027dc0SStanislav Fomichev 		return 0; /* deny everything except custom level */
1121080ef5cSYiFei Zhu 
1138a027dc0SStanislav Fomichev 	if (optval + 1 > optval_end)
1148a027dc0SStanislav Fomichev 		return 0; /* bounds check */
1151080ef5cSYiFei Zhu 
1168a027dc0SStanislav Fomichev 	storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
1178a027dc0SStanislav Fomichev 				     BPF_SK_STORAGE_GET_F_CREATE);
1188a027dc0SStanislav Fomichev 	if (!storage)
1198a027dc0SStanislav Fomichev 		return 0; /* couldn't get sk storage */
1201080ef5cSYiFei Zhu 
1218a027dc0SStanislav Fomichev 	if (!ctx->retval)
1228a027dc0SStanislav Fomichev 		return 0; /* kernel should not have handled
1231080ef5cSYiFei Zhu 			   * SOL_CUSTOM, something is wrong!
1248a027dc0SStanislav Fomichev 			   */
1258a027dc0SStanislav Fomichev 	ctx->retval = 0; /* Reset system call return value to zero */
1268a027dc0SStanislav Fomichev 
1278a027dc0SStanislav Fomichev 	optval[0] = storage->val;
1288a027dc0SStanislav Fomichev 	ctx->optlen = 1;
1298a027dc0SStanislav Fomichev 
1308a027dc0SStanislav Fomichev 	return 1;
1318a027dc0SStanislav Fomichev 
1328a027dc0SStanislav Fomichev out:
1338a027dc0SStanislav Fomichev 	/* optval larger than PAGE_SIZE use kernel's buffer. */
1348a027dc0SStanislav Fomichev 	if (ctx->optlen > page_size)
1358a027dc0SStanislav Fomichev 		ctx->optlen = 0;
1368a027dc0SStanislav Fomichev 	return 1;
1378a027dc0SStanislav Fomichev }
1388a027dc0SStanislav Fomichev 
1398a027dc0SStanislav Fomichev SEC("cgroup/setsockopt")
_setsockopt(struct bpf_sockopt * ctx)140*833d67ecSStanislav Fomichev int _setsockopt(struct bpf_sockopt *ctx)
141*833d67ecSStanislav Fomichev {
142*833d67ecSStanislav Fomichev 	__u8 *optval_end = ctx->optval_end;
143*833d67ecSStanislav Fomichev 	__u8 *optval = ctx->optval;
144*833d67ecSStanislav Fomichev 	struct sockopt_sk *storage;
145*833d67ecSStanislav Fomichev 	struct bpf_sock *sk;
1468a027dc0SStanislav Fomichev 
1476a3a3dccSStanislav Fomichev 	/* Bypass AF_NETLINK. */
1486a3a3dccSStanislav Fomichev 	sk = ctx->sk;
1496a3a3dccSStanislav Fomichev 	if (sk && sk->family == AF_NETLINK)
1506a3a3dccSStanislav Fomichev 		goto out;
1516a3a3dccSStanislav Fomichev 
1526a3a3dccSStanislav Fomichev 	/* Make sure bpf_get_netns_cookie is callable.
1536a3a3dccSStanislav Fomichev 	 */
1546a3a3dccSStanislav Fomichev 	if (bpf_get_netns_cookie(NULL) == 0)
155a0cb12b0SStanislav Fomichev 		return 0;
1568a027dc0SStanislav Fomichev 
1578a027dc0SStanislav Fomichev 	if (bpf_get_netns_cookie(ctx) == 0)
1588a027dc0SStanislav Fomichev 		return 0;
1598a027dc0SStanislav Fomichev 
160a0cb12b0SStanislav Fomichev 	if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
1618a027dc0SStanislav Fomichev 		/* Not interested in SOL_IP:IP_TOS;
162a0cb12b0SStanislav Fomichev 		 * let next BPF program in the cgroup chain or kernel
1638a027dc0SStanislav Fomichev 		 * handle it.
1648a027dc0SStanislav Fomichev 		 */
1658a027dc0SStanislav Fomichev 		ctx->optlen = 0; /* bypass optval>PAGE_SIZE */
1668a027dc0SStanislav Fomichev 		return 1;
1678a027dc0SStanislav Fomichev 	}
1681080ef5cSYiFei Zhu 
1698a027dc0SStanislav Fomichev 	if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
1708a027dc0SStanislav Fomichev 		/* Overwrite SO_SNDBUF value */
1718a027dc0SStanislav Fomichev 
1728a027dc0SStanislav Fomichev 		if (optval + sizeof(__u32) > optval_end)
1738a027dc0SStanislav Fomichev 			return 0; /* bounds check */
1748a027dc0SStanislav Fomichev 
1758a027dc0SStanislav Fomichev 		*(__u32 *)optval = 0x55AA;
176fd5ef31fSStanislav Fomichev 		ctx->optlen = 4;
177fd5ef31fSStanislav Fomichev 
178fd5ef31fSStanislav Fomichev 		return 1;
179fd5ef31fSStanislav Fomichev 	}
1801080ef5cSYiFei Zhu 
181fd5ef31fSStanislav Fomichev 	if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
182fd5ef31fSStanislav Fomichev 		/* Always use cubic */
183fd5ef31fSStanislav Fomichev 
184fd5ef31fSStanislav Fomichev 		if (optval + 5 > optval_end)
185fd5ef31fSStanislav Fomichev 			return 0; /* bounds check */
186fd5ef31fSStanislav Fomichev 
187fd5ef31fSStanislav Fomichev 		memcpy(optval, "cubic", 5);
188a0cb12b0SStanislav Fomichev 		ctx->optlen = 5;
189a0cb12b0SStanislav Fomichev 
190361d3202SYauheni Kaliuta 		return 1;
1911080ef5cSYiFei Zhu 	}
192a0cb12b0SStanislav Fomichev 
193a0cb12b0SStanislav Fomichev 	if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
1941080ef5cSYiFei Zhu 		/* Original optlen is larger than PAGE_SIZE. */
195a0cb12b0SStanislav Fomichev 		if (ctx->optlen != page_size * 2)
196a0cb12b0SStanislav Fomichev 			return 0; /* unexpected data size */
197a0cb12b0SStanislav Fomichev 
198a0cb12b0SStanislav Fomichev 		if (optval + 1 > optval_end)
199a0cb12b0SStanislav Fomichev 			return 0; /* bounds check */
200a0cb12b0SStanislav Fomichev 
201a0cb12b0SStanislav Fomichev 		/* Make sure we can trim the buffer. */
202a0cb12b0SStanislav Fomichev 		optval[0] = 0;
203a0cb12b0SStanislav Fomichev 		ctx->optlen = 1;
204361d3202SYauheni Kaliuta 
2051080ef5cSYiFei Zhu 		/* Usepace buffer is PAGE_SIZE * 2, but BPF
206a0cb12b0SStanislav Fomichev 		 * program can only see the first PAGE_SIZE
207a0cb12b0SStanislav Fomichev 		 * bytes of data.
208a0cb12b0SStanislav Fomichev 		 */
209a0cb12b0SStanislav Fomichev 		if (optval_end - optval != page_size)
2108a027dc0SStanislav Fomichev 			return 0; /* unexpected data size */
2111080ef5cSYiFei Zhu 
2128a027dc0SStanislav Fomichev 		return 1;
2138a027dc0SStanislav Fomichev 	}
2141080ef5cSYiFei Zhu 
2158a027dc0SStanislav Fomichev 	if (ctx->level != SOL_CUSTOM)
2168a027dc0SStanislav Fomichev 		return 0; /* deny everything except custom level */
2178a027dc0SStanislav Fomichev 
2188a027dc0SStanislav Fomichev 	if (optval + 1 > optval_end)
2191080ef5cSYiFei Zhu 		return 0; /* bounds check */
2208a027dc0SStanislav Fomichev 
2218a027dc0SStanislav Fomichev 	storage = bpf_sk_storage_get(&socket_storage_map, ctx->sk, 0,
2228a027dc0SStanislav Fomichev 				     BPF_SK_STORAGE_GET_F_CREATE);
2238a027dc0SStanislav Fomichev 	if (!storage)
2248a027dc0SStanislav Fomichev 		return 0; /* couldn't get sk storage */
2258a027dc0SStanislav Fomichev 
2268a027dc0SStanislav Fomichev 	storage->val = optval[0];
2278a027dc0SStanislav Fomichev 	ctx->optlen = -1; /* BPF has consumed this option, don't call kernel
228 			   * setsockopt handler.
229 			   */
230 
231 	return 1;
232 
233 out:
234 	/* optval larger than PAGE_SIZE use kernel's buffer. */
235 	if (ctx->optlen > page_size)
236 		ctx->optlen = 0;
237 	return 1;
238 }
239