xref: /linux/net/ipv4/tcp_bpf.c (revision f8dd95b29d7ef08c19ec9720564acf72243ddcf6)
1604326b4SDaniel Borkmann // SPDX-License-Identifier: GPL-2.0
2604326b4SDaniel Borkmann /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3604326b4SDaniel Borkmann 
4604326b4SDaniel Borkmann #include <linux/skmsg.h>
5604326b4SDaniel Borkmann #include <linux/filter.h>
6604326b4SDaniel Borkmann #include <linux/bpf.h>
7604326b4SDaniel Borkmann #include <linux/init.h>
8604326b4SDaniel Borkmann #include <linux/wait.h>
9ddce1e09SJakub Sitnicki #include <linux/util_macros.h>
10604326b4SDaniel Borkmann 
11604326b4SDaniel Borkmann #include <net/inet_common.h>
120608c69cSJohn Fastabend #include <net/tls.h>
13604326b4SDaniel Borkmann 
14e5c6de5fSJohn Fastabend void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
15e5c6de5fSJohn Fastabend {
16e5c6de5fSJohn Fastabend 	struct tcp_sock *tcp;
17e5c6de5fSJohn Fastabend 	int copied;
18e5c6de5fSJohn Fastabend 
19e5c6de5fSJohn Fastabend 	if (!skb || !skb->len || !sk_is_tcp(sk))
20e5c6de5fSJohn Fastabend 		return;
21e5c6de5fSJohn Fastabend 
22e5c6de5fSJohn Fastabend 	if (skb_bpf_strparser(skb))
23e5c6de5fSJohn Fastabend 		return;
24e5c6de5fSJohn Fastabend 
25e5c6de5fSJohn Fastabend 	tcp = tcp_sk(sk);
26e5c6de5fSJohn Fastabend 	copied = tcp->copied_seq + skb->len;
27e5c6de5fSJohn Fastabend 	WRITE_ONCE(tcp->copied_seq, copied);
28e5c6de5fSJohn Fastabend 	tcp_rcv_space_adjust(sk);
29e5c6de5fSJohn Fastabend 	__tcp_cleanup_rbuf(sk, skb->len);
30e5c6de5fSJohn Fastabend }
31e5c6de5fSJohn Fastabend 
32604326b4SDaniel Borkmann static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
33604326b4SDaniel Borkmann 			   struct sk_msg *msg, u32 apply_bytes, int flags)
34604326b4SDaniel Borkmann {
35604326b4SDaniel Borkmann 	bool apply = apply_bytes;
36604326b4SDaniel Borkmann 	struct scatterlist *sge;
37604326b4SDaniel Borkmann 	u32 size, copied = 0;
38604326b4SDaniel Borkmann 	struct sk_msg *tmp;
39604326b4SDaniel Borkmann 	int i, ret = 0;
40604326b4SDaniel Borkmann 
41604326b4SDaniel Borkmann 	tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
42604326b4SDaniel Borkmann 	if (unlikely(!tmp))
43604326b4SDaniel Borkmann 		return -ENOMEM;
44604326b4SDaniel Borkmann 
45604326b4SDaniel Borkmann 	lock_sock(sk);
46604326b4SDaniel Borkmann 	tmp->sg.start = msg->sg.start;
47604326b4SDaniel Borkmann 	i = msg->sg.start;
48604326b4SDaniel Borkmann 	do {
49604326b4SDaniel Borkmann 		sge = sk_msg_elem(msg, i);
50604326b4SDaniel Borkmann 		size = (apply && apply_bytes < sge->length) ?
51604326b4SDaniel Borkmann 			apply_bytes : sge->length;
52604326b4SDaniel Borkmann 		if (!sk_wmem_schedule(sk, size)) {
53604326b4SDaniel Borkmann 			if (!copied)
54604326b4SDaniel Borkmann 				ret = -ENOMEM;
55604326b4SDaniel Borkmann 			break;
56604326b4SDaniel Borkmann 		}
57604326b4SDaniel Borkmann 
58604326b4SDaniel Borkmann 		sk_mem_charge(sk, size);
59604326b4SDaniel Borkmann 		sk_msg_xfer(tmp, msg, i, size);
60604326b4SDaniel Borkmann 		copied += size;
61604326b4SDaniel Borkmann 		if (sge->length)
62604326b4SDaniel Borkmann 			get_page(sk_msg_page(tmp, i));
63604326b4SDaniel Borkmann 		sk_msg_iter_var_next(i);
64604326b4SDaniel Borkmann 		tmp->sg.end = i;
65604326b4SDaniel Borkmann 		if (apply) {
66604326b4SDaniel Borkmann 			apply_bytes -= size;
679072931fSPengcheng Yang 			if (!apply_bytes) {
689072931fSPengcheng Yang 				if (sge->length)
699072931fSPengcheng Yang 					sk_msg_iter_var_prev(i);
70604326b4SDaniel Borkmann 				break;
71604326b4SDaniel Borkmann 			}
729072931fSPengcheng Yang 		}
73604326b4SDaniel Borkmann 	} while (i != msg->sg.end);
74604326b4SDaniel Borkmann 
75604326b4SDaniel Borkmann 	if (!ret) {
76604326b4SDaniel Borkmann 		msg->sg.start = i;
77604326b4SDaniel Borkmann 		sk_psock_queue_msg(psock, tmp);
78552de910SJohn Fastabend 		sk_psock_data_ready(sk, psock);
79604326b4SDaniel Borkmann 	} else {
80604326b4SDaniel Borkmann 		sk_msg_free(sk, tmp);
81604326b4SDaniel Borkmann 		kfree(tmp);
82604326b4SDaniel Borkmann 	}
83604326b4SDaniel Borkmann 
84604326b4SDaniel Borkmann 	release_sock(sk);
85604326b4SDaniel Borkmann 	return ret;
86604326b4SDaniel Borkmann }
87604326b4SDaniel Borkmann 
88604326b4SDaniel Borkmann static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
89604326b4SDaniel Borkmann 			int flags, bool uncharge)
90604326b4SDaniel Borkmann {
91*f8dd95b2SDavid Howells 	struct msghdr msghdr = {};
92604326b4SDaniel Borkmann 	bool apply = apply_bytes;
93604326b4SDaniel Borkmann 	struct scatterlist *sge;
94604326b4SDaniel Borkmann 	struct page *page;
95604326b4SDaniel Borkmann 	int size, ret = 0;
96604326b4SDaniel Borkmann 	u32 off;
97604326b4SDaniel Borkmann 
98604326b4SDaniel Borkmann 	while (1) {
99ebf2e886SDavid Howells 		struct bio_vec bvec;
1000608c69cSJohn Fastabend 		bool has_tx_ulp;
1010608c69cSJohn Fastabend 
102604326b4SDaniel Borkmann 		sge = sk_msg_elem(msg, msg->sg.start);
103604326b4SDaniel Borkmann 		size = (apply && apply_bytes < sge->length) ?
104604326b4SDaniel Borkmann 			apply_bytes : sge->length;
105604326b4SDaniel Borkmann 		off  = sge->offset;
106604326b4SDaniel Borkmann 		page = sg_page(sge);
107604326b4SDaniel Borkmann 
108604326b4SDaniel Borkmann 		tcp_rate_check_app_limited(sk);
109604326b4SDaniel Borkmann retry:
110*f8dd95b2SDavid Howells 		msghdr.msg_flags = flags | MSG_SPLICE_PAGES;
1110608c69cSJohn Fastabend 		has_tx_ulp = tls_sw_has_ctx_tx(sk);
112ebf2e886SDavid Howells 		if (has_tx_ulp)
113ebf2e886SDavid Howells 			msghdr.msg_flags |= MSG_SENDPAGE_NOPOLICY;
1140608c69cSJohn Fastabend 
115*f8dd95b2SDavid Howells 		if (size < sge->length && msg->sg.start != msg->sg.end)
116ebf2e886SDavid Howells 			msghdr.msg_flags |= MSG_MORE;
117ebf2e886SDavid Howells 
118ebf2e886SDavid Howells 		bvec_set_page(&bvec, page, size, off);
119ebf2e886SDavid Howells 		iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
120ebf2e886SDavid Howells 		ret = tcp_sendmsg_locked(sk, &msghdr, size);
121604326b4SDaniel Borkmann 		if (ret <= 0)
122604326b4SDaniel Borkmann 			return ret;
123ebf2e886SDavid Howells 
124604326b4SDaniel Borkmann 		if (apply)
125604326b4SDaniel Borkmann 			apply_bytes -= ret;
126604326b4SDaniel Borkmann 		msg->sg.size -= ret;
127604326b4SDaniel Borkmann 		sge->offset += ret;
128604326b4SDaniel Borkmann 		sge->length -= ret;
129604326b4SDaniel Borkmann 		if (uncharge)
130604326b4SDaniel Borkmann 			sk_mem_uncharge(sk, ret);
131604326b4SDaniel Borkmann 		if (ret != size) {
132604326b4SDaniel Borkmann 			size -= ret;
133604326b4SDaniel Borkmann 			off  += ret;
134604326b4SDaniel Borkmann 			goto retry;
135604326b4SDaniel Borkmann 		}
136604326b4SDaniel Borkmann 		if (!sge->length) {
137604326b4SDaniel Borkmann 			put_page(page);
138604326b4SDaniel Borkmann 			sk_msg_iter_next(msg, start);
139604326b4SDaniel Borkmann 			sg_init_table(sge, 1);
140604326b4SDaniel Borkmann 			if (msg->sg.start == msg->sg.end)
141604326b4SDaniel Borkmann 				break;
142604326b4SDaniel Borkmann 		}
143604326b4SDaniel Borkmann 		if (apply && !apply_bytes)
144604326b4SDaniel Borkmann 			break;
145604326b4SDaniel Borkmann 	}
146604326b4SDaniel Borkmann 
147604326b4SDaniel Borkmann 	return 0;
148604326b4SDaniel Borkmann }
149604326b4SDaniel Borkmann 
150604326b4SDaniel Borkmann static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
151604326b4SDaniel Borkmann 			       u32 apply_bytes, int flags, bool uncharge)
152604326b4SDaniel Borkmann {
153604326b4SDaniel Borkmann 	int ret;
154604326b4SDaniel Borkmann 
155604326b4SDaniel Borkmann 	lock_sock(sk);
156604326b4SDaniel Borkmann 	ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
157604326b4SDaniel Borkmann 	release_sock(sk);
158604326b4SDaniel Borkmann 	return ret;
159604326b4SDaniel Borkmann }
160604326b4SDaniel Borkmann 
161a351d608SPengcheng Yang int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
162a351d608SPengcheng Yang 			  struct sk_msg *msg, u32 bytes, int flags)
163604326b4SDaniel Borkmann {
164604326b4SDaniel Borkmann 	struct sk_psock *psock = sk_psock_get(sk);
165604326b4SDaniel Borkmann 	int ret;
166604326b4SDaniel Borkmann 
1672486ab43SWang Yufen 	if (unlikely(!psock))
1682486ab43SWang Yufen 		return -EPIPE;
1692486ab43SWang Yufen 
170604326b4SDaniel Borkmann 	ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
171604326b4SDaniel Borkmann 			tcp_bpf_push_locked(sk, msg, bytes, flags, false);
172604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
173604326b4SDaniel Borkmann 	return ret;
174604326b4SDaniel Borkmann }
175604326b4SDaniel Borkmann EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
176604326b4SDaniel Borkmann 
17788759609SCong Wang #ifdef CONFIG_BPF_SYSCALL
178b6df0078SJakub Kicinski static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
179b6df0078SJakub Kicinski 			     long timeo)
1809f2470fbSCong Wang {
1819f2470fbSCong Wang 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
1829f2470fbSCong Wang 	int ret = 0;
1839f2470fbSCong Wang 
1849f2470fbSCong Wang 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1859f2470fbSCong Wang 		return 1;
1869f2470fbSCong Wang 
1879f2470fbSCong Wang 	if (!timeo)
1889f2470fbSCong Wang 		return ret;
1899f2470fbSCong Wang 
1909f2470fbSCong Wang 	add_wait_queue(sk_sleep(sk), &wait);
1919f2470fbSCong Wang 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1929f2470fbSCong Wang 	ret = sk_wait_event(sk, &timeo,
1939f2470fbSCong Wang 			    !list_empty(&psock->ingress_msg) ||
194d0ac89f6SEric Dumazet 			    !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait);
1959f2470fbSCong Wang 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1969f2470fbSCong Wang 	remove_wait_queue(sk_sleep(sk), &wait);
1979f2470fbSCong Wang 	return ret;
1989f2470fbSCong Wang }
1999f2470fbSCong Wang 
200901546fdSJohn Fastabend static bool is_next_msg_fin(struct sk_psock *psock)
201901546fdSJohn Fastabend {
202901546fdSJohn Fastabend 	struct scatterlist *sge;
203901546fdSJohn Fastabend 	struct sk_msg *msg_rx;
204901546fdSJohn Fastabend 	int i;
205901546fdSJohn Fastabend 
206901546fdSJohn Fastabend 	msg_rx = sk_psock_peek_msg(psock);
207901546fdSJohn Fastabend 	i = msg_rx->sg.start;
208901546fdSJohn Fastabend 	sge = sk_msg_elem(msg_rx, i);
209901546fdSJohn Fastabend 	if (!sge->length) {
210901546fdSJohn Fastabend 		struct sk_buff *skb = msg_rx->skb;
211901546fdSJohn Fastabend 
212901546fdSJohn Fastabend 		if (skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
213901546fdSJohn Fastabend 			return true;
214901546fdSJohn Fastabend 	}
215901546fdSJohn Fastabend 	return false;
216901546fdSJohn Fastabend }
217901546fdSJohn Fastabend 
218c5d2177aSJohn Fastabend static int tcp_bpf_recvmsg_parser(struct sock *sk,
219c5d2177aSJohn Fastabend 				  struct msghdr *msg,
220c5d2177aSJohn Fastabend 				  size_t len,
221c5d2177aSJohn Fastabend 				  int flags,
222c5d2177aSJohn Fastabend 				  int *addr_len)
223c5d2177aSJohn Fastabend {
224e5c6de5fSJohn Fastabend 	struct tcp_sock *tcp = tcp_sk(sk);
225e5c6de5fSJohn Fastabend 	u32 seq = tcp->copied_seq;
226c5d2177aSJohn Fastabend 	struct sk_psock *psock;
227e5c6de5fSJohn Fastabend 	int copied = 0;
228c5d2177aSJohn Fastabend 
229c5d2177aSJohn Fastabend 	if (unlikely(flags & MSG_ERRQUEUE))
230c5d2177aSJohn Fastabend 		return inet_recv_error(sk, msg, len, addr_len);
231c5d2177aSJohn Fastabend 
232d900f3d2SLiu Jian 	if (!len)
233d900f3d2SLiu Jian 		return 0;
234d900f3d2SLiu Jian 
235c5d2177aSJohn Fastabend 	psock = sk_psock_get(sk);
236c5d2177aSJohn Fastabend 	if (unlikely(!psock))
237ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
238c5d2177aSJohn Fastabend 
239c5d2177aSJohn Fastabend 	lock_sock(sk);
240ea444185SJohn Fastabend 
241ea444185SJohn Fastabend 	/* We may have received data on the sk_receive_queue pre-accept and
242ea444185SJohn Fastabend 	 * then we can not use read_skb in this context because we haven't
243ea444185SJohn Fastabend 	 * assigned a sk_socket yet so have no link to the ops. The work-around
244ea444185SJohn Fastabend 	 * is to check the sk_receive_queue and in these cases read skbs off
245ea444185SJohn Fastabend 	 * queue again. The read_skb hook is not running at this point because
246ea444185SJohn Fastabend 	 * of lock_sock so we avoid having multiple runners in read_skb.
247ea444185SJohn Fastabend 	 */
248ea444185SJohn Fastabend 	if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
249ea444185SJohn Fastabend 		tcp_data_ready(sk);
250ea444185SJohn Fastabend 		/* This handles the ENOMEM errors if we both receive data
251ea444185SJohn Fastabend 		 * pre accept and are already under memory pressure. At least
252ea444185SJohn Fastabend 		 * let user know to retry.
253ea444185SJohn Fastabend 		 */
254ea444185SJohn Fastabend 		if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
255ea444185SJohn Fastabend 			copied = -EAGAIN;
256ea444185SJohn Fastabend 			goto out;
257ea444185SJohn Fastabend 		}
258ea444185SJohn Fastabend 	}
259ea444185SJohn Fastabend 
260c5d2177aSJohn Fastabend msg_bytes_ready:
261c5d2177aSJohn Fastabend 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
262901546fdSJohn Fastabend 	/* The typical case for EFAULT is the socket was gracefully
263901546fdSJohn Fastabend 	 * shutdown with a FIN pkt. So check here the other case is
264901546fdSJohn Fastabend 	 * some error on copy_page_to_iter which would be unexpected.
265901546fdSJohn Fastabend 	 * On fin return correct return code to zero.
266901546fdSJohn Fastabend 	 */
267901546fdSJohn Fastabend 	if (copied == -EFAULT) {
268901546fdSJohn Fastabend 		bool is_fin = is_next_msg_fin(psock);
269901546fdSJohn Fastabend 
270901546fdSJohn Fastabend 		if (is_fin) {
271901546fdSJohn Fastabend 			copied = 0;
272e5c6de5fSJohn Fastabend 			seq++;
273901546fdSJohn Fastabend 			goto out;
274901546fdSJohn Fastabend 		}
275901546fdSJohn Fastabend 	}
276e5c6de5fSJohn Fastabend 	seq += copied;
277c5d2177aSJohn Fastabend 	if (!copied) {
278c5d2177aSJohn Fastabend 		long timeo;
279c5d2177aSJohn Fastabend 		int data;
280c5d2177aSJohn Fastabend 
2815b2c5540SJohn Fastabend 		if (sock_flag(sk, SOCK_DONE))
2825b2c5540SJohn Fastabend 			goto out;
2835b2c5540SJohn Fastabend 
2845b2c5540SJohn Fastabend 		if (sk->sk_err) {
2855b2c5540SJohn Fastabend 			copied = sock_error(sk);
2865b2c5540SJohn Fastabend 			goto out;
2875b2c5540SJohn Fastabend 		}
2885b2c5540SJohn Fastabend 
2895b2c5540SJohn Fastabend 		if (sk->sk_shutdown & RCV_SHUTDOWN)
2905b2c5540SJohn Fastabend 			goto out;
2915b2c5540SJohn Fastabend 
2925b2c5540SJohn Fastabend 		if (sk->sk_state == TCP_CLOSE) {
2935b2c5540SJohn Fastabend 			copied = -ENOTCONN;
2945b2c5540SJohn Fastabend 			goto out;
2955b2c5540SJohn Fastabend 		}
2965b2c5540SJohn Fastabend 
297ec095263SOliver Hartkopp 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2985b2c5540SJohn Fastabend 		if (!timeo) {
2995b2c5540SJohn Fastabend 			copied = -EAGAIN;
3005b2c5540SJohn Fastabend 			goto out;
3015b2c5540SJohn Fastabend 		}
3025b2c5540SJohn Fastabend 
3035b2c5540SJohn Fastabend 		if (signal_pending(current)) {
3045b2c5540SJohn Fastabend 			copied = sock_intr_errno(timeo);
3055b2c5540SJohn Fastabend 			goto out;
3065b2c5540SJohn Fastabend 		}
3075b2c5540SJohn Fastabend 
308c5d2177aSJohn Fastabend 		data = tcp_msg_wait_data(sk, psock, timeo);
309c5d2177aSJohn Fastabend 		if (data && !sk_psock_queue_empty(psock))
310c5d2177aSJohn Fastabend 			goto msg_bytes_ready;
311c5d2177aSJohn Fastabend 		copied = -EAGAIN;
312c5d2177aSJohn Fastabend 	}
3135b2c5540SJohn Fastabend out:
314e5c6de5fSJohn Fastabend 	WRITE_ONCE(tcp->copied_seq, seq);
315e5c6de5fSJohn Fastabend 	tcp_rcv_space_adjust(sk);
316e5c6de5fSJohn Fastabend 	if (copied > 0)
317e5c6de5fSJohn Fastabend 		__tcp_cleanup_rbuf(sk, copied);
318c5d2177aSJohn Fastabend 	release_sock(sk);
319c5d2177aSJohn Fastabend 	sk_psock_put(sk, psock);
320c5d2177aSJohn Fastabend 	return copied;
321c5d2177aSJohn Fastabend }
322c5d2177aSJohn Fastabend 
323c0fd336eSYueHaibing static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
324ec095263SOliver Hartkopp 			   int flags, int *addr_len)
325c0fd336eSYueHaibing {
326c0fd336eSYueHaibing 	struct sk_psock *psock;
327c0fd336eSYueHaibing 	int copied, ret;
328c0fd336eSYueHaibing 
32918f02ad1SXiyu Yang 	if (unlikely(flags & MSG_ERRQUEUE))
33018f02ad1SXiyu Yang 		return inet_recv_error(sk, msg, len, addr_len);
33118f02ad1SXiyu Yang 
332d900f3d2SLiu Jian 	if (!len)
333d900f3d2SLiu Jian 		return 0;
334d900f3d2SLiu Jian 
335c0fd336eSYueHaibing 	psock = sk_psock_get(sk);
336c0fd336eSYueHaibing 	if (unlikely(!psock))
337ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
338c0fd336eSYueHaibing 	if (!skb_queue_empty(&sk->sk_receive_queue) &&
33918f02ad1SXiyu Yang 	    sk_psock_queue_empty(psock)) {
34018f02ad1SXiyu Yang 		sk_psock_put(sk, psock);
341ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
34218f02ad1SXiyu Yang 	}
343c0fd336eSYueHaibing 	lock_sock(sk);
344c0fd336eSYueHaibing msg_bytes_ready:
3452bc793e3SCong Wang 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
346c0fd336eSYueHaibing 	if (!copied) {
347c0fd336eSYueHaibing 		long timeo;
348c49661aaSCong Wang 		int data;
349c0fd336eSYueHaibing 
350ec095263SOliver Hartkopp 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
351b6df0078SJakub Kicinski 		data = tcp_msg_wait_data(sk, psock, timeo);
352c0fd336eSYueHaibing 		if (data) {
353c0fd336eSYueHaibing 			if (!sk_psock_queue_empty(psock))
354c0fd336eSYueHaibing 				goto msg_bytes_ready;
355c0fd336eSYueHaibing 			release_sock(sk);
356c0fd336eSYueHaibing 			sk_psock_put(sk, psock);
357ec095263SOliver Hartkopp 			return tcp_recvmsg(sk, msg, len, flags, addr_len);
358c0fd336eSYueHaibing 		}
359c0fd336eSYueHaibing 		copied = -EAGAIN;
360c0fd336eSYueHaibing 	}
361c0fd336eSYueHaibing 	ret = copied;
362c0fd336eSYueHaibing 	release_sock(sk);
363c0fd336eSYueHaibing 	sk_psock_put(sk, psock);
364c0fd336eSYueHaibing 	return ret;
365c0fd336eSYueHaibing }
366c0fd336eSYueHaibing 
367604326b4SDaniel Borkmann static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
368604326b4SDaniel Borkmann 				struct sk_msg *msg, int *copied, int flags)
369604326b4SDaniel Borkmann {
370a351d608SPengcheng Yang 	bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
371604326b4SDaniel Borkmann 	struct sock *sk_redir;
3728ec95b94SWang Yufen 	u32 tosend, origsize, sent, delta = 0;
3737a9841caSPengcheng Yang 	u32 eval;
374604326b4SDaniel Borkmann 	int ret;
375604326b4SDaniel Borkmann 
376604326b4SDaniel Borkmann more_data:
3777246d8edSJohn Fastabend 	if (psock->eval == __SK_NONE) {
3787246d8edSJohn Fastabend 		/* Track delta in msg size to add/subtract it on SK_DROP from
3797246d8edSJohn Fastabend 		 * returned to user copied size. This ensures user doesn't
3807246d8edSJohn Fastabend 		 * get a positive return code with msg_cut_data and SK_DROP
3817246d8edSJohn Fastabend 		 * verdict.
3827246d8edSJohn Fastabend 		 */
3837246d8edSJohn Fastabend 		delta = msg->sg.size;
384604326b4SDaniel Borkmann 		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
3857246d8edSJohn Fastabend 		delta -= msg->sg.size;
3867246d8edSJohn Fastabend 	}
387604326b4SDaniel Borkmann 
388604326b4SDaniel Borkmann 	if (msg->cork_bytes &&
389604326b4SDaniel Borkmann 	    msg->cork_bytes > msg->sg.size && !enospc) {
390604326b4SDaniel Borkmann 		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
391604326b4SDaniel Borkmann 		if (!psock->cork) {
392604326b4SDaniel Borkmann 			psock->cork = kzalloc(sizeof(*psock->cork),
393604326b4SDaniel Borkmann 					      GFP_ATOMIC | __GFP_NOWARN);
394604326b4SDaniel Borkmann 			if (!psock->cork)
395604326b4SDaniel Borkmann 				return -ENOMEM;
396604326b4SDaniel Borkmann 		}
397604326b4SDaniel Borkmann 		memcpy(psock->cork, msg, sizeof(*msg));
398604326b4SDaniel Borkmann 		return 0;
399604326b4SDaniel Borkmann 	}
400604326b4SDaniel Borkmann 
401604326b4SDaniel Borkmann 	tosend = msg->sg.size;
402604326b4SDaniel Borkmann 	if (psock->apply_bytes && psock->apply_bytes < tosend)
403604326b4SDaniel Borkmann 		tosend = psock->apply_bytes;
4047a9841caSPengcheng Yang 	eval = __SK_NONE;
405604326b4SDaniel Borkmann 
406604326b4SDaniel Borkmann 	switch (psock->eval) {
407604326b4SDaniel Borkmann 	case __SK_PASS:
408604326b4SDaniel Borkmann 		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
409604326b4SDaniel Borkmann 		if (unlikely(ret)) {
410604326b4SDaniel Borkmann 			*copied -= sk_msg_free(sk, msg);
411604326b4SDaniel Borkmann 			break;
412604326b4SDaniel Borkmann 		}
413604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
414604326b4SDaniel Borkmann 		break;
415604326b4SDaniel Borkmann 	case __SK_REDIRECT:
416a351d608SPengcheng Yang 		redir_ingress = psock->redir_ingress;
417604326b4SDaniel Borkmann 		sk_redir = psock->sk_redir;
418604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
419cd9733f5SLiu Jian 		if (!psock->apply_bytes) {
420cd9733f5SLiu Jian 			/* Clean up before releasing the sock lock. */
421cd9733f5SLiu Jian 			eval = psock->eval;
422cd9733f5SLiu Jian 			psock->eval = __SK_NONE;
423cd9733f5SLiu Jian 			psock->sk_redir = NULL;
424cd9733f5SLiu Jian 		}
425604326b4SDaniel Borkmann 		if (psock->cork) {
426604326b4SDaniel Borkmann 			cork = true;
427604326b4SDaniel Borkmann 			psock->cork = NULL;
428604326b4SDaniel Borkmann 		}
4298ec95b94SWang Yufen 		sk_msg_return(sk, msg, tosend);
430604326b4SDaniel Borkmann 		release_sock(sk);
431cd9733f5SLiu Jian 
4328ec95b94SWang Yufen 		origsize = msg->sg.size;
433a351d608SPengcheng Yang 		ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
434a351d608SPengcheng Yang 					    msg, tosend, flags);
4358ec95b94SWang Yufen 		sent = origsize - msg->sg.size;
436cd9733f5SLiu Jian 
437cd9733f5SLiu Jian 		if (eval == __SK_REDIRECT)
438cd9733f5SLiu Jian 			sock_put(sk_redir);
439cd9733f5SLiu Jian 
440604326b4SDaniel Borkmann 		lock_sock(sk);
441604326b4SDaniel Borkmann 		if (unlikely(ret < 0)) {
442604326b4SDaniel Borkmann 			int free = sk_msg_free_nocharge(sk, msg);
443604326b4SDaniel Borkmann 
444604326b4SDaniel Borkmann 			if (!cork)
445604326b4SDaniel Borkmann 				*copied -= free;
446604326b4SDaniel Borkmann 		}
447604326b4SDaniel Borkmann 		if (cork) {
448604326b4SDaniel Borkmann 			sk_msg_free(sk, msg);
449604326b4SDaniel Borkmann 			kfree(msg);
450604326b4SDaniel Borkmann 			msg = NULL;
451604326b4SDaniel Borkmann 			ret = 0;
452604326b4SDaniel Borkmann 		}
453604326b4SDaniel Borkmann 		break;
454604326b4SDaniel Borkmann 	case __SK_DROP:
455604326b4SDaniel Borkmann 	default:
456604326b4SDaniel Borkmann 		sk_msg_free_partial(sk, msg, tosend);
457604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
4587246d8edSJohn Fastabend 		*copied -= (tosend + delta);
459604326b4SDaniel Borkmann 		return -EACCES;
460604326b4SDaniel Borkmann 	}
461604326b4SDaniel Borkmann 
462604326b4SDaniel Borkmann 	if (likely(!ret)) {
463604326b4SDaniel Borkmann 		if (!psock->apply_bytes) {
464604326b4SDaniel Borkmann 			psock->eval =  __SK_NONE;
465604326b4SDaniel Borkmann 			if (psock->sk_redir) {
466604326b4SDaniel Borkmann 				sock_put(psock->sk_redir);
467604326b4SDaniel Borkmann 				psock->sk_redir = NULL;
468604326b4SDaniel Borkmann 			}
469604326b4SDaniel Borkmann 		}
470604326b4SDaniel Borkmann 		if (msg &&
471604326b4SDaniel Borkmann 		    msg->sg.data[msg->sg.start].page_link &&
47284472b43SWang Yufen 		    msg->sg.data[msg->sg.start].length) {
47384472b43SWang Yufen 			if (eval == __SK_REDIRECT)
4748ec95b94SWang Yufen 				sk_mem_charge(sk, tosend - sent);
475604326b4SDaniel Borkmann 			goto more_data;
476604326b4SDaniel Borkmann 		}
47784472b43SWang Yufen 	}
478604326b4SDaniel Borkmann 	return ret;
479604326b4SDaniel Borkmann }
480604326b4SDaniel Borkmann 
481604326b4SDaniel Borkmann static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
482604326b4SDaniel Borkmann {
483604326b4SDaniel Borkmann 	struct sk_msg tmp, *msg_tx = NULL;
484604326b4SDaniel Borkmann 	int copied = 0, err = 0;
485604326b4SDaniel Borkmann 	struct sk_psock *psock;
486604326b4SDaniel Borkmann 	long timeo;
48741477662SJakub Kicinski 	int flags;
48841477662SJakub Kicinski 
489ebf2e886SDavid Howells 	/* Don't let internal sendpage flags through */
49041477662SJakub Kicinski 	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
49141477662SJakub Kicinski 	flags |= MSG_NO_SHARED_FRAGS;
492604326b4SDaniel Borkmann 
493604326b4SDaniel Borkmann 	psock = sk_psock_get(sk);
494604326b4SDaniel Borkmann 	if (unlikely(!psock))
495604326b4SDaniel Borkmann 		return tcp_sendmsg(sk, msg, size);
496604326b4SDaniel Borkmann 
497604326b4SDaniel Borkmann 	lock_sock(sk);
498604326b4SDaniel Borkmann 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
499604326b4SDaniel Borkmann 	while (msg_data_left(msg)) {
500604326b4SDaniel Borkmann 		bool enospc = false;
501604326b4SDaniel Borkmann 		u32 copy, osize;
502604326b4SDaniel Borkmann 
503604326b4SDaniel Borkmann 		if (sk->sk_err) {
504604326b4SDaniel Borkmann 			err = -sk->sk_err;
505604326b4SDaniel Borkmann 			goto out_err;
506604326b4SDaniel Borkmann 		}
507604326b4SDaniel Borkmann 
508604326b4SDaniel Borkmann 		copy = msg_data_left(msg);
509604326b4SDaniel Borkmann 		if (!sk_stream_memory_free(sk))
510604326b4SDaniel Borkmann 			goto wait_for_sndbuf;
511604326b4SDaniel Borkmann 		if (psock->cork) {
512604326b4SDaniel Borkmann 			msg_tx = psock->cork;
513604326b4SDaniel Borkmann 		} else {
514604326b4SDaniel Borkmann 			msg_tx = &tmp;
515604326b4SDaniel Borkmann 			sk_msg_init(msg_tx);
516604326b4SDaniel Borkmann 		}
517604326b4SDaniel Borkmann 
518604326b4SDaniel Borkmann 		osize = msg_tx->sg.size;
519604326b4SDaniel Borkmann 		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
520604326b4SDaniel Borkmann 		if (err) {
521604326b4SDaniel Borkmann 			if (err != -ENOSPC)
522604326b4SDaniel Borkmann 				goto wait_for_memory;
523604326b4SDaniel Borkmann 			enospc = true;
524604326b4SDaniel Borkmann 			copy = msg_tx->sg.size - osize;
525604326b4SDaniel Borkmann 		}
526604326b4SDaniel Borkmann 
527604326b4SDaniel Borkmann 		err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
528604326b4SDaniel Borkmann 					       copy);
529604326b4SDaniel Borkmann 		if (err < 0) {
530604326b4SDaniel Borkmann 			sk_msg_trim(sk, msg_tx, osize);
531604326b4SDaniel Borkmann 			goto out_err;
532604326b4SDaniel Borkmann 		}
533604326b4SDaniel Borkmann 
534604326b4SDaniel Borkmann 		copied += copy;
535604326b4SDaniel Borkmann 		if (psock->cork_bytes) {
536604326b4SDaniel Borkmann 			if (size > psock->cork_bytes)
537604326b4SDaniel Borkmann 				psock->cork_bytes = 0;
538604326b4SDaniel Borkmann 			else
539604326b4SDaniel Borkmann 				psock->cork_bytes -= size;
540604326b4SDaniel Borkmann 			if (psock->cork_bytes && !enospc)
541604326b4SDaniel Borkmann 				goto out_err;
542604326b4SDaniel Borkmann 			/* All cork bytes are accounted, rerun the prog. */
543604326b4SDaniel Borkmann 			psock->eval = __SK_NONE;
544604326b4SDaniel Borkmann 			psock->cork_bytes = 0;
545604326b4SDaniel Borkmann 		}
546604326b4SDaniel Borkmann 
547604326b4SDaniel Borkmann 		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
548604326b4SDaniel Borkmann 		if (unlikely(err < 0))
549604326b4SDaniel Borkmann 			goto out_err;
550604326b4SDaniel Borkmann 		continue;
551604326b4SDaniel Borkmann wait_for_sndbuf:
552604326b4SDaniel Borkmann 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
553604326b4SDaniel Borkmann wait_for_memory:
554604326b4SDaniel Borkmann 		err = sk_stream_wait_memory(sk, &timeo);
555604326b4SDaniel Borkmann 		if (err) {
556604326b4SDaniel Borkmann 			if (msg_tx && msg_tx != psock->cork)
557604326b4SDaniel Borkmann 				sk_msg_free(sk, msg_tx);
558604326b4SDaniel Borkmann 			goto out_err;
559604326b4SDaniel Borkmann 		}
560604326b4SDaniel Borkmann 	}
561604326b4SDaniel Borkmann out_err:
562604326b4SDaniel Borkmann 	if (err < 0)
563604326b4SDaniel Borkmann 		err = sk_stream_error(sk, msg->msg_flags, err);
564604326b4SDaniel Borkmann 	release_sock(sk);
565604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
566604326b4SDaniel Borkmann 	return copied ? copied : err;
567604326b4SDaniel Borkmann }
568604326b4SDaniel Borkmann 
569604326b4SDaniel Borkmann static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
570604326b4SDaniel Borkmann 			    size_t size, int flags)
571604326b4SDaniel Borkmann {
572de17c685SDavid Howells 	struct bio_vec bvec;
573de17c685SDavid Howells 	struct msghdr msg = {
574de17c685SDavid Howells 		.msg_flags = flags | MSG_SPLICE_PAGES,
575de17c685SDavid Howells 	};
576604326b4SDaniel Borkmann 
577de17c685SDavid Howells 	bvec_set_page(&bvec, page, size, offset);
578de17c685SDavid Howells 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
579604326b4SDaniel Borkmann 
580de17c685SDavid Howells 	if (flags & MSG_SENDPAGE_NOTLAST)
581de17c685SDavid Howells 		msg.msg_flags |= MSG_MORE;
582604326b4SDaniel Borkmann 
583de17c685SDavid Howells 	return tcp_bpf_sendmsg(sk, &msg, size);
584604326b4SDaniel Borkmann }
585604326b4SDaniel Borkmann 
586604326b4SDaniel Borkmann enum {
587604326b4SDaniel Borkmann 	TCP_BPF_IPV4,
588604326b4SDaniel Borkmann 	TCP_BPF_IPV6,
589604326b4SDaniel Borkmann 	TCP_BPF_NUM_PROTS,
590604326b4SDaniel Borkmann };
591604326b4SDaniel Borkmann 
592604326b4SDaniel Borkmann enum {
593604326b4SDaniel Borkmann 	TCP_BPF_BASE,
594604326b4SDaniel Borkmann 	TCP_BPF_TX,
595c5d2177aSJohn Fastabend 	TCP_BPF_RX,
596c5d2177aSJohn Fastabend 	TCP_BPF_TXRX,
597604326b4SDaniel Borkmann 	TCP_BPF_NUM_CFGS,
598604326b4SDaniel Borkmann };
599604326b4SDaniel Borkmann 
600604326b4SDaniel Borkmann static struct proto *tcpv6_prot_saved __read_mostly;
601604326b4SDaniel Borkmann static DEFINE_SPINLOCK(tcpv6_prot_lock);
602604326b4SDaniel Borkmann static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
603604326b4SDaniel Borkmann 
604604326b4SDaniel Borkmann static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
605604326b4SDaniel Borkmann 				   struct proto *base)
606604326b4SDaniel Borkmann {
607604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE]			= *base;
608d8616ee2SWang Yufen 	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
609f747632bSLorenz Bauer 	prot[TCP_BPF_BASE].close		= sock_map_close;
610604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
611fb4e0a5eSCong Wang 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
612604326b4SDaniel Borkmann 
613604326b4SDaniel Borkmann 	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
614604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
615604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendpage		= tcp_bpf_sendpage;
616c5d2177aSJohn Fastabend 
617c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
618c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
619c5d2177aSJohn Fastabend 
620c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
621c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
622604326b4SDaniel Borkmann }
623604326b4SDaniel Borkmann 
6247b219da4SLorenz Bauer static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
625604326b4SDaniel Borkmann {
6267b219da4SLorenz Bauer 	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
627604326b4SDaniel Borkmann 		spin_lock_bh(&tcpv6_prot_lock);
628604326b4SDaniel Borkmann 		if (likely(ops != tcpv6_prot_saved)) {
629604326b4SDaniel Borkmann 			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
630604326b4SDaniel Borkmann 			smp_store_release(&tcpv6_prot_saved, ops);
631604326b4SDaniel Borkmann 		}
632604326b4SDaniel Borkmann 		spin_unlock_bh(&tcpv6_prot_lock);
633604326b4SDaniel Borkmann 	}
634604326b4SDaniel Borkmann }
635604326b4SDaniel Borkmann 
636604326b4SDaniel Borkmann static int __init tcp_bpf_v4_build_proto(void)
637604326b4SDaniel Borkmann {
638604326b4SDaniel Borkmann 	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
639604326b4SDaniel Borkmann 	return 0;
640604326b4SDaniel Borkmann }
641228a4a7bSJohn Fastabend late_initcall(tcp_bpf_v4_build_proto);
642604326b4SDaniel Borkmann 
643604326b4SDaniel Borkmann static int tcp_bpf_assert_proto_ops(struct proto *ops)
644604326b4SDaniel Borkmann {
645604326b4SDaniel Borkmann 	/* In order to avoid retpoline, we make assumptions when we call
646604326b4SDaniel Borkmann 	 * into ops if e.g. a psock is not present. Make sure they are
647604326b4SDaniel Borkmann 	 * indeed valid assumptions.
648604326b4SDaniel Borkmann 	 */
649604326b4SDaniel Borkmann 	return ops->recvmsg  == tcp_recvmsg &&
650604326b4SDaniel Borkmann 	       ops->sendmsg  == tcp_sendmsg &&
651604326b4SDaniel Borkmann 	       ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
652604326b4SDaniel Borkmann }
653604326b4SDaniel Borkmann 
65451e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
655604326b4SDaniel Borkmann {
656d19da360SLorenz Bauer 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
657d19da360SLorenz Bauer 	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
658604326b4SDaniel Borkmann 
659c5d2177aSJohn Fastabend 	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
660c5d2177aSJohn Fastabend 		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
661c5d2177aSJohn Fastabend 	}
662c5d2177aSJohn Fastabend 
6638a59f9d1SCong Wang 	if (restore) {
6648a59f9d1SCong Wang 		if (inet_csk_has_ulp(sk)) {
6658859a44eSJakub Kicinski 			/* TLS does not have an unhash proto in SW cases,
6668859a44eSJakub Kicinski 			 * but we need to ensure we stop using the sock_map
6678859a44eSJakub Kicinski 			 * unhash routine because the associated psock is being
6688859a44eSJakub Kicinski 			 * removed. So use the original unhash handler.
6698859a44eSJakub Kicinski 			 */
6708859a44eSJakub Kicinski 			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
6718a59f9d1SCong Wang 			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
6728a59f9d1SCong Wang 		} else {
6738a59f9d1SCong Wang 			sk->sk_write_space = psock->saved_write_space;
6748a59f9d1SCong Wang 			/* Pairs with lockless read in sk_clone_lock() */
675fee9ac06SPavel Begunkov 			sock_replace_proto(sk, psock->sk_proto);
6768a59f9d1SCong Wang 		}
6778a59f9d1SCong Wang 		return 0;
6788a59f9d1SCong Wang 	}
6798a59f9d1SCong Wang 
6807b219da4SLorenz Bauer 	if (sk->sk_family == AF_INET6) {
6817b219da4SLorenz Bauer 		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
6828a59f9d1SCong Wang 			return -EINVAL;
683d19da360SLorenz Bauer 
6847b219da4SLorenz Bauer 		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
685d19da360SLorenz Bauer 	}
686d19da360SLorenz Bauer 
6878a59f9d1SCong Wang 	/* Pairs with lockless read in sk_clone_lock() */
688fee9ac06SPavel Begunkov 	sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
6898a59f9d1SCong Wang 	return 0;
690604326b4SDaniel Borkmann }
6918a59f9d1SCong Wang EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
692604326b4SDaniel Borkmann 
693e8025155SJakub Sitnicki /* If a child got cloned from a listening socket that had tcp_bpf
694e8025155SJakub Sitnicki  * protocol callbacks installed, we need to restore the callbacks to
695e8025155SJakub Sitnicki  * the default ones because the child does not inherit the psock state
696e8025155SJakub Sitnicki  * that tcp_bpf callbacks expect.
697e8025155SJakub Sitnicki  */
698e8025155SJakub Sitnicki void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
699e8025155SJakub Sitnicki {
700e8025155SJakub Sitnicki 	struct proto *prot = newsk->sk_prot;
701e8025155SJakub Sitnicki 
702ddce1e09SJakub Sitnicki 	if (is_insidevar(prot, tcp_bpf_prots))
703e8025155SJakub Sitnicki 		newsk->sk_prot = sk->sk_prot_creator;
704e8025155SJakub Sitnicki }
70588759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
706