File Index
Files and directories
Sign in
FILE

tcp_bpf.c

File preview with syntax highlighting.

linux-6.19.14/net/ipv4/tcp_bpf.c C
  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
 15void 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
 33static 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(sizeof(*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
 91static 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);
112retry:
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
153static 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
164int 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}
178EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir);
179
180#ifdef CONFIG_BPF_SYSCALL
181static 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
203static 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
221static int tcp_bpf_recvmsg_parser(struct sock *sk,
222				  struct msghdr *msg,
223				  size_t len,
224				  int flags,
225				  int *addr_len)
226{
227	int peek = flags & MSG_PEEK;
228	struct sk_psock *psock;
229	struct tcp_sock *tcp;
230	int copied_from_self = 0;
231	int copied = 0;
232	u32 seq;
233
234	if (unlikely(flags & MSG_ERRQUEUE))
235		return inet_recv_error(sk, msg, len, addr_len);
236
237	if (!len)
238		return 0;
239
240	psock = sk_psock_get(sk);
241	if (unlikely(!psock))
242		return tcp_recvmsg(sk, msg, len, flags, addr_len);
243
244	lock_sock(sk);
245	tcp = tcp_sk(sk);
246	seq = tcp->copied_seq;
247	/* We may have received data on the sk_receive_queue pre-accept and
248	 * then we can not use read_skb in this context because we haven't
249	 * assigned a sk_socket yet so have no link to the ops. The work-around
250	 * is to check the sk_receive_queue and in these cases read skbs off
251	 * queue again. The read_skb hook is not running at this point because
252	 * of lock_sock so we avoid having multiple runners in read_skb.
253	 */
254	if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
255		tcp_data_ready(sk);
256		/* This handles the ENOMEM errors if we both receive data
257		 * pre accept and are already under memory pressure. At least
258		 * let user know to retry.
259		 */
260		if (unlikely(!skb_queue_empty(&sk->sk_receive_queue))) {
261			copied = -EAGAIN;
262			goto out;
263		}
264	}
265
266msg_bytes_ready:
267	copied = __sk_msg_recvmsg(sk, psock, msg, len, flags, &copied_from_self);
268	/* The typical case for EFAULT is the socket was gracefully
269	 * shutdown with a FIN pkt. So check here the other case is
270	 * some error on copy_page_to_iter which would be unexpected.
271	 * On fin return correct return code to zero.
272	 */
273	if (copied == -EFAULT) {
274		bool is_fin = is_next_msg_fin(psock);
275
276		if (is_fin) {
277			copied = 0;
278			seq++;
279			goto out;
280		}
281	}
282	seq += copied_from_self;
283	if (!copied) {
284		long timeo;
285		int data;
286
287		if (sock_flag(sk, SOCK_DONE))
288			goto out;
289
290		if (sk->sk_err) {
291			copied = sock_error(sk);
292			goto out;
293		}
294
295		if (sk->sk_shutdown & RCV_SHUTDOWN)
296			goto out;
297
298		if (sk->sk_state == TCP_CLOSE) {
299			copied = -ENOTCONN;
300			goto out;
301		}
302
303		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
304		if (!timeo) {
305			copied = -EAGAIN;
306			goto out;
307		}
308
309		if (signal_pending(current)) {
310			copied = sock_intr_errno(timeo);
311			goto out;
312		}
313
314		data = tcp_msg_wait_data(sk, psock, timeo);
315		if (data < 0) {
316			copied = data;
317			goto unlock;
318		}
319		if (data && !sk_psock_queue_empty(psock))
320			goto msg_bytes_ready;
321		copied = -EAGAIN;
322	}
323out:
324	if (!peek)
325		WRITE_ONCE(tcp->copied_seq, seq);
326	tcp_rcv_space_adjust(sk);
327	if (copied > 0)
328		__tcp_cleanup_rbuf(sk, copied);
329
330unlock:
331	release_sock(sk);
332	sk_psock_put(sk, psock);
333	return copied;
334}
335
336static int tcp_bpf_ioctl(struct sock *sk, int cmd, int *karg)
337{
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	*karg = sk_psock_msg_inq(sk);
349	unlock_sock_fast(sk, slow);
350
351	return 0;
352}
353
354static int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
355			   int flags, int *addr_len)
356{
357	struct sk_psock *psock;
358	int copied, ret;
359
360	if (unlikely(flags & MSG_ERRQUEUE))
361		return inet_recv_error(sk, msg, len, addr_len);
362
363	if (!len)
364		return 0;
365
366	psock = sk_psock_get(sk);
367	if (unlikely(!psock))
368		return tcp_recvmsg(sk, msg, len, flags, addr_len);
369	if (!skb_queue_empty(&sk->sk_receive_queue) &&
370	    sk_psock_queue_empty(psock)) {
371		sk_psock_put(sk, psock);
372		return tcp_recvmsg(sk, msg, len, flags, addr_len);
373	}
374	lock_sock(sk);
375msg_bytes_ready:
376	copied = sk_msg_recvmsg(sk, psock, msg, len, flags);
377	if (!copied) {
378		long timeo;
379		int data;
380
381		timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
382		data = tcp_msg_wait_data(sk, psock, timeo);
383		if (data < 0) {
384			ret = data;
385			goto unlock;
386		}
387		if (data) {
388			if (!sk_psock_queue_empty(psock))
389				goto msg_bytes_ready;
390			release_sock(sk);
391			sk_psock_put(sk, psock);
392			return tcp_recvmsg(sk, msg, len, flags, addr_len);
393		}
394		copied = -EAGAIN;
395	}
396	ret = copied;
397
398unlock:
399	release_sock(sk);
400	sk_psock_put(sk, psock);
401	return ret;
402}
403
404static int tcp_bpf_send_verdict(struct sock *sk, struct sk_psock *psock,
405				struct sk_msg *msg, int *copied, int flags)
406{
407	bool cork = false, enospc = sk_msg_full(msg), redir_ingress;
408	struct sock *sk_redir;
409	u32 tosend, origsize, sent, delta = 0;
410	u32 eval;
411	int ret;
412
413more_data:
414	if (psock->eval == __SK_NONE) {
415		/* Track delta in msg size to add/subtract it on SK_DROP from
416		 * returned to user copied size. This ensures user doesn't
417		 * get a positive return code with msg_cut_data and SK_DROP
418		 * verdict.
419		 */
420		delta = msg->sg.size;
421		psock->eval = sk_psock_msg_verdict(sk, psock, msg);
422		delta -= msg->sg.size;
423	}
424
425	if (msg->cork_bytes &&
426	    msg->cork_bytes > msg->sg.size && !enospc) {
427		psock->cork_bytes = msg->cork_bytes - msg->sg.size;
428		if (!psock->cork) {
429			psock->cork = kzalloc(sizeof(*psock->cork),
430					      GFP_ATOMIC | __GFP_NOWARN);
431			if (!psock->cork) {
432				sk_msg_free(sk, msg);
433				*copied = 0;
434				return -ENOMEM;
435			}
436		}
437		memcpy(psock->cork, msg, sizeof(*msg));
438		return 0;
439	}
440
441	tosend = msg->sg.size;
442	if (psock->apply_bytes && psock->apply_bytes < tosend)
443		tosend = psock->apply_bytes;
444	eval = __SK_NONE;
445
446	switch (psock->eval) {
447	case __SK_PASS:
448		ret = tcp_bpf_push(sk, msg, tosend, flags, true);
449		if (unlikely(ret)) {
450			*copied -= sk_msg_free(sk, msg);
451			break;
452		}
453		sk_msg_apply_bytes(psock, tosend);
454		break;
455	case __SK_REDIRECT:
456		redir_ingress = psock->redir_ingress;
457		sk_redir = psock->sk_redir;
458		sk_msg_apply_bytes(psock, tosend);
459		if (!psock->apply_bytes) {
460			/* Clean up before releasing the sock lock. */
461			eval = psock->eval;
462			psock->eval = __SK_NONE;
463			psock->sk_redir = NULL;
464		}
465		if (psock->cork) {
466			cork = true;
467			psock->cork = NULL;
468		}
469		release_sock(sk);
470
471		origsize = msg->sg.size;
472		ret = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
473					    msg, tosend, flags);
474		sent = origsize - msg->sg.size;
475
476		if (eval == __SK_REDIRECT)
477			sock_put(sk_redir);
478
479		lock_sock(sk);
480		sk_mem_uncharge(sk, sent);
481		if (unlikely(ret < 0)) {
482			int free = sk_msg_free(sk, msg);
483
484			if (!cork)
485				*copied -= free;
486		}
487		if (cork) {
488			sk_msg_free(sk, msg);
489			kfree(msg);
490			msg = NULL;
491			ret = 0;
492		}
493		break;
494	case __SK_DROP:
495	default:
496		sk_msg_free(sk, msg);
497		sk_msg_apply_bytes(psock, tosend);
498		*copied -= (tosend + delta);
499		return -EACCES;
500	}
501
502	if (likely(!ret)) {
503		if (!psock->apply_bytes) {
504			psock->eval =  __SK_NONE;
505			if (psock->sk_redir) {
506				sock_put(psock->sk_redir);
507				psock->sk_redir = NULL;
508			}
509		}
510		if (msg &&
511		    msg->sg.data[msg->sg.start].page_link &&
512		    msg->sg.data[msg->sg.start].length)
513			goto more_data;
514	}
515	return ret;
516}
517
518static int tcp_bpf_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
519{
520	struct sk_msg tmp, *msg_tx = NULL;
521	int copied = 0, err = 0, ret = 0;
522	struct sk_psock *psock;
523	long timeo;
524	int flags;
525
526	/* Don't let internal flags through */
527	flags = (msg->msg_flags & ~MSG_SENDPAGE_DECRYPTED);
528	flags |= MSG_NO_SHARED_FRAGS;
529
530	psock = sk_psock_get(sk);
531	if (unlikely(!psock))
532		return tcp_sendmsg(sk, msg, size);
533
534	lock_sock(sk);
535	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
536	while (msg_data_left(msg)) {
537		bool enospc = false;
538		u32 copy, osize;
539
540		if (sk->sk_err) {
541			err = -sk->sk_err;
542			goto out_err;
543		}
544
545		copy = msg_data_left(msg);
546		if (!sk_stream_memory_free(sk))
547			goto wait_for_sndbuf;
548		if (psock->cork) {
549			msg_tx = psock->cork;
550		} else {
551			msg_tx = &tmp;
552			sk_msg_init(msg_tx);
553		}
554
555		osize = msg_tx->sg.size;
556		err = sk_msg_alloc(sk, msg_tx, msg_tx->sg.size + copy, msg_tx->sg.end - 1);
557		if (err) {
558			if (err != -ENOSPC)
559				goto wait_for_memory;
560			enospc = true;
561			copy = msg_tx->sg.size - osize;
562		}
563
564		ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_tx,
565					       copy);
566		if (ret < 0) {
567			sk_msg_trim(sk, msg_tx, osize);
568			goto out_err;
569		}
570
571		copied += ret;
572		if (psock->cork_bytes) {
573			if (size > psock->cork_bytes)
574				psock->cork_bytes = 0;
575			else
576				psock->cork_bytes -= size;
577			if (psock->cork_bytes && !enospc)
578				goto out_err;
579			/* All cork bytes are accounted, rerun the prog. */
580			psock->eval = __SK_NONE;
581			psock->cork_bytes = 0;
582		}
583
584		err = tcp_bpf_send_verdict(sk, psock, msg_tx, &copied, flags);
585		if (unlikely(err < 0))
586			goto out_err;
587		continue;
588wait_for_sndbuf:
589		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
590wait_for_memory:
591		err = sk_stream_wait_memory(sk, &timeo);
592		if (err) {
593			if (msg_tx && msg_tx != psock->cork)
594				sk_msg_free(sk, msg_tx);
595			goto out_err;
596		}
597	}
598out_err:
599	if (err < 0)
600		err = sk_stream_error(sk, msg->msg_flags, err);
601	release_sock(sk);
602	sk_psock_put(sk, psock);
603	return copied > 0 ? copied : err;
604}
605
606enum {
607	TCP_BPF_IPV4,
608	TCP_BPF_IPV6,
609	TCP_BPF_NUM_PROTS,
610};
611
612enum {
613	TCP_BPF_BASE,
614	TCP_BPF_TX,
615	TCP_BPF_RX,
616	TCP_BPF_TXRX,
617	TCP_BPF_NUM_CFGS,
618};
619
620static struct proto *tcpv6_prot_saved __read_mostly;
621static DEFINE_SPINLOCK(tcpv6_prot_lock);
622static struct proto tcp_bpf_prots[TCP_BPF_NUM_PROTS][TCP_BPF_NUM_CFGS];
623
624static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS],
625				   struct proto *base)
626{
627	prot[TCP_BPF_BASE]			= *base;
628	prot[TCP_BPF_BASE].destroy		= sock_map_destroy;
629	prot[TCP_BPF_BASE].close		= sock_map_close;
630	prot[TCP_BPF_BASE].recvmsg		= tcp_bpf_recvmsg;
631	prot[TCP_BPF_BASE].sock_is_readable	= sk_msg_is_readable;
632	prot[TCP_BPF_BASE].ioctl		= tcp_bpf_ioctl;
633
634	prot[TCP_BPF_TX]			= prot[TCP_BPF_BASE];
635	prot[TCP_BPF_TX].sendmsg		= tcp_bpf_sendmsg;
636
637	prot[TCP_BPF_RX]			= prot[TCP_BPF_BASE];
638	prot[TCP_BPF_RX].recvmsg		= tcp_bpf_recvmsg_parser;
639
640	prot[TCP_BPF_TXRX]			= prot[TCP_BPF_TX];
641	prot[TCP_BPF_TXRX].recvmsg		= tcp_bpf_recvmsg_parser;
642}
643
644static void tcp_bpf_check_v6_needs_rebuild(struct proto *ops)
645{
646	if (unlikely(ops != smp_load_acquire(&tcpv6_prot_saved))) {
647		spin_lock_bh(&tcpv6_prot_lock);
648		if (likely(ops != tcpv6_prot_saved)) {
649			tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV6], ops);
650			smp_store_release(&tcpv6_prot_saved, ops);
651		}
652		spin_unlock_bh(&tcpv6_prot_lock);
653	}
654}
655
656static int __init tcp_bpf_v4_build_proto(void)
657{
658	tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
659	return 0;
660}
661late_initcall(tcp_bpf_v4_build_proto);
662
663static int tcp_bpf_assert_proto_ops(struct proto *ops)
664{
665	/* In order to avoid retpoline, we make assumptions when we call
666	 * into ops if e.g. a psock is not present. Make sure they are
667	 * indeed valid assumptions.
668	 */
669	return ops->recvmsg  == tcp_recvmsg &&
670	       ops->sendmsg  == tcp_sendmsg ? 0 : -ENOTSUPP;
671}
672
673#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
674int tcp_bpf_strp_read_sock(struct strparser *strp, read_descriptor_t *desc,
675			   sk_read_actor_t recv_actor)
676{
677	struct sock *sk = strp->sk;
678	struct sk_psock *psock;
679	struct tcp_sock *tp;
680	int copied = 0;
681
682	tp = tcp_sk(sk);
683	rcu_read_lock();
684	psock = sk_psock(sk);
685	if (WARN_ON_ONCE(!psock)) {
686		desc->error = -EINVAL;
687		goto out;
688	}
689
690	psock->ingress_bytes = 0;
691	copied = tcp_read_sock_noack(sk, desc, recv_actor, true,
692				     &psock->copied_seq);
693	if (copied < 0)
694		goto out;
695	/* recv_actor may redirect skb to another socket (SK_REDIRECT) or
696	 * just put skb into ingress queue of current socket (SK_PASS).
697	 * For SK_REDIRECT, we need to ack the frame immediately but for
698	 * SK_PASS, we want to delay the ack until tcp_bpf_recvmsg_parser().
699	 */
700	tp->copied_seq = psock->copied_seq - psock->ingress_bytes;
701	tcp_rcv_space_adjust(sk);
702	__tcp_cleanup_rbuf(sk, copied - psock->ingress_bytes);
703out:
704	rcu_read_unlock();
705	return copied;
706}
707#endif /* CONFIG_BPF_STREAM_PARSER */
708
709int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore)
710{
711	int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
712	int config = psock->progs.msg_parser   ? TCP_BPF_TX   : TCP_BPF_BASE;
713
714	if (psock->progs.stream_verdict || psock->progs.skb_verdict) {
715		config = (config == TCP_BPF_TX) ? TCP_BPF_TXRX : TCP_BPF_RX;
716	}
717
718	if (restore) {
719		if (inet_csk_has_ulp(sk)) {
720			/* TLS does not have an unhash proto in SW cases,
721			 * but we need to ensure we stop using the sock_map
722			 * unhash routine because the associated psock is being
723			 * removed. So use the original unhash handler.
724			 */
725			WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
726			tcp_update_ulp(sk, psock->sk_proto, psock->saved_write_space);
727		} else {
728			WRITE_ONCE(sk->sk_write_space, psock->saved_write_space);
729			/* Pairs with lockless read in sk_clone_lock() */
730			sock_replace_proto(sk, psock->sk_proto);
731		}
732		return 0;
733	}
734
735	if (sk->sk_family == AF_INET6) {
736		if (tcp_bpf_assert_proto_ops(psock->sk_proto))
737			return -EINVAL;
738
739		tcp_bpf_check_v6_needs_rebuild(psock->sk_proto);
740	}
741
742	/* Pairs with lockless read in sk_clone_lock() */
743	sock_replace_proto(sk, &tcp_bpf_prots[family][config]);
744	return 0;
745}
746EXPORT_SYMBOL_GPL(tcp_bpf_update_proto);
747
748/* If a child got cloned from a listening socket that had tcp_bpf
749 * protocol callbacks installed, we need to restore the callbacks to
750 * the default ones because the child does not inherit the psock state
751 * that tcp_bpf callbacks expect.
752 */
753void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
754{
755	struct proto *prot = newsk->sk_prot;
756
757	if (is_insidevar(prot, tcp_bpf_prots))
758		newsk->sk_prot = sk->sk_prot_creator;
759}
760#endif /* CONFIG_BPF_SYSCALL */
~/ Read-only file preview.