xref: /linux/net/ipv4/tcp_bpf.c (revision c27e360545373b7aee9862a5beef3b9fb3df0c25)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3 
4 #include <linux/skmsg.h>
5 #include <linux/filter.h>
6 #include <linux/bpf.h>
7 #include <linux/init.h>
8 #include <linux/wait.h>
9 #include <linux/util_macros.h>
10 
11 #include <net/inet_common.h>
12 #include <net/tls.h>
13 #include <asm/ioctls.h>
14 
15 void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
16 {
17 	struct tcp_sock *tcp;
18 	int copied;
19 
20 	if (!skb || !skb->len || !sk_is_tcp(sk))
21 		return;
22 
23 	if (skb_bpf_strparser(skb))
24 		return;
25 
26 	tcp = tcp_sk(sk);
27 	copied = tcp->copied_seq + skb->len;
28 	WRITE_ONCE(tcp->copied_seq, copied);
29 	tcp_rcv_space_adjust(sk);
30 	__tcp_cleanup_rbuf(sk, skb->len);
31 }
32 
33 static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
34 			   struct sk_msg *msg, u32 apply_bytes)
35 {
36 	bool apply = apply_bytes;
37 	struct scatterlist *sge;
38 	u32 size, copied = 0;
39 	struct sk_msg *tmp;
40 	int i, ret = 0;
41 
42 	tmp = kzalloc_obj(*tmp, __GFP_NOWARN | GFP_KERNEL);
43 	if (unlikely(!tmp))
44 		return -ENOMEM;
45 
46 	lock_sock(sk);
47 	tmp->sg.start = msg->sg.start;
48 	i = msg->sg.start;
49 	do {
50 		sge = sk_msg_elem(msg, i);
51 		size = (apply && apply_bytes < sge->length) ?
52 			apply_bytes : sge->length;
53 		if (!__sk_rmem_schedule(sk, size, false)) {
54 			if (!copied)
55 				ret = -ENOMEM;
56 			break;
57 		}
58 
59 		sk_mem_charge(sk, size);
60 		atomic_add(size, &sk->sk_rmem_alloc);
61 		sk_msg_xfer(tmp, msg, i, size);
62 		copied += size;
63 		if (sge->length)
64 			get_page(sk_msg_page(tmp, i));
65 		sk_msg_iter_var_next(i);
66 		tmp->sg.end = i;
67 		if (apply) {
68 			apply_bytes -= size;
69 			if (!apply_bytes) {
70 				if (sge->length)
71 					sk_msg_iter_var_prev(i);
72 				break;
73 			}
74 		}
75 	} while (i != msg->sg.end);
76 
77 	if (!ret) {
78 		msg->sg.start = i;
79 		if (!sk_psock_queue_msg(psock, tmp))
80 			atomic_sub(copied, &sk->sk_rmem_alloc);
81 		sk_psock_data_ready(sk, psock);
82 	} else {
83 		sk_msg_free(sk, tmp);
84 		kfree(tmp);
85 	}
86 
87 	release_sock(sk);
88 	return ret;
89 }
90 
91 static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
92 			int flags, bool uncharge)
93 {
94 	struct msghdr msghdr = {};
95 	bool apply = apply_bytes;
96 	struct scatterlist *sge;
97 	struct page *page;
98 	int size, ret = 0;
99 	u32 off;
100 
101 	while (1) {
102 		struct bio_vec bvec;
103 		bool has_tx_ulp;
104 
105 		sge = sk_msg_elem(msg, msg->sg.start);
106 		size = (apply && apply_bytes < sge->length) ?
107 			apply_bytes : sge->length;
108 		off  = sge->offset;
109 		page = sg_page(sge);
110 
111 		tcp_rate_check_app_limited(sk);
112 retry:
113 		msghdr.msg_flags = flags | MSG_SPLICE_PAGES;
114 		has_tx_ulp = tls_sw_has_ctx_tx(sk);
115 		if (has_tx_ulp)
116 			msghdr.msg_flags |= MSG_SENDPAGE_NOPOLICY;
117 
118 		if (size < sge->length && msg->sg.start != msg->sg.end)
119 			msghdr.msg_flags |= MSG_MORE;
120 
121 		bvec_set_page(&bvec, page, size, off);
122 		iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, size);
123 		ret = tcp_sendmsg_locked(sk, &msghdr, size);
124 		if (ret <= 0)
125 			return ret;
126 
127 		if (apply)
128 			apply_bytes -= ret;
129 		msg->sg.size -= ret;
130 		sge->offset += ret;
131 		sge->length -= ret;
132 		if (uncharge)
133 			sk_mem_uncharge(sk, ret);
134 		if (ret != size) {
135 			size -= ret;
136 			off  += ret;
137 			goto retry;
138 		}
139 		if (!sge->length) {
140 			put_page(page);
141 			sk_msg_iter_next(msg, start);
142 			sg_init_table(sge, 1);
143 			if (msg->sg.start == msg->sg.end)
144 				break;
145 		}
146 		if (apply && !apply_bytes)
147 			break;
148 	}
149 
150 	return 0;
151 }
152 
153 static int tcp_bpf_push_locked(struct sock *sk, struct sk_msg *msg,
154 			       u32 apply_bytes, int flags, bool uncharge)
155 {
156 	int ret;
157 
158 	lock_sock(sk);
159 	ret = tcp_bpf_push(sk, msg, apply_bytes, flags, uncharge);
160 	release_sock(sk);
161 	return ret;
162 }
163 
164 int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
165 			  struct sk_msg *msg, u32 bytes, int flags)
166 {
167 	struct sk_psock *psock = sk_psock_get(sk);
168 	int ret;
169 
170 	if (unlikely(!psock))
171 		return -EPIPE;
172 
173 	ret = ingress ? bpf_tcp_ingress(sk, psock, msg, bytes) :
174 			tcp_bpf_push_locked(sk, msg, bytes, flags, false);
175 	sk_psock_put(sk, psock);
176 	return ret;
177 }
178 EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
179 
180 #ifdef CONFIG_BPF_SYSCALL
181 static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock,
182 			     long timeo)
183 {
184 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
185 	int ret = 0;
186 
187 	if (sk->sk_shutdown & RCV_SHUTDOWN)
188 		return 1;
189 
190 	if (!timeo)
191 		return ret;
192 
193 	add_wait_queue(sk_sleep(sk), &wait);
194 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
195 	ret = sk_wait_event(sk, &timeo,
196 			    !list_empty(&psock->ingress_msg) ||
197 			    !skb_queue_empty_lockless(&sk->sk_receive_queue), &wait);
198 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
199 	remove_wait_queue(sk_sleep(sk), &wait);
200 	return ret;
201 }
202 
203 static bool is_next_msg_fin(struct sk_psock *psock)
204 {
205 	struct scatterlist *sge;
206 	struct sk_msg *msg_rx;
207 	int i;
208 
209 	msg_rx = sk_psock_peek_msg(psock);
210 	i = msg_rx->sg.start;
211 	sge = sk_msg_elem(msg_rx, i);
212 	if (!sge->length) {
213 		struct sk_buff *skb = msg_rx->skb;
214 
215 		if (skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
216 			return true;
217 	}
218 	return false;
219 }
220 
221 static int tcp_bpf_recvmsg_parser(struct sock *sk,
222 				  struct msghdr *msg,
223 				  size_t len,
224 				  int flags)
225 {
226 	int peek = flags & MSG_PEEK;
227 	struct sk_psock *psock;
228 	struct tcp_sock *tcp;
229 	int copied_from_self = 0;
230 	int copied = 0;
231 	u32 seq;
232 
233 	if (unlikely(flags & MSG_ERRQUEUE))
234 		return inet_recv_error(sk, msg, len);
235 
236 	if (!len)
237 		return 0;
238 
239 	psock = sk_psock_get(sk);
240 	if (unlikely(!psock))
241 		return tcp_recvmsg(sk, msg, len, flags);
242 
243 	lock_sock(sk);
244 	tcp = tcp_sk(sk);
245 	seq = tcp->copied_seq;
246 	/* We may have received data on the sk_receive_queue pre-accept and
247 	 * then we can not use read_skb in this context because we haven't
248 	 * assigned a sk_socket yet so have no link to the ops. The work-around
249 	 * is to check the sk_receive_queue and in these cases read skbs off
250 	 * queue again. The read_skb hook is not running at this point because
251 	 * of lock_sock so we avoid having multiple runners in read_skb.
252 	 */
253 	if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
254 		tcp_data_ready(sk);
255 		/* This handles the ENOMEM errors if we both receive data
256 		 * pre accept and are already under memory pressure. At least
257 		 * let user know to retry.
258 		 */
259 		if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
260 			copied = -EAGAIN;
261 			goto out;
262 		}
263 	}
264 
265 msg_bytes_ready:
266 	copied = __sk_msg_recvmsg(sk, psock, msg, len, flags, &copied_from_self);
267 	/* The typical case for EFAULT is the socket was gracefully
268 	 * shutdown with a FIN pkt. So check here the other case is
269 	 * some error on copy_page_to_iter which would be unexpected.
270 	 * On fin return correct return code to zero.
271 	 */
272 	if (copied == -EFAULT) {
273 		bool is_fin = is_next_msg_fin(psock);
274 
275 		if (is_fin) {
276 			copied = 0;
277 			seq++;
278 			goto out;
279 		}
280 	}
281 	seq += copied_from_self;
282 	if (!copied) {
283 		long timeo;
284 		int data;
285 
286 		if (sock_flag(sk, SOCK_DONE))
287 			goto out;
288 
289 		if (sk->sk_err) {
290 			copied = sock_error(sk);
291 			goto out;
292 		}
293 
294 		if (sk->sk_shutdown & RCV_SHUTDOWN)
295 			goto out;
296 
297 		if (sk->sk_state == TCP_CLOSE) {
298 			copied = -ENOTCONN;
299 			goto out;
300 		}
301 
302 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
303 		if (!timeo) {
304 			copied = -EAGAIN;
305 			goto out;
306 		}
307 
308 		if (signal_pending(current)) {
309 			copied = sock_intr_errno(timeo);
310 			goto out;
311 		}
312 
313 		data = tcp_msg_wait_data(sk, psock, timeo);
314 		if (data < 0) {
315 			copied = data;
316 			goto unlock;
317 		}
318 		if (data && !sk_psock_queue_empty(psock))
319 			goto msg_bytes_ready;
320 		copied = -EAGAIN;
321 	}
322 out:
323 	if (!peek)
324 		WRITE_ONCE(tcp->copied_seq, seq);
325 	tcp_rcv_space_adjust(sk);
326 	if (copied > 0)
327 		__tcp_cleanup_rbuf(sk, copied);
328 
329 unlock:
330 	release_sock(sk);
331 	sk_psock_put(sk, psock);
332 	return copied;
333 }
334 
335 static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
336 {
337 	struct sk_psock *psock;
338 	bool slow;
339 
340 	if (cmd != SIOCINQ)
341 		return tcp_ioctl(sk, cmd, karg);
342 
343 	/* works similar as tcp_ioctl */
344 	if (sk->sk_state == TCP_LISTEN)
345 		return -EINVAL;
346 
347 	slow = lock_sock_fast(sk);
348 	psock = sk_psock_get(sk);
349 	if (unlikely(!psock)) {
350 		unlock_sock_fast(sk, slow);
351 		return tcp_ioctl(sk, cmd, karg);
352 	}
353 	*karg = sk_psock_get_msg_len_nolock(psock);
354 	/* Without a verdict program, ingress data is never diverted to
355 	 * ingress_msg: it stays in sk_receive_queue and is read through
356 	 * the fallback to tcp_recvmsg(), so account for it like
357 	 * tcp_ioctl() does.
358 	 */
359 	if (!READ_ONCE(psock->progs.stream_verdict) &&
360 	    !READ_ONCE(psock->progs.skb_verdict))
361 		*karg += tcp_inq(sk);
362 	sk_psock_put(sk, psock);
363 	unlock_sock_fast(sk, slow);
364 
365 	return 0;
366 }
367 
368 static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
369 			   int flags)
370 {
371 	struct sk_psock *psock;
372 	int copied, ret;
373 
374 	if (unlikely(flags & MSG_ERRQUEUE))
375 		return inet_recv_error(sk, msg, len);
376 
377 	if (!len)
378 		return 0;
379 
380 	psock = sk_psock_get(sk);
381 	if (unlikely(!psock))
382 		return tcp_recvmsg(sk, msg, len, flags);
383 	if (!skb_queue_empty(&sk->sk_receive_queue) &&
384 	    sk_psock_queue_empty(psock)) {
385 		sk_psock_put(sk, psock);
386 		return tcp_recvmsg(sk, msg, len, flags);
387 	}
388 	lock_sock(sk);
389 msg_bytes_ready:
390 	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
391 	if (!copied) {
392 		long timeo;
393 		int data;
394 
395 		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
396 		data = tcp_msg_wait_data(sk, psock, timeo);
397 		if (data < 0) {
398 			ret = data;
399 			goto unlock;
400 		}
401 		if (data) {
402 			if (!sk_psock_queue_empty(psock))
403 				goto msg_bytes_ready;
404 			release_sock(sk);
405 			sk_psock_put(sk, psock);
406 			return tcp_recvmsg(sk, msg, len, flags);
407 		}
408 		copied = -EAGAIN;
409 	}
410 	ret = copied;
411 
412 unlock:
413 	release_sock(sk);
414 	sk_psock_put(sk, psock);
415 	return ret;
416 }
417 
418 static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
419 				struct sk_msg *msg, int *copied, int flags)
420 {
421 	bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
422 	struct sock *sk_redir;
423 	u32 tosend, origsize, sent, delta = 0;
424 	u32 eval;
425 	int ret;
426 
427 more_data:
428 	if (psock->eval == __SK_NONE) {
429 		/* Track delta in msg size to add/subtract it on SK_DROP from
430 		 * returned to user copied size. This ensures user doesn't
431 		 * get a positive return code with msg_cut_data and SK_DROP
432 		 * verdict.
433 		 */
434 		delta = msg->sg.size;
435 		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
436 		delta -= msg->sg.size;
437 	}
438 
439 	if (msg->cork_bytes &&
440 	    msg->cork_bytes > msg->sg.size && !enospc) {
441 		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
442 		if (!psock->cork) {
443 			psock->cork = kzalloc_obj(*psock->cork,
444 						  GFP_ATOMIC | __GFP_NOWARN);
445 			if (!psock->cork) {
446 				sk_msg_free(sk, msg);
447 				*copied = 0;
448 				return -ENOMEM;
449 			}
450 		}
451 		memcpy(psock->cork, msg, sizeof(*msg));
452 		return 0;
453 	}
454 
455 	tosend = msg->sg.size;
456 	if (psock->apply_bytes && psock->apply_bytes < tosend)
457 		tosend = psock->apply_bytes;
458 	eval = __SK_NONE;
459 
460 	switch (psock->eval) {
461 	case __SK_PASS:
462 		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
463 		if (unlikely(ret)) {
464 			*copied -= sk_msg_free(sk, msg);
465 			break;
466 		}
467 		sk_msg_apply_bytes(psock, tosend);
468 		break;
469 	case __SK_REDIRECT:
470 		redir_ingress = psock->redir_ingress;
471 		sk_redir = psock->sk_redir;
472 		sk_msg_apply_bytes(psock, tosend);
473 		if (!psock->apply_bytes) {
474 			/* Clean up before releasing the sock lock. */
475 			eval = psock->eval;
476 			psock->eval = __SK_NONE;
477 			psock->sk_redir = NULL;
478 		}
479 		if (psock->cork) {
480 			cork = true;
481 			psock->cork = NULL;
482 		}
483 		release_sock(sk);
484 
485 		origsize = msg->sg.size;
486 		ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
487 					    msg, tosend, flags);
488 		sent = origsize - msg->sg.size;
489 
490 		if (eval == __SK_REDIRECT)
491 			sock_put(sk_redir);
492 
493 		lock_sock(sk);
494 		sk_mem_uncharge(sk, sent);
495 		if (unlikely(ret < 0)) {
496 			int free = sk_msg_free(sk, msg);
497 
498 			if (!cork)
499 				*copied -= free;
500 		}
501 		if (cork) {
502 			sk_msg_free(sk, msg);
503 			kfree(msg);
504 			msg = NULL;
505 			ret = 0;
506 		}
507 		break;
508 	case __SK_DROP:
509 	default:
510 		sk_msg_free(sk, msg);
511 		sk_msg_apply_bytes(psock, tosend);
512 		*copied -= (tosend + delta);
513 		return -EACCES;
514 	}
515 
516 	if (likely(!ret)) {
517 		if (!psock->apply_bytes) {
518 			psock->eval =  __SK_NONE;
519 			if (psock->sk_redir) {
520 				sock_put(psock->sk_redir);
521 				psock->sk_redir = NULL;
522 			}
523 		}
524 		if (msg &&
525 		    msg->sg.data[msg->sg.start].page_link &&
526 		    msg->sg.data[msg->sg.start].length)
527 			goto more_data;
528 	}
529 	return ret;
530 }
531 
532 static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
533 {
534 	struct sk_msg tmp, *msg_tx = NULL;
535 	int copied = 0, err = 0, ret = 0;
536 	struct sk_psock *psock;
537 	long timeo;
538 	int flags;
539 
540 	/* Don't let internal flags through */
541 	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
542 	flags |= MSG_NO_SHARED_FRAGS;
543 
544 	psock = sk_psock_get(sk);
545 	if (unlikely(!psock))
546 		return tcp_sendmsg(sk, msg, size);
547 
548 	lock_sock(sk);
549 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
550 	while (msg_data_left(msg)) {
551 		bool enospc = false;
552 		u32 copy, osize;
553 
554 		if (sk->sk_err) {
555 			err = -sk->sk_err;
556 			goto out_err;
557 		}
558 
559 		copy = msg_data_left(msg);
560 		if (!sk_stream_memory_free(sk))
561 			goto wait_for_sndbuf;
562 		if (psock->cork) {
563 			msg_tx = psock->cork;
564 		} else {
565 			msg_tx = &tmp;
566 			sk_msg_init(msg_tx);
567 		}
568 
569 		osize = msg_tx->sg.size;
570 		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
571 		if (err) {
572 			if (err != -ENOSPC)
573 				goto wait_for_memory;
574 			enospc = true;
575 			copy = msg_tx->sg.size - osize;
576 		}
577 
578 		ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
579 					       copy);
580 		if (ret < 0) {
581 			sk_msg_trim(sk, msg_tx, osize);
582 			goto out_err;
583 		}
584 
585 		copied += ret;
586 		if (psock->cork_bytes) {
587 			if (size > psock->cork_bytes)
588 				psock->cork_bytes = 0;
589 			else
590 				psock->cork_bytes -= size;
591 			if (psock->cork_bytes && !enospc)
592 				goto out_err;
593 			/* All cork bytes are accounted, rerun the prog. */
594 			psock->eval = __SK_NONE;
595 			psock->cork_bytes = 0;
596 		}
597 
598 		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
599 		if (unlikely(err < 0))
600 			goto out_err;
601 		continue;
602 wait_for_sndbuf:
603 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
604 wait_for_memory:
605 		err = sk_stream_wait_memory(sk, &timeo);
606 		if (err) {
607 			if (msg_tx == &tmp)
608 				sk_msg_free(sk, msg_tx);
609 			goto out_err;
610 		}
611 	}
612 out_err:
613 	if (err < 0)
614 		err = sk_stream_error(sk, msg->msg_flags, err);
615 	release_sock(sk);
616 	sk_psock_put(sk, psock);
617 	return copied > 0 ? copied : err;
618 }
619 
620 enum {
621 	TCP_BPF_IPV4,
622 	TCP_BPF_IPV6,
623 	TCP_BPF_NUM_PROTS,
624 };
625 
626 enum {
627 	TCP_BPF_BASE,
628 	TCP_BPF_TX,
629 	TCP_BPF_RX,
630 	TCP_BPF_TXRX,
631 	TCP_BPF_NUM_CFGS,
632 };
633 
634 static struct proto *tcpv6_prot_saved __read_mostly;
635 static DEFINE_SPINLOCK(tcpv6_prot_lock);
636 static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
637 
638 static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
639 				   struct proto *base)
640 {
641 	prot[TCP_BPF_BASE]			= *base;
642 	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
643 	prot[TCP_BPF_BASE].close		= sock_map_close;
644 	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
645 	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
646 	prot[TCP_BPF_BASE].ioctl		= tcp_bpf_ioctl;
647 
648 	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
649 	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
650 
651 	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
652 	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
653 
654 	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
655 	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
656 }
657 
658 static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
659 {
660 	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
661 		spin_lock_bh(&tcpv6_prot_lock);
662 		if (likely(ops != tcpv6_prot_saved)) {
663 			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
664 			smp_store_release(&tcpv6_prot_saved, ops);
665 		}
666 		spin_unlock_bh(&tcpv6_prot_lock);
667 	}
668 }
669 
670 static int __init tcp_bpf_v4_build_proto(void)
671 {
672 	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
673 	return 0;
674 }
675 late_initcall(tcp_bpf_v4_build_proto);
676 
677 static int tcp_bpf_assert_proto_ops(struct proto *ops)
678 {
679 	/* In order to avoid retpoline, we make assumptions when we call
680 	 * into ops if e.g. a psock is not present. Make sure they are
681 	 * indeed valid assumptions.
682 	 */
683 	return ops->recvmsg  == tcp_recvmsg &&
684 	       ops->sendmsg  == tcp_sendmsg ? 0 : -ENOTSUPP;
685 }
686 
687 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
688 int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
689 			   sk_read_actor_t recv_actor)
690 {
691 	struct sock *sk = strp->sk;
692 	struct sk_psock *psock;
693 	struct tcp_sock *tp;
694 	int copied = 0;
695 
696 	tp = tcp_sk(sk);
697 	rcu_read_lock();
698 	psock = sk_psock(sk);
699 	if (WARN_ON_ONCE(!psock)) {
700 		desc->error = -EINVAL;
701 		goto out;
702 	}
703 
704 	psock->ingress_bytes = 0;
705 	copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
706 				     &psock->copied_seq);
707 	if (copied < 0)
708 		goto out;
709 	/* recv_actor may redirect skb to another socket (SK_REDIRECT) or
710 	 * just put skb into ingress queue of current socket (SK_PASS).
711 	 * For SK_REDIRECT, we need to ack the frame immediately but for
712 	 * SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
713 	 */
714 	tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
715 	tcp_rcv_space_adjust(sk);
716 	__tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
717 out:
718 	rcu_read_unlock();
719 	return copied;
720 }
721 #endif /* CONFIG_BPF_STREAM_PARSER */
722 
723 int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
724 {
725 	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
726 	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
727 
728 	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
729 		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
730 	}
731 
732 	if (restore) {
733 		if (inet_csk_has_ulp(sk)) {
734 			/* TLS does not have an unhash proto in SW cases,
735 			 * but we need to ensure we stop using the sock_map
736 			 * unhash routine because the associated psock is being
737 			 * removed. So use the original unhash handler.
738 			 */
739 			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
740 			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
741 		} else {
742 			WRITE_ONCE(sk->sk_write_space, psock->saved_write_space);
743 			/* Pairs with lockless read in sk_clone_lock() */
744 			sock_replace_proto(sk, psock->sk_proto);
745 		}
746 		return 0;
747 	}
748 
749 	if (sk->sk_family == AF_INET6) {
750 		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
751 			return -EINVAL;
752 
753 		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
754 	}
755 
756 	/* Pairs with lockless read in sk_clone_lock() */
757 	sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
758 	return 0;
759 }
760 EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
761 
762 /* If a child got cloned from a listening socket that had tcp_bpf
763  * protocol callbacks installed, we need to restore the callbacks to
764  * the default ones because the child does not inherit the psock state
765  * that tcp_bpf callbacks expect.
766  */
767 void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
768 {
769 	struct proto *prot = newsk->sk_prot;
770 
771 	if (is_insidevar(prot, tcp_bpf_prots))
772 		newsk->sk_prot = sk->sk_prot_creator;
773 }
774 #endif /* CONFIG_BPF_SYSCALL */
775