xref: /linux/net/ipv4/tcp_bpf.c (revision 7a9841ca025275b5b0edfb0b618934abb6ceec15)
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))
188ec095263SOliver 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 
213ec095263SOliver 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,
236ec095263SOliver 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))
246ec095263SOliver 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);
250ec095263SOliver 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 
259ec095263SOliver 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);
266ec095263SOliver 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;
2818ec95b94SWang Yufen 	u32 tosend, origsize, sent, delta = 0;
282*7a9841caSPengcheng Yang 	u32 eval;
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;
313*7a9841caSPengcheng Yang 	eval = __SK_NONE;
314604326b4SDaniel Borkmann 
315604326b4SDaniel Borkmann 	switch (psock->eval) {
316604326b4SDaniel Borkmann 	case __SK_PASS:
317604326b4SDaniel Borkmann 		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
318604326b4SDaniel Borkmann 		if (unlikely(ret)) {
319604326b4SDaniel Borkmann 			*copied -= sk_msg_free(sk, msg);
320604326b4SDaniel Borkmann 			break;
321604326b4SDaniel Borkmann 		}
322604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
323604326b4SDaniel Borkmann 		break;
324604326b4SDaniel Borkmann 	case __SK_REDIRECT:
325604326b4SDaniel Borkmann 		sk_redir = psock->sk_redir;
326604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
327cd9733f5SLiu Jian 		if (!psock->apply_bytes) {
328cd9733f5SLiu Jian 			/* Clean up before releasing the sock lock. */
329cd9733f5SLiu Jian 			eval = psock->eval;
330cd9733f5SLiu Jian 			psock->eval = __SK_NONE;
331cd9733f5SLiu Jian 			psock->sk_redir = NULL;
332cd9733f5SLiu Jian 		}
333604326b4SDaniel Borkmann 		if (psock->cork) {
334604326b4SDaniel Borkmann 			cork = true;
335604326b4SDaniel Borkmann 			psock->cork = NULL;
336604326b4SDaniel Borkmann 		}
3378ec95b94SWang Yufen 		sk_msg_return(sk, msg, tosend);
338604326b4SDaniel Borkmann 		release_sock(sk);
339cd9733f5SLiu Jian 
3408ec95b94SWang Yufen 		origsize = msg->sg.size;
341604326b4SDaniel Borkmann 		ret = tcp_bpf_sendmsg_redir(sk_redir, msg, tosend, flags);
3428ec95b94SWang Yufen 		sent = origsize - msg->sg.size;
343cd9733f5SLiu Jian 
344cd9733f5SLiu Jian 		if (eval == __SK_REDIRECT)
345cd9733f5SLiu Jian 			sock_put(sk_redir);
346cd9733f5SLiu Jian 
347604326b4SDaniel Borkmann 		lock_sock(sk);
348604326b4SDaniel Borkmann 		if (unlikely(ret < 0)) {
349604326b4SDaniel Borkmann 			int free = sk_msg_free_nocharge(sk, msg);
350604326b4SDaniel Borkmann 
351604326b4SDaniel Borkmann 			if (!cork)
352604326b4SDaniel Borkmann 				*copied -= free;
353604326b4SDaniel Borkmann 		}
354604326b4SDaniel Borkmann 		if (cork) {
355604326b4SDaniel Borkmann 			sk_msg_free(sk, msg);
356604326b4SDaniel Borkmann 			kfree(msg);
357604326b4SDaniel Borkmann 			msg = NULL;
358604326b4SDaniel Borkmann 			ret = 0;
359604326b4SDaniel Borkmann 		}
360604326b4SDaniel Borkmann 		break;
361604326b4SDaniel Borkmann 	case __SK_DROP:
362604326b4SDaniel Borkmann 	default:
363604326b4SDaniel Borkmann 		sk_msg_free_partial(sk, msg, tosend);
364604326b4SDaniel Borkmann 		sk_msg_apply_bytes(psock, tosend);
3657246d8edSJohn Fastabend 		*copied -= (tosend + delta);
366604326b4SDaniel Borkmann 		return -EACCES;
367604326b4SDaniel Borkmann 	}
368604326b4SDaniel Borkmann 
369604326b4SDaniel Borkmann 	if (likely(!ret)) {
370604326b4SDaniel Borkmann 		if (!psock->apply_bytes) {
371604326b4SDaniel Borkmann 			psock->eval =  __SK_NONE;
372604326b4SDaniel Borkmann 			if (psock->sk_redir) {
373604326b4SDaniel Borkmann 				sock_put(psock->sk_redir);
374604326b4SDaniel Borkmann 				psock->sk_redir = NULL;
375604326b4SDaniel Borkmann 			}
376604326b4SDaniel Borkmann 		}
377604326b4SDaniel Borkmann 		if (msg &&
378604326b4SDaniel Borkmann 		    msg->sg.data[msg->sg.start].page_link &&
37984472b43SWang Yufen 		    msg->sg.data[msg->sg.start].length) {
38084472b43SWang Yufen 			if (eval == __SK_REDIRECT)
3818ec95b94SWang Yufen 				sk_mem_charge(sk, tosend - sent);
382604326b4SDaniel Borkmann 			goto more_data;
383604326b4SDaniel Borkmann 		}
38484472b43SWang Yufen 	}
385604326b4SDaniel Borkmann 	return ret;
386604326b4SDaniel Borkmann }
387604326b4SDaniel Borkmann 
388604326b4SDaniel Borkmann static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
389604326b4SDaniel Borkmann {
390604326b4SDaniel Borkmann 	struct sk_msg tmp, *msg_tx = NULL;
391604326b4SDaniel Borkmann 	int copied = 0, err = 0;
392604326b4SDaniel Borkmann 	struct sk_psock *psock;
393604326b4SDaniel Borkmann 	long timeo;
39441477662SJakub Kicinski 	int flags;
39541477662SJakub Kicinski 
39641477662SJakub Kicinski 	/* Don't let internal do_tcp_sendpages() flags through */
39741477662SJakub Kicinski 	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
39841477662SJakub Kicinski 	flags |= MSG_NO_SHARED_FRAGS;
399604326b4SDaniel Borkmann 
400604326b4SDaniel Borkmann 	psock = sk_psock_get(sk);
401604326b4SDaniel Borkmann 	if (unlikely(!psock))
402604326b4SDaniel Borkmann 		return tcp_sendmsg(sk, msg, size);
403604326b4SDaniel Borkmann 
404604326b4SDaniel Borkmann 	lock_sock(sk);
405604326b4SDaniel Borkmann 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
406604326b4SDaniel Borkmann 	while (msg_data_left(msg)) {
407604326b4SDaniel Borkmann 		bool enospc = false;
408604326b4SDaniel Borkmann 		u32 copy, osize;
409604326b4SDaniel Borkmann 
410604326b4SDaniel Borkmann 		if (sk->sk_err) {
411604326b4SDaniel Borkmann 			err = -sk->sk_err;
412604326b4SDaniel Borkmann 			goto out_err;
413604326b4SDaniel Borkmann 		}
414604326b4SDaniel Borkmann 
415604326b4SDaniel Borkmann 		copy = msg_data_left(msg);
416604326b4SDaniel Borkmann 		if (!sk_stream_memory_free(sk))
417604326b4SDaniel Borkmann 			goto wait_for_sndbuf;
418604326b4SDaniel Borkmann 		if (psock->cork) {
419604326b4SDaniel Borkmann 			msg_tx = psock->cork;
420604326b4SDaniel Borkmann 		} else {
421604326b4SDaniel Borkmann 			msg_tx = &tmp;
422604326b4SDaniel Borkmann 			sk_msg_init(msg_tx);
423604326b4SDaniel Borkmann 		}
424604326b4SDaniel Borkmann 
425604326b4SDaniel Borkmann 		osize = msg_tx->sg.size;
426604326b4SDaniel Borkmann 		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
427604326b4SDaniel Borkmann 		if (err) {
428604326b4SDaniel Borkmann 			if (err != -ENOSPC)
429604326b4SDaniel Borkmann 				goto wait_for_memory;
430604326b4SDaniel Borkmann 			enospc = true;
431604326b4SDaniel Borkmann 			copy = msg_tx->sg.size - osize;
432604326b4SDaniel Borkmann 		}
433604326b4SDaniel Borkmann 
434604326b4SDaniel Borkmann 		err = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
435604326b4SDaniel Borkmann 					       copy);
436604326b4SDaniel Borkmann 		if (err < 0) {
437604326b4SDaniel Borkmann 			sk_msg_trim(sk, msg_tx, osize);
438604326b4SDaniel Borkmann 			goto out_err;
439604326b4SDaniel Borkmann 		}
440604326b4SDaniel Borkmann 
441604326b4SDaniel Borkmann 		copied += copy;
442604326b4SDaniel Borkmann 		if (psock->cork_bytes) {
443604326b4SDaniel Borkmann 			if (size > psock->cork_bytes)
444604326b4SDaniel Borkmann 				psock->cork_bytes = 0;
445604326b4SDaniel Borkmann 			else
446604326b4SDaniel Borkmann 				psock->cork_bytes -= size;
447604326b4SDaniel Borkmann 			if (psock->cork_bytes && !enospc)
448604326b4SDaniel Borkmann 				goto out_err;
449604326b4SDaniel Borkmann 			/* All cork bytes are accounted, rerun the prog. */
450604326b4SDaniel Borkmann 			psock->eval = __SK_NONE;
451604326b4SDaniel Borkmann 			psock->cork_bytes = 0;
452604326b4SDaniel Borkmann 		}
453604326b4SDaniel Borkmann 
454604326b4SDaniel Borkmann 		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
455604326b4SDaniel Borkmann 		if (unlikely(err < 0))
456604326b4SDaniel Borkmann 			goto out_err;
457604326b4SDaniel Borkmann 		continue;
458604326b4SDaniel Borkmann wait_for_sndbuf:
459604326b4SDaniel Borkmann 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
460604326b4SDaniel Borkmann wait_for_memory:
461604326b4SDaniel Borkmann 		err = sk_stream_wait_memory(sk, &timeo);
462604326b4SDaniel Borkmann 		if (err) {
463604326b4SDaniel Borkmann 			if (msg_tx && msg_tx != psock->cork)
464604326b4SDaniel Borkmann 				sk_msg_free(sk, msg_tx);
465604326b4SDaniel Borkmann 			goto out_err;
466604326b4SDaniel Borkmann 		}
467604326b4SDaniel Borkmann 	}
468604326b4SDaniel Borkmann out_err:
469604326b4SDaniel Borkmann 	if (err < 0)
470604326b4SDaniel Borkmann 		err = sk_stream_error(sk, msg->msg_flags, err);
471604326b4SDaniel Borkmann 	release_sock(sk);
472604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
473604326b4SDaniel Borkmann 	return copied ? copied : err;
474604326b4SDaniel Borkmann }
475604326b4SDaniel Borkmann 
476604326b4SDaniel Borkmann static int tcp_bpf_sendpage(struct sock *sk, struct page *page, int offset,
477604326b4SDaniel Borkmann 			    size_t size, int flags)
478604326b4SDaniel Borkmann {
479604326b4SDaniel Borkmann 	struct sk_msg tmp, *msg = NULL;
480604326b4SDaniel Borkmann 	int err = 0, copied = 0;
481604326b4SDaniel Borkmann 	struct sk_psock *psock;
482604326b4SDaniel Borkmann 	bool enospc = false;
483604326b4SDaniel Borkmann 
484604326b4SDaniel Borkmann 	psock = sk_psock_get(sk);
485604326b4SDaniel Borkmann 	if (unlikely(!psock))
486604326b4SDaniel Borkmann 		return tcp_sendpage(sk, page, offset, size, flags);
487604326b4SDaniel Borkmann 
488604326b4SDaniel Borkmann 	lock_sock(sk);
489604326b4SDaniel Borkmann 	if (psock->cork) {
490604326b4SDaniel Borkmann 		msg = psock->cork;
491604326b4SDaniel Borkmann 	} else {
492604326b4SDaniel Borkmann 		msg = &tmp;
493604326b4SDaniel Borkmann 		sk_msg_init(msg);
494604326b4SDaniel Borkmann 	}
495604326b4SDaniel Borkmann 
496604326b4SDaniel Borkmann 	/* Catch case where ring is full and sendpage is stalled. */
497604326b4SDaniel Borkmann 	if (unlikely(sk_msg_full(msg)))
498604326b4SDaniel Borkmann 		goto out_err;
499604326b4SDaniel Borkmann 
500604326b4SDaniel Borkmann 	sk_msg_page_add(msg, page, size, offset);
501604326b4SDaniel Borkmann 	sk_mem_charge(sk, size);
502604326b4SDaniel Borkmann 	copied = size;
503604326b4SDaniel Borkmann 	if (sk_msg_full(msg))
504604326b4SDaniel Borkmann 		enospc = true;
505604326b4SDaniel Borkmann 	if (psock->cork_bytes) {
506604326b4SDaniel Borkmann 		if (size > psock->cork_bytes)
507604326b4SDaniel Borkmann 			psock->cork_bytes = 0;
508604326b4SDaniel Borkmann 		else
509604326b4SDaniel Borkmann 			psock->cork_bytes -= size;
510604326b4SDaniel Borkmann 		if (psock->cork_bytes && !enospc)
511604326b4SDaniel Borkmann 			goto out_err;
512604326b4SDaniel Borkmann 		/* All cork bytes are accounted, rerun the prog. */
513604326b4SDaniel Borkmann 		psock->eval = __SK_NONE;
514604326b4SDaniel Borkmann 		psock->cork_bytes = 0;
515604326b4SDaniel Borkmann 	}
516604326b4SDaniel Borkmann 
517604326b4SDaniel Borkmann 	err = tcp_bpf_send_verdict(sk, psock, msg, &copied, flags);
518604326b4SDaniel Borkmann out_err:
519604326b4SDaniel Borkmann 	release_sock(sk);
520604326b4SDaniel Borkmann 	sk_psock_put(sk, psock);
521604326b4SDaniel Borkmann 	return copied ? copied : err;
522604326b4SDaniel Borkmann }
523604326b4SDaniel Borkmann 
524604326b4SDaniel Borkmann enum {
525604326b4SDaniel Borkmann 	TCP_BPF_IPV4,
526604326b4SDaniel Borkmann 	TCP_BPF_IPV6,
527604326b4SDaniel Borkmann 	TCP_BPF_NUM_PROTS,
528604326b4SDaniel Borkmann };
529604326b4SDaniel Borkmann 
530604326b4SDaniel Borkmann enum {
531604326b4SDaniel Borkmann 	TCP_BPF_BASE,
532604326b4SDaniel Borkmann 	TCP_BPF_TX,
533c5d2177aSJohn Fastabend 	TCP_BPF_RX,
534c5d2177aSJohn Fastabend 	TCP_BPF_TXRX,
535604326b4SDaniel Borkmann 	TCP_BPF_NUM_CFGS,
536604326b4SDaniel Borkmann };
537604326b4SDaniel Borkmann 
538604326b4SDaniel Borkmann static struct proto *tcpv6_prot_saved __read_mostly;
539604326b4SDaniel Borkmann static DEFINE_SPINLOCK(tcpv6_prot_lock);
540604326b4SDaniel Borkmann static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
541604326b4SDaniel Borkmann 
542604326b4SDaniel Borkmann static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
543604326b4SDaniel Borkmann 				   struct proto *base)
544604326b4SDaniel Borkmann {
545604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE]			= *base;
546d8616ee2SWang Yufen 	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
547f747632bSLorenz Bauer 	prot[TCP_BPF_BASE].close		= sock_map_close;
548604326b4SDaniel Borkmann 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
549fb4e0a5eSCong Wang 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
550604326b4SDaniel Borkmann 
551604326b4SDaniel Borkmann 	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
552604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
553604326b4SDaniel Borkmann 	prot[TCP_BPF_TX].sendpage		= tcp_bpf_sendpage;
554c5d2177aSJohn Fastabend 
555c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
556c5d2177aSJohn Fastabend 	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
557c5d2177aSJohn Fastabend 
558c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
559c5d2177aSJohn Fastabend 	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
560604326b4SDaniel Borkmann }
561604326b4SDaniel Borkmann 
5627b219da4SLorenz Bauer static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
563604326b4SDaniel Borkmann {
5647b219da4SLorenz Bauer 	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
565604326b4SDaniel Borkmann 		spin_lock_bh(&tcpv6_prot_lock);
566604326b4SDaniel Borkmann 		if (likely(ops != tcpv6_prot_saved)) {
567604326b4SDaniel Borkmann 			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
568604326b4SDaniel Borkmann 			smp_store_release(&tcpv6_prot_saved, ops);
569604326b4SDaniel Borkmann 		}
570604326b4SDaniel Borkmann 		spin_unlock_bh(&tcpv6_prot_lock);
571604326b4SDaniel Borkmann 	}
572604326b4SDaniel Borkmann }
573604326b4SDaniel Borkmann 
574604326b4SDaniel Borkmann static int __init tcp_bpf_v4_build_proto(void)
575604326b4SDaniel Borkmann {
576604326b4SDaniel Borkmann 	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
577604326b4SDaniel Borkmann 	return 0;
578604326b4SDaniel Borkmann }
579228a4a7bSJohn Fastabend late_initcall(tcp_bpf_v4_build_proto);
580604326b4SDaniel Borkmann 
581604326b4SDaniel Borkmann static int tcp_bpf_assert_proto_ops(struct proto *ops)
582604326b4SDaniel Borkmann {
583604326b4SDaniel Borkmann 	/* In order to avoid retpoline, we make assumptions when we call
584604326b4SDaniel Borkmann 	 * into ops if e.g. a psock is not present. Make sure they are
585604326b4SDaniel Borkmann 	 * indeed valid assumptions.
586604326b4SDaniel Borkmann 	 */
587604326b4SDaniel Borkmann 	return ops->recvmsg  == tcp_recvmsg &&
588604326b4SDaniel Borkmann 	       ops->sendmsg  == tcp_sendmsg &&
589604326b4SDaniel Borkmann 	       ops->sendpage == tcp_sendpage ? 0 : -ENOTSUPP;
590604326b4SDaniel Borkmann }
591604326b4SDaniel Borkmann 
59251e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
593604326b4SDaniel Borkmann {
594d19da360SLorenz Bauer 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
595d19da360SLorenz Bauer 	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
596604326b4SDaniel Borkmann 
597c5d2177aSJohn Fastabend 	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
598c5d2177aSJohn Fastabend 		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
599c5d2177aSJohn Fastabend 	}
600c5d2177aSJohn Fastabend 
6018a59f9d1SCong Wang 	if (restore) {
6028a59f9d1SCong Wang 		if (inet_csk_has_ulp(sk)) {
6038859a44eSJakub Kicinski 			/* TLS does not have an unhash proto in SW cases,
6048859a44eSJakub Kicinski 			 * but we need to ensure we stop using the sock_map
6058859a44eSJakub Kicinski 			 * unhash routine because the associated psock is being
6068859a44eSJakub Kicinski 			 * removed. So use the original unhash handler.
6078859a44eSJakub Kicinski 			 */
6088859a44eSJakub Kicinski 			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
6098a59f9d1SCong Wang 			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
6108a59f9d1SCong Wang 		} else {
6118a59f9d1SCong Wang 			sk->sk_write_space = psock->saved_write_space;
6128a59f9d1SCong Wang 			/* Pairs with lockless read in sk_clone_lock() */
613fee9ac06SPavel Begunkov 			sock_replace_proto(sk, psock->sk_proto);
6148a59f9d1SCong Wang 		}
6158a59f9d1SCong Wang 		return 0;
6168a59f9d1SCong Wang 	}
6178a59f9d1SCong Wang 
6187b219da4SLorenz Bauer 	if (sk->sk_family == AF_INET6) {
6197b219da4SLorenz Bauer 		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
6208a59f9d1SCong Wang 			return -EINVAL;
621d19da360SLorenz Bauer 
6227b219da4SLorenz Bauer 		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
623d19da360SLorenz Bauer 	}
624d19da360SLorenz Bauer 
6258a59f9d1SCong Wang 	/* Pairs with lockless read in sk_clone_lock() */
626fee9ac06SPavel Begunkov 	sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
6278a59f9d1SCong Wang 	return 0;
628604326b4SDaniel Borkmann }
6298a59f9d1SCong Wang EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
630604326b4SDaniel Borkmann 
631e8025155SJakub Sitnicki /* If a child got cloned from a listening socket that had tcp_bpf
632e8025155SJakub Sitnicki  * protocol callbacks installed, we need to restore the callbacks to
633e8025155SJakub Sitnicki  * the default ones because the child does not inherit the psock state
634e8025155SJakub Sitnicki  * that tcp_bpf callbacks expect.
635e8025155SJakub Sitnicki  */
636e8025155SJakub Sitnicki void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
637e8025155SJakub Sitnicki {
638e8025155SJakub Sitnicki 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
639e8025155SJakub Sitnicki 	struct proto *prot = newsk->sk_prot;
640e8025155SJakub Sitnicki 
641e8025155SJakub Sitnicki 	if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
642e8025155SJakub Sitnicki 		newsk->sk_prot = sk->sk_prot_creator;
643e8025155SJakub Sitnicki }
64488759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
645