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