xref: /linux/net/ipv4/tcp_bpf.c (revision ec095263a965720e1ca39db1d9c5cd47846c789b)
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>
9604326b4SDaniel Borkmann 
10604326b4SDaniel Borkmann #include <net/inet_common.h>
110608c69cSJohn Fastabend #include <net/tls.h>
12604326b4SDaniel Borkmann 
13604326b4SDaniel Borkmann static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
14604326b4SDaniel Borkmann 			   struct sk_msg *msg, u32 apply_bytes, int flags)
15604326b4SDaniel Borkmann {
16604326b4SDaniel Borkmann 	bool apply = apply_bytes;
17604326b4SDaniel Borkmann 	struct scatterlist *sge;
18604326b4SDaniel Borkmann 	u32 size, copied = 0;
19604326b4SDaniel Borkmann 	struct sk_msg *tmp;
20604326b4SDaniel Borkmann 	int i, ret = 0;
21604326b4SDaniel Borkmann 
22604326b4SDaniel Borkmann 	tmp = kzalloc(sizeof(*tmp), __GFP_NOWARN | GFP_KERNEL);
23604326b4SDaniel Borkmann 	if (unlikely(!tmp))
24604326b4SDaniel Borkmann 		return -ENOMEM;
25604326b4SDaniel Borkmann 
26604326b4SDaniel Borkmann 	lock_sock(sk);
27604326b4SDaniel Borkmann 	tmp->sg.start = msg->sg.start;
28604326b4SDaniel Borkmann 	i = msg->sg.start;
29604326b4SDaniel Borkmann 	do {
30604326b4SDaniel Borkmann 		sge = sk_msg_elem(msg, i);
31604326b4SDaniel Borkmann 		size = (apply && apply_bytes < sge->length) ?
32604326b4SDaniel Borkmann 			apply_bytes : sge->length;
33604326b4SDaniel Borkmann 		if (!sk_wmem_schedule(sk, size)) {
34604326b4SDaniel Borkmann 			if (!copied)
35604326b4SDaniel Borkmann 				ret = -ENOMEM;
36604326b4SDaniel Borkmann 			break;
37604326b4SDaniel Borkmann 		}
38604326b4SDaniel Borkmann 
39604326b4SDaniel Borkmann 		sk_mem_charge(sk, size);
40604326b4SDaniel Borkmann 		sk_msg_xfer(tmp, msg, i, size);
41604326b4SDaniel Borkmann 		copied += size;
42604326b4SDaniel Borkmann 		if (sge->length)
43604326b4SDaniel Borkmann 			get_page(sk_msg_page(tmp, i));
44604326b4SDaniel Borkmann 		sk_msg_iter_var_next(i);
45604326b4SDaniel Borkmann 		tmp->sg.end = i;
46604326b4SDaniel Borkmann 		if (apply) {
47604326b4SDaniel Borkmann 			apply_bytes -= size;
48604326b4SDaniel Borkmann 			if (!apply_bytes)
49604326b4SDaniel Borkmann 				break;
50604326b4SDaniel Borkmann 		}
51604326b4SDaniel Borkmann 	} while (i != msg->sg.end);
52604326b4SDaniel Borkmann 
53604326b4SDaniel Borkmann 	if (!ret) {
54604326b4SDaniel Borkmann 		msg->sg.start = i;
55604326b4SDaniel Borkmann 		sk_psock_queue_msg(psock, tmp);
56552de910SJohn Fastabend 		sk_psock_data_ready(sk, psock);
57604326b4SDaniel Borkmann 	} else {
58604326b4SDaniel Borkmann 		sk_msg_free(sk, tmp);
59604326b4SDaniel Borkmann 		kfree(tmp);
60604326b4SDaniel Borkmann 	}
61604326b4SDaniel Borkmann 
62604326b4SDaniel Borkmann 	release_sock(sk);
63604326b4SDaniel Borkmann 	return ret;
64604326b4SDaniel Borkmann }
65604326b4SDaniel Borkmann 
66604326b4SDaniel Borkmann static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
67604326b4SDaniel Borkmann 			int flags, bool uncharge)
68604326b4SDaniel Borkmann {
69604326b4SDaniel Borkmann 	bool apply = apply_bytes;
70604326b4SDaniel Borkmann 	struct scatterlist *sge;
71604326b4SDaniel Borkmann 	struct page *page;
72604326b4SDaniel Borkmann 	int size, ret = 0;
73604326b4SDaniel Borkmann 	u32 off;
74604326b4SDaniel Borkmann 
75604326b4SDaniel Borkmann 	while (1) {
760608c69cSJohn Fastabend 		bool has_tx_ulp;
770608c69cSJohn Fastabend 
78604326b4SDaniel Borkmann 		sge = sk_msg_elem(msg, msg->sg.start);
79604326b4SDaniel Borkmann 		size = (apply && apply_bytes < sge->length) ?
80604326b4SDaniel Borkmann 			apply_bytes : sge->length;
81604326b4SDaniel Borkmann 		off  = sge->offset;
82604326b4SDaniel Borkmann 		page = sg_page(sge);
83604326b4SDaniel Borkmann 
84604326b4SDaniel Borkmann 		tcp_rate_check_app_limited(sk);
85604326b4SDaniel Borkmann retry:
860608c69cSJohn Fastabend 		has_tx_ulp = tls_sw_has_ctx_tx(sk);
870608c69cSJohn Fastabend 		if (has_tx_ulp) {
880608c69cSJohn Fastabend 			flags |= MSG_SENDPAGE_NOPOLICY;
890608c69cSJohn Fastabend 			ret = kernel_sendpage_locked(sk,
900608c69cSJohn Fastabend 						     page, off, size, flags);
910608c69cSJohn Fastabend 		} else {
92604326b4SDaniel Borkmann 			ret = do_tcp_sendpages(sk, page, off, size, flags);
930608c69cSJohn Fastabend 		}
940608c69cSJohn Fastabend 
95604326b4SDaniel Borkmann 		if (ret <= 0)
96604326b4SDaniel Borkmann 			return ret;
97604326b4SDaniel Borkmann 		if (apply)
98604326b4SDaniel Borkmann 			apply_bytes -= ret;
99604326b4SDaniel Borkmann 		msg->sg.size -= ret;
100604326b4SDaniel Borkmann 		sge->offset += ret;
101604326b4SDaniel Borkmann 		sge->length -= ret;
102604326b4SDaniel Borkmann 		if (uncharge)
103604326b4SDaniel Borkmann 			sk_mem_uncharge(sk, ret);
104604326b4SDaniel Borkmann 		if (ret != size) {
105604326b4SDaniel Borkmann 			size -= ret;
106604326b4SDaniel Borkmann 			off  += ret;
107604326b4SDaniel Borkmann 			goto retry;
108604326b4SDaniel Borkmann 		}
109604326b4SDaniel Borkmann 		if (!sge->length) {
110604326b4SDaniel Borkmann 			put_page(page);
111604326b4SDaniel Borkmann 			sk_msg_iter_next(msg, start);
112604326b4SDaniel Borkmann 			sg_init_table(sge, 1);
113604326b4SDaniel Borkmann 			if (msg->sg.start == msg->sg.end)
114604326b4SDaniel Borkmann 				break;
115604326b4SDaniel Borkmann 		}
116604326b4SDaniel Borkmann 		if (apply && !apply_bytes)
117604326b4SDaniel Borkmann 			break;
118604326b4SDaniel Borkmann 	}
119604326b4SDaniel Borkmann 
120604326b4SDaniel Borkmann 	return 0;
121604326b4SDaniel Borkmann }
122604326b4SDaniel Borkmann 
123604326b4SDaniel Borkmann static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
124604326b4SDaniel Borkmann 			       u32 apply_bytes, int flags, bool uncharge)
125604326b4SDaniel Borkmann {
126604326b4SDaniel Borkmann 	int ret;
127604326b4SDaniel Borkmann 
128604326b4SDaniel Borkmann 	lock_sock(sk);
129604326b4SDaniel Borkmann 	ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
130604326b4SDaniel Borkmann 	release_sock(sk);
131604326b4SDaniel Borkmann 	return ret;
132604326b4SDaniel Borkmann }
133604326b4SDaniel Borkmann 
134604326b4SDaniel Borkmann int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg,
135604326b4SDaniel Borkmann 			  u32 bytes, int flags)
136604326b4SDaniel Borkmann {
137604326b4SDaniel Borkmann 	bool ingress = sk_msg_to_ingress(msg);
138604326b4SDaniel Borkmann 	struct sk_psock *psock = sk_psock_get(sk);
139604326b4SDaniel Borkmann 	int ret;
140604326b4SDaniel Borkmann 
1412486ab43SWang Yufen 	if (unlikely(!psock))
1422486ab43SWang Yufen 		return -EPIPE;
1432486ab43SWang Yufen 
144604326b4SDaniel Borkmann 	ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes, flags) :
145604326b4SDaniel Borkmann 			tcp_bpf_push_locked(sk, msg, bytes, flags, false);
146604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
147604326b4SDaniel Borkmann 	return ret;
148604326b4SDaniel Borkmann }
149604326b4SDaniel Borkmann EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
150604326b4SDaniel Borkmann 
15188759609SCong Wang #ifdef CONFIG_BPF_SYSCALL
152b6df0078SJakub Kicinski static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
153b6df0078SJakub Kicinski 			     long timeo)
1549f2470fbSCong Wang {
1559f2470fbSCong Wang 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
1569f2470fbSCong Wang 	int ret = 0;
1579f2470fbSCong Wang 
1589f2470fbSCong Wang 	if (sk->sk_shutdown & RCV_SHUTDOWN)
1599f2470fbSCong Wang 		return 1;
1609f2470fbSCong Wang 
1619f2470fbSCong Wang 	if (!timeo)
1629f2470fbSCong Wang 		return ret;
1639f2470fbSCong Wang 
1649f2470fbSCong Wang 	add_wait_queue(sk_sleep(sk), &wait);
1659f2470fbSCong Wang 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1669f2470fbSCong Wang 	ret = sk_wait_event(sk, &timeo,
1679f2470fbSCong Wang 			    !list_empty(&psock->ingress_msg) ||
1689f2470fbSCong Wang 			    !skb_queue_empty(&sk->sk_receive_queue), &wait);
1699f2470fbSCong Wang 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1709f2470fbSCong Wang 	remove_wait_queue(sk_sleep(sk), &wait);
1719f2470fbSCong Wang 	return ret;
1729f2470fbSCong Wang }
1739f2470fbSCong Wang 
174c5d2177aSJohn Fastabend static int tcp_bpf_recvmsg_parser(struct sock *sk,
175c5d2177aSJohn Fastabend 				  struct msghdr *msg,
176c5d2177aSJohn Fastabend 				  size_t len,
177c5d2177aSJohn Fastabend 				  int flags,
178c5d2177aSJohn Fastabend 				  int *addr_len)
179c5d2177aSJohn Fastabend {
180c5d2177aSJohn Fastabend 	struct sk_psock *psock;
181c5d2177aSJohn Fastabend 	int copied;
182c5d2177aSJohn Fastabend 
183c5d2177aSJohn Fastabend 	if (unlikely(flags & MSG_ERRQUEUE))
184c5d2177aSJohn Fastabend 		return inet_recv_error(sk, msg, len, addr_len);
185c5d2177aSJohn Fastabend 
186c5d2177aSJohn Fastabend 	psock = sk_psock_get(sk);
187c5d2177aSJohn Fastabend 	if (unlikely(!psock))
188*ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
189c5d2177aSJohn Fastabend 
190c5d2177aSJohn Fastabend 	lock_sock(sk);
191c5d2177aSJohn Fastabend msg_bytes_ready:
192c5d2177aSJohn Fastabend 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
193c5d2177aSJohn Fastabend 	if (!copied) {
194c5d2177aSJohn Fastabend 		long timeo;
195c5d2177aSJohn Fastabend 		int data;
196c5d2177aSJohn Fastabend 
1975b2c5540SJohn Fastabend 		if (sock_flag(sk, SOCK_DONE))
1985b2c5540SJohn Fastabend 			goto out;
1995b2c5540SJohn Fastabend 
2005b2c5540SJohn Fastabend 		if (sk->sk_err) {
2015b2c5540SJohn Fastabend 			copied = sock_error(sk);
2025b2c5540SJohn Fastabend 			goto out;
2035b2c5540SJohn Fastabend 		}
2045b2c5540SJohn Fastabend 
2055b2c5540SJohn Fastabend 		if (sk->sk_shutdown & RCV_SHUTDOWN)
2065b2c5540SJohn Fastabend 			goto out;
2075b2c5540SJohn Fastabend 
2085b2c5540SJohn Fastabend 		if (sk->sk_state == TCP_CLOSE) {
2095b2c5540SJohn Fastabend 			copied = -ENOTCONN;
2105b2c5540SJohn Fastabend 			goto out;
2115b2c5540SJohn Fastabend 		}
2125b2c5540SJohn Fastabend 
213*ec095263SOliver Hartkopp 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2145b2c5540SJohn Fastabend 		if (!timeo) {
2155b2c5540SJohn Fastabend 			copied = -EAGAIN;
2165b2c5540SJohn Fastabend 			goto out;
2175b2c5540SJohn Fastabend 		}
2185b2c5540SJohn Fastabend 
2195b2c5540SJohn Fastabend 		if (signal_pending(current)) {
2205b2c5540SJohn Fastabend 			copied = sock_intr_errno(timeo);
2215b2c5540SJohn Fastabend 			goto out;
2225b2c5540SJohn Fastabend 		}
2235b2c5540SJohn Fastabend 
224c5d2177aSJohn Fastabend 		data = tcp_msg_wait_data(sk, psock, timeo);
225c5d2177aSJohn Fastabend 		if (data && !sk_psock_queue_empty(psock))
226c5d2177aSJohn Fastabend 			goto msg_bytes_ready;
227c5d2177aSJohn Fastabend 		copied = -EAGAIN;
228c5d2177aSJohn Fastabend 	}
2295b2c5540SJohn Fastabend out:
230c5d2177aSJohn Fastabend 	release_sock(sk);
231c5d2177aSJohn Fastabend 	sk_psock_put(sk, psock);
232c5d2177aSJohn Fastabend 	return copied;
233c5d2177aSJohn Fastabend }
234c5d2177aSJohn Fastabend 
235c0fd336eSYueHaibing static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
236*ec095263SOliver Hartkopp 			   int flags, int *addr_len)
237c0fd336eSYueHaibing {
238c0fd336eSYueHaibing 	struct sk_psock *psock;
239c0fd336eSYueHaibing 	int copied, ret;
240c0fd336eSYueHaibing 
24118f02ad1SXiyu Yang 	if (unlikely(flags & MSG_ERRQUEUE))
24218f02ad1SXiyu Yang 		return inet_recv_error(sk, msg, len, addr_len);
24318f02ad1SXiyu Yang 
244c0fd336eSYueHaibing 	psock = sk_psock_get(sk);
245c0fd336eSYueHaibing 	if (unlikely(!psock))
246*ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
247c0fd336eSYueHaibing 	if (!skb_queue_empty(&sk->sk_receive_queue) &&
24818f02ad1SXiyu Yang 	    sk_psock_queue_empty(psock)) {
24918f02ad1SXiyu Yang 		sk_psock_put(sk, psock);
250*ec095263SOliver Hartkopp 		return tcp_recvmsg(sk, msg, len, flags, addr_len);
25118f02ad1SXiyu Yang 	}
252c0fd336eSYueHaibing 	lock_sock(sk);
253c0fd336eSYueHaibing msg_bytes_ready:
2542bc793e3SCong Wang 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
255c0fd336eSYueHaibing 	if (!copied) {
256c0fd336eSYueHaibing 		long timeo;
257c49661aaSCong Wang 		int data;
258c0fd336eSYueHaibing 
259*ec095263SOliver Hartkopp 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
260b6df0078SJakub Kicinski 		data = tcp_msg_wait_data(sk, psock, timeo);
261c0fd336eSYueHaibing 		if (data) {
262c0fd336eSYueHaibing 			if (!sk_psock_queue_empty(psock))
263c0fd336eSYueHaibing 				goto msg_bytes_ready;
264c0fd336eSYueHaibing 			release_sock(sk);
265c0fd336eSYueHaibing 			sk_psock_put(sk, psock);
266*ec095263SOliver Hartkopp 			return tcp_recvmsg(sk, msg, len, flags, addr_len);
267c0fd336eSYueHaibing 		}
268c0fd336eSYueHaibing 		copied = -EAGAIN;
269c0fd336eSYueHaibing 	}
270c0fd336eSYueHaibing 	ret = copied;
271c0fd336eSYueHaibing 	release_sock(sk);
272c0fd336eSYueHaibing 	sk_psock_put(sk, psock);
273c0fd336eSYueHaibing 	return ret;
274c0fd336eSYueHaibing }
275c0fd336eSYueHaibing 
276604326b4SDaniel Borkmann static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
277604326b4SDaniel Borkmann 				struct sk_msg *msg, int *copied, int flags)
278604326b4SDaniel Borkmann {
279031097d9SJakub Kicinski 	bool cork = false, enospc = sk_msg_full(msg);
280604326b4SDaniel Borkmann 	struct sock *sk_redir;
2817246d8edSJohn Fastabend 	u32 tosend, delta = 0;
282cd9733f5SLiu Jian 	u32 eval = __SK_NONE;
283604326b4SDaniel Borkmann 	int ret;
284604326b4SDaniel Borkmann 
285604326b4SDaniel Borkmann more_data:
2867246d8edSJohn Fastabend 	if (psock->eval == __SK_NONE) {
2877246d8edSJohn Fastabend 		/* Track delta in msg size to add/subtract it on SK_DROP from
2887246d8edSJohn Fastabend 		 * returned to user copied size. This ensures user doesn't
2897246d8edSJohn Fastabend 		 * get a positive return code with msg_cut_data and SK_DROP
2907246d8edSJohn Fastabend 		 * verdict.
2917246d8edSJohn Fastabend 		 */
2927246d8edSJohn Fastabend 		delta = msg->sg.size;
293604326b4SDaniel Borkmann 		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
2947246d8edSJohn Fastabend 		delta -= msg->sg.size;
2957246d8edSJohn Fastabend 	}
296604326b4SDaniel Borkmann 
297604326b4SDaniel Borkmann 	if (msg->cork_bytes &&
298604326b4SDaniel Borkmann 	    msg->cork_bytes > msg->sg.size && !enospc) {
299604326b4SDaniel Borkmann 		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
300604326b4SDaniel Borkmann 		if (!psock->cork) {
301604326b4SDaniel Borkmann 			psock->cork = kzalloc(sizeof(*psock->cork),
302604326b4SDaniel Borkmann 					      GFP_ATOMIC | __GFP_NOWARN);
303604326b4SDaniel Borkmann 			if (!psock->cork)
304604326b4SDaniel Borkmann 				return -ENOMEM;
305604326b4SDaniel Borkmann 		}
306604326b4SDaniel Borkmann 		memcpy(psock->cork, msg, sizeof(*msg));
307604326b4SDaniel Borkmann 		return 0;
308604326b4SDaniel Borkmann 	}
309604326b4SDaniel Borkmann 
310604326b4SDaniel Borkmann 	tosend = msg->sg.size;
311604326b4SDaniel Borkmann 	if (psock->apply_bytes && psock->apply_bytes < tosend)
312604326b4SDaniel Borkmann 		tosend = psock->apply_bytes;
313604326b4SDaniel Borkmann 
314604326b4SDaniel Borkmann 	switch (psock->eval) {
315604326b4SDaniel Borkmann 	case __SK_PASS:
316604326b4SDaniel Borkmann 		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
317604326b4SDaniel Borkmann 		if (unlikely(ret)) {
318604326b4SDaniel Borkmann 			*copied -= sk_msg_free(sk, msg);
319604326b4SDaniel Borkmann 			break;
320604326b4SDaniel Borkmann 		}
321604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
322604326b4SDaniel Borkmann 		break;
323604326b4SDaniel Borkmann 	case __SK_REDIRECT:
324604326b4SDaniel Borkmann 		sk_redir = psock->sk_redir;
325604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
326cd9733f5SLiu Jian 		if (!psock->apply_bytes) {
327cd9733f5SLiu Jian 			/* Clean up before releasing the sock lock. */
328cd9733f5SLiu Jian 			eval = psock->eval;
329cd9733f5SLiu Jian 			psock->eval = __SK_NONE;
330cd9733f5SLiu Jian 			psock->sk_redir = NULL;
331cd9733f5SLiu Jian 		}
332604326b4SDaniel Borkmann 		if (psock->cork) {
333604326b4SDaniel Borkmann 			cork = true;
334604326b4SDaniel Borkmann 			psock->cork = NULL;
335604326b4SDaniel Borkmann 		}
33684472b43SWang Yufen 		sk_msg_return(sk, msg, msg->sg.size);
337604326b4SDaniel Borkmann 		release_sock(sk);
338cd9733f5SLiu Jian 
339604326b4SDaniel Borkmann 		ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
340cd9733f5SLiu Jian 
341cd9733f5SLiu Jian 		if (eval == __SK_REDIRECT)
342cd9733f5SLiu Jian 			sock_put(sk_redir);
343cd9733f5SLiu Jian 
344604326b4SDaniel Borkmann 		lock_sock(sk);
345604326b4SDaniel Borkmann 		if (unlikely(ret < 0)) {
346604326b4SDaniel Borkmann 			int free = sk_msg_free_nocharge(sk, msg);
347604326b4SDaniel Borkmann 
348604326b4SDaniel Borkmann 			if (!cork)
349604326b4SDaniel Borkmann 				*copied -= free;
350604326b4SDaniel Borkmann 		}
351604326b4SDaniel Borkmann 		if (cork) {
352604326b4SDaniel Borkmann 			sk_msg_free(sk, msg);
353604326b4SDaniel Borkmann 			kfree(msg);
354604326b4SDaniel Borkmann 			msg = NULL;
355604326b4SDaniel Borkmann 			ret = 0;
356604326b4SDaniel Borkmann 		}
357604326b4SDaniel Borkmann 		break;
358604326b4SDaniel Borkmann 	case __SK_DROP:
359604326b4SDaniel Borkmann 	default:
360604326b4SDaniel Borkmann 		sk_msg_free_partial(sk, msg, tosend);
361604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
3627246d8edSJohn Fastabend 		*copied -= (tosend + delta);
363604326b4SDaniel Borkmann 		return -EACCES;
364604326b4SDaniel Borkmann 	}
365604326b4SDaniel Borkmann 
366604326b4SDaniel Borkmann 	if (likely(!ret)) {
367604326b4SDaniel Borkmann 		if (!psock->apply_bytes) {
368604326b4SDaniel Borkmann 			psock->eval =  __SK_NONE;
369604326b4SDaniel Borkmann 			if (psock->sk_redir) {
370604326b4SDaniel Borkmann 				sock_put(psock->sk_redir);
371604326b4SDaniel Borkmann 				psock->sk_redir = NULL;
372604326b4SDaniel Borkmann 			}
373604326b4SDaniel Borkmann 		}
374604326b4SDaniel Borkmann 		if (msg &&
375604326b4SDaniel Borkmann 		    msg->sg.data[msg->sg.start].page_link &&
37684472b43SWang Yufen 		    msg->sg.data[msg->sg.start].length) {
37784472b43SWang Yufen 			if (eval == __SK_REDIRECT)
37884472b43SWang Yufen 				sk_mem_charge(sk, msg->sg.size);
379604326b4SDaniel Borkmann 			goto more_data;
380604326b4SDaniel Borkmann 		}
38184472b43SWang Yufen 	}
382604326b4SDaniel Borkmann 	return ret;
383604326b4SDaniel Borkmann }
384604326b4SDaniel Borkmann 
385604326b4SDaniel Borkmann static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
386604326b4SDaniel Borkmann {
387604326b4SDaniel Borkmann 	struct sk_msg tmp, *msg_tx = NULL;
388604326b4SDaniel Borkmann 	int copied = 0, err = 0;
389604326b4SDaniel Borkmann 	struct sk_psock *psock;
390604326b4SDaniel Borkmann 	long timeo;
39141477662SJakub Kicinski 	int flags;
39241477662SJakub Kicinski 
39341477662SJakub Kicinski 	/* Don't let internal do_tcp_sendpages() flags through */
39441477662SJakub Kicinski 	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
39541477662SJakub Kicinski 	flags |= MSG_NO_SHARED_FRAGS;
396604326b4SDaniel Borkmann 
397604326b4SDaniel Borkmann 	psock = sk_psock_get(sk);
398604326b4SDaniel Borkmann 	if (unlikely(!psock))
399604326b4SDaniel Borkmann 		return tcp_sendmsg(sk, msg, size);
400604326b4SDaniel Borkmann 
401604326b4SDaniel Borkmann 	lock_sock(sk);
402604326b4SDaniel Borkmann 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
403604326b4SDaniel Borkmann 	while (msg_data_left(msg)) {
404604326b4SDaniel Borkmann 		bool enospc = false;
405604326b4SDaniel Borkmann 		u32 copy, osize;
406604326b4SDaniel Borkmann 
407604326b4SDaniel Borkmann 		if (sk->sk_err) {
408604326b4SDaniel Borkmann 			err = -sk->sk_err;
409604326b4SDaniel Borkmann 			goto out_err;
410604326b4SDaniel Borkmann 		}
411604326b4SDaniel Borkmann 
412604326b4SDaniel Borkmann 		copy = msg_data_left(msg);
413604326b4SDaniel Borkmann 		if (!sk_stream_memory_free(sk))
414604326b4SDaniel Borkmann 			goto wait_for_sndbuf;
415604326b4SDaniel Borkmann 		if (psock->cork) {
416604326b4SDaniel Borkmann 			msg_tx = psock->cork;
417604326b4SDaniel Borkmann 		} else {
418604326b4SDaniel Borkmann 			msg_tx = &tmp;
419604326b4SDaniel Borkmann 			sk_msg_init(msg_tx);
420604326b4SDaniel Borkmann 		}
421604326b4SDaniel Borkmann 
422604326b4SDaniel Borkmann 		osize = msg_tx->sg.size;
423604326b4SDaniel Borkmann 		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
424604326b4SDaniel Borkmann 		if (err) {
425604326b4SDaniel Borkmann 			if (err != -ENOSPC)
426604326b4SDaniel Borkmann 				goto wait_for_memory;
427604326b4SDaniel Borkmann 			enospc = true;
428604326b4SDaniel Borkmann 			copy = msg_tx->sg.size - osize;
429604326b4SDaniel Borkmann 		}
430604326b4SDaniel Borkmann 
431604326b4SDaniel Borkmann 		err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
432604326b4SDaniel Borkmann 					       copy);
433604326b4SDaniel Borkmann 		if (err < 0) {
434604326b4SDaniel Borkmann 			sk_msg_trim(sk, msg_tx, osize);
435604326b4SDaniel Borkmann 			goto out_err;
436604326b4SDaniel Borkmann 		}
437604326b4SDaniel Borkmann 
438604326b4SDaniel Borkmann 		copied += copy;
439604326b4SDaniel Borkmann 		if (psock->cork_bytes) {
440604326b4SDaniel Borkmann 			if (size > psock->cork_bytes)
441604326b4SDaniel Borkmann 				psock->cork_bytes = 0;
442604326b4SDaniel Borkmann 			else
443604326b4SDaniel Borkmann 				psock->cork_bytes -= size;
444604326b4SDaniel Borkmann 			if (psock->cork_bytes && !enospc)
445604326b4SDaniel Borkmann 				goto out_err;
446604326b4SDaniel Borkmann 			/* All cork bytes are accounted, rerun the prog. */
447604326b4SDaniel Borkmann 			psock->eval = __SK_NONE;
448604326b4SDaniel Borkmann 			psock->cork_bytes = 0;
449604326b4SDaniel Borkmann 		}
450604326b4SDaniel Borkmann 
451604326b4SDaniel Borkmann 		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
452604326b4SDaniel Borkmann 		if (unlikely(err < 0))
453604326b4SDaniel Borkmann 			goto out_err;
454604326b4SDaniel Borkmann 		continue;
455604326b4SDaniel Borkmann wait_for_sndbuf:
456604326b4SDaniel Borkmann 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
457604326b4SDaniel Borkmann wait_for_memory:
458604326b4SDaniel Borkmann 		err = sk_stream_wait_memory(sk, &timeo);
459604326b4SDaniel Borkmann 		if (err) {
460604326b4SDaniel Borkmann 			if (msg_tx && msg_tx != psock->cork)
461604326b4SDaniel Borkmann 				sk_msg_free(sk, msg_tx);
462604326b4SDaniel Borkmann 			goto out_err;
463604326b4SDaniel Borkmann 		}
464604326b4SDaniel Borkmann 	}
465604326b4SDaniel Borkmann out_err:
466604326b4SDaniel Borkmann 	if (err < 0)
467604326b4SDaniel Borkmann 		err = sk_stream_error(sk, msg->msg_flags, err);
468604326b4SDaniel Borkmann 	release_sock(sk);
469604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
470604326b4SDaniel Borkmann 	return copied ? copied : err;
471604326b4SDaniel Borkmann }
472604326b4SDaniel Borkmann 
473604326b4SDaniel Borkmann static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
474604326b4SDaniel Borkmann 			    size_t size, int flags)
475604326b4SDaniel Borkmann {
476604326b4SDaniel Borkmann 	struct sk_msg tmp, *msg = NULL;
477604326b4SDaniel Borkmann 	int err = 0, copied = 0;
478604326b4SDaniel Borkmann 	struct sk_psock *psock;
479604326b4SDaniel Borkmann 	bool enospc = false;
480604326b4SDaniel Borkmann 
481604326b4SDaniel Borkmann 	psock = sk_psock_get(sk);
482604326b4SDaniel Borkmann 	if (unlikely(!psock))
483604326b4SDaniel Borkmann 		return tcp_sendpage(sk, page, offset, size, flags);
484604326b4SDaniel Borkmann 
485604326b4SDaniel Borkmann 	lock_sock(sk);
486604326b4SDaniel Borkmann 	if (psock->cork) {
487604326b4SDaniel Borkmann 		msg = psock->cork;
488604326b4SDaniel Borkmann 	} else {
489604326b4SDaniel Borkmann 		msg = &tmp;
490604326b4SDaniel Borkmann 		sk_msg_init(msg);
491604326b4SDaniel Borkmann 	}
492604326b4SDaniel Borkmann 
493604326b4SDaniel Borkmann 	/* Catch case where ring is full and sendpage is stalled. */
494604326b4SDaniel Borkmann 	if (unlikely(sk_msg_full(msg)))
495604326b4SDaniel Borkmann 		goto out_err;
496604326b4SDaniel Borkmann 
497604326b4SDaniel Borkmann 	sk_msg_page_add(msg, page, size, offset);
498604326b4SDaniel Borkmann 	sk_mem_charge(sk, size);
499604326b4SDaniel Borkmann 	copied = size;
500604326b4SDaniel Borkmann 	if (sk_msg_full(msg))
501604326b4SDaniel Borkmann 		enospc = true;
502604326b4SDaniel Borkmann 	if (psock->cork_bytes) {
503604326b4SDaniel Borkmann 		if (size > psock->cork_bytes)
504604326b4SDaniel Borkmann 			psock->cork_bytes = 0;
505604326b4SDaniel Borkmann 		else
506604326b4SDaniel Borkmann 			psock->cork_bytes -= size;
507604326b4SDaniel Borkmann 		if (psock->cork_bytes && !enospc)
508604326b4SDaniel Borkmann 			goto out_err;
509604326b4SDaniel Borkmann 		/* All cork bytes are accounted, rerun the prog. */
510604326b4SDaniel Borkmann 		psock->eval = __SK_NONE;
511604326b4SDaniel Borkmann 		psock->cork_bytes = 0;
512604326b4SDaniel Borkmann 	}
513604326b4SDaniel Borkmann 
514604326b4SDaniel Borkmann 	err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
515604326b4SDaniel Borkmann out_err:
516604326b4SDaniel Borkmann 	release_sock(sk);
517604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
518604326b4SDaniel Borkmann 	return copied ? copied : err;
519604326b4SDaniel Borkmann }
520604326b4SDaniel Borkmann 
521604326b4SDaniel Borkmann enum {
522604326b4SDaniel Borkmann 	TCP_BPF_IPV4,
523604326b4SDaniel Borkmann 	TCP_BPF_IPV6,
524604326b4SDaniel Borkmann 	TCP_BPF_NUM_PROTS,
525604326b4SDaniel Borkmann };
526604326b4SDaniel Borkmann 
527604326b4SDaniel Borkmann enum {
528604326b4SDaniel Borkmann 	TCP_BPF_BASE,
529604326b4SDaniel Borkmann 	TCP_BPF_TX,
530c5d2177aSJohn Fastabend 	TCP_BPF_RX,
531c5d2177aSJohn Fastabend 	TCP_BPF_TXRX,
532604326b4SDaniel Borkmann 	TCP_BPF_NUM_CFGS,
533604326b4SDaniel Borkmann };
534604326b4SDaniel Borkmann 
535604326b4SDaniel Borkmann static struct proto *tcpv6_prot_saved __read_mostly;
536604326b4SDaniel Borkmann static DEFINE_SPINLOCK(tcpv6_prot_lock);
537604326b4SDaniel Borkmann static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
538604326b4SDaniel Borkmann 
539604326b4SDaniel Borkmann static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
540604326b4SDaniel Borkmann 				   struct proto *base)
541604326b4SDaniel Borkmann {
542604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE]			= *base;
543f747632bSLorenz Bauer 	prot[TCP_BPF_BASE].close		= sock_map_close;
544604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
545fb4e0a5eSCong Wang 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
546604326b4SDaniel Borkmann 
547604326b4SDaniel Borkmann 	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
548604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
549604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendpage		= tcp_bpf_sendpage;
550c5d2177aSJohn Fastabend 
551c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
552c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
553c5d2177aSJohn Fastabend 
554c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
555c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
556604326b4SDaniel Borkmann }
557604326b4SDaniel Borkmann 
5587b219da4SLorenz Bauer static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
559604326b4SDaniel Borkmann {
5607b219da4SLorenz Bauer 	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
561604326b4SDaniel Borkmann 		spin_lock_bh(&tcpv6_prot_lock);
562604326b4SDaniel Borkmann 		if (likely(ops != tcpv6_prot_saved)) {
563604326b4SDaniel Borkmann 			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
564604326b4SDaniel Borkmann 			smp_store_release(&tcpv6_prot_saved, ops);
565604326b4SDaniel Borkmann 		}
566604326b4SDaniel Borkmann 		spin_unlock_bh(&tcpv6_prot_lock);
567604326b4SDaniel Borkmann 	}
568604326b4SDaniel Borkmann }
569604326b4SDaniel Borkmann 
570604326b4SDaniel Borkmann static int __init tcp_bpf_v4_build_proto(void)
571604326b4SDaniel Borkmann {
572604326b4SDaniel Borkmann 	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
573604326b4SDaniel Borkmann 	return 0;
574604326b4SDaniel Borkmann }
575228a4a7bSJohn Fastabend late_initcall(tcp_bpf_v4_build_proto);
576604326b4SDaniel Borkmann 
577604326b4SDaniel Borkmann static int tcp_bpf_assert_proto_ops(struct proto *ops)
578604326b4SDaniel Borkmann {
579604326b4SDaniel Borkmann 	/* In order to avoid retpoline, we make assumptions when we call
580604326b4SDaniel Borkmann 	 * into ops if e.g. a psock is not present. Make sure they are
581604326b4SDaniel Borkmann 	 * indeed valid assumptions.
582604326b4SDaniel Borkmann 	 */
583604326b4SDaniel Borkmann 	return ops->recvmsg  == tcp_recvmsg &&
584604326b4SDaniel Borkmann 	       ops->sendmsg  == tcp_sendmsg &&
585604326b4SDaniel Borkmann 	       ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
586604326b4SDaniel Borkmann }
587604326b4SDaniel Borkmann 
58851e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
589604326b4SDaniel Borkmann {
590d19da360SLorenz Bauer 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
591d19da360SLorenz Bauer 	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
592604326b4SDaniel Borkmann 
593c5d2177aSJohn Fastabend 	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
594c5d2177aSJohn Fastabend 		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
595c5d2177aSJohn Fastabend 	}
596c5d2177aSJohn Fastabend 
5978a59f9d1SCong Wang 	if (restore) {
5988a59f9d1SCong Wang 		if (inet_csk_has_ulp(sk)) {
5998859a44eSJakub Kicinski 			/* TLS does not have an unhash proto in SW cases,
6008859a44eSJakub Kicinski 			 * but we need to ensure we stop using the sock_map
6018859a44eSJakub Kicinski 			 * unhash routine because the associated psock is being
6028859a44eSJakub Kicinski 			 * removed. So use the original unhash handler.
6038859a44eSJakub Kicinski 			 */
6048859a44eSJakub Kicinski 			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
6058a59f9d1SCong Wang 			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
6068a59f9d1SCong Wang 		} else {
6078a59f9d1SCong Wang 			sk->sk_write_space = psock->saved_write_space;
6088a59f9d1SCong Wang 			/* Pairs with lockless read in sk_clone_lock() */
6098a59f9d1SCong Wang 			WRITE_ONCE(sk->sk_prot, psock->sk_proto);
6108a59f9d1SCong Wang 		}
6118a59f9d1SCong Wang 		return 0;
6128a59f9d1SCong Wang 	}
6138a59f9d1SCong Wang 
6148a59f9d1SCong Wang 	if (inet_csk_has_ulp(sk))
6158a59f9d1SCong Wang 		return -EINVAL;
6168a59f9d1SCong Wang 
6177b219da4SLorenz Bauer 	if (sk->sk_family == AF_INET6) {
6187b219da4SLorenz Bauer 		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
6198a59f9d1SCong Wang 			return -EINVAL;
620d19da360SLorenz Bauer 
6217b219da4SLorenz Bauer 		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
622d19da360SLorenz Bauer 	}
623d19da360SLorenz Bauer 
6248a59f9d1SCong Wang 	/* Pairs with lockless read in sk_clone_lock() */
6258a59f9d1SCong Wang 	WRITE_ONCE(sk->sk_prot, &tcp_bpf_prots[family][config]);
6268a59f9d1SCong Wang 	return 0;
627604326b4SDaniel Borkmann }
6288a59f9d1SCong Wang EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
629604326b4SDaniel Borkmann 
630e8025155SJakub Sitnicki /* If a child got cloned from a listening socket that had tcp_bpf
631e8025155SJakub Sitnicki  * protocol callbacks installed, we need to restore the callbacks to
632e8025155SJakub Sitnicki  * the default ones because the child does not inherit the psock state
633e8025155SJakub Sitnicki  * that tcp_bpf callbacks expect.
634e8025155SJakub Sitnicki  */
635e8025155SJakub Sitnicki void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
636e8025155SJakub Sitnicki {
637e8025155SJakub Sitnicki 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
638e8025155SJakub Sitnicki 	struct proto *prot = newsk->sk_prot;
639e8025155SJakub Sitnicki 
640e8025155SJakub Sitnicki 	if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
641e8025155SJakub Sitnicki 		newsk->sk_prot = sk->sk_prot_creator;
642e8025155SJakub Sitnicki }
64388759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
644