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